Skip to content

Commit 7161f28

Browse files
committed
[configmap] feat: add configmap document and code.
1 parent 0fb4f74 commit 7161f28

7 files changed

Lines changed: 128 additions & 241 deletions

File tree

README.md

Lines changed: 76 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,7 +1091,7 @@ curl http://192.168.59.100/
10911091

10921092
## Namespace
10931093

1094-
在实际的开发当中,有时候我们需要不同的环境来做开发和测试,例如 `dev` 环境给开发使用,`test` 环境给 QA 使用,那么 k8s 能不能在不同环境 `dev` `test` `uat` `prod` 中区分资源,让不同环境的资源独立不影响呢,答案是肯定的,k8s 提供了名为 Namespace 的资源来帮助隔离资源。
1094+
在实际的开发当中,有时候我们需要不同的环境来做开发和测试,例如 `dev` 环境给开发使用,`test` 环境给 QA 使用,那么 k8s 能不能在不同环境 `dev` `test` `uat` `prod` 中区分资源,让不同环境的资源独立互相不影响呢,答案是肯定的,k8s 提供了名为 Namespace 的资源来帮助隔离资源。
10951095

10961096
在 Kubernetes 中,**名字空间(Namespace)** 提供一种机制,将同一集群中的资源划分为相互隔离的组。 同一名字空间内的资源名称要唯一,但跨名字空间时没有这个要求。 名字空间作用域仅针对带有名字空间的对象,例如 Deployment、Service 等。
10971097

@@ -1132,7 +1132,7 @@ kubectl get namespaces
11321132
# test Active 2m44s
11331133
```
11341134

1135-
那么如何在新的 namespace 下创建资源和获取资源呢?只需要在命令后面加上 `-n namespace` 即可。例如根据上面教程中,在名为 `dev` 的 namespace 下创建 `hellok8s:v3` 的 deployment。
1135+
那么如何在新的 namespace 下创建资源和获取资源呢?只需要在命令后面加上 `-n namespace` 即可。例如根据上面教程中,在名为 `dev` 的 namespace 下创建 `hellok8s:v3` 的 deployment 资源
11361136

11371137
```shell
11381138
kubectl apply -f deployment.yaml -n dev
@@ -1142,246 +1142,129 @@ kubectl get pods -n dev
11421142

11431143
## Configmap
11441144

1145-
在实际的开发当中,有一些配置是不适合放到 docker image 或者代码中的,例如在不同环境 `dev` `test` `uat` `prod` 中,数据库的地址往往是不一样的,这时就需要有一种资源,能够定义配置信息,并在不同的
1145+
上面的教程提到,我们在不同环境 `dev` `test` `uat` `prod` 中区分资源,可以让其资源独立互相不受影响,但是随之而来也会带来一些问题,例如不同环境的数据库的地址往往是不一样的,那么如果在代码中写同一个数据库的地址,就会出现问题。
11461146

1147-
```ruby
1148-
require "sinatra"
1147+
K8S 使用 ConfigMap 来将你的配置数据和应用程序代码分开,将非机密性的数据保存到键值对中。ConfigMap 在设计上不是用来保存大量数据的。在 ConfigMap 中保存的数据不可超过 1 MiB。如果你需要保存超出此尺寸限制的数据,你可能考虑挂载存储卷。
11491148

1150-
set :bind, "0.0.0.0"
1149+
下面我们可以来看一个例子,我们修改之前代码,假设不同环境的数据库地址不同,下面代码从环境变量中获取 `DB_URL`,并将它返回。
11511150

1152-
get "*" do
1153-
message = ENV.fetch("MESSAGE", "Hello, Kubernetes")
1154-
"[v4] #{message} (from #{`hostname`.strip})\n"
1155-
end
1156-
```
1151+
```go
1152+
package main
11571153
1158-
```
1159-
docker build . -t guangzhengli/hellok8s:v4
1160-
docker push guangzhengli/hellok8s:v4
1154+
import (
1155+
"fmt"
1156+
"io"
1157+
"net/http"
1158+
"os"
1159+
)
11611160
1162-
kubectl delete deployment,service,ingress --all
1161+
func hello(w http.ResponseWriter, r *http.Request) {
1162+
host, _ := os.Hostname()
1163+
dbURL := os.Getenv("DB_URL")
1164+
io.WriteString(w, fmt.Sprintf("[v4] Hello, Kubernetes! From host: %s, Get Database Connect URL: %s", host, dbURL))
1165+
}
1166+
1167+
func main() {
1168+
http.HandleFunc("/", hello)
1169+
http.ListenAndServe(":3000", nil)
1170+
}
11631171
```
11641172

1165-
```yaml
1166-
apiVersion: v1
1167-
kind: Service
1168-
metadata:
1169-
name: hellok8s-svc
1170-
spec:
1171-
type: NodePort
1172-
selector:
1173-
app: hellok8s
1174-
ports:
1175-
- port: 4567
1176-
nodePort: 30001
1173+
构建 `hellok8s:v4` 的镜像,推送到远程仓库。并删除之前创建的所有资源。
11771174

