Helm3 Practical Tutorial | Helm3 Common Commands and Deployment Application Practical Cases

Video source: Station B "k8s (Kubernetes) cluster orchestration tool helm3 practical tutorial"

Organize the teacher's course content and test notes while studying, and share them with everyone. Any infringement will be deleted. Thank you for your support!

Attach a summary post: k8s cluster orchestration tool helm3 practical tutorial | summary


1 Common commands of helm3

  1. Helm's common commands use

version: View the helm client version

repo: add, list, remove, update and index chart warehouse, available subcommands: add, index, list, remove, update

search: search for chart packages based on keywords

show: View the basic and detailed information of the chart package, available subcommands: all, chart, readme, values

pull: download and pull the chart package from the remote warehouse and decompress it locally, such as: # helm pull test-repo/tomcat --version 0.4.3 --untar, --untar is decompression, if not added, it is a compressed package

create: Create a chart package and specify the chart package name

install: Install a release instance through the chart package

list: list release instance names

upgrade: update a release instance

rollback: Roll back the release instance from the previous version, and you can also specify the version number to roll back

uninstall: uninstall a release instance

history: get release history, usage: helm history release instance name

package: Package the chart directory into a chart archive file, for example: if we modify the chart, it needs to be packaged. # helm package /opt/helm/work/tomcat (chart directory path)

get: download a release, available subcommands: all, hooks, manifest, notes, values

status: display the status of the release instance name, display the status of the named version

  1. Helm adds chart warehouse and view warehouse similar to yum warehouse or docker warehouse

Add warehouse:

You can add multiple warehouses. When adding a warehouse, remember to name the warehouse, such as: stable, aliyun, or others. Generally, the stable version will be used first.

# helm repo add stable http://mirror.azure.cn/kubernetes/charts #添加微软的,强烈推荐
# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts #添加阿里云的
# helm repo add test-repo http://mirror.kaiyuanshe.cn/kubernetes/charts/ #添加开源社区的
# helm repo list #列出仓库
NAME       URL
test-repo  http://mirror.kaiyuanshe.cn/kubernetes/charts/
stable     http://mirror.azure.cn/kubernetes/charts 
aliyun     https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

Update the repository:

# helm repo update #更新仓库,能更新添加的所有仓库
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "aliyun" chart repository
...Successfully got an update from the "test-repo" chart repository 
...Successfully got an update from the "stable" chart repository

Delete warehouse: helm repo remove warehouse name

# helm repo remove test-repo
  1. Create a chart package:
# helm create mychart #创建一个chart包, chart包名为:mychart
  1. Search the chart package of the remote warehouse, view the chart package information, pull the chart package of the remote warehouse to the local similar to docker search to search the mirror of the harbor warehouse

(1) Search chart package format: helm search repo chart package name

Example:

# helm search repo tomcat
NAME          CHART VERSION   APP VERSION    DESCRIPTION 
stable/tomcat 0.4.3           7.0            DEPRECATED-Deploy a basic tomcat application ...

(2) Check the chart package format: helm show chart chart package name or helm show values ​​chart package name (see details)

Example:

# helm show charts table/tomcat #查看chart包基础信息
apiVersion: v1
appVersion: "7.0"
deprecated: true 
description: DEPRECATED - Deploy a basic tomcat application server with sidecar as web archive container
home: https://github.com/yahavb 
icon: http://tomcat.apache.org/res/images/tomcat.png 
name: tomcat 
version: 0.4.3
# helm show values stable/tomcat #查看chart包详细信息
replicaCount: 1
image:
  webarchive:
    repository: ananwaresystems/webarchive 
    tag: "1.0"
  tomcat:
    repository: tomcat 
    tag: "7.0"
  pullPolicy: IfNotPresent 
  pullSecrets: []
deploy:
  directory: /usr/local/tomcat/webapps
service:
  name: http
  type: LoadBalancer 
  externalPort: 80
  internalPort: 8080
hostPort: 8009
ingress:
  enabled: false 
  annotations: {}
...

(3) Pull chart package format:

