Skip to content

Commit 400c6cb

Browse files
committed
[secret] refactor: rewrite secret code by add hellok8s:v5.
1 parent ad9b3ca commit 400c6cb

6 files changed

Lines changed: 119 additions & 5 deletions

File tree

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,10 @@ echo "ZGJfcGFzc3dvcmQK" | base64 -d
13651365
# db_password
13661366
```
13671367

1368+
这里将 Base64 编码过后的值,填入对应的 key - value 中。
1369+
13681370
```yaml
1371+
# hellok8s-secret.yaml
13691372
apiVersion: v1
13701373
kind: Secret
13711374
metadata:
@@ -1375,14 +1378,15 @@ data:
13751378
```
13761379

13771380
```yaml
1381+
# hellok8s.yaml
13781382
apiVersion: v1
13791383
kind: Pod
13801384
metadata:
13811385
name: hellok8s-pod
13821386
spec:
13831387
containers:
13841388
- name: hellok8s-container
1385-
image: guangzhengli/hellok8s:v4
1389+
image: guangzhengli/hellok8s:v5
13861390
env:
13871391
- name: DB_PASSWORD
13881392
valueFrom:
@@ -1391,7 +1395,42 @@ spec:
13911395
key: DB_PASSWORD
13921396
```
13931397

1394-
Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
1398+
```go
1399+
package main
1400+
1401+
import (
1402+
"fmt"
1403+
"io"
1404+
"net/http"
1405+
"os"
1406+
)
1407+
1408+
func hello(w http.ResponseWriter, r *http.Request) {
1409+
host, _ := os.Hostname()
1410+
dbPassword := os.Getenv("DB_PASSWORD")
1411+
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
1412+
}
1413+
1414+
func main() {
1415+
http.HandleFunc("/", hello)
1416+
http.ListenAndServe(":3000", nil)
1417+
}
1418+
```
1419+
1420+
在代码中读取 `DB_PASSWORD` 环境变量,直接返回对应字符串。Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
1421+
1422+
```shell
1423+
docker build . -t guangzhengli/hellok8s:v5
1424+
1425+
docker push guangzhengli/hellok8s:v5
1426+
1427+
kubectl apply -f hellok8s-secret.yaml
1428+
1429+
kubectl apply -f hellok8s.yaml
1430+
1431+
kubectl port-forward hellok8s-pod 3000:3000
1432+
```
1433+
13951434

13961435
## Job
13971436

docs/secret.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ echo "ZGJfcGFzc3dvcmQK" | base64 -d
2424
# db_password
2525
```
2626

27+
这里将 Base64 编码过后的值,填入对应的 key - value 中。
28+
2729
```yaml
30+
# hellok8s-secret.yaml
2831
apiVersion: v1
2932
kind: Secret
3033
metadata:
@@ -34,14 +37,15 @@ data:
3437
```
3538
3639
```yaml
40+
# hellok8s.yaml
3741
apiVersion: v1
3842
kind: Pod
3943
metadata:
4044
name: hellok8s-pod
4145
spec:
4246
containers:
4347
- name: hellok8s-container
44-
image: guangzhengli/hellok8s:v4
48+
image: guangzhengli/hellok8s:v5
4549
env:
4650
- name: DB_PASSWORD
4751
valueFrom:
@@ -50,4 +54,38 @@ spec:
5054
key: DB_PASSWORD
5155
```
5256
53-
Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
57+
```go
58+
package main
59+
60+
import (
61+
"fmt"
62+
"io"
63+
"net/http"
64+
"os"
65+
)
66+
67+
func hello(w http.ResponseWriter, r *http.Request) {
68+
host, _ := os.Hostname()
69+
dbPassword := os.Getenv("DB_PASSWORD")
70+
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
71+
}
72+
73+
func main() {
74+
http.HandleFunc("/", hello)
75+
http.ListenAndServe(":3000", nil)
76+
}
77+
```
78+
79+
在代码中读取 `DB_PASSWORD` 环境变量,直接返回对应字符串。Secret 的使用方法和前面教程中 ConfigMap 基本一致,这里就不再过多赘述。
80+
81+
```shell
82+
docker build . -t guangzhengli/hellok8s:v5
83+
84+
docker push guangzhengli/hellok8s:v5
85+
86+
kubectl apply -f hellok8s-secret.yaml
87+
88+
kubectl apply -f hellok8s.yaml
89+
90+
kubectl port-forward hellok8s-pod 3000:3000
91+
```

secret/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Dockerfile
2+
FROM golang:1.16-buster AS builder
3+
RUN mkdir /src
4+
ADD . /src
5+
WORKDIR /src
6+
7+
RUN go env -w GO111MODULE=auto
8+
RUN go build -o main .
9+
10+
FROM gcr.io/distroless/base-debian10
11+
12+
WORKDIR /
13+
14+
COPY --from=builder /src/main /main
15+
EXPOSE 3000
16+
ENTRYPOINT ["/main"]

secret/hellok8s-secret.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# hellok8s-secret.yaml
12
apiVersion: v1
23
kind: Secret
34
metadata:

secret/hellok8s.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
# hellok8s.yaml
12
apiVersion: v1
23
kind: Pod
34
metadata:
45
name: hellok8s-pod
56
spec:
67
containers:
78
- name: hellok8s-container
8-
image: guangzhengli/hellok8s:v4
9+
image: guangzhengli/hellok8s:v5
910
env:
1011
- name: DB_PASSWORD
1112
valueFrom:

secret/main.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"os"
8+
)
9+
10+
func hello(w http.ResponseWriter, r *http.Request) {
11+
host, _ := os.Hostname()
12+
dbPassword := os.Getenv("DB_PASSWORD")
13+
io.WriteString(w, fmt.Sprintf("[v5] Hello, Kubernetes! From host: %s, Get Database Connect Password: %s", host, dbPassword))
14+
}
15+
16+
func main() {
17+
http.HandleFunc("/", hello)
18+
http.ListenAndServe(":3000", nil)
19+
}

0 commit comments

Comments
 (0)