1, 什么是helm

官网的介绍:https://helm.sh/ --通过Charts帮助你定义,安装,升级复杂的k8s上面部署的程序,Charts可以方便的创建,共享,发布

Helm helps you manage Kubernetes applications
 — Helm Charts help you define, install, and upgrade even the most complex Kubernetes application.
Charts are easy to create, version, share, and publish — so start using Helm and stop the copy-and-paste.

2, 安装helm

官网安装指南:https://helm.sh/docs/using_helm/#quickstart

角色安装方式
clienthttps://github.com/helm/helm/releases下载v2.14.3,解压安装
serverhelm init --tiller-image=sapcc/tiller:v2.14.3
#1, 安装客户端
wget https://get.helm.sh/helm-v2.14.3-linux-amd64.tar.gz
tar -zxvf  helm-v2.14.3-linux-amd64.tar.gz 
mv linux-amd64/helm /usr/local/bin

[root@master ~]# helm version
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Error: could not find a ready tiller pod

# 2, 安装服务端
#创建tiller角色, 并授权
cat > helm-rabc.yaml <<EOF
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
EOF
kubectl apply -f  helm-rabc.yaml
	
#初始化helm
helm init \
--upgrade \
--history-max 200 \
--stable-repo-url http://mirror.azure.cn/kubernetes/charts/ \
--service-account tiller \
--tiller-image  sapcc/tiller:v2.14.3  # registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.3

#验证
[root@master ~]# helm version
Client: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.14.3", GitCommit:"0e7f3b6637f7af8fcfddb3d2941fcc7cbebb0085", GitTreeState:"clean"}

3, helm运行程序

1, 创建本地chart模板

#1, 创建程序charts文件
[root@master ~]# helm create hello-helm
Creating hello-helm
[root@master ~]# ls hello-helm/
charts  Chart.yaml  templates  values.yaml

[root@master ~]# tree hello-helm/
hello-helm/
├── charts
├── Chart.yaml
├── templates
│   ├── deployment.yaml
│   ├── _helpers.tpl
│   ├── ingress.yaml
│   ├── NOTES.txt
│   ├── service.yaml
│   └── tests
│       └── test-connection.yaml
└── values.yaml
3 directories, 8 files

[root@master ~]# cat hello-helm/values.yaml 
# Default values for hello-helm.
# This is a YAML-formatted file.
# Declare variables to be passed into your templates.
replicaCount: 1
image:
  repository: nginx
  tag: stable
  pullPolicy: IfNotPresent
imagePullSecrets: []
nameOverride: ""
fullnameOverride: ""
service:
  type: ClusterIP
  port: 80

2, 从本地charts安装应用程序

#  从本地charts安装应用程序
[root@master ~]# helm  install hello-helm/   #helm  install --dry-run --debug hello-helm
NAME:   idolized-bobcat
LAST DEPLOYED: Tue Oct 22 14:25:14 2019
NAMESPACE: default
STATUS: DEPLOYED
RESOURCES:
==> v1/Deployment
NAME                        READY  UP-TO-DATE  AVAILABLE  AGE
idolized-bobcat-hello-helm  0/1    0           0          0s
==> v1/Pod(related)
NAME                                         READY  STATUS   RESTARTS  AGE
idolized-bobcat-hello-helm-5865689786-nngq5  0/1    Pending  0         0s
==> v1/Service
NAME                        TYPE       CLUSTER-IP    EXTERNAL-IP  PORT(S)  AGE
idolized-bobcat-hello-helm  ClusterIP  10.1.186.250  <none>       80/TCP   0s

NOTES:
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=hello-helm,app.kubernetes.io/instance=idolized-bobcat" -o jsonpath="{.items[0].metadata.name}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl port-forward $POD_NAME 8080:80
  
#3, 查看创建的release(chart运行的结果就是release)
[root@master helm]# helm list            #helm ls
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	1       	Tue Oct 22 14:25:14 2019	DEPLOYED	hello-helm-0.1.0	1.0        	default  

#4, 访问服务: 按照给出的提示运行
  export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=hello-helm,app.kubernetes.io/instance=idolized-bobcat" -o jsonpath="{.items[0].metadata.name}")
  kubectl port-forward $POD_NAME 8080:80 #此时,该bash窗口会阻塞住,需要开启另一个bash窗口来访问8080端口

#5, 新窗口访问8080端口
[root@master ~]# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
..........

4, helm打包发布chart

#1, 打包现有的char
[root@master ~]# helm package hello-helm
Successfully packaged chart and saved it to: /root/hello-helm-0.1.0.tgz

#2, 查看默认的仓库地址
[root@master ~]# ls .helm/repository/local/
hello-helm-0.1.0.tgz  index.yaml

#3, 查看默认仓库
[root@master ~]# helm search hello-helm
NAME            	CHART VERSION	APP VERSION	DESCRIPTION                
local/hello-helm	0.1.0        	1.0        	A Helm chart for Kubernetes
[root@master ~]# helm repo list
NAME  	URL                                             
stable	https://kubernetes-charts.storage.googleapis.com
local 	http://127.0.0.1:8879/charts           

#4, 修改仓库ip地址: 修改127.0.0.1 为本机ip: 192.168.56.190
[root@master ~]#  helm serve --address 192.168.56.190:8879 &
[1] 13818
[root@master ~]# netstat -anop |grep 8879
tcp        0      0 192.168.56.190:8879     0.0.0.0:*               LISTEN      13818/helm           off (0.00/0/0)
[root@master ~]# helm repo list
NAME  	URL                                             
stable	https://kubernetes-charts.storage.googleapis.com
local 	http://192.168.56.190:8879                      

