十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團隊
量身定制 + 運營維護+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇文章給大家分享的是有關(guān)如何理解kubernetes的配置中心configmap,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。
丁青網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),丁青網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為丁青上1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的丁青做網(wǎng)站的公司定做!
在企業(yè)中,一般都有2-4種環(huán)境,開發(fā),測試,交付,生產(chǎn)這四種。這幾種環(huán)境的配置也有所變化,我們在部署的時候通常不能一個配置文件部署四個環(huán)境。不通用。特別是容器化環(huán)境。
在容器化應(yīng)用中,每個環(huán)境都要獨立的打一個鏡像再給鏡像一個特有的tag,這很麻煩,這就要用到k8s原生的配置中心configMap就是用解決這個問題的。下面看示例。
使用configMap部署應(yīng)用。
這里使用nginx來做示例,簡單粗暴。
默認的nginx數(shù)據(jù)目錄是在:/usr/share/nginx/html 目錄,下面我寫一個配置指定端口和數(shù)據(jù)目錄
1、先將配置文件加載到configMap中
編寫nginx配置
[root@server yaml]# vim nginx.conf server { listen 8081; ##端口為8081 server_name _; root /html; ##改數(shù)據(jù)目錄為/html location / { } }
將上面寫的nginx.conf加載到configMap中
創(chuàng)建configMap有三個方法
1)單個key
命令格式:
例:kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
查看:kubectl get configmaps special-config -o yaml
2)基于目錄,將目錄下的所有文件加載到configMap中
命令格式:
例:kubectl create configmap tomcat-conf --from-file=configmap/conf
查看:kubectl get configmaps tomcat-conf -o yaml
3)基于單個file
命令格式:
例:kubectl create configmap game-conf --from-file=configmap/conf/game.properties
查看:kubectl get configmaps tomcat-conf -o yaml
注意事項:
使用configmap的pod必須要和configmap配置在同一個namespaces,否則識別不了。在創(chuàng)建configmap時在后面加上 -n namespaces名稱
例:kubectl create configmap tomcat-conf --from-file=configmap/conf -n tomcat
這里使用第3種:基于單個file的方法,其它方法也很簡單。
格式:
命令 動作 模塊 自定義名稱 加載的文件
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 ##提示創(chuàng)建成功 [root@server yaml]# [root@server yaml]# kubectl get configmaps ##查看創(chuàng)建的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: |+ ##這一段就是內(nèi)容,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里面的內(nèi)容和我們上面vi的內(nèi)容一致
接下來在部署應(yīng)用的時候使用該配置
編寫部署應(yīng)用的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 --指定數(shù)據(jù)目錄 name: data volumes: - name: data --指定數(shù)據(jù)目錄創(chuàng)建 emptyDir: {} - name: config --指定config使用configMap configMap: name: nginx-config --指定使用configMap中的nginx-config配置 items: --注:也可不指定items,那默認是nginx-config里的所有值都掛載 - key: nginx.conf --使用nginx-config配置的nginx.conf鍵里的內(nèi)容 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
部署應(yīng)用
[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.1308081:28081/TCP 15s
部署成功,pod已經(jīng)成功運行,服務(wù)發(fā)現(xiàn)已經(jīng)做好了28081外部訪問入口
2、驗證nginx應(yīng)用是否使用了configMap中的nginx-config
登陸部署的應(yīng)用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的內(nèi)容 server { listen 8081; server_name _; root /html; location / { } } root@my-nginx-55fd7587b7-7fscq:/# root@my-nginx-55fd7587b7-7fscq:/# ls -d /html/ --數(shù)據(jù)目錄也已經(jīng)創(chuàng)建好了 /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 --可以看到這個配置文件是一個相對路徑不是實體文件
上面已經(jīng)驗證了應(yīng)用確實使用了configMap的配置
這樣我們就可以把不同的環(huán)境配置文件都在k8s里建一份,在部署的時候指定對應(yīng)環(huán)境的配置文件即可。
這就解決了不同環(huán)境,在部署應(yīng)用時要重新打包鏡像的問題。
以上就是如何理解kubernetes的配置中心configmap,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。