1178-
---
1175+
```shell
1176+
docker build . -t guangzhengli/hellok8s:v4
1177+
docker push guangzhengli/hellok8s:v4
11791178
1180-
apiVersion: apps/v1
1181-
kind: Deployment
1182-
metadata:
1183-
name: hellok8s
1184-
spec:
1185-
replicas: 2
1186-
selector:
1187-
matchLabels:
1188-
app: hellok8s
1189-
template:
1190-
metadata:
1191-
labels:
1192-
app: hellok8s
1193-
spec:
1194-
containers:
1195-
- image: guangzhengli/hellok8s:v4
1196-
name: hellok8s-container
1179+
kubectl delete deployment,service,ingress --all
11971180
```
11981181

1199-
```shell
1200-
kubectl apply -f hellok8s.yaml
1182+
接下来创建不同 namespace 的 configmap 来存放 `DB_URL`。
12011183

1202-
curl localhost:30001
1203-
# [v4] Hello, Kubernetes (from hellok8s-69dbd44879-vt8dv)
1204-
```
1184+
创建 `hellok8s-config-dev.yaml` 文件
12051185

12061186
```yaml
1207-
apiVersion: apps/v1
1208-
kind: Deployment
1187+
apiVersion: v1
1188+
kind: ConfigMap
12091189
metadata:
1210-
name: hellok8s
1211-
spec:
1212-
replicas: 2
1213-
selector:
1214-
matchLabels:
1215-
app: hellok8s
1216-
template:
1217-
metadata:
1218-
labels:
1219-
app: hellok8s
1220-
spec:
1221-
containers:
1222-
- image: guangzhengli/hellok8s:v4
1223-
name: hellok8s-container
1224-
env:
1225-
- name: MESSAGE
1226-
value: "It works!"
1227-
```
1228-
1229-
```shell
1230-
kubectl apply -f hellok8s.yaml
1231-
1232-
curl localhost:30001
1233-
# [v4] It works! (from hellok8s-568f64dd94-bfxhs)
1190+
name: hellok8s-config
1191+
data:
1192+
DB_URL: "http://DB_ADDRESS_DEV"
12341193
```
12351194

1236-
### configmap
1195+
创建 `hellok8s-config-test.yaml` 文件
12371196

12381197
```yaml
1239-
# hellok8s-config.yaml
12401198
apiVersion: v1
12411199
kind: ConfigMap
12421200
metadata:
12431201
name: hellok8s-config
12441202
data:
1245-
MESSAGE: "It works with a ConfigMap!"
1203+
DB_URL: "http://DB_ADDRESS_TEST"
12461204
```
12471205

1248-
```yaml
1249-
apiVersion: apps/v1
1250-
kind: Deployment
1251-
metadata:
1252-
name: hellok8s
1253-
spec:
1254-
replicas: 2
1255-
selector:
1256-
matchLabels:
1257-
app: hellok8s
1258-
template:
1259-
metadata:
1260-
labels:
1261-
app: hellok8s
1262-
spec:
1263-
containers:
1264-
- image: guangzhengli/hellok8s:v4
1265-
name: hellok8s-container
1266-
env:
1267-
- name: MESSAGE
1268-
valueFrom:
1269-
configMapKeyRef:
1270-
name: hellok8s-config
1271-
key: MESSAGE
1272-
```
1206+
分别在 `dev` `test` 两个 namespace 下创建相同的 `ConfigMap`,名字都叫 hellok8s-config,但是存放的 Pair 对中 Value 值不一样。
12731207

12741208
```shell
1275-
kubectl apply -f hellok8s-config.yaml
1276-
1277-
kubectl apply -f hellok8s.yaml
1278-
1279-
kubectl apply -f service.yaml
1209+
kubectl apply -f hellok8s-config-dev.yaml -n dev
1210+
# configmap/hellok8s-config created
12801211
1281-
curl localhost:30001
1282-
# [v4] It works with a ConfigMap! (from hellok8s-54d5fb5765-nl62z)
1283-
```
1284-
1285-
### Getting all the variables from a ConfigMap
1212+
kubectl apply -f hellok8s-config-test.yaml -n test
1213+
# configmap/hellok8s-config created
12861214
1215+
kubectl get configmap --all-namespaces
1216+
NAMESPACE NAME DATA AGE
1217+
dev hellok8s-config 1 3m12s
1218+
test hellok8s-config 1 2m1s
12871219
```
1288-
env:
1289-
- name: VAR1
1290-
valueFrom:
1291-
configMapKeyRef:
1292-
name: hellok8s-config
1293-
key: VAR1
1294-
1295-
- name: VAR2
1296-
valueFrom:
1297-
configMapKeyRef:
1298-
name: hellok8s-config
1299-
key: VAR2
13001220

1301-
- name: VAR3
1302-
valueFrom:
1303-
configMapKeyRef:
1304-
name: hellok8s-config
1305-
key: VAR3
1306-
# ...
1307-
```
1221+
接着使用 POD 的方式来部署 `hellok8s:v4`,其中 `env.name` 表示的是将 configmap 中的值写进环境变量,这样代码从环境变量中获取 `DB_URL`,这个 KEY 名称必须保持一致。`valueFrom` 代表从哪里读取,`configMapKeyRef` 这里表示从名为 `hellok8s-config` 的 `configMap` 中读取 `KEY=DB_URL` 的 Value。
13081222

