Skip to content

Commit

Permalink
[secret] refactor: rewrite secret code by add hellok8s:v5.
Browse files Browse the repository at this point in the history
  • Loading branch information
guangzhengli committed Sep 13, 2022
1 parent ad9b3ca commit 400c6cb
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 5 deletions.
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1365,7 +1365,10 @@ echo "ZGJfcGFzc3dvcmQK" | base64 -d
# db_password
```

这里将 Base64 编码过后的值,填入对应的 key - value 中。

```yaml
# hellok8s-secret.yaml
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -1375,14 +1378,15 @@ data:
```

```yaml
# hellok8s.yaml
apiVersion: v1
kind: Pod
metadata:
name: hellok8s-pod
spec:
containers:
- name: hellok8s-container
image: guangzhengli/hellok8s:v4
image: guangzhengli/hellok8s:v5
env:
- name: DB_PASSWORD
valueFrom:
Expand All @@ -1391,7 +1395,42 @@ spec:
key: DB_PASSWORD
```

Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
```go
package main
import (
"fmt"
"io"
"net/http"
"os"
)
func hello(w http.ResponseWriter, r *http.Request) {
host, _ := os.Hostname()
dbPassword := os.Getenv("DB_PASSWORD")
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
}
func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":3000", nil)
}
```

在代码中读取 `DB_PASSWORD` 环境变量,直接返回对应字符串。Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。

```shell
docker build . -t guangzhengli/hellok8s:v5
docker push guangzhengli/hellok8s:v5
kubectl apply -f hellok8s-secret.yaml
kubectl apply -f hellok8s.yaml
kubectl port-forward hellok8s-pod 3000:3000
```


## Job

Expand Down
42 changes: 40 additions & 2 deletions docs/secret.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ echo "ZGJfcGFzc3dvcmQK" | base64 -d
# db_password
```

这里将 Base64 编码过后的值,填入对应的 key - value 中。

```yaml
# hellok8s-secret.yaml
apiVersion: v1
kind: Secret
metadata:
Expand All @@ -34,14 +37,15 @@ data:
```
```yaml
# hellok8s.yaml
apiVersion: v1
kind: Pod
metadata:
name: hellok8s-pod
spec:
containers:
- name: hellok8s-container
image: guangzhengli/hellok8s:v4
image: guangzhengli/hellok8s:v5
env:
- name: DB_PASSWORD
valueFrom:
Expand All @@ -50,4 +54,38 @@ spec:
key: DB_PASSWORD
```
Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
```go
package main

import (
"fmt"
"io"
"net/http"
"os"
)

func hello(w http.ResponseWriter, r *http.Request) {
host, _ := os.Hostname()
dbPassword := os.Getenv("DB_PASSWORD")
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
}

func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":3000", nil)
}
```

在代码中读取 `DB_PASSWORD` 环境变量,直接返回对应字符串。Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。

```shell
docker build . -t guangzhengli/hellok8s:v5

docker push guangzhengli/hellok8s:v5

kubectl apply -f hellok8s-secret.yaml

kubectl apply -f hellok8s.yaml

kubectl port-forward hellok8s-pod 3000:3000
```
16 changes: 16 additions & 0 deletions secret/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Dockerfile
FROM golang:1.16-buster AS builder
RUN mkdir /src
ADD . /src
WORKDIR /src

RUN go env -w GO111MODULE=auto
RUN go build -o main .

FROM gcr.io/distroless/base-debian10

WORKDIR /

COPY --from=builder /src/main /main
EXPOSE 3000
ENTRYPOINT ["/main"]
1 change: 1 addition & 0 deletions secret/hellok8s-secret.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# hellok8s-secret.yaml
apiVersion: v1
kind: Secret
metadata:
Expand Down
3 changes: 2 additions & 1 deletion secret/hellok8s.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# hellok8s.yaml
apiVersion: v1
kind: Pod
metadata:
name: hellok8s-pod
spec:
containers:
- name: hellok8s-container
image: guangzhengli/hellok8s:v4
image: guangzhengli/hellok8s:v5
env:
- name: DB_PASSWORD
valueFrom:
Expand Down
19 changes: 19 additions & 0 deletions secret/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
)

func hello(w http.ResponseWriter, r *http.Request) {
host, _ := os.Hostname()
dbPassword := os.Getenv("DB_PASSWORD")
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
}

func main() {
http.HandleFunc("/", hello)
http.ListenAndServe(":3000", nil)
}

0 comments on commit 400c6cb

Please sign in to comment.