HELM命令详解


1、Helm的命令详解

查看helm的命令帮助

[root@k8s-master01 ~]# helm --help
Usage:
  helm [command]

Available Commands:
  completion  generate autocompletions script for the specified shell
  create      create a new chart with the given name
  dependency  manage a chart's dependencies
  env         helm client environment information
  get         download extended information of a named release
  help        Help about any command
  history     fetch release history
......

Flags:
      --add-dir-header                   If true, adds the file directory to the header
      --alsologtostderr                  log to standard error as well as files
      --debug                            enable verbose output
  -h, --help                             help for helm
      --kube-apiserver string            the address and the port for the Kubernetes API server
      --kube-context string              name of the kubeconfig context to use
      --kube-token string                bearer token used for authentication
      --kubeconfig string                path to the kubeconfig file
......
Use "helm [command] --help" for more information about a command.

从上面可以看到Helm的命令有三类,分别对应repo、Chart、release

repo:

helm repo list 列举出添加的repository仓库
helm repo add [RepoName] [RepoUrl] 添加仓库
helm repo update 更新仓库

Chart:

helm search 查找可用的Chart模板
helm pull, fetch [chart URL | repo/chartname] 下载指定的Chart包到本地
helm install 根据指定的Chart 部署一个Release到K8s
helm create 创建自己的Chart模板
helm lint PATH 检查自定义的Chart模板是否正确
helm package 打包Chart,一般是一个压缩包文件
helm show, inspect [command] 查看关于chart的信息,如定义信息、README和values.yaml文件内容 [command]包含all、chart、readme、values等资源

release: release基于namespace进行隔离

helm list, ls 列出已经部署的Release
helm delete [RELEASE] 删除一个Release。
helm status [RELEASE] 查看指定的Release信息。
helm upgrade 升级某个Release。
helm rollback [RELEASE] [REVISION] 回滚Release到指定发布版本。
helm get values [RELEASE] 查看Release的配置文件值。
helm history [RELEASE] 查看某个release的升级记录

2 、使用Helm部署常见的应用

2.1 部署、测试nfs-client-provisioner

之前创建的PV都是静态PV,是提前创建好的PV,可供PVC进行绑定。如果每次都要手动创建PV,那样太麻烦了。
我们可以创建一个StorageClass,然后在定义PVC时,声明使用自定义的StorageClass,然后根据PVC动态地创建PV。PVC然后去绑定PV,这样就省去了每次需要手工创建PV的麻烦。

2.1.1 安装NFS服务器

在k8s-master01上安装nfs服务器

#在k8s-master01节点安装NFS服务器
[root@k8s-master01 helm]# yum install -y nfs-common nfs-utils  rpcbind
[root@k8s-master01 helm]# mkdir /data && chmod 777 /data
[root@k8s-master01 helm]# chown nfsnobody /data
[root@k8s-master01 helm]# cat > /etc/exports << EOF
/data *(rw,no_root_squash,no_all_squash,sync)
EOF
#启动nfs
[root@k8s-master01 helm]# systemctl start rpcbind && systemctl start nfs-server
#设置开启自启
[root@k8s-master01 helm]# systemctl enable rpcbind && systemctl enable nfs-server
在k8s-node01、k8s-node02安装客户端工具

[root@k8s-node01 helm]# yum install -y nfs-utils
[root@k8s-node01 helm]# systemctl start nfs-utils && systemctl enable nfs-utils
2.1.2 部署nfs-client-provisioner动态创建PV

在日常学习kubernetes时,经常需要使用把一些数据(例如:数据库、日志等)存储起来,不随着容器的删除而丢失,也就是说一些pod需要持久化存储。对于需要持久化的工作负载可以声明一个pvc,然后需要有合适的pv可供绑定。现在可以使用helm部署nfs-client-provisioner,可以根据pvc(在资源清单中需要storeageClass定义为nfs-client-provisioner)动态地创建pv。存储卷的实现有很多种,此处选择比较容易实现的NFS作为存储。

可以访问 nfs-client-provisioner地址里面有关于配置的详细说明。

安装nfs-client-provisioner

[root@k8s-master01 helm]# helm install nfs-client-provisioner stable/nfs-client-provisioner --set nfs.server=x.x.x.x --set nfs.path=/xxxx --set storageClass.defaultClass=true