#5, 搜索本地刚打包的chart
[root@master ~]# helm search hello-helm
NAME            	CHART VERSION	APP VERSION	DESCRIPTION                
local/hello-helm	0.1.0        	1.0        	A Helm chart for Kubernetes

5, helm更新仓库索引/升级/删除/回滚release

#1, 修改现有的chart, 并修改版本号
[root@master ~]#  sed  -i "s/version: 0.1.0/version: 0.2.0/" hello-helm/Chart.yaml
[root@master ~]# helm package hello-helm
Successfully packaged chart and saved it to: /root/hello-helm-0.2.0.tgz
[root@master ~]# ls .helm/repository/local/
hello-helm-0.1.0.tgz  hello-helm-0.2.0.tgz  index.yaml
[root@master ~]# helm search hello-helm -l #  -l, --versions         show the long listing, with each version of each chart on its own line
NAME            	CHART VERSION	APP VERSION	DESCRIPTION                
local/hello-helm	0.2.0        	1.0        	A Helm chart for Kubernetes
local/hello-helm	0.1.0        	1.0        	A Helm chart for Kubernetes

#查看仓库中的release tar包: 发现0.2.0这个版本的仓库地址没有修改好,需要仓库更新索引
[root@master ~]# curl http://192.168.56.190:8879
........
  <li>hello-helm<ul>
    <li><a href="http://127.0.0.1:8879/charts/hello-helm-0.2.0.tgz">hello-helm-0.2.0</a></li>
    <li><a href="http://192.168.56.190:8879/hello-helm-0.1.0.tgz">hello-helm-0.1.0</a></li>
#更新仓库索引
[root@master ~]# helm repo index  --url=http://192.168.56.190:8879  .helm/repository/local/
[root@master ~]# curl http://192.168.56.190:8879
........
  <li>hello-helm<ul>
    <li><a href="http://192.168.56.190:8879/hello-helm-0.2.0.tgz">hello-helm-0.2.0</a></li>
    <li><a href="http://192.168.56.190:8879/hello-helm-0.1.0.tgz">hello-helm-0.1.0</a></li>
    
 # 2, 升级程序
[root@master ~]# helm list
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	1       	Tue Oct 22 14:25:14 2019	DEPLOYED	hello-helm-0.1.0	1.0        	default  

[root@master aa]# helm upgrade idolized-bobcat local/hello-helm
Release "idolized-bobcat" has been upgraded.
LAST DEPLOYED: Tue Oct 22 16:36:39 2019
NAMESPACE: default
STATUS: DEPLOYED
.....

[root@master aa]# helm list
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	2       	Tue Oct 22 16:36:39 2019	DEPLOYED	hello-helm-0.2.0	1.0        	default  

[root@master aa]# helm history idolized-bobcat
REVISION	UPDATED                 	STATUS    	CHART           	DESCRIPTION     
1       	Tue Oct 22 14:25:14 2019	SUPERSEDED	hello-helm-0.1.0	Install complete
2       	Tue Oct 22 16:36:39 2019	DEPLOYED	hello-helm-0.2.0	Upgrade complete

#3, 回滚程序
[root@master aa]# helm rollback idolized-bobcat 1
Rollback was a success.
[root@master aa]# helm history  idolized-bobcat
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	5       	Tue Oct 22 16:41:15 2019	DEPLOYED	hello-helm-0.1.0	1.0        	default  
REVISION	UPDATED                 	STATUS    	CHART           	DESCRIPTION     
1       	Tue Oct 22 14:25:14 2019	SUPERSEDED	hello-helm-0.1.0	Install complete
2       	Tue Oct 22 16:34:39 2019	SUPERSEDED	hello-helm-0.2.0	Upgrade complete
3       	Tue Oct 22 16:41:34 2019	DEPLOYED  	hello-helm-0.1.0	Rollback to 1   

#4, 删除release
[root@master ~]# helm ls 
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	6       	Tue Oct 22 16:41:34 2019	DEPLOYED	hello-helm-0.1.0	1.0        	default  
[root@master ~]# helm delete idolized-bobcat
release "idolized-bobcat" deleted
[root@master ~]# helm ls 
[root@master ~]# helm ls -a
NAME             	REVISION	UPDATED                 	STATUS 	CHART           	APP VERSION	NAMESPACE
idolized-bobcat  	6       	Tue Oct 22 16:41:34 2019	DELETED	hello-helm-0.1.0	1.0        	default  

[root@master aa]#  helm history  idolized-bobcat
NAME           	REVISION	UPDATED                 	STATUS  	CHART           	APP VERSION	NAMESPACE
idolized-bobcat	5       	Tue Oct 22 16:41:15 2019	DEPLOYED	hello-helm-0.1.0	1.0        	default  
REVISION	UPDATED                 	STATUS    	CHART           	DESCRIPTION     
1       	Tue Oct 22 14:25:14 2019	SUPERSEDED	hello-helm-0.1.0	Install complete
2       	Tue Oct 22 16:34:39 2019	SUPERSEDED	hello-helm-0.2.0	Upgrade complete
3       	Tue Oct 22 16:41:34 2019	DEPLOYED  	hello-helm-0.1.0	Rollback to 1   
4       	Tue Oct 22 16:44:34 2019	DELETED  	hello-helm-0.1.0		Deletion complete

#确认删除/或撤销删除
[root@master ~]# helm delete idolized-bobcat --purge   # helm rollback  idolized-bobcat  1
release "idolized-bobcat" deleted
[root@master ~]# helm ls -a
NAME             	REVISION	UPDATED                 	STATUS 	CHART          	APP VERSION	NAMESPACE
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