kubernetes的配置中心configmap

在企業中,一般都有2-4種環境,開發,測試,交付,生產這四種。這幾種環境的配置也有所變化,我們在部署的時候通常不能一個配置文件部署四個環境。不通用。特別是容器化環境。

在容器化應用中,每個環境都要獨立的打一個鏡像再給鏡像一個特有的tag,這很麻煩,這就要用到k8s原生的配置中心configMap就是用解決這個問題的。下面看示例。

使用configMap部署應用。
這裏使用nginx來做示例,簡單粗暴。
默認的nginx數據目錄是在:/usr/share/nginx/html 目錄,下面我寫一個配置指定端口和數據目錄

1、先將配置文件加載到configMap中
編寫nginx配置

[root@server yaml]# vim nginx.conf 
server {
   listen       8081;           ##端口爲8081
   server_name  _;
   root         /html;           ##改數據目錄爲/html

   location / {
   }

}

將上面寫的nginx.conf加載到configMap中
創建configMap有三個方法
1)單個key
2)基於目錄,將目錄下的所有文件加載到configMap中
3)基於單個file

這裏使用第3種:基於單個file的方法,其它方法也很簡單,可自行百度,google
格式:
命令 動作 模塊 自定義名稱 加載的文件
kubectl create configmap nginx-config --from-file=./nginx.conf

[root@server yaml]# kubectl create configmap nginx-config --from-file=./nginx.conf 
configmap/nginx-config created                            ##提示創建成功
[root@server yaml]# 
[root@server yaml]# kubectl get configmaps        ##查看創建的nginx-config配置是否成功
NAME           DATA   AGE
nginx-config   1      5s

驗證configMap中的nginx-config配置是否和先前vi的nginx.conf一樣

[root@server yaml]# kubectl get configmaps/nginx-config -o yaml
apiVersion: v1
data:
  nginx.conf: |+                                               ##這一段就是內容,nginx.conf是該文件的鍵
    server {
       listen       8081;
       server_name  _;
       root         /html;

       location / {
       }

    }

kind: ConfigMap
metadata:
  creationTimestamp: "2019-01-31T09:20:08Z"
  name: nginx-config
  namespace: default
  resourceVersion: "416047"
  selfLink: /api/v1/namespaces/default/configmaps/nginx-config
  uid: 67199b66-2539-11e9-821c-000c2963b9a7
[root@server yaml]# 

可以看到data:下面的nginx.conf裏面的內容和我們上面vi的內容一致
接下來在部署應用的時候使用該配置

編寫部署應用的yaml文件

[root@server yaml]# vim  nginx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx:1.9
        ports:
        - containerPort: 8081
        volumeMounts:                              --就是這一段使用configMap配置
        - mountPath: /etc/nginx/conf.d      --將配置文件掛載到哪裏
          name: config
        - mountPath: /html                         --指定數據目錄
          name: data
      volumes:
        - name: data                                  --指定數據目錄創建
          emptyDir: {}
        - name: config                                --指定config使用configMap
          configMap:
            name: nginx-config                    --指定使用configMap中的nginx-config配置
            items:
            - key: nginx.conf                        --使用nginx-config配置的nginx.conf鍵裏的內容
              path: nginx.conf

---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  type: NodePort
  selector:
    app: my-nginx
  ports:
    - protocol: TCP
      port: 8081
      targetPort: 8081
      nodePort: 28081

部署應用

[root@server yaml]# kubectl create -f nginx.yaml 
deployment.extensions/my-nginx created
service/nginx-svc created
[root@server yaml]# 
[root@server yaml]# 
[root@server yaml]# kubectl get pod 
NAME                        READY   STATUS    RESTARTS   AGE
my-nginx-55fd7587b7-7fscq   1/1     Running   0          9s
[root@server yaml]# 
[root@server yaml]# kubectl get svc |grep nginx
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
nginx-svc    NodePort    10.68.230.130   <none>        8081:28081/TCP   15s

部署成功,pod已經成功運行,服務發現已經做好了28081外部訪問入口

2、驗證nginx應用是否使用了configMap中的nginx-config
登陸部署的應用pod裏去查看一下

[root@server yaml]# kubectl exec -it my-nginx-55fd7587b7-7fscq bash     --登陸容器
root@my-nginx-55fd7587b7-7fscq:/# 
root@my-nginx-55fd7587b7-7fscq:/# cat /etc/nginx/conf.d/nginx.conf        --查看配置確實是上面vi的內容
server {
   listen       8081;
   server_name  _;
   root         /html;

   location / {
   }

}

root@my-nginx-55fd7587b7-7fscq:/# 
root@my-nginx-55fd7587b7-7fscq:/# ls -d /html/                                     --數據目錄也已經創建好了
/html/
root@my-nginx-55fd7587b7-7fscq:/#
root@my-nginx-55fd7587b7-7fscq:/# ls -l /etc/nginx/conf.d/
total 0
lrwxrwxrwx 1 root root 17 Jan 31 09:31 nginx.conf -> ..data/nginx.conf        --可以看到這個配置文件是一個相對路徑不是實體文件

上面已經驗證了應用確實使用了configMap的配置

這樣我們就可以把不同的環境配置文件都在k8s裏建一份,在部署的時候指定對應環境的配置文件即可。
這就解決了不同環境,在部署應用時要重新打包鏡像的問題。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章