Kubernetes(k8s)包管理工具Helm:Helm包管理

一.系统环境

本文主要基于Kubernetes1.21.9和Linux操作系统CentOS7.4。

服务器版本 docker软件版本 Kubernetes(k8s)集群版本 CPU架构
CentOS Linux release 7.4.1708 (Core) Docker version 20.10.12 v1.21.9 x86_64

Kubernetes集群架构:k8scloude1作为master节点,k8scloude2,k8scloude3作为worker节点。

服务器 操作系统版本 CPU架构 进程 功能描述
k8scloude1/192.168.110.130 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kube-apiserver,etcd,kube-scheduler,kube-controller-manager,kubelet,kube-proxy,coredns,calico k8s master节点
k8scloude2/192.168.110.129 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点
k8scloude3/192.168.110.128 CentOS Linux release 7.4.1708 (Core) x86_64 docker,kubelet,kube-proxy,calico k8s worker节点

二.前言

在Kubernetes中,部署一个应用程序需要进行多个步骤,其中包括创建ConfigMap、Secret和Deployment等对象。但是,使用Helm这样的基于Kubernetes的包管理器,可以将所有这些操作打包成一个Chart,从而简化了应用程序的部署流程和管理。

使用Helm进行包管理的前提是已经有一套可以正常运行的Kubernetes集群,关于Kubernetes(k8s)集群的安装部署,可以查看博客《Centos7 安装部署Kubernetes(k8s)集群》https://www.cnblogs.com/renshengdezheli/p/16686769.html。

三.包管理工具Helm简介

Helm是一个基于Kubernetes的包管理器,它由两部分组成:Helm客户端和Tiller服务器。Helm客户端从称为Chart Repositories的位置获取预配置的Charts,然后使用Tiller将它们安装到Kubernetes集群中。

Helm是一个开源项目,旨在简化Kubernetes应用程序的安装和部署。它允许您将应用程序打包为一个称为Chart的可重复的包,并在Kubernetes集群中部署和管理这些应用程序。

具体来说,Helm解决了以下问题:

  1. 将Kubernetes应用程序打包到chart中,使其易于分享和重用。
  2. 管理应用程序依赖关系,因此不需要手动下载或配置库。
  3. 轻松升级应用程序,通过使用helm upgrade命令,在没有任何停机时间的情况下更新已部署的应用程序。
  4. 通过使用helm rollback命令,回滚到以前的版本,以防出现故障。
  5. 在不同的Kubernetes集群之间轻松移植应用程序。

Kubernetes的Helm Chart是一种用于部署应用程序的包管理器,允许用户描述、安装和升级复杂的应用程序。它是一个预定义模板的集合,这些模板描述了在Kubernetes中运行应用程序所需的所有资源,如Pod、Service、Ingress、ConfigMap等。

每个Chart都描述了一个Kubernetes资源的集合,例如一个应用程序或数据库。Chart中包含最终用户所需的所有信息,如Docker镜像名称、端口号和环境变量等。通过使用Helm,我们可以更轻松地部署和管理Kubernetes应用程序。

Helm Chart由多个文件组成,其中包括:

  • Chart.yaml:定义了当前Chart的版本、名称、描述和依赖项。
  • values.yaml:包含了指定Chart的默认值或者用户自定义的值。
  • templates目录:包含了要在Kubernetes中部署的所有资源定义文件。

使用Helm Chart可以帮助用户避免手动创建资源的复杂性和重复性,使得应用程序的部署更加容易和快速,并且支持在不同的环境中轻松地重现相同的配置。此外,还可以通过命令行修改values.yaml中的值来定制化每个Chart实例的特定配置

四.安装部署helm

创建放helm文件的目录。

[root@k8scloude1 ~]# mkdir helm

[root@k8scloude1 ~]# cd helm/

创建helm命名空间。

[root@k8scloude1 helm]# kubectl create ns helm
namespace/helm created

切换命名空间。

[root@k8scloude1 helm]# kubens helm
Context "kubernetes-admin@kubernetes" modified.
Active namespace is "helm".

下载最新版的 helm,下载地址为: https://github.com/helm/helm/releases,提前下载所需要的文件 helm-v3.8.0-linux-amd64.tar.gz 及对应的 checksum 文件。

[root@k8scloude1 helm]# ls
helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256sum

下载安装 helm 的脚本。

[root@k8scloude1 helm]# curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 > get
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 11156  100 11156    0     0  10041      0  0:00:01  0:00:01 --:--:-- 10041

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256sum

因为此脚本会自动到互联网去下载最新版的 helm,所以我们需要修改此脚本以实现使用本地已经下载好的 helm 文件。主要修改如下三个地方 ,因为我们已经下载的 helm 版本是 v3.8.0 版本,所以先修改 get 文件直接指定 helm 的版本 为v3.8.0: 在 get 103~105 行里 checkDesiredVersion 函数,改为下面内容:

[root@k8scloude1 helm]# vim get 

103	checkDesiredVersion() {
104	    TAG=v3.8.0
 105	}

126	downloadFile() {
   127	  HELM_DIST="helm-$TAG-$OS-$ARCH.tar.gz"
   128	  DOWNLOAD_URL="https://get.helm.sh/$HELM_DIST"
   129	  CHECKSUM_URL="$DOWNLOAD_URL.sha256"
   130	  HELM_TMP_ROOT="$(mktemp -dt helm-installer-XXXXXX)"
   131	  HELM_TMP_FILE="$HELM_TMP_ROOT/$HELM_DIST"
   132	  HELM_SUM_FILE="$HELM_TMP_ROOT/$HELM_DIST.sha256"
   133	  echo "Downloading $DOWNLOAD_URL"
   134	  cp helm-v3.8.0* $HELM_TMP_ROOT
   135	}

161	verifyChecksum() {
   162	  printf "Verifying checksum... "
   163	  local sum=$(openssl sha1 -sha256 ${HELM_TMP_FILE} | awk '{print $2}')
   164	  local expected_sum=$(cat ${HELM_SUM_FILE})
   165	  echo "Done."
   166	}

赋予可执行权限。

[root@k8scloude1 helm]# chmod +x get 

[root@k8scloude1 helm]# ll 
总用量 13324
-rwxr-xr-x 1 root root    10177 2月  19 16:09 get
-rw-r--r-- 1 root root 13626774 2月  19 15:55 helm-v3.8.0-linux-amd64.tar.gz
-rw-r--r-- 1 root root       97 2月  19 15:56 helm-v3.8.0-linux-amd64.tar.gz.sha256sum

重命名checksum文件。

[root@k8scloude1 helm]# cp helm-v3.8.0-linux-amd64.tar.gz.sha256sum helm-v3.8.0-linux-amd64.tar.gz.sha256

[root@k8scloude1 helm]# ll -h
总用量 14M
-rwxr-xr-x 1 root root 10K 2月  19 16:09 get
-rw-r--r-- 1 root root 13M 2月  19 15:55 helm-v3.8.0-linux-amd64.tar.gz
-rw-r--r-- 1 root root  97 2月  19 16:11 helm-v3.8.0-linux-amd64.tar.gz.sha256
-rw-r--r-- 1 root root  97 2月  19 15:56 helm-v3.8.0-linux-amd64.tar.gz.sha256sum

运行 get。

[root@k8scloude1 helm]# ./get 
Downloading https://get.helm.sh/helm-v3.8.0-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
[root@k8scloude1 helm]# helm version
version.BuildInfo{Version:"v3.8.0", GitCommit:"d14138609b01886f544b2025f5000351c9eb092e", GitTreeState:"clean", GoVersion:"go1.17.5"}

helm安装成功 。

[root@k8scloude1 helm]# ls /usr/local/bin/helm 
/usr/local/bin/helm

五.配置helm以及helm常用命令

helm默认不支持tab键自动补全命令,配置helm自动补全。

[root@k8scloude1 helm]# vim /etc/profile

#在/etc/profile里添加source <(helm completion bash)
[root@k8scloude1 helm]# grep helm /etc/profile
source <(helm completion bash)

[root@k8scloude1 helm]# source /etc/profile

查看helm版本,现在就可以自动补全命令了。

[root@k8scloude1 helm]# helm version 
version.BuildInfo{Version:"v3.8.0", GitCommit:"d14138609b01886f544b2025f5000351c9eb092e", GitTreeState:"clean", 
GoVersion:"go1.17.5"}

查看helm现在的仓库源。

[root@k8scloude1 helm]# helm repo list
Error: no repositories to show

添加微软的源。

[root@k8scloude1 helm]# helm repo add azure http://mirror.azure.cn/kubernetes/charts/
"azure" has been added to your repositories

