HELM 如何使用模板变量部署多个应用

94 篇文章 9 订阅
31 篇文章 3 订阅


1. K8S 安装 Helm

curl http://49.232.8.65/shell/helm/helm-v3.5.0_install.sh | bash
helm version
helm list
helm repo list
#helm repo add stable https://charts.helm.sh/stable
#helm repo add stable http://mirror.azure.cn/kubernetes/charts
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update
helm repo remove stable

2. 创建 chart

[root@master ~]#helm create zc-chart
Creating zc-chart
[root@master ~]#ls
zc-chart
[root@master ~]#tree zc-chart/
zc-chart/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files

3. Helm 内置对象

内置对象官方文档:https://helm.sh/docs/chart_template_guide/builtin_objects/

3.1 Release 对象

Release 对象描述了版本发布自身的一些信息。它包含了以下对象:

对象名称描述
.Release.Namerelease 的名称
.Release.Namespacerelease 的命名空间
.Release.IsUpgrade如果当前操作是升级或回滚的话,该值为 true
.Release.IsInstall如果当前操作是安装的话,该值为 true
.Release.Revision获取此次修订的版本号。初次安装时为 1,每次升级或回滚都会递增
.Release.Service获取渲染当前模板的服务名称。一般都是 Helm

3.2 Values 对象

Values 对象描述的是 value.yaml 文件中的内容,默认为空。使用 Value 对象可以获取到 value.yaml 文件中已定义的任何数值

Value 键值对获取方式
name: aaron.Values.name
info: name: aaron.Values.info.name

3.3 Chart 对象

Chart 对象用于获取 chart.yaml 文件中的内容:

对象名称描述
.Chart.Name获取 Chart 的名称
.Chart.Version获取 Chart 的版本

3.4 Capabilities 对象

Capabilities 对象提供了关于 Kubernetes 集群相关的信息。该对象有如下方法:

对象名称描述
.Capabilities.APIVersions返回 Kubernetes 集群 API 版本信息集合
.Capabilities.APIVersions.Has $version用于检测指定的版本或资源在 Kubernetes 集群中是否可用,例如 batch/v1 或 apps/v1/Deployment
.Capabilities.KubeVersion 和 Capabilities.KubeVersion.Version都用于获取 Kubernetes 的版本号
.Capabilities.KubeVersion.MajorKubernetes 的主版本号
.Capabilities.KubeVersion.MinorKubernetes 的小版本号

3.5 Template 对象

Template 对象用于获取当前模板的信息,它包含如下两个对象:

对象名称描述
.Template.Name用于获取当前模板的名称和路径(例如:mychart/templates/mytemplate.yaml)
.Template.BasePath用于获取当前模板的路径(例如:mychart/templates)

4. 使用 chart 部署多个服务

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

删除多余配置:

[root@master ~]#tree chart-demo/
chart-demo/
├── charts
├── Chart.yaml
├── templates
│   ├── deploy.yaml
│   └── service.yaml
└── values.yaml

2 directories, 5 files