# helm pull 远程仓库chart包名 --version 0.4.3 --untar #从远程仓库拉取指定版本的chart包到本地并解压, --untar是解压, 不加就是压缩包
# helm pull 远程仓库chart包名 --untar #从远程仓库拉取最新版本的chart包到本地并解压, --untar是解压, 不加就是压缩包

Example:

# helm pulls table/tomcat:0.4.3 --untar 
# helm pull test-repo/tomcat --version 0.4.3 --untar #从远程仓库拉取指定版本的chart包到本地并解压
# ls tomcat/
Chart.yaml README.md templates values.yaml 
# helm pull test-repo/tomcat --untar #从远程仓库拉取最新版本的chart包到本地并解压
# ls tomcat/
Chart.yaml README.md templates values.yaml
  1. Helm installs a release instance name through various types of char packages to deploy k8s-related resources (such as: pod, deployment, svc, ingress, etc., defined according to the template file)

1) Install the release instance from the chart official warehouse added to the local (installed online from the official warehouse)

2) Install the release instance with the compressed package pulled from the chart warehouse (the downloaded compressed package is installed offline locally for release)

3) After decompressing the compressed package pulled from the chart warehouse, install the release instance from the decompressed directory (decompress the downloaded compressed package, and install the release instance offline from the decompressed directory)

4) Install the release instance directly from a network address (such as an http server) warehouse compression package

5) Create a chart package locally, edit your own yaml file by customizing, and install the release instance through the local chart package

# helm search repo tomcat
# helm install tomcat1 stable/tomcat #从加入到本地的chart社区仓库(从官方仓库在线安装) 安装release实例, tomcat1为release实例名
# helm install tomcat2 tomcat-0.4.3.tgz #从chart仓库拉下来的压缩包进行安装release实例(从本地存档文件离线安装) 
# helm install tomcat3 tomcat #从chart仓库拉下来的压缩包解压后, 从解压目录安装release实例(从解压目录离线安装) 
# helm install db http://url.../mysql-1.6.9.tqz #从一个网络地址仓库压缩包直接安装release实例(从下载服务器安装), db为release实例名

Uninstall the release instance:

# helm uninstall release实例名 后面详细介绍命令
  1. Helm basic command usage (mainly three commands)

install installs the release instance (actually the installation of the k8s application)

upgrade Upgrade the release instance (actually the upgrade of the k8s application)

rollback rolls back the release instance (actually the rollback of the k8s application)

2 Practical cases of helm3 deployment applications (release, upgrade, rollback, uninstall)

  1. Review the commonly used helm basic commands (mainly three commands)

install installs the release instance (actually the installation of the k8s application)

upgrade Upgrade the release instance (actually the upgrade of the k8s application)

rollback rolls back the release instance (actually the rollback of the k8s application)

Install the release instance: (The installation of the release instance from various angles has been introduced above, and we will use one of them to demonstrate the installation below)

# helm create chart名  #指定chart名, 创建一个chart包, 通过对chart里template的yaml文件自定义编写成自己需要的后再进行安装
# helm install release实例名 chart目录路径 #指定release实例名和chart包里录路径进行安装release实例

Example:

# helm create mychart #创建一个chart包, chart包名为:mychart 
# helm install test-release ./mychart #指定release实例名和chart包目录路径进行安装release实例

Upgrade the release instance:

# helm upgrade release实例名 chart名 --set imageTag=1.19 #指定release名和chart名进行相关set设置的升级
# helm upgrade release实例名 chart名 -f /.../mychart/values.yaml #指定release示例名和chart名和values.yaml文件升级

Example:

# helm upgrade test-release-nginx mychart --set imageTag=1.19 #指定release实例名和chart名set升级
# helm upgrade test-release-nginx mychart -f /root/helm/mychart/values.yaml #指定release示例名和chart名和values.yaml文件升级

Roll back the release instance:

# helm rollback release实例名 #指定release实例名, 回滚到上一个版本
# helm rollback release实例名 版本号 #指定release实例名, 回滚到指定版本, 注意版本号是release的版本号, 不是镜像版本号

Example:

# helm rollback web-nginx
# helm rollback web-nginx 1.17.10

Get release instance history:

# helm history release实例名

