一: 构建一个Helm Chart
[root@node-16 ~]# helm create mychart
Creating mychart
[root@node-16 ~]# ls mychart/
charts Chart.yaml templates values.yaml
[root@node-16 ~]#
启动创建的mychart
[root@node-16 ~]# helm install test mychart/
NAME: test
LAST DEPLOYED: Tue Apr 28 14:07:53 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
NOTES:
1. Get the application URL by running these commands:
export POD_NAME=$(kubectl get pods --namespace default -l "app.kubernetes.io/name=mychart,app.kubernetes.io/instance=test" -o jsonpath="{.items[0].metadata.name}")
echo "Visit http://127.0.0.1:8080 to use your application"
kubectl --namespace default port-forward $POD_NAME 8080:80
[root@node-16 ~]#
[root@node-16 ~]#
[root@node-16 ~]# kubectl get pod|grep test
test-mychart-b5cd6d7c8-r8vf4 1/1 Running 0 56s
[root@node-16 ~]#
[root@node-16 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db01-mysql ClusterIP 10.0.0.206 <none> 3306/TCP 2d
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 6d22h
metrics-app ClusterIP 10.0.0.254 <none> 80/TCP 4d4h
mysql ClusterIP 10.0.0.91 <none> 3306/TCP 61m
mysql-02 ClusterIP 10.0.0.169 <none> 3306/TCP 41m
nginx-service NodePort 10.0.0.75 <none> 80:30574/TCP 6d4h
nginx-service-nfs02 NodePort 10.0.0.73 <none> 80:31835/TCP 6d21h
test-mychart ClusterIP 10.0.0.24 <none> 80/TCP 6m49s
web ClusterIP 10.0.0.186 <none> 80/TCP 4d21h
[root@node-16 ~]# curl 10.0.0.24
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@node-16 ~]#
[root@node-16 ~]# curl -I 10.0.0.24
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Tue, 28 Apr 2020 06:16:32 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 23 Apr 2019 10:18:21 GMT
Connection: keep-alive
ETag: "5cbee66d-264"
Accept-Ranges: bytes
[root@node-16 ~]#
[root@node-16 ~]#
[root@node-16 ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
db01 default 1 2020-04-26 13:56:58.07831662 +0800 CST deployed mysql-1.6.3 5.7.28
mysql default 1 2020-04-28 13:13:23.036473313 +0800 CST deployed mysql-1.6.3 5.7.28
mysql-02 default 1 2020-04-28 13:33:06.455685197 +0800 CST deployed mysql-1.6.3 5.7.28
test default 1 2020-04-28 14:07:53.478943141 +0800 CST deployed mychart-0.1.0 1.16.0
[root@node-16 ~]#
[root@node-16 ~]#
[root@node-16 mychart]# tree
.
├── charts
├── Chart.yaml
├── templates
│?? ├── deployment.yaml
│?? ├── _helpers.tpl
│?? ├── ingress.yaml
│?? ├── NOTES.txt
│?? ├── serviceaccount.yaml
│?? ├── service.yaml
│?? └── tests
│?? └── test-connection.yaml
└── values.yaml
3 directories, 9 files
[root@node-16 mychart]#
[root@node-16 mychart]#
删除templates 里面的yaml ,重新部署一个charts
[root@node-16 mychart]# tree .
.
├── charts
├── Chart.yaml
├── templates
└── values.yaml
2 directories, 2 files
[root@node-16 mychart]#
创建一个deployment 到templates
[root@node-16 templates]#
[root@node-16 templates]#
[root@node-16 templates]# kubectl create deployment mychart --image=nginx:1.16 -o yaml --dry-run
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mychart
name: mychart
spec:
replicas: 1
selector:
matchLabels:
app: mychart
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mychart
spec:
containers:
- image: nginx:1.16
name: nginx
resources: {}
status: {}
[root@node-16 templates]#
重定向到本地的deployment.yaml文件
[root@node-16 templates]#
[root@node-16 templates]# kubectl create deployment mychart --image=nginx:1.16 -o yaml --dry-run > deployment.yaml
[root@node-16 templates]#
[root@node-16 templates]#
修改deployment.yaml 文件,把不用的代码删除
[root@node-16 templates]#
[root@node-16 templates]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mychart
name: mychart
spec:
replicas: 1
selector:
matchLabels:
app: mychart
template:
metadata:
labels:
app: mychart
spec:
containers:
- image: nginx:1.16
name: nginx
[root@node-16 templates]#
把deployment模板的 镜像和副本数设置变量
[root@node-16 templates]# cat deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ .Values.name }}
spec:
replicas: {{ .Values.replicas }}
selector:
matchLabels:
app: mychart
template:
metadata:
labels:
app: mychart
spec:
containers:
- image: {{ .Values.image }}:{{ .Values.imageTag }}
name: nginx
[root@node-16 templates]#
变量在..valume.yaml 中定义,如变量的值如下:
[root@node-16 templates]# vi ../values.yaml
[root@node-16 mychart]# cat values.yaml
name: mychars
replicas: 3
image: nginx
imageTag: latest
[root@node-16 mychart]#
接下来可以安装charts
[root@node-16 ~]# helm install mycharts mychart/
NAME: mycharts
LAST DEPLOYED: Tue Apr 28 15:03:35 2020
NAMESPACE: default
STATUS: deployed
REVISION: 1
TEST SUITE: None
[root@node-16 ~]#
[root@node-16 ~]# kubectl get deployment|grep mychars
mychars 3/3 3 3 95s
[root@node-16 ~]
[root@node-16 ~]# kubectl get pod|grep mychars
mychars-bc975b9d4-gdbr9 1/1 Running 0 78s
mychars-bc975b9d4-v2dcg 1/1 Running 0 78s
mychars-bc975b9d4-x6xz7 1/1 Running 0 78s
[root@node-16 ~]#
[root@node-16 ~]# helm list
NAME NAMESPACE REVISION UPDATED STATUS CHART APP VERSION
mycharts default 1 2020-04-28 15:03:35.111744041 +0800 CST deployed mychart-0.1.0 1.16.0
[root@node-16 ~]#
查看渲染后的代码结果
[root@node-16 ~]#
[root@node-16 ~]# helm get manifest mychars
Error: release: not found
[root@node-16 ~]# helm get manifest mycharts
---
# Source: mychart/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mychars
spec:
replicas: 3
selector:
matchLabels:
app: mychart
template:
metadata:
labels:
app: mychart
spec:
containers:
- image: nginx:latest
name: nginx
[root@node-16 ~]#
升级、回滚和删除
发布新版本的chart时,或者当您要更改发布的配置时,可以使用该helm upgrade 命令。
# helm upgrade --set imageTag=1.17 web mychart
# helm upgrade -f values.yaml web mychart
如果在发布后没有达到预期的效果,则可以使用helm rollback回滚到之前的版本。
例如将应用回滚到第一个版本:
# helm rollback web 2
卸载发行版,请使用以下helm uninstall命令:
# helm uninstall web
查看历史版本配置信息
# helm get --revision 1 web
打包
[root@node-16 ~]# helm package mychart
Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz
[root@node-16 ~]#
[root@node-16 ~]#