K8s包管理工具-helm:helm安装

article/2025/7/20 17:53:29

1  Helm的简介

每个成功的软件平台都有一个优秀的打包系统,比如 Debian、Ubuntu 的 apt,Redhat、Centos 的 yum。而 Helm 则是 Kubernetes 上的包管理器。

1.1  为什么k8s需要Helm?

Kubernetes 能够很好地组织和编排容器,但它缺少一个更高层次的应用打包工具,而 Helm 就是来干这件事的。

举个例子,我们需要部署一个MySQL服务,Kubernetes则需要部署以下对象:

① 为了能够让外界访问到MySQL,需要部署一个mysql的service;

②需要进行定义MySQL的密码,则需要部署一个Secret;

③Mysql的运行需要持久化的数据存储,此时还需要部署PVC;

④保证后端mysql的运行,还需要部署一个Deployment,以支持以上的对象。

针对以上对象,我们可以使用YAML文件进行定义并部署,但是仅仅对于单个的服务支持,如果应用需要由一个甚至几十个这样的服务组成,需要几十甚至更多的yaml文件,这些yaml分散存放,如果某天进行项目恢复的话,很难知道部署顺序,依赖关系等,而所有这些包括

  • 基于yaml配置的集中存放
  • 基于项目的打包
  • 组件间的依赖

都可以通过helm来进行解决。

1.2  Helm基本概念

Helm 可以理解为 Kubernetes 的包管理工具,可以方便地发现、共享和使用为Kubernetes构建的应用,它包含几个基本概念

  • Chart:一个 Helm 包,其中包含了运行一个应用所需要的镜像、依赖和资源定义等,还可能包含 Kubernetes 集群中的服务定义
  • Release: 在 Kubernetes 集群上运行的 Chart 的一个实例。在同一个集群上,一个 Chart 可以安装很多次。每次安装都会创建一个新的 release。例如一个 MySQL Chart,如果想在服务器上运行两个数据库,就可以把这个 Chart 安装两次。每次安装都会生成自己的 Release,会有自己的 Release 名称。
  • Repository:用于发布和存储 Chart 的仓库。

1.3   Helm组件

 

Helm由两部分组成,客户端helm和服务端tiller。

Helm Client 是用户命令行工具,其主要负责如下:

  • 本地 chart 开发
  • 仓库管理
  • 与 Tiller sever 交互
  • 发送预安装的 chart
  • 查询 release 信息
  • 要求升级或卸载已存在的 release

Tiller Server是一个部署在Kubernetes集群内部的 server,其与 Helm client、Kubernetes API server 进行交互。Tiller server 主要负责如下:

  • 监听来自 Helm client 的请求
  • 通过 chart 及其配置构建一次发布
  • 安装 chart 到Kubernetes集群,并跟踪随后的发布
  • 通过与Kubernetes交互升级或卸载 chart

简单的说,client 管理 charts,而 server 管理发布 release

1.4   工作流程

首先helm把需要的应用程序包Chart从repository仓库下载到本地。若需定制化操作,则需修改模板文件和values文件,使用模板文件和values文件对配置清单进行赋值。

 helm把请求提交给Tiller,Tiller接收到请求后连接apiserver,通过认证授权,apiserver负责完成创建。

创建完成后,这个运行在集群上的实例不再称作Chart,而被称作release。

helm主机中,helm的家目录下若存在需要的Chart包,则以后再创建时不再从网上拉取Chart包,直接使用本地Chart包。

2  部署Helm

helm部署文档

Helm的部署方式有两种:预编译的二进制程序和源码编译安装,这里使用二进制的方式进行安装

2.1  下载helm

我们可以在Helm Realese页面下载二进制文件,这里下载的v2.13.1版本,解压后将可执行文件helm拷贝到/usr/local/bin目录下即可,这样Helm客户端就在这台机器上安装完成了。

wget https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz --no-check-certificate
tar -xf helm-v2.13.1-linux-amd64.tar.gz
cd linux-amd64/
cp helm /usr/bin/  #下载解压完成后,直接将helm执行文件放入PATH环境变量中就可以使用
helm -h

2.2  部署Tiller

helm第一次init时,需要链接api-server并进行认证,所以在运行helm时,会去读取kube-config文件,所以必须确认当前用户存在kube-config文件。

Tiller运行在K8s集群之上,也必须拥有集群的管理权限,也就是需要一个serviceaccount,进行一个clusterrolebinding到cluster-admin。

Tiller的RBAC配置示例链接:

https://github.com/helm/helm/blob/master/docs/rbac.md

#[root@k8s-master ~]# cd helm/
#[root@k8s-master helm]# ls
#[root@k8s-master helm]#  cat  tiller_rbac.yaml
apiVersion: v1
kind: ServiceAccount
metadata:name: tillernamespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:name: tiller
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: cluster-admin
subjects:- kind: ServiceAccountname: tillernamespace: kube-system

[root@k8s-master helm]# kubectl apply -f tiller-rbac.yaml
serviceaccount/tiller created
clusterrolebinding.rbac.authorization.k8s.io/tiller created
[root@k8s-master helm]# kubectl get sa -n kube-system |grep tiller
tiller                               1         7s
[root@k8s-master helm]#

#由于墙的原因,所以我们使用阿里的chart仓库,注意指定的tiller版本需要和helm版本一致

[root@k8s-master helm]# helm init  --service-account tiller  --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.13.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
Creating /root/.helm
Creating /root/.helm/repository
Creating /root/.helm/repository/cache
Creating /root/.helm/repository/local
Creating /root/.helm/plugins
Creating /root/.helm/starters
Creating /root/.helm/cache/archive
Creating /root/.helm/repository/repositories.yaml
Adding stable repo with URL: https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
Adding local repo with URL: http://127.0.0.1:8879/charts
$HELM_HOME has been configured at /root/.helm.Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
To prevent this, run `helm init` with the --tiller-tls-verify flag.
For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
Happy Helming!
[root@k8s-master helm]# kubectl get pod -n kube-system |grep tiller #查看tiller的pod是否创建成功 
tiller-deploy-86b574cb79-4xhwh            1/1     Running     0          22s
[root@k8s-master helm]#
[root@k8s-master helm]# helm version
Client: &version.Version{SemVer:"v2.13.1", GitCommit:"618447cbf203d147601b4b9bd7f8c37a5d39fbb4", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.13.1", GitCommit:"618447cbf203d147601b4b9bd7f8c37a5d39fbb4", GitTreeState:"clean"}
[root@k8s-master helm]#

如果希望在安装时自定义一些参数,可以参考一下的一些参数:

  • --canary-image:安装canary分支,即项目的Master分支
  • --tiller-image:安装指定版本的镜像,默认和helm同版本
  • --kube-context:安装到指定的Kubernetes集群
  • --tiller-namespace:安装到指定的名称空间,默认为kube-system

Tiller将数据存储在ConfigMap资源当中,卸载或重装不会导致数据丢失,卸载Tiller的方法有以下两种:

(1)kubectl delete deployment tiller-deploy --n kube-system

(2)heml reset

3  helm命令补全

#BASH 请执行
source <(helm completion bash)
echo "source <(helm completion bash)" | sudo tee -a /etc/profile

4  创建第一个chart

[root@k8s-master helm]# helm create hello-helm
Creating hello-helm
[root@k8s-master helm]# tree hello-helm
hello-helm
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml3 directories, 8 files
[root@k8s-master helm]#

我们通过查看templates目录下面的deployment.yaml文件可以看出默认创建的 Chart 是一个 nginx 服务,具体的每个文件是干什么用的,我们可以前往 Helm 官方文档进行查看,后面会和大家详细讲解的。比如这里我们来安装 1.7.9 这个版本的 nginx,则我们更改 value.yaml 文件下面的 image tag 即可,将默认的 stable 更改为 1.7.9,为了测试方便,我们把 Service 的类型也改成 NodePort

[root@k8s-master helm]# cd hello-helm/
[root@k8s-master hello-helm]# ls
charts  Chart.yaml  templates  values.yaml
[root@k8s-master hello-helm]# cd templates/
[root@k8s-master templates]# ls
deployment.yaml  _helpers.tpl  ingress.yaml  NOTES.txt  service.yaml  tests
[root@k8s-master templates]# vim deployment.yaml
[root@k8s-master templates]# cd ..
[root@k8s-master hello-helm]# ls
charts  Chart.yaml  templates  values.yaml
[root@k8s-master hello-helm]# vim values.yaml
[root@k8s-master hello-helm]# cat values.yaml
# Default values for hello-helm.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.replicaCount: 1image:repository: nginxtag: 1.7.9pullPolicy: IfNotPresentnameOverride: ""
fullnameOverride: ""service:type: NodePortport: 80ingress:enabled: falseannotations: {}# kubernetes.io/ingress.class: nginx# kubernetes.io/tls-acme: "true"hosts:- host: chart-example.localpaths: []tls: []#  - secretName: chart-example-tls#    hosts:#      - chart-example.localresources: {}# We usually recommend not to specify default resources and to leave this as a conscious# choice for the user. This also increases chances charts run on environments with little# resources, such as Minikube. If you do want to specify resources, uncomment the following# lines, adjust them as necessary, and remove the curly braces after 'resources:'.# limits:#   cpu: 100m#   memory: 128Mi# requests:#   cpu: 100m#   memory: 128MinodeSelector: {}tolerations: []affinity: {}

安装下这个 Chart :

[root@k8s-master hello-helm]# cd ..
[root@k8s-master helm]# helm install ./hello-helm
NAME:   cold-llama
LAST DEPLOYED: Tue Sep 24 10:34:37 2019
NAMESPACE: default
STATUS: DEPLOYEDRESOURCES:
==> v1/Deployment
NAME                   READY  UP-TO-DATE  AVAILABLE  AGE
cold-llama-hello-helm  0/1    1           0          0s==> v1/Pod(related)
NAME                                    READY  STATUS             RESTARTS  AGE
cold-llama-hello-helm-6bc7d7cbfd-rsz4l  0/1    ContainerCreating  0         0s==> v1/Service
NAME                   TYPE      CLUSTER-IP      EXTERNAL-IP  PORT(S)       AGE
cold-llama-hello-helm  NodePort  10.107.195.201  <none>       80:30369/TCP  0sNOTES:
1. Get the application URL by running these commands:export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services cold-llama-hello-helm)export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")echo http://$NODE_IP:$NODE_PORT[root@k8s-master helm]# kubectl get pods,svc | grep hello-helmpod/cold-llama-hello-helm-6bc7d7cbfd-rsz4l    1/1     Running   0          5m8s
service/cold-llama-hello-helm   NodePort    10.107.195.201   <none>        80:30369/TCP   5m8s
[root@k8s-master helm]#


 

等到 Pod 创建完成后,我们可以根据创建的 Service 的 NodePort 来访问该服务了,然后在浏览器中打开http://nodeIP: 30369就可以正常的访问我们刚刚部署的 nginx 应用了。 

[root@k8s-master helm]# curl -I 10.6.76.23:30369
HTTP/1.1 200 OK
Server: nginx/1.7.9  #指定版本
Date: Tue, 24 Sep 2019 02:41:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Dec 2014 16:25:09 GMT
Connection: keep-alive
ETag: "54999765-264"
Accept-Ranges: bytes[root@k8s-master helm]#

4.1  helm list查看release:

[root@k8s-master helm]# helm list
NAME            REVISION        UPDATED                         STATUS          CHART                   APP VERSION     NAMESPACE
cold-llama      1               Tue Sep 24 10:34:37 2019        DEPLOYED        hello-helm-0.1.0        1.0             default
[root@k8s-master helm]#

4.2  helm packag打包chart:

[root@k8s-master helm]# helm package hello-helm
Successfully packaged chart and saved it to: /root/helm/hello-helm-0.1.0.tgz
[root@k8s-master helm]# pwd
/root/helm
[root@k8s-master helm]# ls
hello-helm  hello-helm-0.1.0.tgz
[root@k8s-master helm]#

然后我们就可以将打包的tgz文件分发到任意的服务器上,通过helm fetch就可以获取到该 Chart 了

4.3 helm delete临时删除release

[root@k8s-master helm]# kubectl get pods,svc,deploy | grep hello-helmpod/cold-llama-hello-helm-6bc7d7cbfd-rsz4l    1/1     Running   0          9m36s
service/cold-llama-hello-helm   NodePort    10.107.195.201   <none>        80:30369/TCP   9m36sdeployment.extensions/cold-llama-hello-helm    1/1     1            1           9m36s
[root@k8s-master helm]# helm list
NAME            REVISION        UPDATED                         STATUS          CHART                   APP VERSION     NAMESPACE
cold-llama      1               Tue Sep 24 10:34:37 2019        DEPLOYED        hello-helm-0.1.0        1.0             default
[root@k8s-master helm]# helm delete cold-llama
release "cold-llama" deleted
[root@k8s-master helm]# kubectl get pods,svc,deploy | grep hello-helmpod/cold-llama-hello-helm-6bc7d7cbfd-rsz4l    0/1     Terminating   0          10m
[root@k8s-master helm]# kubectl get pods,svc,deploy | grep hello-helm

然后我们看到kubernetes集群上的该 nginx 服务也已经被删除了。


http://www.hkcw.cn/article/DDvVMFkhZB.shtml

相关文章

OpenAI 宕机 | 如何让 k8s 集群更稳定

注:本文为 2024 年 12 月 “ OpenAI 宕机分析” 相关文章合辑。 未整理去重。 ChatGPT 的故障,官方这样解释… 明说 YYM 2024 年 12 月 14 日 18:50 广东 2024 12 11 (PST) OpenAI 经历了一次服务故障,OpenAI 给出了详细的解释和预防方案。下面是我阅读的分析和部分解读。…

自定义资源支持:K8s Device Plugin 从原理到实现

本文主要分析 k8s 中的 device-plugin 机制工作原理&#xff0c;并通过实现一个简单的 device-plugin 来加深理解。 1. 背景 默认情况下&#xff0c;k8s 中的 Pod 只能申请 CPU 和 Memory 这两种资源&#xff0c;就像下面这样&#xff1a; resources:requests:memory: "…

在K8S上部署OceanBase的最佳实践

在K8S上部署OceanBase的最佳实践 目录 1. 背景与选型 1.1 为什么选择OB1.2 为什么选择ob-operator实现OB on K8S 2. 部署实操 2.1 环境准备2.2 安装 ob-operator2.3 配置 OB 集群2.4 配置 OBProxy 集群2.5 Headless Service 和 CoreDNS 配置2.6 监控与运维 2.6.1 Promethues部…

Centos 安装 Kubernets(k8s)

Centos安装部署Kubernetes (k8s) cat /etc/os-release #查看系统版本 uname -r #查看系统内核 1.设置主机名 准备三台服务器&#xff0c;至少2核2G hostnamectl set-hostname k8s-master #在第一台机器上执行hostnamectl set-hostname k8s-node01 #在第二台机器上执行hostna…

K8s - openeuler2203SP1安装 K8s + flannel

环境说明 [rootmaster-1 ~]# uname -a Linux master-1 5.10.0-136.12.0.86.oe2203sp1.x86_64 #1 SMP Tue Dec 27 17:50:15 CST 2022 x86_64 x86_64 x86_64 GNU/Linux安装过程 1、安装 containerd 下载 tar 包 # 确保没有使用官方仓库的containerd [rootlocalhost ~]# yum rem…

K8S从0完整搭建

介绍 以下搭建的环境使用的是VMware虚拟机。这里大家可以自行选择。K8S每个版本可能都会有差异&#xff0c;请保证相关组件的兼容性。 准备工具 部署版本介绍 名称 版本 描述 centos 7.9 docker 20.10.8 k8s v1.20.10 calico v3.20.0 K8S节点规划 3个m…

【Kubernetes】k8s的helm扩展之监控管理、日志管理、部署efk【elk的升级版】详细说明

部署prometheus【mon节点】 镜像查看【需要先配置好helm源,这里是配置的ali源】【集群必须通外网才能部署啊】helm search repo prometheus-operator 安装prometheushelm install mon ali/prometheus-operator【后面就是上面查询到的helm信息】 下载完毕以后呢就会有一个helm…

最全Linux centos9环境基于k8s(k3s)搭建HomeAssistant,并接入米家官方XiaoMiHome插件

背景 环境 1、linux centos 9&#xff08;该环境的ip可以被你的其他设备访问到&#xff0c;例如你可以用windows访问到家里的服务器&#xff09; 2、k3s集群已完成部署&#xff0c;参考:国内k3s环境搭建-CSDN博客 3、HA部署方式采用容器化部署 概念 1、k3s&#xff1a;k8…

Git 使用全指南

Git 使用全指南 第一部分&#xff1a;核心概念 什么是 Git&#xff1f; 一个分布式版本控制系统 (DVCS)。目标&#xff1a; 跟踪文件变化、协作开发、回溯历史、管理不同开发线&#xff08;分支&#xff09;。核心思想&#xff1a; 每个开发者都有完整的仓库副本&#xff08;包…

Leetcode 269. 火星词典

1.题目基本信息 1.1.题目描述 现有一种使用英语字母的外星文语言&#xff0c;这门语言的字母顺序与英语顺序不同。 给定一个字符串列表 words &#xff0c;作为这门语言的词典&#xff0c;words 中的字符串已经 按这门新语言的字母顺序进行了排序 。 请你根据该词典还原出此…