上面的命令创建一个release实例、配置nfs-server地址、挂载的路径、设置为默认的class

查看创建的nfs-client-provisioner

[root@k8s-master01 nfs-client-provisioner]# helm list
NAME                  	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART                       	APP VERSION
nfs-client-provisioner	default  	1       	2020-09-23 15:04:48.690645046 +0800 CST	deployed	nfs-client-provisioner-1.2.9	3.1.0

查看storageclass

[root@k8s-master01 nfs]# kubectl get sc
NAME                   PROVISIONER                            AGE
nfs-client (default)   cluster.local/nfs-client-provisioner   63m

这里default的含义是当定义pvc时,不声明storageClassName时,就会使用默认的default。

查看nfs-client-provisioner对应的Pod

[root@k8s-master01 nfs-client-provisioner]# kubectl get pod
NAME                                      READY   STATUS    RESTARTS   AGE
nfs-client-provisioner-6999f4847d-ck52d   1/1     Running   0          34m

2.1.3 测试能否根据pvc动态创建pv

创建一个pvc检测能否动态创建pv,并且绑定到pv中

[root@k8s-master01 nfs]# cat > test-pvc.yaml << EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: test-nfs-pvc
spec:
  accessModes:

  - ReadWriteMany
    storageClassName: nfs-client
      resources:
    requests:
      storage: 1Gi
    EOF
    storageClassName: nfs-client #需要定义为刚刚创建的nfs-client。这里不写也可以,因为已经将default设置为了nfs-client。

创建pvc,查看能否动态创建pv,pvc能够绑定到对应的pv上。

[root@k8s-master01 nfs]# kubectl apply -f test-pvc.yaml 
persistentvolumeclaim/test-nfs-pvc created
[root@k8s-master01 nfs]# kubectl get pv,pvc
NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS   REASON   AGE
persistentvolume/pvc-f5207723-c172-4076-bedb-f603115230eb   1Gi        RWX            Delete           Bound    default/test-nfs-pvc   nfs-client              6s

NAME                                 STATUS   VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
persistentvolumeclaim/test-nfs-pvc   Bound    pvc-f5207723-c172-4076-bedb-f603115230eb   1Gi        RWX            nfs-client     6s

这里动态创建了一个pv,可以看到PVC处于Bound的状态。
查看/nfs文件夹下的内容。这里的/nfs为使用helm创建nfs-client的release实例时设置的nfs.path的值

[root@k8s-master01 nfs]# ls -l /data
总用量 0
drwxrwxrwx 2 root root 6 923 16:03 default-test-nfs-pvc-pvc-f5207723-c172-4076-bedb-f603115230eb

文件夹的格式为

<namespace-name>-<pvc-name>-<pv-name>-<uuid>

3、关于StorageClass回收策略对数据的影响

3.1第一种配置
archiveOnDelete: "false"  
reclaimPolicy: Delete   #默认没有配置,默认值为Delete

测试结果

1.pod删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
2.sc删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
3.删除PVC后,PV被删除且NFS Server对应数据被删除

3.2第二种配置
archiveOnDelete: "false"  
reclaimPolicy: Retain  

测试结果

1.pod删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
2.sc删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
3.删除PVC后,PV不会别删除,且状态由Bound变为Released,NFS Server对应数据被保留
4.重建sc后,新建PVC会绑定新的pv,旧数据可以通过拷贝到新的PV中

3.3第三种配置
archiveOnDelete: "ture"  
reclaimPolicy: Retain  

测试结果

1.pod删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
2.sc删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
3.删除PVC后,PV不会别删除,且状态由Bound变为Released,NFS Server对应数据被保留
4.重建sc后,新建PVC会绑定新的pv,旧数据可以通过拷贝到新的PV中

3.4第四种配置
archiveOnDelete: "ture"  
reclaimPolicy: Delete 

测试结果

1.pod删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
2.sc删除重建后数据依然存在,旧pod名称及数据依然保留给新pod使用
3.删除PVC后,PV不会别删除,且状态由Bound变为Released,NFS Server对应数据被保留
4.重建sc后,新建PVC会绑定新的pv,旧数据可以通过拷贝到新的PV中
总结:除以第一种配置外,其他三种配置在PV/PVC被删除后数据依然保留


文章作者: IW
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 IW !
  目录