使用 Helm 部署 Consul 集羣
Helm 介紹
helm
是 kubernetes
的包管理器。它相當於 CentOS
的 yum
,Ubuntu
的 apt
。
在 helm
中有三大概念:
-
Chart
:Helm 使用的包格式稱爲chart
。chart
就是一個描述Kubernetes
相關資源的文件集合。單個 chart 可以用來部署一些簡單的, 類似於 memcache pod,或者某些複雜的 HTTP 服務器以及 web 全棧應用、數據庫、緩存等 -
Repo
:chart
的存放倉庫,社區的 Helm chart 倉庫位於 Artifact Hub,也可以創建運行自己的私有 chart 倉庫 -
Release
:運行在Kubernetes
集羣中的chart
的實例。一個chart
通常可以在同一個集羣中安裝多次,而每一次安裝都會創建一個新的release
總結:Helm
安裝 charts
到 Kubernetes
集羣中,每次安裝都會創建一個新的 release
。你可以在 Helm
的 chart repositories
中尋找新的 chart
。
準備階段
擁有一個 Kubernetes
集羣,如下:
| 類型 | IP 地址 | 系統版本 | 配置 | | --- | --- | --- | --- | | Master 主節點 | 192.168.19.136 | CentOS Linux release 7.9.2009 (Core) | 4 核 4G | | Work 工作節點 1 | 192.168.19.137 | CentOS Linux release 7.9.2009 (Core) | 4 核 4G | | Work 工作節點 2 | 192.168.19.138 | CentOS Linux release 7.9.2009 (Core) | 4 核 4G |
選擇 Helm 版本
| Helm 版本 | 支持的 Kubernetes 版本 | | --- | --- | | 3.5.x | 1.20.x - 1.17.x | | 3.4.x | 1.19.x - 1.16.x | | 3.3.x | 1.18.x - 1.15.x | | 3.2.x | 1.18.x - 1.15.x | | 3.1.x | 1.17.x - 1.14.x | | 3.0.x | 1.16.x - 1.13.x | | 2.16.x | 1.16.x - 1.15.x | | 2.15.x | 1.15.x - 1.14.x | | 2.14.x | 1.14.x - 1.13.x | | 2.13.x | 1.13.x - 1.12.x | | 2.12.x | 1.12.x - 1.11.x | | 2.11.x | 1.11.x - 1.10.x | | 2.10.x | 1.10.x - 1.9.x | | 2.9.x | 1.10.x - 1.9.x | | 2.8.x | 1.9.x - 1.8.x | | 2.7.x | 1.8.x - 1.7.x | | 2.6.x | 1.7.x - 1.6.x | | 2.5.x | 1.6.x - 1.5.x | | 2.4.x | 1.6.x - 1.5.x | | 2.3.x | 1.5.x - 1.4.x | | 2.2.x | 1.5.x - 1.4.x | | 2.1.x | 1.5.x - 1.4.x | | 2.0.x | 1.4.x - 1.3.x |
注:Helm 2
採用 client/server
架構,分爲 Helm
客戶端和 Tiller
服務端,而 Helm3
移除了 Tiller
,也就是說 Helm 3
只要安裝 Helm
就好了。
關於 Helm 2
和 Helm 3
的區別可以閱讀官方文檔:https://helm.sh/zh/docs/faq/
本文會選擇 Helm 3.4.2
進行安裝。
下載安裝 Helm 3.4.2
訪問 https://github.com/helm/helm/releases 選擇對應的版本,下載Master
節點
tar -zxvf helm-v3.4.2-linux-amd64.tar.gz
移動到 /usr/local/bin
mv linux-amd64/helm /usr/local/bin/helm
查看是否安裝成功
helm version
image.png
使用 Helm 部署 Consul 集羣
Helm 基本用法
在部署 consul 之前,先來看看 helm 的基本用法。
查找 Charts
:
helm search hub # 從 Artifact Hub 中查找並列出 helm charts。Artifact Hub中存放了大量不同的倉庫。
helm search repo # 從你添加(使用 helm repo add)到本地 helm 客戶端中的倉庫中進行查找。
添加 HashiCorp Helm
倉庫:
helm repo add hashicorp https://helm.releases.hashicorp.com
# 查看已添加的倉庫列表
helm repo list
搜索 consul
:
helm search repo consul
image.png
Consul 所需環境準備
命名空間
創建一個命名空間,後續都會在此命名空間上進行操作:
kubectl create namespace consul
image.png
存儲
由於 consul
部署的時候會創建使用 PVC:PersistentVolumeClaim
的 Pod
,Pod
中的應用通過 PVC
進行數據的持久化,而 PVC
使用 PV: PersistentVolume
進行數據的最終持久化處理。所以我們要準備好存儲資源供應,否則 consul-server
會因爲獲取不到存儲資源而一直處於 pending
狀態,有以下兩種方案:
方案 1,手動創建靜態 PV 進行存儲供應
cat <<EOF | kubectl apply -f -
---
kind: PersistentVolume
apiVersion: v1
metadata:
name: pv-volume-consul-0
namespace: consul
labels:
type: local
spec:
storageClassName: ""
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/consul/data"
---
kind: PersistentVolume
apiVersion: v1
metadata:
name: pv-volume-consul-1
namespace: consul
labels:
type: local
spec:
storageClassName: ""
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/consul/data"
EOF
查看:
kubectl get pv -n consul -o wide
image.png
方案 2,通過 StorageClass 實現動態卷供應
在所有節點上安裝 nfs-utils:
yum install -y nfs-utils
# 創建nfs目錄
mkdir -p /mnt/nfs
# 配置nfs權限
cat>/etc/exports<<EOF
/mnt/nfs/ 192.168.19.0/24(insecure,rw,anonuid=0,anongid=0,all_squash,sync)
EOF
# 啓動nfs服務
systemctl start rpcbind.service
systemctl start nfs-server.service
# 設置開機自啓
systemctl enable rpcbind.service
systemctl enable nfs-server.service
# 配置生效
exportfs -r
# 添加倉庫源
helm repo add azure http://mirror.azure.cn/kubernetes/charts/
# 搜索nfs-client-provisioner
helm search repo nfs-client-provisioner
# 安裝nfs-client-provisioner
helm install nfs-storage azure/nfs-client-provisioner -n consul \
--set nfs.server=192.168.19.138 \
--set nfs.path=/mnt/nfs \
--set storageClass.name=nfs-storage \
--set storageClass.defaultClass=true
# 查看StorageClass
kubectl get sc -n consul
PVC
需要申請 PV
時,StorageClass
就會自動爲我們創建 PV
了。
配置文件
創建 config.yaml
:
global:
name: consul # 設置用於 Helm chart 中所有資源的前綴
ui:
service: # 爲 Consul UI 配置服務
type: 'NodePort' # 服務類型爲 NodePort
server:
replicas: 2 # 要運行的服務器的數量,即集羣數
affinity: "" # 允許每個節點上運行更多的Pod
storage: '10Gi' # 定義用於配置服務器的 StatefulSet 存儲的磁盤大小
storageClass: "" # 使用Kubernetes集羣的默認 StorageClass
securityContext: # 服務器 Pod 的安全上下文,以 root 用戶運行
fsGroup: 2000
runAsGroup: 2000
runAsNonRoot: false
runAsUser: 0
更多配置可以參考:https://www.consul.io/docs/k8s/helm
image.png
開始安裝
helm install hi-consul hashicorp/consul -n consul -f config.yaml
hi-consul
:你命名的 release 名字
hashicorp/consul
:你想安裝的 chart 的名稱
-n
:指定命名空間
-f
:傳遞配置文件
kubectl get pods -n consul -o wide -w
kubectl get svc -n consul -o wide
題外話
# 查看helm已安裝列表
helm list -n consul
# 卸載
helm uninstall hi-consul -n consul
# 更多
helm help
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/djuuzDlSkw2gykg325jnUw