13091223
```yaml
1310-
apiVersion: apps/v1
1311-
kind: Deployment
1224+
apiVersion: v1
1225+
kind: Pod
13121226
metadata:
1313-
name: hellok8s
1227+
name: hellok8s-pod
13141228
spec:
1315-
replicas: 2
1316-
selector:
1317-
matchLabels:
1318-
app: hellok8s
1319-
template:
1320-
metadata:
1321-
labels:
1322-
app: hellok8s
1323-
spec:
1324-
containers:
1325-
- image: guangzhengli/hellok8s:v4
1326-
name: hellok8s-container
1327-
envFrom:
1328-
- configMapRef:
1229+
containers:
1230+
- name: hellok8s-container
1231+
image: guangzhengli/hellok8s:v4
1232+
env:
1233+
- name: DB_URL
1234+
valueFrom:
1235+
configMapKeyRef:
13291236
name: hellok8s-config
1237+
key: DB_URL
13301238
```
13311239

1332-
```
1333-
kubectl apply -f hellok8s-updated.yaml
1240+
下面分别在 `dev` `test` 两个 namespace 下创建 `hellok8s:v4`,接着通过 `port-forward` 的方式访问不同 namespace 的服务,可以看到返回的 `Get Database Connect URL: http://DB_ADDRESS_TEST` 是不一样的!
13341241

1335-
curl localhost:30001
1336-
# [v4] It works with a ConfigMap! (from hellok8s-54d5fb5765-nl62z)
1337-
```
1242+
```shell
1243+
kubectl apply -f hellok8s.yaml -n dev
1244+
# pod/hellok8s-pod created
13381245
1339-
### Exposing ConfigMap as files
1246+
kubectl apply -f hellok8s.yaml -n test
1247+
# pod/hellok8s-pod created
13401248
1341-
```yaml
1342-
apiVersion: apps/v1
1343-
kind: Deployment
1344-
metadata:
1345-
name: hellok8s
1346-
spec:
1347-
replicas: 2
1348-
selector:
1349-
matchLabels:
1350-
app: hellok8s
1351-
template:
1352-
metadata:
1353-
labels:
1354-
app: hellok8s
1355-
spec:
1356-
volumes:
1357-
- name: config
1358-
configMap:
1359-
name: hellok8s-config
1360-
containers:
1361-
- image: guangzhengli/hellok8s:v4
1362-
name: hellok8s-container
1363-
volumeMounts:
1364-
- name: config
1365-
mountPath: /config
1366-
```
1249+
kubectl port-forward hellok8s-pod 3000:3000 -n dev
13671250
1368-
```shell
1369-
kubectl apply -f hellok8s.yaml
1370-
kubectl apply -f hellok8s-config.yaml
1251+
curl http://localhost:3000
1252+
# [v4] Hello, Kubernetes! From host: hellok8s-pod, Get Database Connect URL: http://DB_ADDRESS_DEV
13711253
1372-
kubectl get pods
1373-
# NAME READY STATUS
1374-
# hellok8s-8c56675c9-7gxpv 1/1 Running
1375-
# hellok8s-8c56675c9-bfk8t 1/1 Running
1254+
kubectl port-forward hellok8s-pod 3000:3000 -n test
13761255
1377-
# Replace the pod name to what you have running locally
1378-
kubectl exec -it hellok8s-8c56675c9-7gxpv -- sh
1379-
cat /config/MESSAGE
1380-
# It works with a ConfigMap!
1256+
curl http://localhost:3000
1257+
# [v4] Hello, Kubernetes! From host: hellok8s-pod, Get Database Connect URL: http://DB_ADDRESS_TEST
13811258
```
13821259

1260+
作业:如果环境变量有很多的话,还需要这样一个一个写吗?去官网找一种将所有环境变量都挂载的方式。
1261+
1262+
如何代码不是从环境变量中读取数据,而是从文件中读取呢?去官网找一种将 configmap 挂载到容器文件的方式。
1263+
13831264
## Secret
13841265

1266+
上面提到,我们会选择
1267+
13851268
```yaml
13861269
# hellok8s-secret.yaml
13871270
apiVersion: v1

configmaps/Dockerfile

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
FROM ruby:2.6.1-alpine3.9
1+
# Dockerfile
2+
FROM golang:1.16-buster AS builder
3+
RUN mkdir /src
4+
ADD . /src
5+
WORKDIR /src
26

3-
RUN apk add curl && gem install sinatra
4-
COPY app.rb .
5-
ENTRYPOINT ["ruby", "app.rb"]
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"]

configmaps/app.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ kind: ConfigMap
33
metadata:
44
name: hellok8s-config
55
data:
6-
MESSAGE: "It works with a ConfigMap!"
6+
DB_URL: "http://DB_ADDRESS_DEV"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: hellok8s-config
5+
data:
6+
DB_URL: "http://DB_ADDRESS_TEST"

0 commit comments

Comments
 (0)