?
?
Helm
-
什么是 Helm
-
安裝 Helm
-
重要概念
-
使用 Helm
1 簡介
官網(wǎng)地址: Helm
Helm是一個(gè)Kubernetes應(yīng)用程序包管理工具,它允許你輕松管理和部署Kubernetes應(yīng)用程序。Helm通過使用稱為Charts的預(yù)定義模板來簡化Kubernetes應(yīng)用程序的部署和管理。Chart包含了一組Kubernetes對(duì)象定義,可以描述一個(gè)應(yīng)用程序的完整部署和資源需求,包括Deployment、Service、ConfigMap、Secret等。使用Helm,你可以輕松地安裝、升級(jí)、卸載和回滾Kubernetes應(yīng)用程序。
同時(shí),Helm還提供了一些便捷的功能,如依賴管理、全局變量、條件渲染等,可以幫助你更好地管理應(yīng)用程序的部署。Helm有兩個(gè)主要的組件:Helm客戶端(helm)和Helm服務(wù)器(Tiller)。Helm客戶端可以在本地運(yùn)行,而Tiller則運(yùn)行在Kubernetes集群中,并負(fù)責(zé)將Charts轉(zhuǎn)換為Kubernetes對(duì)象。
2 安裝
下載地址: Releases · helm/helm · GitHub
每個(gè)Helm 版本都提供了各種操作系統(tǒng)的二進(jìn)制版本,這些版本可以手動(dòng)下載和安裝。
-
下載 需要的版本
-
解壓(
tar -zxvf helm-v3.0.0-linux-amd64.tar.gz
) -
在解壓目錄中找到
helm
程序,移動(dòng)到需要的目錄中(mv linux-amd64/helm /usr/local/bin/helm
) -
添加倉庫: helm repo add bitnami https://charts.bitnami.com/bitnami
-
驗(yàn)證安裝:
helm help
.
3 重要概念
-
Chart 代表著 Helm 包。它包含在 Kubernetes 集群內(nèi)部運(yùn)行應(yīng)用程序,工具或服務(wù)所需的所有資源定義。你可以把它看作是 Homebrew formula,Apt dpkg,或 Yum RPM 在Kubernetes 中的等價(jià)物。
-
Repository(倉庫) 是用來存放和共享 charts 的地方。它就像 Perl 的 CPAN 檔案庫網(wǎng)絡(luò) 或是 Fedora 的 軟件包倉庫,只不過它是供 Kubernetes 包所使用的。
-
Release 是運(yùn)行在 Kubernetes 集群中的 chart 的實(shí)例。一個(gè) chart 通??梢栽谕粋€(gè)集群中安裝多次。每一次安裝都會(huì)創(chuàng)建一個(gè)新的 release。以 MySQL chart為例,如果你想在你的集群中運(yùn)行兩個(gè)數(shù)據(jù)庫,你可以安裝該chart兩次。每一個(gè)數(shù)據(jù)庫都會(huì)擁有它自己的 release 和 release name。
4 使用
4.1 搜索 charts
Helm 自帶一個(gè)強(qiáng)大的搜索命令,可以用來從兩種來源中進(jìn)行搜索:
-
helm search hub
從 Artifact Hub 中查找并列出 helm charts。 Artifact Hub中存放了大量不同的倉庫。 -
helm search repo
從你添加(使用helm repo add
)到本地 helm 客戶端中的倉庫中進(jìn)行查找。該命令基于本地?cái)?shù)據(jù)進(jìn)行搜索,無需連接互聯(lián)網(wǎng)。
4.2 安裝
使用 helm install
命令來安裝一個(gè)新的 helm 包。最簡單的使用方法只需要傳入兩個(gè)參數(shù):你命名的release名字和你想安裝的chart的名稱。
[root@k8s-node1 ~]# helm install nginx bitnami/nginx
NAME: nginx
LAST DEPLOYED: Wed Apr ?5 07:18:04 2023
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 13.2.34
APP VERSION: 1.23.4
?
** Please be patient while the chart is being deployed **
NGINX can be accessed through the following DNS name from within your cluster:
?
? nginx.kube-system.svc.cluster.local (port 80)
?
To access NGINX from outside the cluster, follow the steps below:
?
1. Get the NGINX URL by running these commands:
?
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
? ? ? Watch the status with: 'kubectl get svc --namespace kube-system -w nginx'
?
? ?export SERVICE_PORT=$(kubectl get --namespace kube-system -o jsonpath="{.spec.ports[0].port}" services nginx)
? ?export SERVICE_IP=$(kubectl get svc --namespace kube-system nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
? ?echo "http://${SERVICE_IP}:${SERVICE_PORT}"
注意: 安裝chart時(shí)創(chuàng)建了一個(gè)新的 release 對(duì)象。上述發(fā)布被命名為
nginx
。 (如果想讓Helm生成一個(gè)名稱,刪除發(fā)布名稱并使用--generate-name
。)
helm 安裝順序: Helm | 使用Helm
4.3 查看列表
你可以通過 helm list
命令看到當(dāng)前部署的所有 release:
[root@k8s-node1 ~]# helm list
NAME ? NAMESPACE ? ? ? REVISION ? ? ? UPDATED ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? STATUS ? ? ? ? CHART ? ? ? ? ? APP VERSION
nginx ? kube-system ? ? 1 ? ? ? ? ? ? ? 2023-04-05 07:34:48.421708425 -0400 EDT deployed ? ? ? nginx-13.2.34 ? 1.23.4
4.3 查看狀態(tài)
你可以使用 helm status
來追蹤 release 的狀態(tài),或是重新讀取配置信息:
[root@k8s-node1 ~]# helm status nginx
NAME: nginx
LAST DEPLOYED: Wed Apr ?5 07:18:04 2023
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
CHART NAME: nginx
CHART VERSION: 13.2.34
APP VERSION: 1.23.4
?
** Please be patient while the chart is being deployed **
NGINX can be accessed through the following DNS name from within your cluster:
?
? nginx.kube-system.svc.cluster.local (port 80)
?
To access NGINX from outside the cluster, follow the steps below:
?
1. Get the NGINX URL by running these commands:
?
NOTE: It may take a few minutes for the LoadBalancer IP to be available.
? ? ? Watch the status with: 'kubectl get svc --namespace kube-system -w nginx'
?
? ?export SERVICE_PORT=$(kubectl get --namespace kube-system -o jsonpath="{.spec.ports[0].port}" services nginx)
? ?export SERVICE_IP=$(kubectl get svc --namespace kube-system nginx -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
? ?echo "http://${SERVICE_IP}:${SERVICE_PORT}"
4.4 卸載
[root@k8s-node1 ~]# helm uninstall nginx
release "nginx" uninstalled
5 安裝自定義 Chart
上述安裝方式只會(huì)使用 chart 的默認(rèn)配置選項(xiàng)。很多時(shí)候,我們需要自定義 chart 來指定我們想要的配置。
5.1 拉取自定義的 chart
[root@k8s-node1 ~]# helm create app
注意: 當(dāng)前目錄中會(huì)出現(xiàn)一個(gè) nginx-xxx.tgz 文件 這個(gè)文件就是 charts 的包。
5.2 查看 chart 目錄結(jié)構(gòu)
app/
Chart.yaml
values.yaml
charts/
templates/
...
-
templates/
目錄包括了模板文件。當(dāng)Helm評(píng)估chart時(shí),會(huì)通過模板渲染引擎將所有文件發(fā)送到templates/
目錄中。 然后收集模板的結(jié)果并發(fā)送給Kubernetes。 -
values.yaml
文件也導(dǎo)入到了模板。這個(gè)文件包含了chart的 默認(rèn)值 。這些值會(huì)在用戶執(zhí)行helm install
或helm upgrade
時(shí)被覆蓋。 -
Chart.yaml
文件包含了該chart的描述。你可以從模板中訪問它。charts/
目錄 可以 包含其他的chart(稱之為 子chart)。
5.3 自定義模板
$ rm -rf mychart/templates/*
-
編寫 namespace.yaml
apiVersion: v1
kind: Namespace
metadata:
name: {{ .Chart.Name }}
namespace: {{ .Values.namespace }}
-
編寫 deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Chart.Name}}
namespace: {{.Values.namespace}}
labels:
? app: {{ .Chart.Name}}
spec:
replicas: {{ .Values.replicas }}
template:
? metadata:
? ? name: {{ .Chart.Name}}
? ? labels:
? ? ? app: {{ .Chart.Name}}
? spec:
? ? containers:
? ? ? - name: {{ .Chart.Name}}
? ? ? ? image: {{ .Values.image}}
? ? ? ? imagePullPolicy: {{.Values.imagePullPolicy}}
? ? ? ? ports:
? ? ? ? ? - containerPort: {{.Values.containerPort}}
? ? restartPolicy: {{ .Values.restartPolicy }}
selector:
? matchLabels:
? ? app: {{ .Chart.Name}}
-
編寫 service.yml
apiVersion: v1
kind: Service
metadata:
name: {{.Chart.Name}}
namespace: {{.Values.namespace}}
spec:
selector:
? app: {{.Chart.Name}}
ports:
? - port: {{.Values.service.port}}
? ? targetPort: {{.Values.containerPort}}
type: {{ .Values.service.type }}
-
編寫 Chart.yml文章來源:http://www.zghlxwxcb.cn/news/detail-702707.html
apiVersion: v2
name: app
description: A Helm chart for Kubernetes
# A chart can be either an 'application' or a 'library' chart.
#
# Application charts are a collection of templates that can be packaged into versioned archives
# to be deployed.
#
# Library charts provide useful utilities or functions for the chart developer. They're included as
# a dependency of application charts to inject those utilities and functions into the rendering
# pipeline. Library charts do not define any templates and therefore cannot be deployed.
type: application
?
# This is the chart version. This version number should be incremented each time you make changes
# to the chart and its templates, including the app version.
# Versions are expected to follow Semantic Versioning (https://semver.org/)
version: 0.1.0
?
# This is the version number of the application being deployed. This version number should be
# incremented each time you make changes to the application. Versions are not expected to
# follow Semantic Versioning. They should reflect the version the application is using.
# It is recommended to use it with quotes.
appVersion: "1.16.0"
-
編寫 values.yaml文章來源地址http://www.zghlxwxcb.cn/news/detail-702707.html
replicas: 1
namespace: app
image: nginx:1.19
imagePullPolicy: IfNotPresent
restartPolicy: Always
containerPort: 80
?
service:
port: 80
type: ClusterIP
5.4 驗(yàn)證是否存在錯(cuò)誤
$ helm lint app
5.5 打包自定義 chart
$ helm package app
5.6 安裝 chart
$ helm install app myapp-1.tgz
到了這里,關(guān)于【云原生】kubernetes應(yīng)用程序包管理工具Helm的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!