使用 helm 部署 k8s 資源

helm 是基於 kubernetes 的包管理器。它之於 kubernetes 就如 yum 之於 centospip 之於 pythonnpm 之於 javascriptjavascript

helm 的引入對於管理集羣有哪些幫助呢?java

  1. 更方便地部署基礎設施,如 gitlabpostgresprometheusgrafana
  2. 更方便地部署本身的應用,爲公司內部的項目配置 Chart,使用 helm 結合 CI,在 k8s 中部署應用一行命令般簡單

固然 helm 更偉大的做用在於運維人員與開發人員的職責分離node

讓開發人員寫 k8s 資源配置文件是不現實的python

  1. 不是全部開發都瞭解k8s,或者說不多,不瞭解 k8s 很難寫資源配置部署文件
  2. 開發的主要職能仍是在業務上

因而 helm 應時而出,運維寫配置文件,開發給配置文件填不多的參數linux

本篇文章主要介紹如何安裝 helm 以及如何使用 helm 部署基礎服務和業務git

介紹

  • Chart: 一系列 k8s 資源集合的命名,它包含一系列 k8s 資源配置文件的模板與參數,可供靈活配置
  • release: 當一個 Chart 部署後生成一個 release,chart/relase 相似於 docker 中 image/container
  • repo: 即 chart 的倉庫,其中有不少個 chart 可供選擇,如官方 helm/charts

使用 helm create 建立一個 chart,瞭解簡單 chart 的目錄結構程序員

關於如何安裝 helm 參考下一小節github

# 建立一個 chart
$ helm create todo
Creating todo

$ cd todo

# 打印 chart 目錄,主要文件有 Chart.yaml 與 values.yaml
# --dirsfirst 先打印文件夾名稱
$ tree --dirsfirst
.
├── charts
├── templates
│   ├── tests
│   │   └── test-connection.yaml
│   ├── NOTES.txt
│   ├── _helpers.tpl
│   ├── deployment.yaml
│   ├── ingress.yaml
│   └── service.yaml
├── Chart.yaml
└── values.yaml

3 directories, 8 files
複製代碼

查看主要的兩個文件目錄redis

  • templates/: 這是運維大哥寫的配置文件模板,示例是最簡單應用的資源配置,但複雜應用還會有 pvc,role,service-acount 等等
  • values.yaml: 這是給開發小弟寫的可選參數,可是大部分參數都被運維大哥給內置了

使用 helm 部署基礎服務

真實案例能夠參看個人系列文章其它章節docker

這裏講述一些關於部署基礎服務的通常步驟。假設咱們須要部署 redis

這裏使用 helm v3,語法與 v2 可能略有出入

01 查找相關 Chart

使用 helm search hub

$ helm search hub redis
URL                                                     CHART VERSION   APP VERSION     DESCRIPTION
https://hub.helm.sh/charts/bitnami/redis                9.5.0           5.0.5           Open source, advanced key-value store. It is of...
https://hub.helm.sh/charts/hephy/redis                  v2.4.0                          A Redis database for use inside a Kubernetes cl...
https://hub.helm.sh/charts/incubator/redis-cache        0.5.0           4.0.12-alpine   A pure in-memory redis cache, using statefulset...
複製代碼

02 選定 Chart,跟進 Chart 的官方文檔

咱們選定 stable/redis 這個 chart。

跟蹤官方文檔,設置相關參數,存儲爲 values-production.yaml

# Production configuration
$ helm install stable/redis --values values-production.yaml
複製代碼

若是有必要時會添加 repo: helm repo add

# helm repo add stable https://kubernetes-charts.storage.googleapis.com/

# 列出全部 repo
$ helm repo list
NAME            URL
stable          https://kubernetes-charts.storage.googleapis.com/
apphub          https://apphub.aliyuncs.com
jetstack        https://charts.jetstack.io
複製代碼

03 校驗部署狀態

這與須要部署的資源有關,不過通常也就分爲 ServicePodPVC

安裝 helm

這裏參考官方文檔 安裝 helm

helm 在 helm v2 時分爲客戶端 (helm client,即命令行工具) 與服務端 (helm server) 兩部分,在服務端又叫 Tiller,安裝 Tiller 時會在集羣中部署一個 Pod

helm 此時已發佈了 v3,相比 v2 而言,它去掉了 Tiller,更加具體的變動參考 helm v3 FAQ

安裝客戶端 (helm client)

helm client 須要安裝在你能夠訪問 k8s 集羣的任何服務器,如同 kubectl,不過一般來講,你須要安裝在兩個地方

  1. k8s 集羣的 master node
  2. 你本身的 PC/mac 上

在 helm v3 中,只須要安裝 helm client

在 mac 上進行安裝

$ brew install kubernetes-helm
複製代碼

在 linux 上進行安裝

$ curl -LO https://git.io/get_helm.sh
$ chmod 700 get_helm.sh
$ ./get_helm.sh
複製代碼

根據源碼包進行安裝,比較推薦(畢竟使用以上兩種方案可能有網絡問題),至於如何使用 rsync 見如下安裝步驟

使用腳本安裝在國內會出現網絡問題,須要在代理節點安裝並使用 rsync 或者 scp 移動到目標位置

示例選擇 2.14.3 進行安裝,helm 的最新版本能夠參考 helm/helm Releases

能夠選擇 3.0+ 版本的安裝,目前最新版本是 Helm v3.0.0-rc.1 (2019/11/01)

# 下載 MAC 上適用的軟件包
# -L: 追蹤重定向連接
# -O: 保存到本地
# -S: 打印錯誤
$ curl -SLO https://get.helm.sh/helm-v2.14.3-darwin-amd64.tar.gz 
 # 下載 CentOS 上適用的軟件包
$ curl -SLO https://get.helm.sh/helm-v2.14.3-linux-amd64.tar.gz
 # 若是有網絡問題,請在代理節點下載並 rsync 到目標節點,若是沒有,跳過此步
$ rsync -avhzP proxy:/root/helm-v2.14.3-linux-amd64.tar.gz .
 # 若是在 mac 上
$ tar -zxvf helm-v2.14.3-darwin-amd64.tar.gz 
 # 若是在 centos 上
$ tar -zxvf helm-v2.14.3-linux-amd64.tar.gz
 # 進入相應目錄,移至 /bin 目錄
$ mv linux-amd64/helm /usr/local/bin/helm
複製代碼

安裝 tiller (helm server)

若是使用了 Helm v3,則不用安裝 tiller

01 下載鏡像

tiller 的鏡像 gcr.io/kubernetes-helm/tiller:v2.14.3 在 gcr.io 上,這意味着在國內網絡須要先下載到代理節點,再移動到目標位置。具體步驟參照之前章節

02 安裝 Tiller

當安裝好 helm 命令行工具後,使用 helm init 安裝 tiller。安裝 tiller 時會在 k8s 上部署一個 pod。

$ helm init
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
複製代碼

根據提示此時已安裝成功,校驗 Pod 狀態

# 查看 tiller 是否出在運行狀態
$ kubectl get pods --all-namespaces

# 查看 helm 與 tiller 版本
$ helm version
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
複製代碼

更多文章


我是山月,一個夢想跑步與登山的程序員,我會按期分享全棧文章在我的公衆號中,歡迎交流

歡迎關注公衆號山月行,我會按期分享一些先後端以及運維的文章
相關文章
相關標籤/搜索