添加阿里的源。

[root@k8scloude1 helm]# helm repo add ali https://apphub.aliyuncs.com
"ali" has been added to your repositories

查看helm现在的仓库源。

[root@k8scloude1 helm]# helm repo list
NAME 	URL                                      
azure	http://mirror.azure.cn/kubernetes/charts/
ali  	https://apphub.aliyuncs.com     

删除仓库源并重新添加。

[root@k8scloude1 helm]# helm repo rm ali
"ali" has been removed from your repositories

[root@k8scloude1 helm]# helm repo list
NAME 	URL                                      
azure	http://mirror.azure.cn/kubernetes/charts/

[root@k8scloude1 helm]# helm repo add ali https://apphub.aliyuncs.com
"ali" has been added to your repositories

[root@k8scloude1 helm]# helm repo list
NAME 	URL                                      
azure	http://mirror.azure.cn/kubernetes/charts/
ali  	https://apphub.aliyuncs.com  

在仓库里搜索可用的mysql。

[root@k8scloude1 helm]# helm search repo mysql
NAME                           	CHART VERSION	APP VERSION	DESCRIPTION                                       
ali/mysql                      	6.8.0        	8.0.19     	Chart to create a Highly available MySQL cluster  
ali/mysqldump                  	2.6.0        	2.4.1      	A Helm chart to help backup MySQL databases usi...
ali/mysqlha                    	1.0.0        	5.7.13     	MySQL cluster with a single master and zero or ...
ali/prometheus-mysql-exporter  	0.5.2        	v0.11.0    	A Helm chart for prometheus mysql exporter with...
azure/mysql                    	1.6.9        	5.7.30     	DEPRECATED - Fast, reliable, scalable, and easy...
......              
azure/mariadb                  	7.3.14       	10.3.22    	DEPRECATED Fast, reliable, scalable, and easy t...

下载一个mariadb。

[root@k8scloude1 helm]# helm pull ali/mariadb

六.使用helm安装应用

先下载一个nginx。

[root@k8scloude1 helm]# helm pull ali/nginx

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum    nginx-5.1.5.tgz

解压nginx。

[root@k8scloude1 helm]# tar xf nginx-5.1.5.tgz

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum    nginx  nginx-5.1.5.tgz

查看nginx目录。

[root@k8scloude1 helm]# cd nginx/

[root@k8scloude1 nginx]# ls
Chart.yaml  ci  README.md  templates  values.schema.json  values.yaml

安装nginx。

[root@k8scloude1 nginx]# helm install nginx .
NAME: nginx
LAST DEPLOYED: Sat Feb 19 17:26:36 2022
NAMESPACE: helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Get the NGINX URL:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace helm -w nginx'

  export SERVICE_IP=$(kubectl get svc --namespace helm nginx --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
  echo "NGINX URL: http://$SERVICE_IP/"

可以看到Nginx安装成功,pod和svc都自动创建好了。

[root@k8scloude1 nginx]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nginx-847f49b454-7bjdt   1/1     Running   0          38s   10.244.112.145   k8scloude2   <none>           <none>

[root@k8scloude1 nginx]# kubectl get svc --namespace helm -w nginx
NAME    TYPE           CLUSTER-IP       EXTERNAL-IP       PORT(S)                      AGE
nginx   LoadBalancer   10.109.104.128   192.168.110.190   80:30683/TCP,443:30462/TCP   53s

^C[root@k8scloude1 nginx]# kubectl get svc -o wide
NAME    TYPE           CLUSTER-IP       EXTERNAL-IP       PORT(S)                      AGE   SELECTOR
nginx   LoadBalancer   10.109.104.128   192.168.110.190   80:30683/TCP,443:30462/TCP   67s   app.kubernetes.io/instance=nginx,app.kubernetes.io/name=nginx

nginx可以正常访问。

[root@k8scloude1 nginx]# curl 192.168.110.190
<!DOCTYPE html>
<html>
......
<h1>Welcome to nginx!</h1>
......
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

查看已经安装了的应用。

[root@k8scloude1 nginx]# helm ls
NAME 	NAMESPACE	REVISION	UPDATED                               	STATUS  	CHART      	APP VERSION
nginx	helm     	1       	2022-02-19 17:26:36.31742504 +0800 CST	deployed	nginx-5.1.5	1.16.1     

删除nginx,对应的pod和svc都被删除了。

[root@k8scloude1 nginx]# helm delete nginx
release "nginx" uninstalled

[root@k8scloude1 nginx]# helm ls
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION

[root@k8scloude1 nginx]# kubectl get svc -o wide
No resources found in helm namespace.

[root@k8scloude1 nginx]# kubectl get pod -o wide
No resources found in helm namespace.

返回目录。

[root@k8scloude1 nginx]# pwd
/root/helm/nginx

[root@k8scloude1 nginx]# cd ../

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum   nginx  nginx-5.1.5.tgz

如果是在nginx目录外面,则这样安装nginx。

[root@k8scloude1 helm]# helm install nginx nginx
NAME: nginx
LAST DEPLOYED: Sat Feb 19 17:33:13 2022
NAMESPACE: helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Get the NGINX URL:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace helm -w nginx'

  export SERVICE_IP=$(kubectl get svc --namespace helm nginx --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
  echo "NGINX URL: http://$SERVICE_IP/"

成功安装Nginx,pod和svc都自动创建好了。

[root@k8scloude1 helm]# helm ls
NAME 	NAMESPACE	REVISION	UPDATED                                	STATUS  	CHART      	APP VERSION
nginx	helm     	1       	2022-02-19 17:33:13.542578135 +0800 CST	deployed	nginx-5.1.5	1.16.1     

[root@k8scloude1 helm]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nginx-847f49b454-fb9kn   1/1     Running   0          19s   10.244.112.146   k8scloude2   <none>           <none>

[root@k8scloude1 helm]# kubectl get svc -o wide
NAME    TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)                      AGE   SELECTOR
nginx   LoadBalancer   10.97.175.212   192.168.110.190   80:30373/TCP,443:32731/TCP   25s   app.kubernetes.io/instance=nginx,app.kubernetes.io/name=nginx

删除nginx,对应的pod和svc都被删除了。

[root@k8scloude1 helm]# helm delete nginx
release "nginx" uninstalled

[root@k8scloude1 helm]# kubectl get svc -o wide
No resources found in helm namespace.

[root@k8scloude1 helm]# kubectl get pod -o wide
No resources found in helm namespace.

七.搭建helm私有仓库

删除压缩包nginx-5.1.5.tgz 。

[root@k8scloude1 helm]# pwd
/root/helm

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum    nginx  nginx-5.1.5.tgz

[root@k8scloude1 helm]# rm -rf nginx-5.1.5.tgz 

打包nginx。

[root@k8scloude1 helm]# helm package nginx/
Successfully packaged chart and saved it to: /root/helm/nginx-5.1.5.tgz

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum  mariadb  mariadb-7.3.9.tgz  mysql  mysql-6.8.0.tgz  nginx  nginx-5.1.5.tgz

建立一个索引文件,索引文件记录了这个chart应该放在哪里?--url指定仓库。

[root@k8scloude1 helm]# helm repo index . --url http://192.168.110.129/mycharts

[root@k8scloude1 helm]# ls
get  helm-v3.8.0-linux-amd64.tar.gz  helm-v3.8.0-linux-amd64.tar.gz.sha256  helm-v3.8.0-linux-amd64.tar.gz.sha256sum  index.yaml  mariadb  mariadb-7.3.9.tgz  mysql  mysql-6.8.0.tgz  nginx  nginx-5.1.5.tgz

[root@k8scloude1 helm]# cat index.yaml 
apiVersion: v1
entries:
  mariadb:
  - apiVersion: v1
    appVersion: 10.3.22
    created: "2022-02-19T19:22:41.80690689+08:00"
    description: Fast, reliable, scalable, and easy to use open-source relational
      database system. MariaDB Server is intended for mission-critical, heavy-load
  ......
    - email: containers@bitnami.com
      name: Bitnami
    name: nginx
    sources:
    - https://github.com/bitnami/bitnami-docker-nginx
    urls:
    - http://192.168.110.129/mycharts/nginx-5.1.5.tgz
    version: 5.1.5
generated: "2022-02-19T19:22:41.805118188+08:00"

使用Nginx镜像在192.168.110.129上创建一个容器,用来作为helm仓库,-p进行端口映射,-v进行目录映射,关于docker容器的详细操作可以查看博客《一文搞懂docker容器基础:docker镜像管理,docker容器管理》。

[root@k8scloude2 ~]# docker run -dit --name=nginx --restart=always -p 80:80 -v /mycharts:/usr/share/nginx/html/mycharts nginx
ea7cd28f6dd8477f9666e13523076106f210bb10c0d754e2fcaa3c230bdb7180

查询80端口,可以看到80端口已经被监听。

[root@k8scloude2 ~]# netstat -antup | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      2926/docker-proxy   
tcp6       0      0 :::80                   :::*                    LISTEN      2932/docker-proxy   

把打包好的nginx和索引文件传过去。

[root@k8scloude1 helm]# scp nginx-5.1.5.tgz index.yaml 192.168.110.129:/mycharts
root@192.168.110.129's password: 
nginx-5.1.5.tgz                                                                                                                                                                 100% 9548     2.9MB/s   00:00    
index.yaml                                                                                                                                                                      100% 2355     2.1MB/s   00:00    

文件被传了过来,因为做了目录映射,文件已经在docker容器里了。

[root@k8scloude2 ~]# ls /mycharts/
index.yaml  nginx-5.1.5.tgz

[root@k8scloude2 ~]# docker exec -it nginx ls /usr/share/nginx/html/mycharts
index.yaml  nginx-5.1.5.tgz

添加我们自定义的helm 源,http://192.168.110.129/mycharts 就是我们的仓库地址。

[root@k8scloude1 helm]# helm repo add myhelmrepo http://192.168.110.129/mycharts
"myhelmrepo" has been added to your repositories

查看现有的仓库源。

[root@k8scloude1 helm]# helm repo list
NAME      	URL                                      
azure     	http://mirror.azure.cn/kubernetes/charts/
ali       	https://apphub.aliyuncs.com              
myhelmrepo	http://192.168.110.129/mycharts

在仓库里搜索可用的nginx,myhelmrepo/nginx这个我们私有仓库里的包也被搜索出来了。

[root@k8scloude1 helm]# helm search repo nginx
NAME                        	CHART VERSION	APP VERSION         	DESCRIPTION                                       
ali/nginx                   	5.1.5        	1.16.1              	Chart for the nginx server                        
ali/nginx-ingress           	1.30.3       	0.28.0              	An nginx Ingress controller that uses ConfigMap...
......
azure/nginx-lego            	0.3.1        	                    	Chart for nginx-ingress-controller and kube-lego  
myhelmrepo/nginx            	5.1.5        	1.16.1              	Chart for the nginx server                        
azure/gcloud-endpoints      	0.1.2        	1                   	DEPRECATED Develop, deploy, protect and monitor...

直接安装我们自定义仓库里的nginx。

[root@k8scloude1 helm]# helm install nginx myhelmrepo/nginx
NAME: nginx
LAST DEPLOYED: Sat Feb 19 19:29:46 2022
NAMESPACE: helm
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
Get the NGINX URL:

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace helm -w nginx'

  export SERVICE_IP=$(kubectl get svc --namespace helm nginx --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")
  echo "NGINX URL: http://$SERVICE_IP/"

成功安装Nginx,pod和svc都自动创建好了。

[root@k8scloude1 helm]# kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP               NODE         NOMINATED NODE   READINESS GATES
nginx-847f49b454-bthlz   0/1     Running   0          10s   10.244.112.148   k8scloude2   <none>           <none>

[root@k8scloude1 helm]# kubectl get svc -o wide
NAME    TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)                      AGE   SELECTOR
nginx   LoadBalancer   10.108.203.94   192.168.110.190   80:31653/TCP,443:30585/TCP   14s   app.kubernetes.io/instance=nginx,app.kubernetes.io/name=nginx

删除nginx,对应的pod和svc都被删除了。

[root@k8scloude1 helm]# helm delete nginx
release "nginx" uninstalled

[root@k8scloude1 helm]# kubectl get svc -o wide
No resources found in helm namespace.

[root@k8scloude1 helm]# kubectl get pod -o wide
No resources found in helm namespace.

[root@k8scloude1 helm]# helm ls
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION

八.总结

本文介绍了Helm的基本使用方法,以及如何使用它来管理Kubernetes应用程序。通过Helm,我们可以更轻松地部署和管理Kubernetes应用程序。

posted @ 2023-06-14 16:13  人生的哲理  阅读(569)  评论(4编辑  收藏  举报