Kubernetes 與 Helm:使用同一個 Chart 部署多個應用


k8s 集群搭建好了,准備將 docker swarm 上的應用都遷移到 k8s 上,但需要一個一個應用寫 yaml 配置文件,不僅要編寫 deployment.yaml 還要編寫 service.yaml ,而很多應用的配置是差不多的,這個繁瑣工作讓人有些望而卻步。

k8s 有沒有針對這個問題的解救之道呢?發現了救星 Helm —— k8s 應用程序包管理器,實際操作體驗一下。

首先在 k8s master 節點上安裝 helm ,用下面的1行命令就可以搞定。

curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash

接下來創建一個 chart (chart 就是 helm 的包包)

helm create cnblogs-chart

注:准備基於這個 chart 部署多個不同的應用。

helm 會創建一個文件夾,我們來看看文件夾中的內容:

cnblogs-chart
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

關於這些文件的用途,詳見園子里的博文 kubernetes實戰篇之helm示例yaml文件詳細介紹

下面根據我們的部署場景修改 chart 中的這些配置文件,由於我們想使用同一個 chart 部署很多個應用,需要盡可能減少重復配置,所以在配置時會更多地基於約定。假設我們部署的應用名稱是 cache-api ,那 helm 的 release 名稱也用 cache-api ,docker 鏡像的名稱也用 cache-api ,deployment 與 service 的名稱也用 cache-api ,ConfigMap 的名稱是 cache-api-appsettings 。

修改 templates 中的配置(共享公用配置)

1)修改 deployment.yaml 中的配置

  • metadata.name 的值修改為 .Release.Name
  • containers.name 的值改為 .Release.Name
  • containers. image 的值改為 {{ .Release.Name }}:{{ .Values.image.version }}
  • 添加 containers. workingDir 容器工作目錄配置
  • 添加 containers.command 容器啟動命令配置
  • 添加 containers.env 環境變量配置
  • matchLabelslabels 的值都改為 {{ .Release.Name }}
  • 添加將 configMap 安裝為 volume 的配置用於應用讀取 appsettings.Production.json 。
metadata:
  name: {{ .Release.Name }}
  labels:
    name: {{ .Release.Name }}
spec:
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
  spec:
    containers:
      - name: {{ .Release.Name }}    
        image: "{{ .Release.Name }}:{{ .Values.image.version }}"
        workingDir: /app
        command: 
            - sh
            - run.sh
        env:
            - name: TZ
              value: "Asia/Shanghai"
         volumeMounts:
            - name: appsettings
              mountPath: /app/appsettings.Production.json
              subPath: appsettings.Production.json
              readOnly: true
          volumes:
        - name: appsettings
          configMap:  
            name: "{{ .Release.Name }}-appsettings"

2)修改 service.yaml

也是用約定的應用名稱 name: {{ .Release.Name }}

kind: Service
metadata:
  name: {{ .Release.Name  }}
  labels:
    name: {{ .Release.Name }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: {{ .Release.Name }}

修改 values.yaml 中的配置(共享默認配置)

  • image.pullPolicy 修改為 Always
  • 添加 image.version 並設置為 latest
  • imagePullSecrets 中添加 secret 名稱。
  • serviceAccount.create 設置為 false 。
  • resourceslimitsrequests 中設置 CPU 與內存限制。
replicaCount: 1

image:
  repository: {}
  version: latest
  pullPolicy: Always

imagePullSecrets: 
  - name: regcred
nameOverride: ""
fullnameOverride: ""

serviceAccount:
  create: false
  name:

podSecurityContext: {}
securityContext: {}

service:
  type: ClusterIP
  port: 80

ingress:
  enabled: false

resources: 
  limits:
    cpu: 2
    memory: 2G
  requests:
     cpu: 100m
     memory: 64Mi

nodeSelector: {}
tolerations: []
affinity: {}

部署應用

1)驗證配置

運行下面的命令驗證配置是否正確

helm install cache-api --set image.version=1.0 --dry-run --debug .

2)部署應用

如果配置驗證通過,就可以用下面的命令部署應用了。

helm install cache-api --set image.version=1.0 .

查看已部署的應用。

helm ls                                            
NAME     	NAMESPACE 	REVISION	UPDATED                                	STATUS  	CHART              	APP VERSION
cache-api	production	1       	2020-01-22 17:17:30.414863452 +0800 CST	deployed	cnblogs-chart-0.1.0	1

3)部署多個應用

現在可以基於前面創建的 chart 部署多個應用,只需通過 helm install 命令上傳參數傳遞應用的相關配置信息,比如部署 news-web 與 q-web 這2個應用,可以分別使用下面的命令:

helm install news-web --set image.version=1.0.4,resources.limits.cpu=1 --dry-run --debug cnblogs-chart/
helm install ing-web --set image.version=1.3.11,resources.limits.cpu=1.5 --dry-run --debug cnblogs-chart/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM