Helm⾃定义模板以及私有库部署Helm⾃定义模板以及私有库部署
开发⾃⼰的chart
[root@master ~]# helm create mychart
Creating mychart
[root@master ~]# tree -C mychart/
mychart/
├── charts
├── Chart.yaml
├── templates
│├── deployment.yaml
│├── _helpers.tpl
│├── ingress.yaml
│├──
│├── 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.
3、通过chart本地⽬录安装
[root@master ~]# tar zxf redis-1.
[root@master ~]# tree -C redis
redis
├── Chart.yaml
├── README.md
├── templates
│├── deployment.yaml
│├── _helpers.tpl
│├── networkpolicy.yaml
│├──
│├── pvc.yaml
│├── secrets.yaml
│└── svc.yaml
└── values.yaml
[root@master ~]# helm install redis
4、通过URL安装
[root@master ~]# helm install xxx/ -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
#可以看到这⾥的端⼝与我们指定的并不⼀致
nodeselector
因为这些yaml⽂件都是引⽤template模板,template模板⽬录下的service.yaml⾥⾯没有nodePort字段,只需要在service.yaml⽂件⾥添加就⾏
#实例的更新
[root@master templates]# pwd
/root/mychart/templates
[root@master templates]# vim service.yam
spec:
type: {{ .pe }}
ports:
- port: {{ .Values.service.port }}
targetPort: http
protocol: TCP
name: http
nodePort: {{ .dePort}}
#再更新以下,进⾏验证
[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=mych art
[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.
[root@master ~]# ls
mychart-0.
3、执⾏helm repo index⽣成仓库的index⽂件
[root@master ~]# mkdir myrepo
[root@master ~]# mv mychart-0. myrepo/
[root@master ~]# ls myrepo/
mychart-0.
[root@master ~]# helm repo index myrepo/ --url 192.168.1.50:8080/charts #第⼀步运⾏的容器的IP+端⼝[root@master ~]# ls myrepo/
index.yaml mychart-0.
[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:
-
192.168.1.50:8080/charts/mychart-0.
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. node01:/var/www/charts
index.yaml 100% 400 0.4KB/s 00:00
mychart-0. 100% 2861 2.8KB/s 00:00
#在node01上进⾏验证
[root@node01 ~]# ls /var/www/charts
index.yaml mychart-0.
5、添加新的repo仓库
[root@master myrepo]# helm repo add myrepo 192.168.1.50:8080/charts
"myrepo" has been added to your repositories
[root@master myrepo]# helm repo list
NAME URL
stable kubernetes.oss-cn-hangzhou.aliyuncs/charts
local 127.0.0.1:8879/charts #这个是不能跨主机的只能在本地使⽤
myrepo 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源中
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论