(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
[root@master ~/chart-demo/templates]#cat deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  namespace: {{ .Release.Namespace }}
  #namespace: {{ default "default" .Values.ns}}
  labels:
    app: {{ .Release.Name  }}
spec:
  replicas: {{ default 2 .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: {{ .Release.Name }}:{{ .Values.image.version }}
          env:
            - name: PATH
              value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            - name: LANG
              value: C.UTF-8
            - name: JAVA_HOME
              value: /usr/lib/jvm/java-8-openjdk-amd64
            - name: JAVA_VERSION
              value: 8u111
          livenessProbe:
            httpGet:
              path: /
              port: 80
          readinessProbe:
            httpGet:
              path: /
              port: 80
          resources:
            limits:
              cpu: 500m
              memory: 2Gi
            requests:
              cpu: 250m
              memory: 256Mi

{{ .Release.Name }} 这个引用的是 helm install Release.Name testchart/, Release.Name 是什么那么 {{ .Release.Name }} 的值就是什么。

(2) 修改 service.yaml

用约定的应用名称 name: {{ .Release.Name }}

[root@master ~/chart-demo/templates]#cat service.yaml 
apiVersion: v1
kind: Service
metadata:
  name: {{ .Release.Name }}
  namespace: {{ .Release.Namespace }}
  labels:
    name: {{ .Release.Name }}
spec:
  type: {{ .Values.service.type }}
  ports:
    - port: {{ .Values.service.port }}
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: {{ .Release.Name }}

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

  • image.pullPolicy:IfNotPresent、Always
  • 添加 image.version 并设置为 latest
  • imagePullSecrets 中添加 secret 名称
  • serviceAccount.create 设置为 false
  • resourceslimitsrequests 中设置 CPU 与内存限制,deployment.yaml 和 values.yaml 有一个设置就可以
[root@master ~/zc-chart]#cat values.yaml
replicaCount: 1

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

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: {}

4.3 部署服务

(1) 验证配置

运行下面的命令验证配置是否正确

helm install nginx --debug --dry-run --set image.version=1.18.0 -n test ./chart-demo

–dry-run 和 --debug 调试参数,帮助你验证模板正确性,并把渲染后的模板打印出来,而不会真正的去部署。

[root@master ~]#helm install nginx --debug --dry-run --set image.version=1.18.0 -n test ./chart-demo
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /root/chart-demo

NAME: nginx
LAST DEPLOYED: Wed Aug 17 20:19:07 2022
NAMESPACE: test
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
image:
  version: 1.18.0

COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: {}
  version: 1.18.0
imagePullSecrets:
- name: regcred
ingress:
  enabled: false
nameOverride: ""
nodeSelector: {}
podSecurityContext: {}
replicaCount: 1
resources:
  limits:
    cpu: 2
    memory: 2G
  requests:
    cpu: 100m
    memory: 64Mi
securityContext: {}
service:
  port: 80
  type: ClusterIP
serviceAccount:
  create: false
  name: ""
tolerations: []

HOOKS:
MANIFEST:
---
# Source: chart-demo/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: test
  labels:
    name: nginx
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: nginx
---
# Source: chart-demo/templates/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: test
  #namespace: default
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.18.0
          env:
            - name: PATH
              value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            - name: LANG
              value: C.UTF-8
            - name: JAVA_HOME
              value: /usr/lib/jvm/java-8-openjdk-amd64
            - name: JAVA_VERSION
              value: 8u111
          livenessProbe:
            httpGet:
              path: /
              port: http
          readinessProbe:
            httpGet:
              path: /
              port: http
          resources:
            limits:
              cpu: 500m
              memory: 2Gi
            requests:
              cpu: 250m
              memory: 256Mi

(2) 部署应用

[root@master ~]#helm install nginx --set image.version=1.18.0 -n test ./chart-demo
NAME: nginx
LAST DEPLOYED: Wed Aug 17 20:27:55 2022
NAMESPACE: test
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master ~]#kubectl get pods,deploy -o wide -n test
NAME                         READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
pod/nginx-6f6448cc7c-fzjw4   1/1     Running   0          2s    10.244.2.11   node02   <none>           <none>
pod/nginx-6f6448cc7c-vzqhb   1/1     Running   0          1s    10.244.1.14   node01   <none>           <none>

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
deployment.apps/nginx   2/2     2            2           2s    nginx        nginx:1.18.0   app=nginx

(3) 部署其他应用

我们在 deployment.yaml 增加了就绪和存活探针检测,限定了端口 80,部署其他服务可能不通用,删除此段配置

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .Release.Name }}
  namespace: {{ .Release.Namespace }}
  #namespace: {{ default "default" .Values.ns}}
  labels:
    app: {{ .Release.Name  }}
spec:
  replicas: {{ default 2 .Values.replicas }}
  selector:
    matchLabels:
      app: {{ .Release.Name }}
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}
    spec:
      containers:
        - name: {{ .Release.Name }}
          image: {{ .Release.Name }}:{{ .Values.image.version }}
          env:
            - name: PATH
              value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            - name: LANG
              value: C.UTF-8
            - name: JAVA_HOME
              value: /usr/lib/jvm/java-8-openjdk-amd64
            - name: JAVA_VERSION
              value: 8u111
          resources:
            limits:
              cpu: 500m
              memory: 2Gi
            requests:
              cpu: 250m
              memory: 256Mi

部署 redis

[root@master ~]#helm install redis --set image.version=latest -n test ./chart-demo
NAME: redis
LAST DEPLOYED: Wed Aug 17 20:43:31 2022
NAMESPACE: test
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master ~]#kubectl get pods,deploy -o wide -n test
NAME                         READY   STATUS    RESTARTS   AGE    IP            NODE     NOMINATED NODE   READINESS GATES
pod/nginx-6f6448cc7c-fzjw4   1/1     Running   0          13m    10.244.2.11   node02   <none>           <none>
pod/nginx-6f6448cc7c-vzqhb   1/1     Running   0          13m    10.244.1.14   node01   <none>           <none>
pod/redis-79c48bc987-pmb5m   1/1     Running   0          2m6s   10.244.1.18   node01   <none>           <none>
pod/redis-79c48bc987-w7lfr   1/1     Running   0          2m6s   10.244.2.15   node02   <none>           <none>

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES         SELECTOR
deployment.apps/nginx   2/2     2            2           13m    nginx        nginx:1.18.0   app=nginx
deployment.apps/redis   2/2     2            2           2m6s   redis        redis:latest   app=redis

(4) 卸载

