前面讲解了很多helm的知识点,今天创建一个自己的helm案例,在实操中进一步熟悉helm的使用。
httpbin.org是我在工作中经常用的到一个网站,它可以显示返回用户的IP等一系列主机头想要的信息。官方也很贴心的制作了镜像,方便我们在本地使用,本次将用它为例,通过helm创建一个本地的httpbin的pod。
创建httpbin的heml例子
1.创建目录,并且创建一个默认的helm目录结构
root@k8s-master:~/helm# mkdir -vp helmtest
mkdir: created directory 'helmtest'
root@k8s-master:~/helm# cd helmtest/
root@k8s-master:~/helm/helmtest# helm create myapp01
Creating myapp01
root@k8s-master:~/helm/helmtest# tree myapp01/
myapp01/
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
2.修改配置文件
修改 myapp01/value.yaml
(镜像、ingress、server等信息)
image:
repository: nginx #镜像地址
# This sets the pull policy for images.
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: " "
====修改为====
image:
repository: m.daocloud.io/docker.io/kennethreitz/httpbin #镜像地址
# This sets the pull policy for images.
pullPolicy: IfNotPresent
# Overrides the image tag whose default is the chart appVersion.
tag: "latest"
...
service:
type: ClusterIP
port: 80
====修改为====
service:
type: NodePort #这里不改也可以
port: 80 #容器的端口
targetPort: 80 #暴露容器80对应server的端口
...
ingress:
enabled: false
className: ""
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
hosts:
- host: chart-example.local
paths:
- path: /
pathType: ImplementationSpecific
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
====修改为====
ingress:
enabled: true #修改了这里
className: ""
annotations: {}
kubernetes.io/ingress.class: nginx #修改了这里
# 开启use-regex,启用path的正则匹配
nginx.ingress.kubernetes.io/use-regex: "true" #修改了这里
# kubernetes.io/tls-acme: "true"
hosts:
- host: www.2025helm.com #修改了这里
paths:
- path: /
pathType: ImplementationSpecific
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
...
由于一般镜像没有自检功能,所以这一段注释
livenessProbe:
httpGet:
path: /
port: http
readinessProbe:
httpGet:
path: /
port: http
====修改为====
#livenessProbe:
# httpGet:
# path: /
# port: http
#readinessProbe:
# httpGet:
# path: /
# port: http
修改 myapp01/Chart.yaml
(版本号等信息)
# 这里可以修改名字,这里就不做修改了
name: myapp01
# 修改版本号
appVersion: "1.16.0"
====修改为====
appVersion: "202503"
修改 myapp01/templates/service.yaml
(servers的targetPort信息)
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
====修改为====
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: {{ .Values.service.targetPort}} #修改这里
protocol: TCP
name: http
修改 myapp01/templates/NOTES.txt
(安装后,显示的提示)
由于启用了ingress,如果跑成功会显示下面输入的内容
3.干跑一下看下配置是否正确
root@k8s-master:~/helm/helmtest# helm install --dry-run --debug myapp01 myapp01/
install.go:225: 2025-04-08 18:50:56.924811149 +0800 CST m=+0.204651618 [debug] Original chart version: ""
install.go:242: 2025-04-08 18:50:56.925029701 +0800 CST m=+0.204870168 [debug] CHART PATH: /root/helm/helmtest/myapp01
NAME: myapp01
LAST DEPLOYED: Tue Apr 8 18:50:56 2025
NAMESPACE: default
STATUS: pending-install
REVISION: 1
USER-SUPPLIED VALUES:
{}
...
NOTES:
1. Get the application URL by running these commands:
http://www.2025helm.com/
"If successful, you will see this message" #修改了这里
4.使用命令开始安装
root@k8s-master:~/helm/helmtest# helm install myapp01 myapp01/
NAME: myapp01
LAST DEPLOYED: Tue Apr 8 19:27:34 2025
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
http://www.2025helm.com/
"If successful, you will see this message" #修改了这里
5.测试
查看helm状态和版本号
root@k8s-master:~/helm/helmtest# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
myapp01 default 1 2025-04-08 19:27:34.588330558 +0800 CST deployed myapp01-0.1.0 202503
查看svc端口
root@k8s-master:~/helm/helmtest# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 18d
myapp01 NodePort 10.96.228.21 <none> 80:30680/TCP 39s
查看ingress
root@k8s-master:~/helm/helmtest# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
myapp01 <none> www.2025helm.com 80 30s
修改hosts后测试
root@k8s-master:~/helm/helmtest# curl http://www.2025helm.com/ip
{
"origin": "172.21.176.3"
}
6.升级版本和回滚
升级版本
修改myapp01/Chart.yaml
appVersion: "202503"
====修改为====
appVersion: "202504"
升级命令 helm upgrade myapp01 myapp01
版本变成了202504
root@k8s-master:~/helm/helmtest# helm upgrade myapp01 myapp01
Release "myapp01" has been upgraded. Happy Helming!
NAME: myapp01
LAST DEPLOYED: Tue Apr 8 20:15:19 2025
NAMESPACE: default
STATUS: deployed
REVISION: 2
NOTES:
1. Get the application URL by running these commands:
http://www.2025helm.com/
"If successful, you will see this message" #修改了这里
root@k8s-master:~/helm/helmtest#
root@k8s-master:~/helm/helmtest# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
myapp01 default 2 2025-04-08 20:15:19.996340164 +0800 CST deployed myapp01-0.1.0 202504
查看历史版本
可以看到现在已经有2个版本了
root@k8s-master:~/helm/helmtest# helm history myapp01
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Tue Apr 8 20:12:48 2025 superseded myapp01-0.1.0 202503 Install complete
2 Tue Apr 8 20:15:19 2025 deployed myapp01-0.1.0 202504 Upgrade complete
回滚版本
helm rollback myapp01 <REVISION>
# 查看目前REVISION版本为2, APP VERSION为202504
root@k8s-master:~/helm/helmtest# helm history myapp01
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Tue Apr 8 20:12:48 2025 superseded myapp01-0.1.0 202503 Install complete
2 Tue Apr 8 20:15:19 2025 deployed myapp01-0.1.0 202504 Upgrade complete
# 这里的1为通过history 查看到的REVISION
root@k8s-master:~/helm/helmtest# helm rollback myapp01 1
Rollback was a success! Happy Helming!
# 查看目前REVISION版本为3, APP VERSION又回滚为202503
root@k8s-master:~/helm/helmtest# helm history myapp01
REVISION UPDATED STATUS CHART APP VERSION DESCRIPTION
1 Tue Apr 8 20:12:48 2025 superseded myapp01-0.1.0 202503 Install complete
2 Tue Apr 8 20:15:19 2025 superseded myapp01-0.1.0 202504 Upgrade complete
3 Tue Apr 8 20:17:53 2025 deployed myapp01-0.1.0 202503 Rollback to 1
至此我们自己创建的Helm案例讲解告一段落,现在helm的使用越来越多,甚至很多第三方都已经只支持helm安装,所以学好helm很重要,后面我们也会继续在实战中再次使用!