若依框架-Feign的应用

代码资料链接&#xff1a;https://download.csdn.net/download/ly1h1/90945836 1.背景 若依的微服务框架&#xff0c;少不了各微服务之间的接口调用&#xff0c;以下是采用feign来进行微服务之间的方法调用。 2.案例说明 在system模块下的某个接口&#xff0c;调用factory&am…

印度奥迪沙邦一巴士翻车 50余人被困 紧急救援展开

6月2日,一辆巴士在印度东部奥迪沙邦的山路上发生翻车事故,车内50多名乘客被困。事故发生在当天清晨,巴士在下坡过程中于弯道上失去平衡,导致车辆失控。事故发生后,当地村民和紧急救援人员迅速展开救援行动,当地政府也派出救援队赶往现场。目前,官方尚未公布具体的伤亡情…

从4小时到20分钟 青岛港科技升级货物“秒通关”

作为中国北方重要的国际航运枢纽,青岛港的航线通达全球700多个港口,一季度集装箱吞吐量同比增速达到了7.4%。在当下复杂多变的国际贸易形势下,这座港口如何找准方向为外贸企业“保驾护航”?港口“流量”剧增折射外贸新机遇在山东港口青岛港前湾港区,这艘前往美国东部的集装…

Flannel MAC地址冲突导致Pod 跨节点通信异常

问题背景 客户在扩容 Kubernetes 节点后&#xff0c;发现部分服务 Pod 跨节点通信异常&#xff0c;表现为&#xff1a; • Pod 间通信间歇性失败&#xff1b; • 某些业务服务异常或响应慢&#xff1b; • 怀疑是网络问题引起的。 问题排查 1️⃣ 初步排查网络路由信息 我们…

[前端计算机网络]资源加载过程的详细性能信息浏览器加载资源的全过程

资源加载过程的详细性能信息 基于 PerformanceResourceTiming 对象对页面中某个资源加载过程的详细性能信息进行采集与封装&#xff0c;并结合了计算机网络中的请求生命周期进行度量。 export function observeEvent():void{const type"resource";const entryHand…

德布劳内将接受那不勒斯体检 加盟在即

据报道,德布劳内将加盟那不勒斯。预计他将在比利时国家队比赛结束后接受那不勒斯的体检。上月公布的比利时世预赛名单中包括了德布劳内。比利时队将在世预赛中先后对阵北马其顿和威尔士,其中与威尔士的比赛将于本月10日北京时间2点45分开球。现年33岁的德布劳内在今夏合同到期…

《在人间》是2025最难看懂的剧吗 烧脑剧情挑战观众

《在人间》这部剧集让艺绽君感到难以评价。这种“难评”并非贬义,而是因为观剧过程中真实地感受到了大脑爆炸、脑细胞死了不少,只能用一个相对中立的词汇来形容这部“神剧”。《在人间》共8集,隶属于爱奇艺的微尘剧场,这一剧场以短小精悍的作品著称。主创团队中有知名导演兼…

警方辟谣北京有人高空撒一千万:不是故意的,系工人施工碰倒钱箱 干活不慎掉落

5月29日,北京昌平区住总万科天地一带发生了一起撒钱事件。有市民发帖称,有人在楼上撒了一千万元。视频画面显示,空中飘着几张纸币,一些市民在楼下接钱。次日,北京七里渠派出所工作人员表示,当事人是因为工作时不小心掉落了钱,并提醒市民不要听信网络谣言。至于掉落的金钱…

96岁老兵走失找回 曾为陈毅元帅送信 抗战英雄平安归家

6月2日,山东省济宁市一救援队成功找回了走失的96岁高龄抗战老兵吕企荣老人,老人身体无碍。吕企荣家住泗水县济河街道何家庄村。5月31日早晨7时许,老人从家中步行到村北侧的小公园遛弯,直到中午都没有回家。家属通过监控发现,老人沿着城区北侧万象城附近道路一直向北走,直…

地磁暴雷暴大风与暴雨交织登场 双预警齐发

6月2日6时,中央气象台发布暴雨蓝色预警和强对流天气蓝色预警,覆盖福建、广东、广西等十余省份。中国气象局国家空间天气监测预警中心指出,6月1日至3日可能连续发生地磁暴,6月2日左右,我国北部有机会出现较为明显的极光,部分地区甚至有出现红绿复合极光的可能。预计6月2日…