八. k8s--configmap學習筆記

[TOC]html

爲何使用configmap

不少狀況下咱們爲某一應用作好鏡像,當咱們想修改其中的一些參數的時候,就變得比較麻煩,又要從新制做鏡像,咱們是否是有一種方式,讓鏡像根據不一樣的場景調用咱們不一樣的配置文件呢,那咱們就須要用到 k8s 的另一種資源,那就是 ConfigMap。nginx

咱們知道,在幾乎全部的應用開發中,都會涉及到配置文件的變動,好比說在web的程序中,須要鏈接數據庫,緩存甚至是隊列等等。而咱們的一個應用程序從寫第一行代碼開始,要經歷開發環境、測試環境、預發佈環境只到最終的線上環境。而每個環境都要定義其獨立的各類配置。若是咱們不能很好的管理這些配置文件,你的運維工做將頓時變的無比的繁瑣。爲此業內的一些大公司專門開發了本身的一套配置管理中心,如360的Qcon,百度的disconf等。kubernetes也提供了本身的一套方案,即ConfigMap。kubernetes經過ConfigMap來實現對容器中應用的配置管理。web

建立configmap的四種方式

ConfigMap是用來存儲配置文件的kubernetes資源對象,全部的配置內容都存儲在etcd中。shell

建立ConfigMap的方式有4種:數據庫

  • 經過直接在命令行中指定configmap參數建立,即--from-literal=key=value

    kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com
    
    #查看configmap
    [root@master ~]# kubectl get cm
    NAME           DATA   AGE
    nginx-config   2      4s
    
    #查看configmap的具體信息
    [root@master ~]# kubectl describe configmaps nginx-config
    Name:         nginx-config
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    nginx_port:
    ----
    80
    server_name:
    ----
    myapp.magedu.com
    Events:  <none>
  • 經過指定文件建立,即將一個配置文件建立爲一個ConfigMap,--from-file=File_Path

    #文件內容
    cat manifests/configmap/www.conf
    server {
            server_name myapp.magedu.com;
        listen 80;
        root /data/web/html
    }
    
    #經過文件建立configmap
    kubectl create configmap nginx-www --from-file=./manifests/configmap/www.conf
    
    #查看configmap
    [root@master configmap]# kubectl describe configmaps nginx-www
    Name:         nginx-www
    Namespace:    default
    Labels:       <none>
    Annotations:  <none>
    
    Data
    ====
    www.conf:
    ----
    server {
      server_name myapp.magedu.com;
        listen 80;
        root /data/web/html
    }
    
    Events:  <none>
  • 經過一個文件內多個鍵值對,--from-env-file=

    cat << EOF > env.txt
    db.host=10.0.0.50
    db.port=3306
    EOF
    kubectl create cm env-cm --from-env-file=env.txt
    若是有多個env文件, 只有最後一個env文件會生效
    [root@master configmap_test]# cat game.properties
    enemies=aliens
    lives=3
    enemies.cheat=true
    enemies.cheat.level=noGoodRotten
    secret.code.passphrase=UUDDLRLRBABAS
    secret.code.allowed=true
    secret.code.lives=30
    
    [root@master configmap_test]# cat ui.properties
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
    #執行命令建立configmap
    kubectl create configmap configmap-env --from-env-file=./game.properties --from-env-file=./ui.properties
    #能夠看到, 只有ui.properties生效了
    [root@master configmap_test]# kubectl get configmaps configmap-env -o yaml
    apiVersion: v1
    data:
      allow.textmode: "true"
      color.bad: yellow
      color.good: purple
      how.nice.to.look: fairlyNice
    kind: ConfigMap
    metadata:
      creationTimestamp: "2019-09-11T01:58:17Z"
      name: configmap-env
      namespace: default
      resourceVersion: "186936"
      selfLink: /api/v1/namespaces/default/configmaps/configmap-env
      uid: 4e36009f-267c-4713-8a7a-99d8f6dd3039
  • 事先寫好標準的configmap的yaml文件,而後kubectl apply -f 建立

    [root@master configmap]# cat test.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: cm-4
    data:
      db.host: 10.0.0.50
      db.port: "3306"
    [root@master configmap]# kubectl apply -f test.yaml
    [root@master configmap]# kubectl describe cm cm-4
    Name:         cm-4
    Namespace:    default
    Labels:       <none>
    Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                    {"apiVersion":"v1","data":{"db.host":"10.0.0.50","db.port":"3306"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"cm-4","...
    
    Data
    ====
    db.host:
    ----
    10.0.0.50
    db.port:
    ----
    3306
    Events:  <none>

configmap結合pod使用

使用ConfigMap有二種方式:api

第一種是經過環境變量的方式,直接傳遞給pod

apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-1   #name必須小寫
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    create-by: tianpei.wang
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
#建立configmap
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.magedu.com

#建立pod經過configmap注入到pod內
[root@master configmap]# kubectl apply -f pod-cm-1.yaml
pod/pod-cm-1 created

能夠看到已經成功注入到pod中了
[root@master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh
/ # printenv |grep NGINX
NGINX_SERVER_PORT=80
NGINX_SERVER_NAME=myapp.magedu.com
當以環境變量的方式注入pod時, 只在pod啓動時加載, 後續更改configmap不會同步到pod內
## 第二種是做爲volume的方式掛載到pod內
apiVersion: v1
kind: Pod
metadata:
  name: pod-cm-2   #name必須小寫
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    create-by: tianpei.wang
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
    ports:
    - name: http
      containerPort: 80
    volumeMounts:
      - name: nginxconf
        mountPath: /etc/nginx/config.d
        readOnly: true
  volumes:
    - name: nginxconf
      configMap:
        name: nginx-config
#經過上邊的yaml文件建立pod, 並將configmap以volumes的形式掛載到pod內
kubectl apply -f pod-cm-2.yaml

#能夠看到已經生效了
[root@master configmap]# 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

#將configmap的端口更改成8080
[root@master configmap]# kubectl edit cm nginx-config
configmap/nginx-config edited
[root@master configmap]# kubectl describe cm nginx-config
Name:         nginx-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
nginx_port:
----
8080
server_name:
----
myapp.magedu.com
Events:  <none>

#等待一段時間後生效了
[root@master configmap]# 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
/etc/nginx/config.d # cat nginx_port
/etc/nginx/config.d # cat nginx_port
/etc/nginx/config.d # cat nginx_port
8080
​``````shell
wget https://k8s.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties

configmap的item使用

建立configmap緩存

apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm

將configmap中的SPECIAL_LEVEL掛載到pod/etc/config/keysapp

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh","-c","sleep 3600"]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: SPECIAL_LEVEL
          path: keys

能夠看到已經生效了運維

[root@master configmap_test]# kubectl exec -ti test-pod -- /bin/sh
/ # cat /etc/config/keys
very
相關文章
相關標籤/搜索