ConfigMap
数据库连接地址,这种可能根据部署环境变化的,我们不应该写死在代码里。
Kubernetes 为我们提供了 ConfigMap,可以方便的配置一些变量。文档
- 创建 yaml
configmap.yaml1 2 3 4 5 6
| apiVersion: v1 kind: ConfigMap metadata: name: mongo-config data: mongoHost: mongodb-0.mongodb
|
- 部署 yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| $ kubectl apply -f configmap.yaml configmap/mongo-config created
$ kubectl get configmap NAME DATA AGE kube-root-ca.crt 1 5d7h mongo-config 1 8s
$ kubectl get configmap mongo-config -o yaml apiVersion: v1 data: mongoHost: mongodb-0.mongodb kind: ConfigMap metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"mongoHost":"mongodb-0.mongodb"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"mongo-config","namespace":"default"}} creationTimestamp: "2022-01-12T08:29:46Z" name: mongo-config namespace: default resourceVersion: "48470" uid: 8cfaa997-0e6c-4290-b620-cd9db02d9a16
|
Secret
一些重要数据,例如密码、TOKEN,我们可以放到 secret 中。文档,配置证书
注意,数据要进行 Base64 编码
- 创建 yaml
secret.yaml1 2 3 4 5 6 7 8 9 10
| apiVersion: v1 kind: Secret metadata: name: mongo-secret
type: Opaque data: mongo-username: bW9uZ291c2Vy mongo-password: bW9uZ29wYXNz
|
- 部署 yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| $ kubectl apply -f secret.yaml secret/mongo-secret created
$ kubectl get secret NAME TYPE DATA AGE default-token-m2x8s kubernetes.io/service-account-token 3 5d7h mongo-secret Opaque 2 9s
$ kubectl get secret mongo-secret -o yaml apiVersion: v1 data: mongo-password: bW9uZ29wYXNz mongo-username: bW9uZ291c2Vy kind: Secret metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","data":{"mongo-password":"bW9uZ29wYXNz","mongo-username":"bW9uZ291c2Vy"},"kind":"Secret","metadata":{"annotations":{},"name":"mongo-secret","namespace":"default"},"type":"Opaque"} creationTimestamp: "2022-01-12T09:05:51Z" name: mongo-secret namespace: default resourceVersion: "49988" uid: a2e1f7e5-c5b8-4814-928a-5422c78f8678 type: Opaque
|
使用方法
作为环境变量使用
举例一:自定义的镜像
之前的 deployment
类型的 test-k8s,若镜像里面要使用环境变量,则可以在 yaml 文件中加入 env
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| apiVersion: apps/v1 kind: Deployment metadata: name: test-k8s spec: replicas: 2 selector: matchLabels: app: test-k8s template: metadata: labels: app: test-k8s spec: initContainers: - name: wait-mongo image: busybox:1.28 command: ['sh', '-c', "until nslookup mongodb; do echo waiting for mongo; sleep 2; done"] containers: - name: test-k8s image: ccr.ccs.tencentyun.com/k8s-tutorial/test-k8s:v4-configmap env: - name: MONGO_USERNAME valueFrom: secretKeyRef: name: mongo-secret key: mongo-username - name: MONGO_PASSWORD valueFrom: secretKeyRef: name: mongo-secret key: mongo-password - name: MONGO_ADDRESS valueFrom: configMapKeyRef: name: mongo-config key: mongoHost --- apiVersion: v1 kind: Service metadata: name: test-k8s spec: selector: app: test-k8s type: NodePort ports: - nodePort: 31000 port: 8080 targetPort: 8080
|
举例二:公共镜像
之前 statefulset
类型的 mongodb
加上用户名和密码
mongo.yaml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| apiVersion: apps/v1 kind: StatefulSet metadata: name: mongodb spec: serviceName: mongodb replicas: 1 selector: matchLabels: app: mongodb serviceName: mongodb template: metadata: labels: app: mongodb spec: containers: - name: mongo image: mongo:4.4 imagePullPolicy: IfNotPresent env: - name: MONGO_INITDB_ROOT_USERNAME valueFrom: secretKeyRef: name: mongo-secret key: mongo-username - name: MONGO_INITDB_ROOT_PASSWORD valueFrom: secretKeyRef: name: mongo-secret key: mongo-password --- apiVersion: v1 kind: Service metadata: name: mongodb spec: selector: app: mongodb type: ClusterIP clusterIP: None ports: - port: 27017 protocol: TCP targetPort: 27017
|
挂载为文件(更适合证书文件)
挂载后,会在容器中对应路径生成文件,一个 key 一个文件,内容就是 value,文档
pod.yaml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mypod image: redis volumeMounts: - name: foo mountPath: "/etc/foo" readOnly: true volumes: - name: foo secret: secretName: mysecret
|