一、簡介
什么是 Helm Helm 為團隊提供了在 Kubernetes 內部創建、安裝和管理應用程序時需要協作的工具,有點類似於 Ubuntu 中的 APT 或 CentOS 中的 YUM。
有了 Helm,開發者可以:
-
查找要安裝和使用的預打包軟件(Chart)
-
輕松創建和托管自己的軟件包
-
將軟件包安裝到任何 K8s 集群中
-
查詢集群以查看已安裝和正在運行的程序包
-
更新、刪除、回滾或查看已安裝軟件包的歷史記錄
Helm 組件和相關術語
helm
-
Helm 是一個命令行下的客戶端工具。主要用於 Kubernetes 應用程序 Chart 的創建、打包、發布以及創建和管理本地和遠程的 Chart 倉庫。
Chart
-
Helm 的軟件包,采用 TAR 格式。類似於 APT 的 DEB 包或者 YUM 的 RPM 包,其包含了一組定義 Kubernetes 資源相關的 YAML 文件。
Repoistory
-
Helm 的軟件倉庫,Repository 本質上是一個 Web 服務器,該服務器保存了一系列的 Chart 軟件包以供用戶下載,並且提供了一個該 Repository 的 Chart 包的清單文件以供查詢。Helm 可以同時管理多個不同的 Repository。
Release
-
使用 helm install 命令在 Kubernetes 集群中部署的 Chart 稱為 Release。可以理解為 Helm 使用 Chart 包部署的一個應用實例。
二、安裝helm
地址 https://helm.sh/docs/intro/install/
# wget https://get.helm.sh/helm-v3.3.4-linux-amd64.tar.gz # tart -zxvf helm-v3.3.4-linux-amd64.tar.gz # mv linux-amd64/helm /usr/local/bin/helm # helm version version.BuildInfo{Version:"v3.3.4", GitCommit:"a61ce5633af99708171414353ed49547cf05013d", GitTreeState:"clean", GoVersion:"go1.14.9"}
命令補全
# vim ~/.bashrc source <(helm completion bash) # source ~/.bashrc
三、使用
3.1 添加常用倉庫
$ helm repo add stable https://kubernetes-charts.storage.googleapis.com/ $ helm repo add bitnami https://charts.bitnami.com/bitnami $ helm repo add incubator https://kubernetes-charts-incubator.storage.googleapis.com/ $ helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx $ helm repo update # Make sure we get the latest list of charts $ helm repo add ali-stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts #阿里雲
# helm repo list NAME URL ingress-nginx https://kubernetes.github.io/ingress-nginx stable https://kubernetes-charts.storage.googleapis.com/ bitnami https://charts.bitnami.com/bitnami incubator https://kubernetes-charts-incubator.storage.googleapis.com/
3.2 安裝一個mysql的chart
# helm install stable/mysql --generate-name NAME: mysql-1604294571 LAST DEPLOYED: Mon Nov 2 13:22:54 2020 NAMESPACE: default STATUS: deployed REVISION: 1 NOTES: MySQL can be accessed via port 3306 on the following DNS name from within your cluster: mysql-1604294571.default.svc.cluster.local To get your root password run: MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-1604294571 -o jsonpath="{.data.mysql-root-password}" | base64 --decode; echo) To connect to your database: 1. Run an Ubuntu pod that you can use as a client: kubectl run -i --tty ubuntu --image=ubuntu:16.04 --restart=Never -- bash -il 2. Install the mysql client: $ apt-get update && apt-get install mysql-client -y 3. Connect using the mysql cli, then provide your password: $ mysql -h mysql-1604294571 -p To connect to your database directly from outside the K8s cluster: MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 # Execute the following command to route the connection: kubectl port-forward svc/mysql-1604294571 3306 mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
我們需要創建一個pvc,掛載到mysql這個pod中,才能起來mysql
創建storageClass
mysql-sc.yaml
apiVersion: storage.k8s.io/v1 kind: StorageClass metadata: name: mysql-sc # Change "rook-ceph" provisioner prefix to match the operator namespace if needed provisioner: rook-ceph.rbd.csi.ceph.com parameters: # clusterID is the namespace where the rook cluster is running clusterID: rook-ceph # Ceph pool into which the RBD image shall be created pool: replicapool # RBD image format. Defaults to "2". imageFormat: "2" # RBD image features. Available for imageFormat: "2". CSI RBD currently supports only `layering` feature. imageFeatures: layering # The secrets contain Ceph admin credentials. csi.storage.k8s.io/provisioner-secret-name: rook-csi-rbd-provisioner csi.storage.k8s.io/provisioner-secret-namespace: rook-ceph csi.storage.k8s.io/controller-expand-secret-name: rook-csi-rbd-provisioner csi.storage.k8s.io/controller-expand-secret-namespace: rook-ceph csi.storage.k8s.io/node-stage-secret-name: rook-csi-rbd-node csi.storage.k8s.io/node-stage-secret-namespace: rook-ceph # Specify the filesystem type of the volume. If not specified, csi-provisioner # will set default as `ext4`. Note that `xfs` is not recommended due to potential deadlock # in hyperconverged settings where the volume is mounted on the same node as the osds. csi.storage.k8s.io/fstype: ext4 # Delete the rbd volume when a PVC is deleted reclaimPolicy: Delete
創建pvc
mysql-pvc.yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-1604294571 spec: storageClassName: mysql-sc accessModes: - ReadWriteOnce resources: requests: storage: 2Gi
查看mysql啟動情況
# kubectl get po NAME READY STATUS RESTARTS AGE mysql-1604294571-69d68bb95b-2mx6p 1/1 Running 0 38m
查看使用helm安裝的Release(有namespace區分)
# helm list NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION mysql-1604294571 default 1 2020-11-02 13:22:54.106827838 +0800 CST deployed mysql-1.6.7 5.7.30
卸載
# helm uninstall mysql-1604294571