[root@master ~]#kubectl get pods,deploy -o wide -n test
NAME                         READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
pod/nginx-6f6448cc7c-fzjw4   1/1     Running   0          16m     10.244.2.11   node02   <none>           <none>
pod/nginx-6f6448cc7c-vzqhb   1/1     Running   0          16m     10.244.1.14   node01   <none>           <none>
pod/redis-79c48bc987-pmb5m   1/1     Running   0          5m22s   10.244.1.18   node01   <none>           <none>
pod/redis-79c48bc987-w7lfr   1/1     Running   0          5m22s   10.244.2.15   node02   <none>           <none>

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES         SELECTOR
deployment.apps/nginx   2/2     2            2           16m     nginx        nginx:1.18.0   app=nginx
deployment.apps/redis   2/2     2            2           5m22s   redis        redis:latest   app=redis
[root@master ~]#helm uninstall redis -n test
release "redis" uninstalled
[root@master ~]#helm uninstall nginx -n test
release "nginx" uninstalled
[root@master ~]#kubectl get pods,deploy -o wide -n test
No resources found in test namespace.

5. 模板优化

修改 deploy 的 image 字段

上面应用名称和镜像名都用的 {{ .Release.Name }},这样部署服务会导致命名冲突,需要修改

image: {{ .Values.image.repository }}:{{ .Values.image.tag }}

尝试运行

helm install xyz-product -n test --debug --dry-run --set image.repository=registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus --set image.tag=v2.34.0 ./chart-demo
[root@master ~]#helm install xyz-product -n test --debug --dry-run --set image.repository=registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus --set image.tag=v2.34.0 ./chart-demo
install.go:173: [debug] Original chart version: ""
install.go:190: [debug] CHART PATH: /root/chart-demo

coalesce.go:200: warning: cannot overwrite table with non table for repository (map[])
NAME: xyz-product
LAST DEPLOYED: Wed Aug 17 21:58:14 2022
NAMESPACE: test
STATUS: pending-install
REVISION: 1
TEST SUITE: None
USER-SUPPLIED VALUES:
image:
  repository: registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus
  tag: v2.34.0

coalesce.go:200: warning: cannot overwrite table with non table for repository (map[])
COMPUTED VALUES:
affinity: {}
fullnameOverride: ""
image:
  pullPolicy: IfNotPresent
  repository: registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus
  tag: v2.34.0
  version: latest
imagePullSecrets:
- name: regcred
ingress:
  enabled: false
nameOverride: ""
nodeSelector: {}
podSecurityContext: {}
replicaCount: 1
resources:
  limits:
    cpu: 2
    memory: 2G
  requests:
    cpu: 100m
    memory: 64Mi
securityContext: {}
service:
  port: 80
  type: ClusterIP
serviceAccount:
  create: false
  name: ""
tolerations: []

HOOKS:
MANIFEST:
---
# Source: chart-demo/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
  name: xyz-product
  namespace: test
  labels:
    name: xyz-product
spec:
  type: ClusterIP
  ports:
    - port: 80
      targetPort: http
      protocol: TCP
      name: http
  selector:
    app: xyz-product
---
# Source: chart-demo/templates/deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: xyz-product
  namespace: test
  #namespace: default
  labels:
    app: xyz-product
spec:
  replicas: 2
  selector:
    matchLabels:
      app: xyz-product
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: xyz-product
    spec:
      containers:
        - name: xyz-product
          image: registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus:v2.34.0
          env:
            - name: PATH
              value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
            - name: LANG
              value: C.UTF-8
            - name: JAVA_HOME
              value: /usr/lib/jvm/java-8-openjdk-amd64
            - name: JAVA_VERSION
              value: 8u111
          resources:
            limits:
              cpu: 500m
              memory: 2Gi
            requests:
              cpu: 250m
              memory: 256Mi

运行查看结果

[root@master ~]#helm install xyz-product -n test --set image.repository=registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus --set image.tag=v2.34.0 ./chart-demo
coalesce.go:200: warning: cannot overwrite table with non table for repository (map[])
NAME: xyz-product
LAST DEPLOYED: Wed Aug 17 21:58:32 2022
NAMESPACE: test
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@master ~]#kubectl get pods,deploy,svc -o wide -n test
NAME                               READY   STATUS    RESTARTS   AGE     IP            NODE     NOMINATED NODE   READINESS GATES
pod/xyz-product-76496fccc4-46zbq   1/1     Running   0          4m46s   10.244.2.17   node02   <none>           <none>
pod/xyz-product-76496fccc4-j5grs   1/1     Running   0          4m46s   10.244.1.20   node01   <none>           <none>

NAME                          READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS    IMAGES                                                             SELECTOR
deployment.apps/xyz-product   2/2     2            2           4m46s   xyz-product   registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus:v2.34.0   app=xyz-product

NAME                  TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE     SELECTOR
service/xyz-product   ClusterIP   10.107.135.218   <none>        80/TCP    4m46s   app=xyz-product
[root@master ~]#helm list -n test
NAME       	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART           	APP VERSION
xyz-product	test     	1       	2022-08-17 21:58:32.387961176 +0800 CST	deployed	chart-demo-0.1.0	1.22.5     

Kubernetes 与 Helm:使用同一个 Chart 部署多个应用

使用 Helm 批量部署应用

kubernetes 实战篇之 helm 示例 yaml 文件文件详细介绍

如果 HELM 模板中不存在命名空间,如何创建该命名空间

k8s 中使用 helm

helm 3 定制模板


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值