kubernetes Helm之构建一个 Helm Chart
一、创建helm chart
可以使用以下命令来创建一个名为mychart 的 helm chart:
[root@server1 helm]# helm create mychart
Creating mychart
- 1
- 2
创建后会在目录创建一个mychart目录:
[root@server1 helm]# ls
mychart redis-ha
[root@server1 helm]# tree mychart/ #查看结构
mychart/
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── hpa.yaml
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── serviceaccount.yaml
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
其中mychart目录下的templates
目录中保存有部署的模板文件,values.yaml
中定义了部署的变量,Chart.yaml
文件包含有version(chart版本)和appVersion(包含应用的版本)。
现在我们来更改变量文件values.yaml
:
[root@server1 mychart]# vim values.yaml
- 1
选择镜像及标签和副本数(这里设置1个)。
编辑完成后检查依赖和模板配置是否正确:
[root@server1 mychart]# helm lint .
==> Linting .
[INFO] Chart.yaml: icon is recommended
1 chart(s) linted, 0 chart(s) failed
- 1
- 2
- 3
- 4
- 5
将应用打包:
[root@server1 mychart]# cd ..
[root@server1 helm]# helm package mychart/
Successfully packaged chart and saved it to: /root/helm/mychart-0.1.0.tgz
- 1
- 2
- 3
打包后会在当前目录下生成一个名为mychart-0.1.0.tgz
压缩包:
[root@server1 helm]# ls
mychart mychart-0.1.0.tgz redis-ha
- 1
- 2
其中0.1.0
为在Chart.yaml
文件中定义的version(chart版本)信息。
二、构建本地chart仓库
helm v3 需要外部仓库软件的支持,我们这里使用以前部署的harbor仓库(新版本的harbor仓库支持helm chart库)。
harbor仓库的部署可以参考:https://blog.csdn.net/qq_35887546/article/details/105411743
部署好之后需要在harbor创建一个公有项目来存放chart:
可以看出现在仓库还没有chart。
将仓库添加到helm:
[root@server1 helm]# helm repo add mychart https://reg.westos.org/chartrepo/charts
Error: looks like "https://reg.westos.org/chartrepo/charts" is not a valid chart repository or cannot be reached: Get https://reg.westos.org/chartrepo/charts/index.yaml: x509: certificate signed by unknown authority
- 1
- 2
可以看出报错是缺少证书,可以将证书复制到redhat的全局证书地址:
[root@server1 helm]# cd /etc/docker/certs.d/reg.westos.org/
[root@server1 reg.westos.org]# ls
ca.crt
[root@server1 reg.westos.org]# cp ca.crt /etc/pki/ca-trust/source/anchors/
[root@server1 reg.westos.org]# update-ca-trust #更新证书
- 1
- 2
- 3
- 4
- 5
再次添加:
[root@server1 reg.westos.org]# helm repo add mychart https://reg.westos.org/chartrepo/charts
"mychart" has been added to your repositories
- 1
- 2
添加成功,查看仓库: