[TOC]mysql
操做系統:CentOS7
kubernetes:1.11nginx
# 下載腳本並執行安裝 curl https://raw.githubusercontent.com/kubernetes/helm/master/scripts/get > get_helm.sh bash get_helm.sh
# 查看Helm客戶端的版本號 helm version
注意版本號,後續容器服務的版本要和這個一致, 不然會出現問題。git
Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
# 在 Kubernetes 羣集上安裝 Tiller(helm服務端),注意和上面版本號一致 helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.9.1 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
說明:
helm init --upgrade
會在Kubernetes集羣上安裝配置Tiller, 倉庫默認使用https://kubernetes-charts.storage.googleapis.com 。因爲國內沒法訪問相關域名,可使用阿里雲容器服務提供的鏡像和站點。github
查看容器運行狀況web
kubectl get pod --all-namespaces|grep tiller
redis
kube-system tiller-deploy-b67849f44-cs4qr 1/1 Running 0 46m
從Kubernetes 1.6開始,API Server啓用了RBAC受權。而Tiller部署沒有定義受權的ServiceAccount,這會致使訪問API Server時被拒絕。咱們能夠採用以下方法,爲Tiller部署添加受權。sql
kubectl create serviceaccount --namespace kube-system tiller kubectl create clusterrolebinding tiller-cluster-rule --clusterrole=cluster-admin --serviceaccount=kube-system:tiller kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'
# 查看已部署的應用 helm list
卸載helm服務端json
helm reset # helm reset --force
# 先移除原先的倉庫 helm repo remove stable # 添加新的倉庫地址 helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts # 更新倉庫 helm repo update
Monocular基於web的用戶界面,用於管理Kubernetes應用打包爲Helm Charts。它容許您從多個存儲庫搜索和發現可用Chats,並安裝在您的集羣中,只須要鼠標點點就能完成。ubuntu
準備條件vim
helm install stable/nginx-ingress
helm install stable/nginx-ingress --set controller.hostNetwork=true
安裝
helm repo add monocular https://helm.github.io/monocular helm install --name monocular monocular/monocular
默認它是配置到ingress中的,經過ingress節點IP就能訪問。
注意
安裝過程當中,pvc建立提示失敗,須要手動干預提供可用pvc。
kubeapps是一個基於web的用戶界面,用於部署和管理在kubernetes羣集中的應用程序。Kubeapps容許你:
helm
的應用程序helm
的應用程序鏈接到外部服務安裝
kubectl create namespace kubeapps helm repo add bitnami https://charts.bitnami.com/bitnami helm install --name kubeapps --namespace kubeapps bitnami/kubeapps
kubectl port-forward
不推薦,最好配置ingress,而Monocular已自動配置ingress;chart repo是一個可用來存儲index.yml與打包的chart文件的HTTP server。
當要分享chart時,須要上傳chart文件到chart倉庫。任何一個能可以提供YAML與tar文件的HTTP server均可以當作chart倉庫,好比Google Cloud Storage (GCS) bucket、Amazon S3 bucket、Github Pages或建立你本身的web服務器。官方chart倉庫由Kubernetes Charts維護, Helm容許咱們建立私有chart倉庫。
查看目前的repo,helm repo list
NAME URL stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts local http://localhost:8879/charts monocular https://helm.github.io/monocular bitnami https://charts.bitnami.com/bitnami incubator http://storage.googleapis.com/kubernetes-charts-incubator
helm
執行tiller命令後默認會配置一個名爲l的本地repo。
一個chart倉庫由一個chart包與index.yaml
文件組成,index.yaml
記錄了chart倉庫中所有chart的索引,一個本地chart倉庫的佈局例子以下:
~/.helm/ |-- cache | `-- archive | |-- drupal-0.9.2.tgz | `-- mariadb-1.0.3.tgz |-- plugins |-- repository | |-- cache | | |-- fantastic-charts-index.yaml | | |-- local-index.yaml -> /home/ts1/.helm/repository/local/index.yaml | | |-- mariadb-1.0.3.tgz-index.yaml | | |-- memcached-1.2.1.tgz-index.yaml | | |-- mychart_xia-0.1.0.tgz-index.yaml | | |-- mysql-0.2.8.tgz-index.yaml | | |-- stable-index.yaml | | |-- test-0.1.0.tgz-index.yaml | | `-- test-0.1.8.tgz-index.yaml | |-- local | | |-- index.yaml | | |-- mychart-0.1.0.tgz | | |-- mychart_xia-0.1.0.tgz | | |-- mysql-0.2.8.tgz | | |-- mysql-6.19.centos-29.tgz | | |-- test-0.1.0.tgz | | |-- test-0.1.8.tgz | | `-- test-0.1.9.tgz | `-- repositories.yaml `-- starters
~/.helm/repository/local/index.yaml文件中記錄了chart的諸如名稱、url、version等一些metadata信息。
mkdir -p /data/helm/charts cat > /data/helm/start_local_helm.sh <<EOF #!/usr/bin/env bash helm_path=\$(which helm) helm_pid=\$(pidof \$helm_path) helm_data_path="/data/helm/charts" if [ -z "\$helm_pid" ]; then cd \$helm_data_path nohup \$helm_path serve --address 0.0.0.0:8879 --repo-path \$helm_data_path & else echo -e "helm already running." fi exit 0 EOF sh /data/helm/start_local_helm.sh
helm serve --help
This command starts a local chart repository server that serves charts from a local directory. The new server will provide HTTP access to a repository. By default, it will scan all of the charts in '$HELM_HOME/repository/local' and serve those over the local IPv4 TCP port (default '127.0.0.1:8879'). This command is intended to be used for educational and testing purposes only. It is best to rely on a dedicated web server or a cloud-hosted solution like Google Cloud Storage for production use. See https://github.com/kubernetes/helm/blob/master/docs/chart_repository.md#hosting-chart-repositories for more information on hosting chart repositories in a production setting. Usage: helm serve [flags] Flags: --address string address to listen on (default "127.0.0.1:8879") --repo-path string local directory path from which to serve charts --url string external URL of chart repository Global Flags: --debug enable verbose output --home string location of your Helm config. Overrides $HELM_HOME (default "/root/.helm") --host string address of Tiller. Overrides $HELM_HOST --kube-context string name of the kubeconfig context to use --tiller-connection-timeout int the duration (in seconds) Helm will wait to establish a connection to tiller (default 300) --tiller-namespace string namespace of Tiller (default "kube-system")
添加本地repohelm repo add local http://192.168.105.92:8879/charts
上面步驟中,已經建立了一個本地的repo,接下來說述如何在repo中增長一個可用來部署的軟件包chart。chart須遵循 SemVer 2 規則填寫正確的版本格式。各類chart包能夠在github下載。
由於官方chart裏的image鏡像被牆的可能和本身定製參數的設置,咱們將修改過的chart添加到本機chart中。
cd /data/helm helm fetch incubator/zookeeper --untar helm package zookeeper mv zookeeper-1.1.1.tgz charts/
注
helm package
的做用是在當前目錄下將軟件打包爲tgz,假如這個軟件包中有requirement.yaml,則打包時還須要加上--dependency-update
,用來update dependencies from "requirements.yaml" to dir "charts/" before packaging
更新index.yaml文件
cd /data/helm helm repo index charts --url http://192.168.105.92:8879/charts helm repo remove local helm repo add local http://192.168.105.92:8879/charts
查看chart是否上傳倉庫成功:helm search zookeeper|grep local
local/zookeeper 1.1.1 3.4.10 Centralized service for maintaining configurati...
獲取chart
獲取版本爲0.3.5的mysql並解壓縮包:
$ helm fetch stable/mysql --version 0.3.5 --untar $ ls mysql/ Chart.yaml README.md templates values.yaml $ helm lint mysql ==> Linting mysql Lint OK 1 chart(s) linted, no failures
利用helm lint命令檢查下載的chart是否存在問題:
$ helm lint mysql ==> Linting mysql Lint OK 1 chart(s) linted, no failures
建立自定義charthelm create mychart
查看mychart結構:
mychart/ ├── charts ├── Chart.yaml ├── templates │ ├── deployment.yaml │ ├── _helpers.tpl │ ├── ingress.yaml │ ├── NOTES.txt │ └── service.yaml └── values.yaml 2 directories, 7 files
生成chart目錄裏有Chart.yaml
, values.yaml
and NOTES.txt
等文件,下面分別對chart中幾個重要文件解釋:Chart.yaml
包含了chart的metadata,描述了Chart名稱、描述信息與版本。values.yaml
:存儲了模板文件變量。templates/
:記錄了所有模板文件。charts/
:依賴chart存儲路徑。
其中mychart/templates/
的文件及其做用以下:NOTES.txt
:給出了部署chart後的幫助文檔,例如如何使用chart、列出默認的設置等。deployment.yaml
:建立 Kubernetes deployment的yaml文件。service.yaml
:建立deployment的service endpoint yams文件。_helpers.tpl
: 模板使用幫助文件。
chart安裝有如下幾種方式:
指定chart: helm install stable/mariadb
指定打包的chart: helm install ./nginx-1.2.3.tgz
指定打包目錄: helm install ./nginx
指定chart包URL: helm install https://example.com/charts/nginx-1.2.3.tgz
覆蓋chart中的默認值,經過指定配置文件方式:helm install -f myvalues.yaml ./redis
或者經過–set key=value形式:helm install --set name=prod ./redis
安裝release名稱爲mysql例子以下,請注意NOTES中對Mysql的使用說明:
vim mysql/values.yaml
找到storageClass,並修改其值,這裏咱們使用可用的動態卷ceph-rbd
storageClass: "ceph-rbd"`
安裝release
[root@lab1 helm]# helm install -n mysql -f mysql/values.yaml --set resources.requests.memory=512Mi mysql NAME: mysql LAST DEPLOYED: Mon Aug 27 11:23:27 2018 NAMESPACE: default STATUS: DEPLOYED RESOURCES: ==> v1/Secret NAME TYPE DATA AGE mysql-mysql Opaque 2 0s ==> v1/PersistentVolumeClaim NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE mysql-mysql Pending ceph-rbd 0s ==> v1/Service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE mysql-mysql ClusterIP 10.101.206.24 <none> 3306/TCP 0s ==> v1beta1/Deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE mysql-mysql 1 1 1 0 0s ==> v1/Pod(related) NAME READY STATUS RESTARTS AGE mysql-mysql-7f56cd565b-sfmgj 0/1 Pending 0 0s NOTES: MySQL can be accessed via port 3306 on the following DNS name from within your cluster: mysql-mysql.default.svc.cluster.local To get your root password run: MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace default mysql-mysql -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-mysql -p To connect to your database directly from outside the K8s cluster: MYSQL_HOST=127.0.0.1 MYSQL_PORT=3306 # Execute the following commands to route the connection: export POD_NAME=$(kubectl get pods --namespace default -l "app=mysql-mysql" -o jsonpath="{.items[0].metadata.name}") kubectl port-forward $POD_NAME 3306:3306 mysql -h ${MYSQL_HOST} -P${MYSQL_PORT} -u root -p${MYSQL_ROOT_PASSWORD}
查看release狀態helm status mysql
NAME: mysql LAST DEPLOYED: Mon Aug 27 11:23:27 2018 NAMESPACE: default STATUS: DEPLOYED
或經過helm list -a查看所有的release,tag 「-a」是查看所有的release,包括已部署、部署失敗、正在刪除、已刪除release等。
更新release
helm upgrade mysql -f mysql/values.yaml --set resources.requests.memory=1024Mi mysql
查看指定release的歷史部署版本信息:
[root@lab1 helm]# helm hist mysql REVISION UPDATED STATUS CHART DESCRIPTION 1 Mon Aug 27 11:23:27 2018 SUPERSEDED mysql-0.3.5 Install complete 2 Mon Aug 27 11:26:09 2018 DEPLOYED mysql-0.3.5 Upgrade complete
查看指定release的歷史版本部署時部分配置信息,以resources.requests.memory爲例,符合查看部署符合預期:即第一次部署resources.requests.memory設置爲512Mi,第二次的升級resources.requests.memory設置爲1024Mi:
helm get --revision 1 mysql
版本回滾
回滾到第一次的版本:
[root@lab1 helm]# helm rollback --debug mysql 1 [debug] Created tunnel using local port: '44164' [debug] SERVER: "127.0.0.1:44164" Rollback was a success! Happy Helming!
查看mysql release的版本信息,當前已經回滾到REVISION爲1的版本:
[root@lab1 helm]# helm hist mysql REVISION UPDATED STATUS CHART DESCRIPTION 1 Mon Aug 27 11:23:27 2018 SUPERSEDED mysql-0.3.5 Install complete 2 Mon Aug 27 11:26:09 2018 SUPERSEDED mysql-0.3.5 Upgrade complete 3 Mon Aug 27 11:29:24 2018 SUPERSEDED mysql-0.3.5 Rollback to 1
刪除release
[root@lab1 helm]# helm delete mysql release "mysql" deleted
確認release 是否刪除:
[root@lab1 helm]# helm ls -a mysql NAME REVISION UPDATED STATUS CHART NAMESPACE mysql 4 Mon Aug 27 11:29:45 2018 DELETED mysql-0.3.5 default
即便刪除的release ,其發佈的歷史信息仍是繼續被保存。
[root@lab1 helm]# helm hist mysql REVISION UPDATED STATUS CHART DESCRIPTION 1 Mon Aug 27 11:23:27 2018 SUPERSEDED mysql-0.3.5 Install complete 2 Mon Aug 27 11:26:09 2018 SUPERSEDED mysql-0.3.5 Upgrade complete 3 Mon Aug 27 11:29:24 2018 SUPERSEDED mysql-0.3.5 Rollback to 1 4 Mon Aug 27 11:29:45 2018 DELETED mysql-0.3.5 Deletion complete
能夠恢復一個已經刪除的release:
[root@lab1 helm]# helm rollback --debug mysql 2 [debug] Created tunnel using local port: '33811' [debug] SERVER: "127.0.0.1:33811" Error: "mysql" has no deployed releases
若是但願完全刪除一個release,能夠用以下命令:
[root@lab1 helm]# helm delete --purge mysql release "mysql" deleted
再次查看剛被刪除的mysql release,提示已經沒法找到,符合預期:
[root@lab1 helm]# helm hist mysql Error: release: "mysql" not found
Helm對release的版本管理
在上面例子中,已經展現了Helm對release的很是強大的版本管理功能,好比經過」helm list -a」查看有哪些release,經過」 helm hist「查看某一個具體的release發佈過的歷史版本,以及經過」 helm get --revision」,查看某個release的一次歷史版本對應的具體應用配置信息等。即便已經被刪除的release仍然有記錄,而且經過Helm可以快速回滾到已刪除release的某個發佈過的歷史版本。Helm的這些版本管理功能,Kubernetes原生並不支持。
參考資料:
[1] https://helm.sh/
[2] https://whmzsu.github.io/helm-doc-zh-cn/
[3] 簡化Kubernetes應用部署工具-Helm簡介
[4] 簡化Kubernetes應用部署工具-Helm安裝
[5] 簡化Kubernetes應用部署工具-Helm之應用部署
[6] 簡化Kubernetes應用部署工具-Helm之Release配置
[7] https://github.com/helm/monocular
[8] https://github.com/kubeapps/kubeapps