K8S使用Deployment&Configmap操作示例

ps:有問題可以加微信:Lover_718,添加務必加上備註!
無意中發現了一個很好的軟件測試網站,忍不住分享一下給大家。覺得很實用,所以分享給大家。點這裏可以跳轉到教程。

基於對當前系統理解及配置的規劃,通過K8S部署應用,需要三大利器。configMap用於統一管理配置信息,service用於對外提供服務,deployment用於部署pod及系統升級更新。

創建 Service

這裏給出一個創建服務示例代碼[web-test-service.yaml]:

apiVersion: v1
kind: Service
metadata:
  name: web-test
  namespace: wel
spec:
  ports:
  - name: test-8000
    port: 8000
    targetPort: 8000
  - name: test-8005
    port: 8005
    targetPort: 8005
  - name: test-8009
    port: 8009
    targetPort: 8009
  selector:
    app: web-wel
    role: front

針對各種屬性字段這裏不做過多解釋,這裏創建一個wel-test的service的資源,服務暴露的端口有:8000/8005/8009。通過selector選擇使用該service的pod,這裏選擇標籤爲“app: web-wel、role: front”的pod使用該服務。

通過命令創建服務:

kubectl create -f web-test-service.yaml

命令創建成功,可以通過如下命令查看創建的service:

kubectl get svc -n wel

刪除service:

kubectl delete svc web-test -n wel

創建configMap:

由於有好多配置相關的文件,可以把服務配置相關的文件都放在目錄wel-config中,通過如下命令在namespace爲wel下創建一個名爲wel-config的configMap:

kubectl create configmap wel-config --from-file=./wel-config/ -n wel

可以通過如下命令查看wel-config的信息,並以yaml格式展示:

kubectl get cm wel-config  -o yaml -n wel

如果後續配置文件中的信息發生調整,可以通過下述命令編輯configMap:

kubectl edit cm wel-config -n wel

刪除創建的configMap:

kubectl delete cm wel-config -n wel

創建Deployment:

給出創建deployment的代碼示例文件【web-wel-deployment.yaml】:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-wel
  namespace: wel
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web-wel
      role: front
  template:
    metadata:
      labels:
         app: web-wel
         role: front
    spec:
      containers:
        - name: web-wel
          image: wel/web:1.0.3
          ports:
            - containerPort: 8000
            - containerPort: 8005
            - containerPort: 8009
          command: ['/bin/sh']
          args: ["-c","su - wel -c "sh /home/bin/tomcat.sh start "; while true; do tailf /home/wel/logs/wel.log;sleep 10;done"]
          volumeMounts:
            - name: config
              mountPath: /home/wel/config
            - name: hosts
              mountPath: /etc/hosts
              subPath: hosts
      volumes:
        - name: config
          configMap:
            name: wel-config
            items:
            - key: config.properties
              path: config.properties
            - key: wel.conf
              path:wels.conf
        - name: hosts
          configMap:
            name: wel-config
            items:
            - key: hosts
              path: hosts

上述創建一個名爲web-wel的Deployment資源。在模板中的數據卷模塊引用Configmap中的內容給出解釋:

volumes: --數據卷定義
  - name: config  --數據的名稱,該名稱要與volumeMounts中的name值保持一致
    configMap: --數據來源與configMap
      name: wel-config  --configMap的名稱與上述創建的configMap中的name保持一致
      items:  --元素
      - key: config.properties  --對應configMap中data下面的key值
        path: config.properties  --把key對應的值放入到path對應的文件中,進入pod中查看其實是鏈接

如何在容器中掛載數據,在下面給出解釋:

volumeMounts: --數據卷掛載申明
- name: config --數據卷名稱
  mountPath: /home/wel/config  --容器中的配置目錄
- name: hosts --數據卷名稱
  mountPath: /etc/hosts --容器中的目錄
  subPath: hosts --子目錄
  • 使用mountPath會使用數據卷中的數據覆蓋容器中已有的文件,但好處是調整configMap中的數據時,pod會熱更新。編輯過configMap之後,可進入pod查看配置數據已經更新。
  • 使用subPath不會覆蓋目錄中的數據,只會覆蓋subpath中指定的文件。但不好之處在於,編輯配置文件中的hosts值,pod中對應配置值不會更新,只有重啓pod之後或者重新部署pod之後,修改的配置纔會生效

創建deployment的命令如下:

kubectl create -f web-wel-deployment.yaml

執行命令之後無報錯,可以執行命令查看deployment:

kubectl get deployment -n wel

可以查看在創建deployment時,創建的ReplicaSet用於管理pod:

kubectl get rs -n wel

通過deployment創建的ReplicaSet的命名爲:deployment名稱-隨機數。

可以通過命令刪除deployment:

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