本文收錄在容器技術學習系列文章總目錄html
ConfigMap用於保存配置數據的鍵值對,能夠用來保存單個屬性,也能夠用來保存配置文件。ConfigMap跟secret很相似,但它能夠更方便地處理不包含敏感信息的字符串。node
建立一個名爲nginx-config的configmap,指定端口和server namemysql
[root@master ~]# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.along.com configmap/nginx-config created [root@master ~]# kubectl get cm NAME DATA AGE nginx-config 2 11s [root@master ~]# kubectl describe cm nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== nginx_port: ---- 80 server_name: ---- myapp.along.com Events: <none>
(1)準備文件nginx
[root@master ~]# mkdir configmap [root@master ~]# cd configmap [root@master configmap]# vim www.conf server { server_name myapp.along.com; listen 80; root /data/web/html/; }
(2)建立查詢認證web
[root@master configmap]# kubectl create configmap nginx-www --from-file=./www.conf configmap/nginx-www created [root@master configmap]# kubectl get cm NAME DATA AGE nginx-config 2 3m nginx-www 1 5s [root@master configmap]# kubectl describe cm nginx-www Name: nginx-www Namespace: default Labels: <none> Annotations: <none> Data ==== www.conf: ---- server { server_name myapp.along.com; listen 80; root /data/web/html/; } Events: <none>
經過使用環境變量傳入pod的configmap,不能實時更新sql
(1)編寫configmap的yaml文件docker
[root@master configmap]# vim pod-configmap.yaml apiVersion: v1 kind: Pod metadata: name: pod-cm-1 namespace: default labels: app: myapp tier: frontend annotations: along.com/created-by: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 env: - name: NGINX_SERVER_PORT valueFrom: configMapKeyRef: name: nginx-config key: nginx_port - name: NGINX_SERVER_NAME valueFrom: configMapKeyRef: name: nginx-config key: server_name
(2)建立pod,查詢認證json
[root@master configmap]# kubectl apply -f pod-configmap.yaml pod/pod-cm-1 created [root@master configmap]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-cm-1 1/1 Running 0 41s ---查詢pod內部變量 [root@master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX_SERVER NGINX_SERVER_PORT=80 NGINX_SERVER_NAME=myapp.along.com
(3)經過環境變量導入configmap,修改configmap後,pod中內容不會更改vim
① 使用edit修改configmap,把nginx_port 80改成8080api
[root@master configmap]# kubectl edit cm nginx-config ... ... nginx_port: "8080" #把80改成8080 ... ... configmap/nginx-config edited
② 查詢,configmap被修改,可是pod中變量並未修改
由於configmap只是在容器啓動時加載生效;如今pod已經建立,再修改,不會生效
------cm已經修改------ [root@master configmap]# kubectl describe cm nginx-config Data ==== nginx_port: ---- 8080 server_name: ---- myapp.along.com Events: <none> ------可是pod實際沒有改變------ [root@master configmap]# kubectl exec -it pod-cm-1 -- printenv |grep NGINX_SERVER NGINX_SERVER_PORT=80 NGINX_SERVER_NAME=myapp.along.com
經過使用存儲卷傳入pod的configmap,能夠實時更新
(1)編寫configmap的yaml文件,並建立configmap
建立一個volume,使用上邊建立好的名爲nginx-config的configmap
[root@master configmap]# vim pod-configmap-2.yaml apiVersion: v1 kind: Pod metadata: name: pod-cm-2 namespace: default labels: app: myapp tier: frontend annotations: along.com/created-by: "cluster admin" spec: volumes: - name: nginxconf configMap: name: nginx-config containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: nginxconf mountPath: /etc/nginx/config.d/ readOnly: true [root@master configmap]# kubectl apply -f pod-configmap-2.yaml pod/pod-cm-2 created
(2)登入pod中,查詢驗證
[root@master configmap]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-cm-2 1/1 Running 0 7s [root@master ~]# kubectl exec -it pod-cm-2 -- /bin/sh / # cd /etc/nginx/config.d/ /etc/nginx/config.d # ls nginx_port server_name /etc/nginx/config.d # cat nginx_port 80 /etc/nginx/config.d # cat server_name myapp.along.com
(3)經過環境變量導入configmap,修改configmap後,pod中內容會更改
① 使用edit修改configmap,把nginx_port 80改成8080
[root@master ~]# kubectl edit cm nginx-config apiVersion: v1 data: nginx_port: "8080" server_name: myapp.along.com ... ... configmap/nginx-config edited
② 再登入pod查看,發現已經改變
[root@master ~]# kubectl exec -it pod-cm-2 -- /bin/sh / # cat /etc/nginx/config.d/nginx_port 8080/
[root@master configmap]# vim pod-configmap-3.yaml apiVersion: v1 kind: Pod metadata: name: pod-cm-3 namespace: default labels: app: myapp tier: frontend annotations: along.com/created-by: "cluster admin" spec: volumes: - name: nginxconf configMap: name: nginx-www containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: nginxconf mountPath: /etc/nginx/conf.d/ readOnly: true
[root@master configmap]# kubectl apply -f pod-configmap-3.yaml pod/pod-cm-3 created [root@master configmap]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-cm-3 1/1 Running 0 24s
[root@master configmap]# kubectl exec -it pod-cm-3 -- /bin/sh / # cat /etc/nginx/conf.d/www.conf server { server_name myapp.along.com; listen 80; root /data/web/html/; } / # nginx -T |tail -7 #-T查詢nginx的配置信息 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful # configuration file /etc/nginx/conf.d/www.conf: server { server_name myapp.along.com; listen 80; root /data/web/html/; } ---生成nginx的主頁內容 / # mkdir -p /data/web/html / # vi /data/web/html/index.html <h1>Nginx Server configured by CM</h1>
(1)在master上新開一個窗口,查詢pod對應的IP
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE pod-cm-3 1/1 Running 0 7m 10.244.1.124 node2
(2)在任意節點上配置host,使其能連通此pod
[root@node1 ~]# vim /etc/hosts 10.244.1.124 myapp.along.com
(3)訪問pod,成功
[root@node1 ~]# curl myapp.along.com <h1>Nginx Server configured by CM</h1>
(1)修改configmap的配置,將nginx的端口由80改成8888
[root@master ~]# kubectl edit cm nginx-www apiVersion: v1 data: www.conf: "server {\n\tserver_name myapp.along.com;\n\tlisten 8888;\n\troot /data/web/html/;\n}\n" ... ... configmap/nginx-www edited
(2)在pod內還須要重載nginx配置(如今是手工操做,後面會使用k8s工具完成)
/ # cat /etc/nginx/conf.d/www.conf 查詢configmap的修改是否生效 server { server_name myapp.along.com; listen 8888; root /data/web/html/; } / # nginx -s reload 重載一下nginx配置 2019/02/25 02:32:00 [notice] 16#16: signal process started
(3)在node節點上訪問驗證,成功
[root@node1 ~]# curl myapp.along.com:8888 <h1>Nginx Server configured by CM</h1>
---建立secret [root@master ~]# kubectl create secret generic mysql-root-passwd --from-literal=password=MyP@ss123 secret/mysql-root-passwd created ---查詢secret信息 [root@master ~]# kubectl get secret NAME TYPE DATA AGE default-token-wjbzf kubernetes.io/service-account-token 3 35d mysql-root-passwd Opaque 1 11s ---查詢詳細信息 [root@master ~]# kubectl describe secret mysql-root-passwd Name: mysql-root-passwd Namespace: default Labels: <none> Annotations: <none> Type: Opaque Data ==== password: 9 bytes #已經進行64位加密 ---以yaml文件顯示信息 [root@master ~]# kubectl get secret mysql-root-passwd -o yaml apiVersion: v1 data: password: TXlQQHNzMTIz kind: Secret metadata: creationTimestamp: 2018-10-10T03:14:04Z name: mysql-root-passwd namespace: default resourceVersion: "436965" selfLink: /api/v1/namespaces/default/secrets/mysql-root-passwd uid: 8adbf6ae-cc3a-11e8-bb48-005056277243 type: Opaque ---解密 [root@master ~]# echo TXlQQHNzMTIz |base64 -d MyP@ss123
(1)編寫yaml文件,建立pod
[root@master configmap]# vim pod-secret-1.yaml apiVersion: v1 kind: Pod metadata: name: pod-secret-1 namespace: default labels: app: myapp tier: frontend annotations: along.com/created-by: "cluster admin" spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 env: - name: MYSQL_ROOT_PASSWD valueFrom: secretKeyRef: name: mysql-root-passwd key: password [root@master configmap]# kubectl apply -f pod-secret-1.yaml pod/pod-secret-1 created
(2)查詢並認證
[root@master configmap]# kubectl get pods NAME READY STATUS RESTARTS AGE pod-secret-1 1/1 Running 0 14s ---驗證,查詢pod中的環境變量,篩選出MYSQL_ROOT_PASSWD [root@master configmap]# kubectl exec pod-secret-1 -- printenv |grep MYSQL MYSQL_ROOT_PASSWD=MyP@ss123