5分鐘教你在kubernetes集羣上安裝Helm,並部署應用

翻譯於Helm Tutorial: How To Install and Configure Helm

這篇文章將一步步教你如何在Kubernetes集羣安裝和配置helm,並用其部署和管理應用程序。

前置條件

開始使用helm之前,應具備以下條件。

  1. 正在運行的kubernetes集羣

  2. kubernetes集羣API Endpoint應該可以從運行Helm的機器上訪問。

  3. 使用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組件添加必要的權限。

所以需要做以下操作:

  1. 創建名稱爲tillerService Account
  2. 創建tillerService Account具有集羣管理員權限的ClusterRoleBinding

我們將在一個yaml文件中添加Service AccountclusterRoleBinding

創建一個名爲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參數指定tillerimage地址。可以在公共Google GCR註冊中心找到所有tillerdocker映像。由於國內網絡原因,可以從阿里鏡像倉庫拉取。即把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等知識等提升硬實力的文章,期待你的關注。 轉載說明:務必註明來源(註明:來源於公衆號:我的小碗湯, 作者:小碗湯)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章