這篇文章將一步步教你如何在Kubernetes集羣安裝和配置helm
,並用其部署和管理應用程序。
前置條件
開始使用helm
之前,應具備以下條件。
-
正在運行的kubernetes集羣
-
kubernetes集羣API Endpoint應該可以從運行
Helm
的機器上訪問。 -
使用kubectl對集羣進行身份驗證,它應該具有集羣管理員權限。
Helm架構
安裝Helm[客戶端]
在命令行執行以下命令。
curl -L https://git.io/get_helm.sh | bash
由於國內網絡原因,下載helm包時會失敗。我已經將get_helm.sh腳本和helm的安裝包打包:提取碼:jrko
helm-v2.16.1-linux-amd64.tar.gz
[root@master helm]# bash get_helm.sh
Preparing to install helm and tiller into /usr/local/bin
helm installed into /usr/local/bin/helm
tiller installed into /usr/local/bin/tiller
Run 'helm init' to configure helm.
爲Tiller 創建具有集羣管理員權限的Service Account
Tiller
是Helm的服務端組件。Tiller
將被安裝在kubernetes集羣中,Helm
客戶端會與其交互,從而使用Helm charts
部署應用程序。
Helm
將管理k8s集羣資源。因此,我們需要向安裝在集羣kube-system
命令空間中的tiller
組件添加必要的權限。
所以需要做以下操作:
- 創建名稱爲
tiller
的Service Account
- 創建
tiller
對Service Account
具有集羣管理員權限的ClusterRoleBinding
。
我們將在一個yaml文件中添加Service Account
和clusterRoleBinding
。
創建一個名爲helm-rbac.yaml
的文件,並將以下內容複製到該文件中。
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
使用kubectl創建:
kubectl apply -f helm-rbac.yam
初始化Helm:部署Tiller
下一步是初始化Helm。當初始化Helm
時,將在kube-system名稱空間中部署一個名爲tiller-deploy
的deploy。
使用以下命令初始化Helm。
helm init --service-account=tiller --history-max 300
如果要安裝指定的tiller
版本,則可以在init
命令中使用--tiller-image
參數指定tiller
image地址。可以在公共Google GCR註冊中心找到所有tiller
docker映像。由於國內網絡原因,可以從阿里鏡像倉庫拉取。即把gcr.io/kubernetes-helm/tiller:v2.14.1
替換爲registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1
helm init --service-account=tiller --tiller-image=registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --history-max 300
執行後如下:
[root@master helm]# helm init --service-account=tiller --tiller-image=registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.1 --history-max 300
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-charts.storage.googleapis.com
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
如果不加“ --service-account = tiller”參數,則會出現以下錯誤。
Error: no available release name found
可以使用kubectl在kube-system名稱空間中查看tiller
部署。
kubectl get deployment tiller-deploy -n kube-system
使用Helm部署應用
現在,讓我們使用Helm部署Nginx應用。
執行以下helm install
命令,在kubernetes集羣中部署ingress nginx
。它將從github倉庫中下載nginx-ingress helm chart
。
helm install stable/nginx-ingress --name nginx-ingress
可以使用以下命令檢查helm chart
是否安裝。
helm ls
可以使用delete
命令刪除剛纔的部署。例如:
helm delete nginx-ingress
從kubernetes集羣中刪除Helm(Tiller)
如果要從kubernetes集羣中刪除Tiller
,請使用以下命令:
helm reset
由於某種原因,如果引發錯誤,請使用以下命令強制將其刪除。
helm reset --force
也可以使用kubectl
命令將其刪除。
kubectl delete deployment tiller-deploy --namespace kube-system
作者簡介
作者:小碗湯,一位熱愛、認真寫作的小夥,目前維護原創公衆號:『我的小碗湯』,專注於寫golang、docker、kubernetes等知識等提升硬實力的文章,期待你的關注。 轉載說明:務必註明來源(註明:來源於公衆號:我的小碗湯, 作者:小碗湯)