Example:

# helm history test #test为release实例名
REVISION UPDATED     STATUS                              CHART APP VERSION     DESCRIPTION 
1                    Sun Nov 27 12:34:52 2022 deployed   tomcat-0.4.37.0       Install complete

Uninstall the release instance:

# helm uninstall release实例名

Example:

# helm uninstall test-release-nginx #uninstall直接跟release名, 卸载release实例
  1. Helm3 deploys custom application cases (release, upgrade, rollback, uninstall) to deploy nginx service as an example, other applications are similar

1) Prepare the environment k8s cluster

# kubectl get node
NAME     STATUS    ROLES                   AGE   VERSION 
m1       Ready     control-plane,master    253d  v1.20.4
m2       Ready     control-plane,master    253d  v1.20.4
m3       Ready     control-plane,master    253d  v1.20.4
n1       Ready     <none>                  253d  v1.20.4

2) Create a template chart package, delete the original content, and customize it to the content we need. Later, we will customize the deployed yaml file

[root@m1 ~]# helm create nginx-chart
[root@m1 ~]# ls
nginx-chart
[root@m1 ~]# cd nginx-chart/
[root@m1 nginx-chart]# ls
charts Chart.yaml templates values.yaml
[root@m1 nginx-chart]# > values.yaml #清空所有默认定义的变量,后面我们根据需要自定义
[root@m1 nginx-chart]# rm -rf templates/* #删除原来所有默认的部署yaml文件, 后面我们根据需要自定义

Template yaml file for custom deployment:

[root@m1 nginx-chart]# vim templates/nginx-deploy-service.yaml #自定义需要的yaml模板文件, deployment和svc, 通过nodeport暴露
apiVersion:apps/v1
kind: Deployment 
metadata:
  name: {
   
   { .Values.deployment_name }}
spec:
  replicas: {
   
   { .Values.replicas }}
  selector: 
    matchLabels:
      app: {
   
   { .Values.pod_label }}
  template:
    metadata:
      labels:
        app: {
   
   { .Values.pod_label }}
    spec:
      containers:
      - image: {
   
   { .Values.image }}:{
   
   { .Values.imageTag }}
        name: {
   
   { .Values.container_name }}
        ports:
        - containerPort: {
   
   { .Values.containerport }}
---
apiVersion: v1
kind: Service 
metadata:
  name: {
   
   { .Values.service_name }}
  namespace: {
   
   { .Values.namespace }}
spec:
  type: NodePort 
  ports:
  - port: {
   
   { .Values.port }}
    targetPort: {
   
   { .Values.targetport }}
    nodePort: {
   
   { .Values.nodeport }}
    protocol: TCP 
  selector:
    app: {
   
   { .Values.pod_label }}
[root@m1 nginx-chart]# vim values.yaml #自定义需要的变量文件
deployment_name: nginx-deployment 
replicas: 2
pod_label: nginx-pod-label 
image: nginx 
imageTag: 1.17
container_name: nginx-container 
service_name: nginx-service 
namespace: default 
port: 80
targetport: 80
containerport: 80
nodeport: 30001

3) Install a release instance through the chart package (that is, deploy a k8s nginx application, version: 1.17)

