Helm架构由Helm客户端、Tiller服务器端和Chart仓库所组成;Tiller部署在Kubernetes中,Helm客户端从Chart仓库中获取Chart安装包,并将其安装部署到Kubernetes集群中。
#下载安装包
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.12.2-linux-amd64.tar.gz
tar -zxvf helm-v2.12.2-linux-amd64.tar.gz
cd linux-amd64/
cp helm /usr/local/bin/
rbca-config.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system
这里使用阿里的镜像和阿里的charts仓库,因为默认仓库和镜像需要×××
helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.12.2 --aliyun-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
查看tiller是否安装完成
# kubectl get pod -n kube-system -l app=helm
NAME READY STATUS RESTARTS AGE
tiller-deploy-84bcb9978c-5xccb 1/1 Running 2 10d
验证是否正常,注意 helm和tiller 版本要一致
# helm version
Client: &version.Version{SemVer:"v2.12.2", GitCommit:"7d2b0c73d734f6586ed222a567c5d103fed435be", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.12.2", GitCommit:"7d2b0c73d734f6586ed222a567c5d103fed435be", GitTreeState:"clean"}
helm reset 或 helm reset --force
查看仓库列表
# helm repo list
NAME URL
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami
emar http://192.168.1.57:8879
helm repo remove reponame
helm repo add stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm repo update
helm serve --address 192.168.1.57:8879 --repo-path /data/helm/repository/ --url http://192.168.1.57:8879/charts/ &
kubeapps是一个基于web的用户界面,用于部署和管理在kubernetes群集中的应用程序。Kubeapps允许你:
安装访问
helm repo add bitnami https://charts.bitnami.com/bitnami
helm install --name kubeapps --namespace kubeapps bitnami/kubeapps
kubectl create serviceaccount kubeapps-operator
kubectl create clusterrolebinding kubeapps-operator --clusterrole=cluster-admin --serviceaccount=default:kubeapps-operator
#使用port-forward代理访问pod,可以临时从外网访问。这种方式临时使用可以,最好改成ingress模式
export POD_NAME=$(kubectl get pods -n kubeapps -l "app=kubeapps,release=kubeapps" -o jsonpath="{.items[0].metadata.name}")
kubectl port-forward --address 0.0.0.0 -n kubeapps $POD_NAME 8080:8080
卸载kubeapps
helm delete --purge kubeapps
kubectl delete crd apprepositories.kubeapps.com
下面我们通过一个完整的示例来学习如何使用 Helm 创建、打包、分发、安装、升级及回退Kubernetes应用。
helm create firstchart
查看mychart结构:
firstchart/
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ └── service.yaml
└── values.yaml
生成chart目录里有Chart.yaml, values.yaml and NOTES.txt等文件,下面分别对chart中几个重要文件解释:
其中firstchart/templates/的文件及其作用如下:
Templates 目录下 YAML 文件模板的值默认都是在 values.yaml 里定义的,比如在 deployment.yaml 中定义的容器镜像。
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
其中的 .Values.image.repository 的值就是在 values.yaml 里定义的 nginx,.Values.image.tag 的值就是 stable。
$ cat firstchart/values.yaml|grep repository
repository: nginx
$ cat firstchart/values.yaml|grep tag
tag: stable
以上两个变量值是在 create chart 的时候就自动生成的默认值,你可以根据实际情况进行修改。
打开 Chart.yaml, 填写你部署的应用的详细信息,以 firstchart 为例:
$ cat firstchart/Chart.yaml
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes
name: firstchart
version: 0.1.0
编辑 values.yaml,它默认会在 Kubernetes 部署一个 Nginx。下面是 firstchart 应用的 values.yaml 文件的内容:
$ cat mychart/values.yaml
# Default values for mychart.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
service:
type: ClusterIP
port: 80
ingress:
enabled: false
annotations: {}
# kubernetes.io/ingress.class: nginx
# kubernetes.io/tls-acme: "true"
path: /
hosts:
- chart-example.local
tls: []
# - secretName: chart-example-tls
# hosts:
# - chart-example.local
resources: {}
# We usually recommend not to specify default resources and to leave this as a conscious
# choice for the user. This also increases chances charts run on environments with little
# resources, such as Minikube. If you do want to specify resources, uncomment the following
# lines, adjust them as necessary, and remove the curly braces after 'resources:'.
# limits:
# cpu: 100m
# memory: 128Mi
# requests:
# cpu: 100m
# memory: 128Mi
nodeSelector: {}
tolerations: []
affinity: {}
$ helm lint firstchart/
==> Linting .
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, no failures
如果文件格式错误,可以根据提示进行修改。
$ helm package firstchart
Successfully packaged chart and saved it to: /data/helm/repository/firstchart-0.1.0.tgz
firstchart 目录会被打包为一个 firstchart-0.1.0.tgz 格式的压缩包,该压缩包会被放到当前目录下。
如果你想看到更详细的输出,可以加上 --debug 参数来查看打包的输出,输出内容应该类似如下:
$ helm package firstchart --debug
Successfully packaged chart and saved it to: /data/helm/repository/firstchart-0.1.0.tgz
[debug] Successfully saved /home/k8s/mychart-0.1.0.tgz to /data/helm/repository/
虽然我们已经打包了 Chart 并发布到了 Helm 的本地目录中,但通过 helm search 命令查找,并不能找不到刚才生成的 firstchart包。
$ helm search mychart
No results found
这是因为 Repository 目录中的 Chart 包还没有被 Helm 管理。通过 helm repo list 命令可以看到目前 Helm 中已配置的 Repository 的信息。
# helm repo list
NAME URL
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
bitnami https://charts.bitnami.com/bitnami
emar http://192.168.1.57:8879
通过 helm repo index 命令将 Chart 的 Metadata 记录更新在 index.yaml 文件中:
# 更新 Helm Repository 的索引文件
$ cd /data/helm/repository/
$ helm repo index --url=http://192.168.1.57:8879/charts .
现在再次查找 firstchart 包,就可以搜索到了。
$ helm repo update
$ helm search firstchart
NAME CHART VERSION APP VERSION DESCRIPTION
firstchart 0.1.0 1.0 A Helm chart for Kubernetes
注: 因为网上文档都是deployment,我这里使用daemonset来展示
来看下基于busybox,daemonset的配置
#tree .
.
├── charts
├── Chart.yaml
├── templates
│ ├── busybox-ds.yaml
│ ├── _helpers.tpl
│ └── NOTES.txt
└── values.yaml
查看value
# more values.yaml
# Default values for busybox-ds.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
image:
repository: busybox
tag: latest
pullPolicy: IfNotPresent
查看template里面的busybox-ds.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: busybox
spec:
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
command:
- sleep
- "3600"
nodeSelector:
ds-test: dstest
status:
Chart 被发布到仓储后,就可以通过 helm install 命令部署该 Chart。
当使用helm install 命令部署应用时,实际上就是将 templates 目录下的模板文件渲染成 Kubernetes 能够识别的 YAML 格式。
在部署前我们可以使用
helm install --dry-run --debug --name
命令来验证 Chart 的配置。该输出中包含了模板的变量配置与最终渲染的 YAML 文件。
[root@kvm-ht242057 busybox-ds]# helm install --debug --dry-run --set image.tag='2.0' .
[debug] Created tunnel using local port: '22888'
[debug] SERVER: "127.0.0.1:22888"
[debug] Original chart version: ""
[debug] CHART PATH: /etc/kubernetes/addons/helm/busybox-ds
NAME: bailing-terrier
REVISION: 1
RELEASED: Tue Jan 29 15:20:54 2019
CHART: busybox-ds-0.2.1
USER-SUPPLIED VALUES:
image:
tag: "2.0"
COMPUTED VALUES:
image:
pullPolicy: IfNotPresent
repository: busybox
tag: "2.0"
HOOKS:
MANIFEST:
---
# Source: busybox-ds/templates/busybox-ds.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: busybox
spec:
template:
metadata:
labels:
app: busybox
spec:
containers:
- name: busybox
image: busybox:2.0
command:
- sleep
- "3600"
nodeSelector:
ds-test: dstest
status:
验证完成没有问题后,我们就可以使用以下命令将其部署到 Kubernetes 上了。
# 部署时需指定 Chart 名及 Release(部署的实例)名。
helm install emar/busybox-ds --name busybox
Release "busybox" has been upgraded. Happy Helming!
LAST DEPLOYED: Thu Jan 24 17:39:26 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1beta1/DaemonSet
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox 1 1 1 0 1 ds-test=dstest 24h
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
busybox-n7725 1/1 Running 24 24h
NOTES:
please ignore it, this is a test
node selector:
ds-test: dstest
完成部署后,现在 busybox 就已经部署到 Kubernetes 集群上。
# kubectl get ds
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox 1 1 1 0 1 ds-test=dstest 5d22h
使用下面的命令列出的所有已部署的 Release 以及其对应的 Chart。
# helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
busybox 6 Thu Jan 24 17:40:02 2019 DEPLOYED busybox-ds-0.2.1 1.0 default
dealing-buffalo 1 Thu Jan 24 17:39:02 2019 FAILED busybox-ds-0.2.1 1.0 default
kubeapps 1 Tue Jan 22 17:45:00 2019 DEPLOYED kubeapps-1.2.3 v1.1.1 kubeapps
wobbling-butterfly 1 Thu Jan 24 17:25:48 2019 FAILED busybox-ds-0.2.1 1.0 default
还可以使用 helm status 查询一个特定的 Release 的状态。
helm status busybox
LAST DEPLOYED: Thu Jan 24 17:40:02 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1beta1/DaemonSet
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox 1 1 1 0 1 ds-test=dstest 5d22h
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
busybox-n7725 1/1 Running 142 5d22h
NOTES:
please ignore it, this is a test
node selector:
ds-test: dstest
从上面 helm list 输出的结果中我们可以看到有一个 Revision(更改历史)字段,该字段用于表示某一个 Release 被更新的次数,我们可以用该特性对已部署的 Release 进行回滚
将版本号从 0.1.0 修改为 0.2.0, 然后使用 helm package 命令打包并发布到本地仓库
$ cat busybox-ds/Chart.yaml
apiVersion: v1
appVersion: "1.0"
description: A Helm chart for Kubernetes,test Daemonset
name: busybox
version: 0.2.0
$ helm package busybox-ds
Successfully packaged chart and saved it to: /data/helm/repository/busybox-ds-0.2.0.tgz
我们可以看到在本地仓库中 busybox-ds 有两个版本。
# helm search busy -l
NAME CHART VERSION APP VERSION DESCRIPTION
emar/busybox-ds 0.2.0 1.0 A Helm chart for Kubernetes, test Daemonset
emar/busybox-ds 0.1.0 1.0 A Helm chart for Kubernetes
现在用 helm upgrade 命令将已部署的 busybox 升级到新版本。你可以通过 --version 参数指定需要升级的版本号,如果没有指定版本号,则缺省使用最新版本。
helm upgrade emar/busybox-ds --name busybox
Release "busybox" has been upgraded. Happy Helming!
LAST DEPLOYED: Thu Jan 24 17:39:26 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1beta1/DaemonSet
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
busybox 1 1 1 0 1 ds-test=dstest 24h
==> v1/Pod(related)
NAME READY STATUS RESTARTS AGE
busybox-n7725 1/1 Running 24 24h
NOTES:
please ignore it, this is a test
node selector:
ds-test: dstest
完成后,可以看到已部署的 mike-test 被升级到 0.2.0 版本
helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
busybox 6 Thu Jan 24 17:40:02 2019 DEPLOYED busybox-ds-0.2.0 2.0 default
如果更新后的程序由于某些原因运行有问题,需要回退到旧版本的应用。首先我们可以使用 helm history 命令查看一个 Release 的所有变更记录
# helm history busybox
REVISION UPDATED STATUS CHART DESCRIPTION
1 Wed Jan 23 17:04:18 2019 SUPERSEDED busybox-ds-0.1.1 Install complete
2 Wed Jan 23 17:09:47 2019 SUPERSEDED busybox-ds-0.2.1 Upgrade complete
3 Wed Jan 23 17:11:01 2019 SUPERSEDED busybox-ds-0.1.1 Rollback to 1
4 Wed Jan 23 17:13:05 2019 SUPERSEDED busybox-ds-0.2.1 Upgrade complete
5 Thu Jan 24 17:39:26 2019 SUPERSEDED busybox-ds-0.2.1 Upgrade complete
6 Thu Jan 24 17:40:02 2019 DEPLOYED busybox-ds-0.2.1 Upgrade complete
其次,我们可以使用下面的命令对指定的应用进行回退。
$ helm rollback busybox 1
Rollback was a success! Happy Helming!
如果每次上线回滚都要打包,更新chart仓,明显是不切实际的。因为业务上线不仅要保证稳定、可追溯,也要保证时效。
1、使用set参数,动态指定镜像tag。这里要用到values.yaml
以busybox举例:
values.yaml
# Default values for busybox-ds.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
image:
repository: busybox
tag: latest
pullPolicy: IfNotPresent
关于values的相关命令
查看charts默认配置
helm inspect values busybox
覆盖chart中的默认值,通过指定配置文件方式
helm install -f values.yaml ./busybox
或者通过–set key=value形式
helm install --set image.tag='2.0' ./busybox
单独跑查看配置是否正常
helm install -debug --dry-run --set image.tag='2.0'
# helm delete busybox
release "busybox" deleted
# helm ls -a busybox
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
busybox 1 Thu Jan 24 17:39:02 2019 DELETED busybox-ds-0.2.1 1.0 default
# helm ls --deleted
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
busybox 1 Thu Jan 24 17:39:02 2019 DELETED busybox-ds-0.2.1 1.0 default
# helm hist busybox
REVISION UPDATED STATUS CHART DESCRIPTION
1 Thu Jan 24 17:39:02 2019 DELETED busybox-ds-0.2.1 Deletion complete
2 Wed Jan 23 17:09:47 2019 SUPERSEDED busybox-ds-0.2.1 Upgrade complete
3 Wed Jan 23 17:11:01 2019 SUPERSEDED busybox-ds-0.1.1 Rollback to 1
# helm delete --purge busybox
release "busybox" deleted
# helm hist busybox
Error: release: "busybox" not found
# helm ls 命令也已均无查询记录。
# helm ls --deleted
# helm ls -a busybox
转载于:https://blog.51cto.com/foxhound/2347904