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
[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
内置对象官方文档:https://helm.sh/docs/chart_template_guide/builtin_objects/
Release 对象描述了版本发布自身的一些信息。它包含了以下对象:
对象名称 | 描述 |
---|---|
.Release.Name | release 的名称 |
.Release.Namespace | release 的命名空间 |
.Release.IsUpgrade | 如果当前操作是升级或回滚的话,该值为 true |
.Release.IsInstall | 如果当前操作是安装的话,该值为 true |
.Release.Revision | 获取此次修订的版本号。初次安装时为 1,每次升级或回滚都会递增 |
.Release.Service | 获取渲染当前模板的服务名称。一般都是 Helm |
Values 对象描述的是 value.yaml 文件中的内容,默认为空。使用 Value 对象可以获取到 value.yaml 文件中已定义的任何数值
Value 键值对 | 获取方式 |
---|---|
name: aaron | .Values.name |
info: name: aaron | .Values.info.name |
Chart 对象用于获取 chart.yaml 文件中的内容:
对象名称 | 描述 |
---|---|
.Chart.Name | 获取 Chart 的名称 |
.Chart.Version | 获取 Chart 的版本 |
Capabilities 对象提供了关于 Kubernetes 集群相关的信息。该对象有如下方法:
对象名称 | 描述 |
---|---|
.Capabilities.APIVersions | 返回 Kubernetes 集群 API 版本信息集合 |
.Capabilities.APIVersions.Has $version | 用于检测指定的版本或资源在 Kubernetes 集群中是否可用,例如 batch/v1 或 apps/v1/Deployment |
.Capabilities.KubeVersion 和 Capabilities.KubeVersion.Version | 都用于获取 Kubernetes 的版本号 |
.Capabilities.KubeVersion.Major | Kubernetes 的主版本号 |
.Capabilities.KubeVersion.Minor | Kubernetes 的小版本号 |
Template 对象用于获取当前模板的信息,它包含如下两个对象:
对象名称 | 描述 |
---|---|
.Template.Name | 用于获取当前模板的名称和路径(例如:mychart/templates/mytemplate.yaml) |
.Template.BasePath | 用于获取当前模板的路径(例如:mychart/templates) |
删除多余配置:
[root@master ~]#tree chart-demo/ chart-demo/ ├── charts ├── Chart.yaml ├── templates │ ├── deploy.yaml │ └── service.yaml └── values.yaml 2 directories, 5 files
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 将
metadata.name
的值修改为.Release.Name
- 将
containers.name
的值改为.Release.Name
- 将
containers. image
的值改为{{ .Release.Name }}:{{ .Values.image.version }}
- 添加
containers. workingDir
容器工作目录配置- 添加
containers.command
容器启动命令配置- 添加
containers.env
环境变量配置- 将
matchLabels
与labels
的值都改为{{ .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 }} 的值就是什么。
用约定的应用名称 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 }}
image.pullPolicy
:IfNotPresent、Always- 添加
image.version
并设置为latest
- 在
imagePullSecrets
中添加secret
名称- 将
serviceAccount.create
设置为false
- 在
resources
的limits
与requests
中设置 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: {}
运行下面的命令验证配置是否正确
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
[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
我们在 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
[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.
修改 deploy 的 image 字段
上面应用名称和镜像名都用的
{{ .Release.Name }}
,这样部署服务会导致命名冲突,需要修改image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
- 1
尝试运行
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 部署多个应用
kubernetes 实战篇之 helm 示例 yaml 文件文件详细介绍