开发自己的chart
[root@master ~]# helm create mychart
Creating mychart
[root@master ~]# tree -C mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── ingress.yaml
│ ├── NOTES.txt
│ ├── service.yaml
│ └── tests
│ └── test-connection.yaml
└── values.yaml
[root@master mychart]# pwd
/root/mychart
[root@master mychart]# vim values.yaml
replicaCount: 1
image:
repository: nginx
tag: stable
pullPolicy: IfNotPresent
---------- #这些都都是默认信息
因为这是我们自己开发的,所以有可能会出现错误,一般我们在部署之前都会进行调试
调试
[root@master ~]# helm install --dry-run --debug mychart
修改values.yaml文件,模拟一些错误来查看效果
[root@master ~]# helm install --dry-run --debug mychart
): error converting YAML to JSON: yaml: line 12: could not find expected ':'
#会告诉你第12行少了个:但是这个12行不是特别准确,只是一个大概的方向
安装chart
四种方法
- 通过仓库安装
- 将chart下载下来,通过tar包安装
- 通过chart本地目录安装(将tar包解压得到的目录)
- 通过URL安装
1、通过仓库安装(以redis为例)
[root@master ~]# helm install stable/redis -n redis
2、将chart下载下来,通过tar包安装
[root@master ~]# helm fetch stable/redis
[root@master ~]# helm install redis-1.1.15.tgz
3、通过chart本地目录安装
[root@master ~]# tar zxf redis-1.1.15.tgz
[root@master ~]# tree -C redis
redis
├── Chart.yaml
├── README.md
├── templates
│ ├── deployment.yaml
│ ├── _helpers.tpl
│ ├── networkpolicy.yaml
│ ├── NOTES.txt
│ ├── pvc.yaml
│ ├── secrets.yaml
│ └── svc.yaml
└── values.yaml
[root@master ~]# helm install redis
4、通过URL安装
[root@master ~]# helm install http://xxx/charts/xxx.tgz -n name
使用本地目录安装刚刚自定义的mychart:
要求:
副本Pod数量为3个
service类型为NodePort
映射的端口为31033
[root@master ~]# cd mychart/
[root@master mychart]# vim values.yaml
replicaCount: 3
service:
type: NodePort
port: 80
nodePort: 31033
[root@master mychart]# helm install -n test ../mychart/
[root@master mychart]# kubectl get pod
NAME READY STATUS RESTARTS AGE
test-mychart-657bfc65b8-5j8qn 1/1 Running 0 30s
test-mychart-657bfc65b8-bgt2s 1/1 Running 0 30s
test-mychart-657bfc65b8-v2sph 1/1 Running 0 30s
[root@master mychart]# kubectl get deployments.
NAME READY UP-TO-DATE AVAILABLE AGE
test-mychart 3/3 3 3 46s
[root@master mychart]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 61d
test-mychart NodePort 10.104.53.170 <none> 80:30393/TCP 50s
#可以看到这里的端口与我们指定的并不一致
因为这些yaml文件都是引用template模板,template模板目录下的service.yaml里面没有nodePort字段,只需要在service.yaml文件里添加就行
#实例的更新
[root@master templates]# pwd
/root/mychart/templates
[root@master templates]# vim service.yam
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
nodePort: {{ .Values.service.nodePort}}
#再更新以下,进行验证
[root@master ~]# helm upgrade test mychart/ -f mychart/values.yaml
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 61d
test-mychart NodePort 10.104.53.170 <none> 80:31033/TCP 10m
#端口已经改变了
一般在企业都是使用自己的私有镜像,那么就需要搭建私有仓库
[root@master ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.70:5000
[root@master ~]# systemctl daemon-reload
[root@master ~]# systemctl restart docke
#node01、node02也加入私有仓库
[root@node01 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.70:5000
[root@node01 ~]# systemctl daemon-reload
[root@node01 ~]# systemctl restart docke
[root@node02 ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.70:5000
[root@node02 ~]# systemctl daemon-reload
[root@node02 ~]# systemctl restart docke
导入私有镜像,并上传到私有仓库
[root@master ~]# docker load < httpd-v1.tar && docker load < httpd-v2.tar && docker load < httpd-v3.tar
[root@master ~]# docker tag httpd:v1 192.168.1.70:5000/httpd:v1
[root@master ~]# docker push 192.168.1.70:5000/httpd:v1
[root@master ~]# docker push 192.168.1.70:5000/httpd:v1
[root@master ~]# docker tag httpd:v2 192.168.1.70:5000/httpd:v2
[root@master ~]# docker tag httpd:v3 192.168.1.70:5000/httpd:v3
[root@master ~]# docker push 192.168.1.70:5000/httpd:v2
[root@master ~]# docker push 192.168.1.70:5000/httpd:v3
node01、node02拉去镜像
[root@node01 ~]# docker pull 192.168.1.70:5000/httpd:v1
[root@node01 ~]# docker pull 192.168.1.70:5000/httpd:v2
[root@node01 ~]# docker pull 192.168.1.70:5000/httpd:v3
[root@node02 ~]# docker pull 192.168.1.70:5000/httpd:v1
[root@node02 ~]# docker pull 192.168.1.70:5000/httpd:v2
[root@node02 ~]# docker pull 192.168.1.70:5000/httpd:v3
练习:
使用mychart部署一个实例,名为bdqn,使用私有镜像v1版本
完成之后,将实例做一个升级,将镜像改为v2版本
#修改yaml文件,运行实例
[root@master ~]# vim mychart/values.yaml
image:
repository: 192.168.1.70:5000/httpd
tag: v1
pullPolicy: IfNotPresent
[root@master ~]# helm install -n bdqn mychart/
[root@master ~]# kubectl get deployments. -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
bdqn-mychart 1/1 1 1 8s mychart 192.168.1.70:5000/httpd:v1 app.kubernetes.io/instance=bdqn,app.kubernetes.io/name=mychart
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
bdqn-mychart-574ffc5496-bd8vf 1/1 Running 0 22s
#实例升级,通过yaml文件的方式
[root@master ~]# vim mychart/values.yaml
image:
repository: 192.168.1.70:5000/httpd
tag: v2
[root@master ~]# helm upgrade bdqn mychart/ -f mychart/values.yaml
[root@master ~]# kubectl get deployments. -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
bdqn-mychart 1/1 1 1 2m54s mychart 192.168.1.70:5000/httpd:v2 app.kubernetes.io/instance=bdqn,app.kubernetes.io/name=mychart
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
bdqn-mychart-85dcbbcb8f-8h47q 1/1 Running 0 5m28s
#还可以通过命令的方式
[root@master ~]# helm upgrade bdqn mychart/ --set imageTAG=v2
//这种方法更新完成后,查看deployment的时候,镜像显示是没有更新的
#还可以通过edit的方式进行更改
[root@master ~]# kubectl edit deployments. bdqn-mychart
spec:
containers:
- image: 192.168.1.70:5000/httpd:v3
[root@master ~]# kubectl get deployments. -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
bdqn-mychart 1/1 1 1 28m mychart 192.168.1.70:5000/httpd:v3 app.kubernetes.io/instance=bdqn,app.kubernetes.io/name=mychar
创建自己的Repo仓库
我们自己创建的chart包,如果公司内其他的同事也需要用,我们可以cp一份给他,但是效率太低,所以就可以创建一个repo仓库,解决这个需求
1、在node01上运行一个httpd的容器(作为私有仓库)
[root@node01 ~]# mkdir /var/www
[root@node01 ~]# docker run -d -p 8080:80 -v /var/www/:/usr/local/apache2/htdocs httpd:latest
[root@node01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03e5e3ef5c95 httpd:latest "httpd-foreground" 13 seconds ago Up 12 seconds 0.0.0.0:8080->80/tcp kind_roentgen
2、master节点上,使用helm package将mychart目录打包
[root@master ~]# helm package mychart/
Successfully packaged chart and saved it to: /root/mychart-0.1.0.tgz
[root@master ~]# ls
mychart-0.1.0.tgz
3、执行helm repo index生成仓库的index文件
[root@master ~]# mkdir myrepo
[root@master ~]# mv mychart-0.1.0.tgz myrepo/
[root@master ~]# ls myrepo/
mychart-0.1.0.tgz
[root@master ~]# helm repo index myrepo/ --url http://192.168.1.50:8080/charts #第一步运行的容器的IP+端口
[root@master ~]# ls myrepo/
index.yaml mychart-0.1.0.tgz
[root@master ~]# cat myrepo/index.yaml
apiVersion: v1
entries:
mychart:
- apiVersion: v1
appVersion: "1.0"
created: "2020-03-04T11:08:33.079034645+08:00"
description: A Helm chart for Kubernetes
digest: f2a297c4b377ae7f208848bef8823eeb74ebb7270d8bf07f58270678d0784056
name: mychart
urls:
- http://192.168.1.50:8080/charts/mychart-0.1.0.tgz
version: 0.1.0
generated: "2020-03-04T11:08:33.07808906+08:00"
4、将生成的tar包和index.yaml上传到node01的/var/www/charts目录下
#因为node01上没有sharts目录,所以需要创建
[root@node01 ~]# mkdir /var/www/chart
[root@master ~]# cd myrepo/
[root@master myrepo]# scp index.yaml mychart-0.1.0.tgz node01:/var/www/charts
index.yaml 100% 400 0.4KB/s 00:00
mychart-0.1.0.tgz 100% 2861 2.8KB/s 00:00
#在node01上进行验证
[root@node01 ~]# ls /var/www/charts
index.yaml mychart-0.1.0.tgz
5、添加新的repo仓库
[root@master myrepo]# helm repo add myrepo http://192.168.1.50:8080/charts
"myrepo" has been added to your repositories
[root@master myrepo]# helm repo list
NAME URL
stable https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
local http://127.0.0.1:8879/charts #这个是不能跨主机的只能在本地使用
myrepo http://192.168.1.50:8080/charts #这个可以跨主机
至此,已经可以正常供内网环境使用这个charts包的私有仓库了
6、验证,我们就可以直接使用新的repo仓库部署实例了
[root@master myrepo]# helm search mychart
NAME CHART VERSION APP VERSION DESCRIPTION
local/mychart 0.1.0 1.0 A Helm chart for Kubernetes
myrepo/mychart 0.1.0 1.0 A Helm chart for Kubernetes
[root@master myrepo]# helm install myrepo/mychart -n test
[root@master ~]# helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
test 1 Wed Mar 4 11:23:25 2020 DEPLOYED mychart-0.1.0 1.0
7、如果以后仓库中新添加了chart包,需要用helm repo update命令更新本地的index文件
练习:
新创建一个bdqn的chart包,然后将chart包上传到上述repo源中
#创建新的chart
[root@master ~]# helm create bdqn
Creating bdqn
#将这个chart目录打包
[root@master ~]# helm package bdqn/
Successfully packaged chart and saved it to: /root/bdqn-0.1.0.tgz
#移动到myrepo下
[root@master ~]# mv bdqn-0.1.0.tgz myrepo/
[root@master ~]# ls myrepo/
bdqn-0.1.0.tgz index.yaml mychart-0.1.0.tgz
#更新index文件
[root@master ~]# helm repo index myrepo/ --url http://192.168.1.50:8080/charts
[root@master myrepo]# scp bdqn-0.1.0.tgz index.yaml node01:/var/www/charts
bdqn-0.1.0.tgz 100% 2826 2.8KB/s 00:00
index.yaml 100% 720 0.7KB/s 00:00
#更新repo仓库
[root@master myrepo]# helm repo update
Hang tight while we grab the latest from your chart repositories...
...Skip local chart repository
...Successfully got an update from the "myrepo" chart repository
...Successfully got an update from the "stable" chart repository
Update Complete.
#搜索验证
[root@master myrepo]# helm search bdqn
NAME CHART VERSION APP VERSION DESCRIPTION
local/bdqn 0.1.0 1.0 A Helm chart for Kubernetes
myrepo/bdqn 0.1.0 1.0 A Helm chart for Kubernetes
使用url部署这个bdqn实例
[root@master ~]# helm install http://192.168.1.50:8080/charts/bdqn-0.1.0.tgz -n t1
[root@master ~]# helm list
NAME REVISION UPDATED STATUS CHART APP VERSION NAMESPACE
t1 1 Wed Mar 4 11:52:50 2020 DEPLOYED bdqn-0.1.0 1.0 default
test 1 Wed Mar 4 11:23:25 2020 DEPLOYED mychart-0.1.0 1.0