Skip to content

1. 手动发布Helm

1.1 创建

bash
helm create demo
helm create demo
  • 查看目录结构
bash
[root@kube-master demo]# tree -L 2
.
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
└── values.yaml

3 directories, 9 files
[root@kube-master demo]# tree -L 2
.
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
└── values.yaml

3 directories, 9 files
  • 修改配置文件
  1. 编辑自描述文件 Chart.yaml , 修改version和appVersion信息
yaml
apiVersion: v2
name: demo
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.0 # 修改 项目版本号

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "v1" # 修改 镜像版本
apiVersion: v2
name: demo
description: A Helm chart for Kubernetes

# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application

# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 1.0.0 # 修改 项目版本号

# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "v1" # 修改 镜像版本
  1. 编辑values.yaml配置文件

修改副本数和镜像

image-20240716174336009

1.2 打包Chart

  • 检查语法是否正确
bash
helm lint demo
helm lint demo
  • 打包自定义的chart
bash
[root@kube-master helm]# helm package demo
Successfully packaged chart and saved it to: /root/helm/demo-0.1.0.tgz
[root@kube-master helm]# helm package demo
Successfully packaged chart and saved it to: /root/helm/demo-0.1.0.tgz

1.3 发布Chart

将打包好的Chart发布到一个Helm Repository中。可以使用helm repo add命令添加一个Repository,然后使用helm push命令将Chart推送到Repository中

helm repo add myrepo https://example.com/charts
helm push nginx-helm-1.0.0.tgz myrepo
helm repo add myrepo https://example.com/charts
helm push nginx-helm-1.0.0.tgz myrepo

1.4 安装Release

bash
[root@kube-master helm]# helm  install demo demo-0.1.0.tgz
NAME: demo
LAST DEPLOYED: Tue Jul 16 17:46:20 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=demo,app.kubernetes.io/instance=demo" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
[root@kube-master helm]# helm  install demo demo-0.1.0.tgz
NAME: demo
LAST DEPLOYED: Tue Jul 16 17:46:20 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=demo,app.kubernetes.io/instance=demo" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace default $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace default port-forward $POD_NAME 8080:$CONTAINER_PORT
  • 查看pod
bash
[root@kube-master helm]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS      AGE
demo-857bf7c87b-l9lkv                     1/1     Running   0             17s
demo-857bf7c87b-mrx7x                     1/1     Running   0             17s
[root@kube-master helm]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS      AGE
demo-857bf7c87b-l9lkv                     1/1     Running   0             17s
demo-857bf7c87b-mrx7x                     1/1     Running   0             17s

1.5 管理Release

更新

  1. 编辑自描述文件 Chart.yaml , 修改version和appVersion信息
  2. 重新打包
  • 检查语法
helm lint demo
helm lint demo
  • 打包
helm package demo
helm package demo
  1. 更新chart
bash
helm upgrade demo demo-2.0.0.tgz
helm upgrade demo demo-2.0.0.tgz

回滚

  • 查看当前版本
bash
[root@kube-master helm]# helm list
NAME	NAMESPACE	REVISION	UPDATED                               	STATUS  	CHART     	APP VERSION
demo	default  	2       	2024-07-16 18:04:14.47907591 +0800 CST	deployed	demo-2.0.0	v2
[root@kube-master helm]# helm list
NAME	NAMESPACE	REVISION	UPDATED                               	STATUS  	CHART     	APP VERSION
demo	default  	2       	2024-07-16 18:04:14.47907591 +0800 CST	deployed	demo-2.0.0	v2
  • 查看历史版本
bash
[root@kube-master helm]# helm history demo
REVISION	UPDATED                 	STATUS    	CHART     	APP VERSION	DESCRIPTION
1       	Tue Jul 16 18:03:50 2024	superseded	demo-1.0.0	v1         	Install complete
2       	Tue Jul 16 18:04:14 2024	deployed  	demo-2.0.0	v2         	Upgrade complete
[root@kube-master helm]# helm history demo
REVISION	UPDATED                 	STATUS    	CHART     	APP VERSION	DESCRIPTION
1       	Tue Jul 16 18:03:50 2024	superseded	demo-1.0.0	v1         	Install complete
2       	Tue Jul 16 18:04:14 2024	deployed  	demo-2.0.0	v2         	Upgrade complete
  • 回滚指定版本
bash
[root@kube-master helm]# helm rollback demo 1
Rollback was a success! Happy Helming!
[root@kube-master helm]# helm rollback demo 1
Rollback was a success! Happy Helming!

2. 案例

安装mysql

bash
 helm show values stable/mysql
 
 #下载
 helm pull stable/mysql --untar
 
 #在线安装
 helm install  chart_name stable/mysql
 helm show values stable/mysql
 
 #下载
 helm pull stable/mysql --untar
 
 #在线安装
 helm install  chart_name stable/mysql