[root@m1 ~]# ls nginx-chart/
charts Chart.yaml templates values.yaml 
[root@m1 ~1]# helm install nginx-release ./nginx-chart/  #安装一个release实例, 实例名:nginx-release
NAME: nginx-release 
LAST DEPLOYED: Sun Nov 27 14:37:52 2022
NAMESPACE: default 
STATUS: deployed 
REVISION: 1
TEST SUITE: None 
[root@m1 ~]# helm list  #列出release实例
NAME           NAMESPACE  REVISION UPDATED  STATUS                                            CHART              APP VERSION 
nginx-release  default    1                 2022-11-27 14:37:52.390161033 +0800 CST deployed  nginx-chart-0.1.0  1.16.0
[root@m1 ~]# kubectl get pod,svc,ep  #查看部署的k8s应用(nginx服务)
NAME                                   READY   STATUS   RESTARTS  AGE 
pod/nginx-deployment-5c8469b67f-5pq8m  1/1     Running  0         59s 
pod/nginx-deployment-5c8469b67f-j8c7v  1/1     Running  0         59s 
NAME                  TYPE         CLUSTER-IP   EXTERNAL-IP   PORT(S)       AGE 
service/kubernetes    ClusterIP    10.1.0.1     <none>        443/TCP       253d 
service/nginx-service NodePort     10.1.28.254  <none>        80:30001/TCP  59s 
NAME                     ENDPOINTS                                                    AGE 
endpoints/kubernetes     192.168.27.128:6443,192.168.27.129:6443,192.168.27.130:6443  253d 
endpoints/nginx-service  10.244.3.16:80,10.244.3.17:80                                59s 
[root@m1 ~]# kubectl get podnginx-deployment-5c8469b67f-5pq8m -o yaml | grep image:  #查看部署pod的镜像版本
            f:image: {}
  - image: nginx:1.17
    image: nginx:1.17
[root@m1 ~]# curl -I 10.1.28.254
HTTP/1.1 200 OK
Server: nginx/1.17.10

It can be accessed through nodeport: enter in the browser: http://192.168.27.131:30001/  can also be accessed

4) Upgrade the release instance version (upgrade nginx version 1.17 to 1.20.0)

[root@m1 ~]# vim nginx-chart/values.yaml #修改变量文件,指定相应版本
deployment_name: nginx-deployment 
replicas: 2
pod_label: nginx-pod-label 
image: nginx
imageTag: 1.20.0
container_name: nginx-container 
service_name: nginx-service 
namespace: default 
port: 80
targetport: 80
containerport: 80
nodeport: 30001
# helm upgrade nginx-release nginx-chart -f /root/nginx-chart/values.yaml #指定release实例名和chart名和values.yaml文件升级
Release "nginx-release" has been upgraded. Happy Helming!
NAME: nginx-release 
LAST DEPLOYED: Sun Nov 27 14:41:12 2022
NAMESPACE: default 
STATUS: deployed 
REVISION: 2
TEST SUITE: None
#或使用:helm upgrade nginx-release nginx-chart --set imageTag=1.20 指定release实例名和chart名set升级
[root@m1 ~]# helm list #升级后查看
NAME            NAMESPACE   REVISION UPDATED   STATUS                                              CHART              APP VERSION 
nginx-release   default     2                  2022-11-27 14:41:12.755644465 +0800 CST deployed    nginx-chart-0.1.0  1.16.0
[root@m1 ~]# kubectl get pod,svc,ep #升级后查看
NAME                                    READY   STATUS   RESTARTS   AGE 
pod/nginx-deployment-7dd78bf775-lh558   1/1     Running  0          67s 
pod/nginx-deployment-7dd78bf775-t4v98   1/1     Running  0          68s 
NAME                  TYPE         CLUSTER-IP   EXTERNAL-IP   PORT(S)       AGE 
service/kubernetes    ClusterIP    10.1.0.1     <none>        443/TCP       253d 
service/nginx-service NodePort     10.1.28.254  <none>        80:30001/TCP  4m28s
NAME                     ENDPOINTS                                                    AGE 
endpoints/kubernetes     192.168.27.128:6443,192.168.27.129:6443,192.168.27.130:6443  253d 
endpoints/nginx-service  10.244.3.16:80,10.244.3.17:80                                4m28s
[root@m1 ~]# kubectl get podnginx-deployment-7dd78bf775-lh558 -o yaml | grep image:  #查看部署pod的镜像版本
            f:image: {}
  - image: nginx:1.20.0
    image: nginx:1.20.0

5) Roll back the release instance version (roll back nginx version 1.20.0 to 1.17)

# helm rollback release实例名  #指定release实例名, 回滚到上一个版本
# helm rollback release实例名 版本号  #指定release实例名, 回滚到指定版本, 注意版本号是release的版本号, 不是镜像版本号

a) Roll back to the previous version:

