八、kubernetes应用的包管理Helm工具

一 helm简介

  • Helm是Kubernetes 应用的包管理工具,主要用来管理 Charts,类似Linux系统的yum。

  • Helm Chart是用来封装Kubernetes原生应用程序的一系列YAML文件。可以在你部署应用的时候自定义应用程序的一些 Metadata,以便于应用程序的分发。

  • 对于应用发布者而言

  • 通过Helm打包应用、管理应用依赖关系、管理应用版本并发布应用到软件仓库。

  • 对于使用者而言

  • 使用Helm后可以以简单的方式在Kubernetes上查找、安装、升级、回滚、卸载应用程序

二 部署helm

2.1 官网与资源

官网: Helm | 快速入门指南

软件资源: Releases · helm/helm · GitHub

2.2部署helm

helm所需的软件包在“一、Kubernetes 简介及部署方法”中,需要的可以去下载

2.2.1 安装helm

[root@k8s-master ~]# mkdir helm
[root@k8s-master ~]# cd helm/
[root@k8s-master helm]# ls
helm-push_0.10.4_linux_amd64.tar.gz  helm-v3.14.2-linux-amd64.tar.gz  nginx-15.14.0.tgz
[root@k8s-master helm]# tar zxf helm-push_0.10.4_linux_amd64.tar.gz 
[root@k8s-master helm]# tar zxf helm-v3.14.2-linux-amd64.tar.gz 
[root@k8s-master helm]# ls
bin                                  LICENSE            plugin.yaml
helm-push_0.10.4_linux_amd64.tar.gz  linux-amd64
helm-v3.14.2-linux-amd64.tar.gz      nginx-15.14.0.tgz
[root@k8s-master helm]# cd linux-amd64/
[root@k8s-master linux-amd64]# ls
helm  LICENSE  README.md
[root@k8s-master linux-amd64]# cp -p helm /usr/local/bin/

2.2.2 配置helm命令补齐

[root@k8s-master linux-amd64]# echo "source <(helm completion bash)" >> ~/.bashrc
[root@k8s-master linux-amd64]# source   ~/.bashrc
[root@k8s-master linux-amd64]# helm version
version.BuildInfo{Version:"v3.14.2", GitCommit:"c309b6f0ff63856811846ce18f3bdc93d2b4d54b", GitTreeState:"clean", GoVersion:"go1.21.7"}

三 helm常用操作

命令描述
create创建一个 chart 并指定名字
dependency管理 chart 依赖
get下载一个 release。可用子命令:all、hooks、manifest、notes、values
history获取 release 历史
install安装一个 chart
list列出 release
package将 chart 目录打包到 chart 存档文件中
pull从远程仓库中下载 chart 并解压到本地 # helm pull stable/mysql -- untar
repo添加,列出,移除,更新和索引 chart 仓库。可用子命令:add、index、 list、remove、update
rollback从之前版本回滚
search根据关键字搜索 chart。可用子命令:hub、repo
show查看 chart 详细信息。可用子命令:all、chart、readme、values
status显示已命名版本的状态
template本地呈现模板
uninstall卸载一个 release
upgrade更新一个 release
version查看 helm 客户端版本

 2.3.2 管理第三方repo源

#添加阿里云仓库
[root@k8s-master helm]# helm repo  add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
"aliyun" has been added to your repositories

#添加bitnami仓库
[root@k8s-master helm]# helm repo add bitnami https://charts.bitnami.com/bitnami
"bitnami" has been added to your repositories

#查看仓库信息
[root@k8s-master helm]# helm repo list
NAME   	URL                                                   
aliyun 	https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami	https://charts.bitnami.com/bitnami 

#查看仓库存储helm清单
[root@k8s-master helm]# helm search repo  aliyun

#删除第三方存储库
[root@k8s-master helm]# helm repo list
NAME   	URL                                                   
aliyun 	https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami	https://charts.bitnami.com/bitnami                    
[root@k8s-master helm]# helm repo remove aliyun
"aliyun" has been removed from your repositories
[root@k8s-master helm]# helm repo list
NAME   	URL                               
bitnami	https://charts.bitnami.com/bitnami

2.3.3 helm的使用方法

1 查找chart

[root@k8s-master helm]# helm  search  repo nginx
NAME                            	CHART VERSION	APP VERSION	DESCRIPTION                                       
bitnami/nginx                   	18.1.11      	1.27.1     	NGINX Open Source is a web server that can be a...
bitnami/nginx-ingress-controller	11.4.1       	1.11.2     	NGINX Ingress Controller is an Ingress controll...
bitnami/nginx-intel             	2.1.15       	0.4.9      	DEPRECATED NGINX Open Source for Intel is a lig...

2 查看chart信息

[root@k8s-master helm]# helm show chart bitnami/nginx
annotations:
  category: Infrastructure
  images: |
    - name: git
      image: docker.io/bitnami/git:2.46.0-debian-12-r0
    - name: nginx
      image: docker.io/bitnami/nginx:1.27.1-debian-12-r2
    - name: nginx-exporter
      image: docker.io/bitnami/nginx-exporter:1.3.0-debian-12-r2
  licenses: Apache-2.0

3 安装chart 包

[root@k8s-master ~]# helm install test bitnami/nginx
NAME: test
LAST DEPLOYED: Wed Sep 11 13:48:34 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 18.1.11
APP VERSION: 1.27.1
[root@k8s-master ~]# helm list
NAME	NAMESPACE	REVISION	UPDATED                                	STATUS    CHART        	APP VERSION
test	default  	1       	2024-09-11 13:48:34.262103297 +0800 CST	deployed  nginx-18.1.11	1.27.1     
[root@k8s-master ~]# kubectl get pods
NAME                          READY   STATUS    RESTARTS   AGE
test-nginx-55bd9d6f95-hdfwq   0/1     Pending   0          74s


#查看项目的发布状态
[root@k8s-master ~]# helm status test
NAME: test
LAST DEPLOYED: Wed Sep 11 13:48:34 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 18.1.11
APP VERSION: 1.27.1

#卸载项目
[root@k8s-master ~]# helm uninstall test 
release "test" uninstalled
[root@k8s-master ~]# kubectl get pods
No resources found in default namespace.
[root@k8s-master ~]# helm list
NAME	NAMESPACE	REVISION	UPDATED	STATUS	CHART	APP VERSION

2.3.4 安装项目前预定义项目选项

#拉取项目
[root@k8s-master helm]# helm pull bitnami/nginx
[root@k8s-master helm]# ls
bin                                  LICENSE            nginx-18.1.11.tgz
helm-push_0.10.4_linux_amd64.tar.gz  linux-amd64        plugin.yaml
helm-v3.14.2-linux-amd64.tar.gz      nginx-15.14.0.tgz

[root@k8s-master helm]# tar zxf nginx-18.1.11.tgz

[root@k8s-master helm]# cd nginx/
[root@k8s-master nginx]# ls
Chart.lock  charts  Chart.yaml  README.md  templates  values.schema.json  values.yaml

[root@k8s-master nginx]# ls templates/			#项目模板
deployment.yaml          ingress.yaml                 serviceaccount.yaml
extra-list.yaml          networkpolicy.yaml           servicemonitor.yaml
health-ingress.yaml      NOTES.txt                    svc.yaml
_helpers.tpl             pdb.yaml                     tls-secret.yaml
hpa.yaml                 prometheusrules.yaml
ingress-tls-secret.yaml  server-block-configmap.yaml

[root@k8s-master nginx]# vim values.yaml 		#项目变量文件
13   imageRegistry: "www.test.com"

四 构建helm中的chart包

4.1 Helm Chart目录结构

#建立chart项目
[root@k8s-master helm]# helm create test
Creating test
[root@k8s-master helm]# ls
bin                                  LICENSE      nginx-15.14.0.tgz  test
helm-push_0.10.4_linux_amd64.tar.gz  linux-amd64  nginx-18.1.11.tgz
helm-v3.14.2-linux-amd64.tar.gz      nginx        plugin.yaml
[root@k8s-master helm]# tree test/
test/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── hpa.yaml
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── serviceaccount.yaml
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml

3 directories, 10 files

4.2 构建方法

[root@k8s-master helm]# cd test/
[root@k8s-master test]# vim Chart.yaml
  4 type: application
  5 version: 0.1.0        #项目版本
  6 appVersion: "v1"      #软件版本

[root@k8s-master test]# vim values.yaml 
  7 image:
  8   repository: luohailin/myapp
  9   pullPolicy: IfNotPresent
 10   # Overrides the image tag whose default is the chart appVersion.
 11   tag: "v1"
 46 ingress:
 47   enabled: true
 48   className: "nginx"
 49   annotations: {}
 50     # kubernetes.io/ingress.class: nginx
 51     # kubernetes.io/tls-acme: "true"
 52   hosts:
 53     - host: reg.test.com
 54       paths:
 55         - path: /
 56           pathType: ImplementationSpecific
 57   tls: []
 

#语法检测
[root@k8s-master test]# helm lint .
==> Linting .
[INFO] Chart.yaml: icon is recommended

1 chart(s) linted, 0 chart(s) failed

#项目打包
[root@k8s-master test]# cd ..
[root@k8s-master helm]# helm package test/
Successfully packaged chart and saved it to: /root/helm/test-0.1.0.tgz
[root@k8s-master helm]# ls
bin                                  LICENSE      nginx-15.14.0.tgz  test
helm-push_0.10.4_linux_amd64.tar.gz  linux-amd64  nginx-18.1.11.tgz  test-0.1.0.tgz
helm-v3.14.2-linux-amd64.tar.gz      nginx        plugin.yaml

#项目可以通过各种分享方式发方为任何人后部署即可
[root@k8s-master helm]# helm install  test test-0.1.0.tgz 
NAME: test
LAST DEPLOYED: Wed Sep 11 14:28:12 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  http://reg.test.com/

[root@k8s-master helm]# kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
test-6c645c6fcb-dqdnz   1/1     Running   0          8s
[root@k8s-master helm]# kubectl get  ingress
NAME       CLASS   HOSTS                     ADDRESS         PORTS     AGE
ingress4   nginx   myapp-tls.timinglee.org   172.25.254.20   80, 443   2d17h
test       nginx   reg.test.com                              80        22s

五 构建helm仓库

5.1 在harbor仓库中构建一个公开的项目

5.2 安装helm push插件

官方网址:GitHub - chartmuseum/helm-push: Helm plugin to push chart package to ChartMuseum

5.2.1 在线安装

如果网络没问题情况下直接安装即可(下不了就去软件压缩包里面找)

[root@k8s-master helm]# dnf install git -y
[root@k8s-master helm]# helm plugin install https://github.com/chartmuseum/helm-push

5.2.2 离线安装

#创建helm plugin的存放目录
[root@k8s-master helm]# mkdir  ~/.local/share/helm/plugins/helm-push -p

#解压push插件包到指定目录
[root@k8s-master helm]# tar zxf helm-push_0.10.4_linux_amd64.tar.gz -C ~/.local/share/helm/plugins/helm-push
[root@k8s-master helm]# ls ~/.local/share/helm/plugins/helm-push
bin  LICENSE  plugin.yaml

#查看helm调用命令是否成功
[root@k8s-master helm]# helm cm-push --help
Helm plugin to push chart package to ChartMuseum

Examples:

  $ helm cm-push mychart-0.1.0.tgz chartmuseum       # push .tgz from "helm package"
  $ helm cm-push . chartmuseum                       # package and push chart directory
  $ helm cm-push . --version="1.2.3" chartmuseum     # override version in Chart.yaml
  $ helm cm-push . https://my.chart.repo.com         # push directly to chart repo URL

5.3 上传项目到仓库

5.3.1 添加仓库

#为helm添加证书
[root@k8s-master helm]# cp /etc/docker/certs.d/www.test.com/ca.crt /etc/pki/ca-trust/source/anchors/

#更新本地ca认证库
[root@k8s-master helm]# update-ca-trust

#再次添加仓库
[root@k8s-master helm]# helm repo add test https://admin:redhat@www.test.com/chartrepo/test
"test" has been added to your repositories

5.3.2 上传本地项目

#命令执行格式
helm cm-push <项目名称> <仓库名称> -u admin -p lee
[root@k8s-master helm]# helm cm-push timinglee-0.1.0.tgz timinglee -u admin -p lee
Pushing timinglee-0.1.0.tgz to timinglee...
Done.

#查看项目上传情况
[root@k8s-master helm]# helm search  repo timinglee		#上传后数据未更新
No results found

#更新仓库
[root@k8s-master helm]# helm repo update timinglee
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "timinglee" chart repository
Update Complete. ⎈Happy Helming!⎈

#再次查看
[root@k8s-master helm]# helm search  repo timinglee
NAME                    CHART VERSION   APP VERSION     DESCRIPTION
timinglee/timinglee     0.1.0           v1              A Helm chart for Kubernetes

#安装项目
[root@k8s-master helm]# helm install timinglee timinglee/timinglee
NAME: timinglee
LAST DEPLOYED: Tue Sep 10 16:48:14 2024
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
  http://myapp.timinglee.org/

[root@k8s-master helm]# curl  myapp.timinglee.org
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值