[root@m1 ~]# helm rollback nginx-release #回滚到上一个版本, 指的是release的版本, 不是镜像版本
Rollback was a success! Happy Helming!
[root@m1 ~]# helm list #回滚后查看
NAME           NAMESPACE   REVISION UPDATED   STATUS                                             CHART                APP VERSION 
nginx-release  default     3                  2022-11-27 14:44:14.618247802 +0800 CST deployed   nginx-chart-0.1.0    1.16.0
[root@m1 ~]# kubectl get pod,svc,ep #回滚后查看
NAME                                    READY   STATUS   RESTARTS   AGE 
pod/nginx-deployment-5c8469b67f-7tw8c   1/1     Running  0          2m29s
pod/nginx-deployment-5c8469b67f-h4mbv   1/1     Running  0          2m28s
NAME                  TYPE         CLUSTER-IP   EXTERNAL-IP   PORT(S)       AGE 
service/kubernetes    ClusterIP    10.1.0.1     <none>        443/TCP       253d 
service/nginx-service NodePort     10.1.28.254  <none>        80:30001/TCP  8m51s
NAME                     ENDPOINTS                                                    AGE 
endpoints/kubernetes     192.168.27.128:6443,192.168.27.129:6443,192.168.27.130:6443  253d 
endpoints/nginx-service  10.244.3.16:80,10.244.3.17:80                                8m51s
[root@m1 ~]# kubectl get podnginx-deployment-7dd78bf775-lh558 -o yaml | grep image:  #查看回滚后部署的版本
            f:image: {}
  - image: nginx:1.17
    image: nginx:1.17

b) Roll back to the specified version

release version 1 image: 1.17

release version 2 image: 1.20.0

release version 3 image: 1.17 (after rolling back to 1.17, the release version will also increase)

It is currently release version 3, corresponding to mirror: version 1.17, now roll back the version to specified version 2, corresponding to mirror: 1.20.0

[root@m1 ~]# helm list 
NAME           NAMESPACE   REVISION UPDATED   STATUS                                            CHART             APP VERSION 
nginx-release  default     3                  2022-11-27 14:44:14.618247802 +0800 CST deployed  nginx-chart-0.1.0 1.16.0
[root@m1 ~]# helm rollback nginx-release 2  #回滚到指定的版本, 指的是release的版本, 不是镜像版本
Rollback was a success! Happy Helming!
[root@m1 ~]# helm list 
NAME           NAMESPACE   REVISION UPDATED   STATUS                                            CHART             APP VERSION 
nginx-release  default     4                  2022-11-27 14:52:42.547226093 +0800 CST deployed  nginx-chart-0.1.0 1.16.0
[root@m1 ~]# kubectl get pod,svc,ep  #回滚后查看
NAME                                    READY   STATUS   RESTARTS   AGE 
pod/nginx-deployment-7dd78bf775-sklx6   1/1     Running  0          43s
pod/nginx-deployment-7dd78bf775-xmjp4   1/1     Running  0          42s 
NAME                  TYPE         CLUSTER-IP   EXTERNAL-IP   PORT(S)       AGE 
service/kubernetes    ClusterIP    10.1.0.1     <none>        443/TCP       253d 
service/nginx-service NodePort     10.1.28.254  <none>        80:30001/TCP  15m
NAME                     ENDPOINTS                                                    AGE 
endpoints/kubernetes     192.168.27.128:6443,192.168.27.129:6443,192.168.27.130:6443  253d 
endpoints/nginx-service  10.244.3.16:80,10.244.3.17:80                                15m
[root@m1 ~]# kubectl get podnginx-deployment-7dd78bf775-sklx6 -o yaml | grep image:  #查看回滚后部署的版本
            f:image: {}
  - image: nginx:1.20.0
    image: nginx:1.20.0

6) Uninstall and delete the release instance

[root@m1 ~]# helm list 
NAME           NAMESPACE  REVISION UPDATED  STATUS                                             CHART              APP VERSION 
nginx-release  default    5                 2022-11-27 14:54:53.057933249 +0800 CST deployed   nginx-chart-0.1.0  1.16.0
[root@m1 ~]# helm uninstall nginx-release #卸载删除release实例
[root@m1 ~]# helm list 
NAME           NAMESPACE  REVISION UPDATED  STATUS    

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/131636151