1、想要实现jenkins一键自动发布代码的话,还需要手动在k8s部署一下集群项目。首先,创建一个deloypment,如下所示:
1 [root@k8s-master ~]# kubectl run feiji --image=192.168.110.133:5000/feiji:v1 --replicas=2 --re 2 --record --replicas= --requests= --request-timeout= --restart= 3 [root@k8s-master ~]# kubectl run feiji --image=192.168.110.133:5000/feiji:v1 --replicas=2 --record 4 deployment "feiji" created 5 [root@k8s-master ~]#
使用record参数进行记录,可以通过下面的命令进行查看,可以查看镜像版本,如下所示:
1 [root@k8s-master ~]# kubectl rollout history deployment feiji 2 deployments "feiji" 3 REVISION CHANGE-CAUSE 4 1 kubectl run feiji --image=192.168.110.133:5000/feiji:v1 --replicas=2 --record 5 6 [root@k8s-master ~]#
可以通过命令进行查看,这个deploy已经启动了两个副本Pod,如下所示:
1 [root@k8s-master ~]# kubectl get all 2 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE 3 deploy/feiji 2 2 2 2 2m 4 5 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE 6 svc/kubernetes 10.254.0.1 <none> 443/TCP 11h 7 8 NAME DESIRED CURRENT READY AGE 9 rs/feiji-2742263606 2 2 2 2m 10 11 NAME READY STATUS RESTARTS AGE 12 po/feiji-2742263606-0w2cz 1/1 Running 0 2m 13 po/feiji-2742263606-ltdvr 1/1 Running 0 2m 14 [root@k8s-master ~]# kubectl get all -o wide 15 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE 16 deploy/feiji 2 2 2 2 2m 17 18 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 19 svc/kubernetes 10.254.0.1 <none> 443/TCP 11h <none> 20 21 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR 22 rs/feiji-2742263606 2 2 2 2m feiji 192.168.110.133:5000/feiji:v1 pod-template-hash=2742263606,run=feiji 23 24 NAME READY STATUS RESTARTS AGE IP NODE 25 po/feiji-2742263606-0w2cz 1/1 Running 0 2m 172.16.78.4 k8s-node3 26 po/feiji-2742263606-ltdvr 1/1 Running 0 2m 172.16.31.6 k8s-node2 27 [root@k8s-master ~]#
此时项目还不能被外界所访问,还需要暴漏一个端口号,如下所示:
1 [root@k8s-master ~]# kubectl expose deployment feiji --port=80 --type=NodePort 2 service "feiji" exposed 3 [root@k8s-master ~]# kubectl get all 4 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE 5 deploy/feiji 2 2 2 2 4m 6 7 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE 8 svc/feiji 10.254.23.13 <nodes> 80:32283/TCP 7s 9 svc/kubernetes 10.254.0.1 <none> 443/TCP 11h 10 11 NAME DESIRED CURRENT READY AGE 12 rs/feiji-2742263606 2 2 2 4m 13 14 NAME READY STATUS RESTARTS AGE 15 po/feiji-2742263606-0w2cz 1/1 Running 0 4m 16 po/feiji-2742263606-ltdvr 1/1 Running 0 4m 17 [root@k8s-master ~]# kubectl get all -o wide 18 NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE 19 deploy/feiji 2 2 2 2 4m 20 21 NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR 22 svc/feiji 10.254.23.13 <nodes> 80:32283/TCP 9s run=feiji 23 svc/kubernetes 10.254.0.1 <none> 443/TCP 11h <none> 24 25 NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR 26 rs/feiji-2742263606 2 2 2 4m feiji 192.168.110.133:5000/feiji:v1 pod-template-hash=2742263606,run=feiji 27 28 NAME READY STATUS RESTARTS AGE IP NODE 29 po/feiji-2742263606-0w2cz 1/1 Running 0 4m 172.16.78.4 k8s-node3 30 po/feiji-2742263606-ltdvr 1/1 Running 0 4m 172.16.31.6 k8s-node2 31 [root@k8s-master ~]#
此时访问地址,http://192.168.110.133:32283/。此项目是可以进行访问的。
1 [root@k8s-master ~]# curl 192.168.110.133:32283 | grep 'feiji' 2 % Total % Received % Xferd Average Speed Time Time Time Current 3 Dload Upload Total Spent Left Speed 4 100 1081 100 1081 0 0 216k 0 --:--:-- --:--:-- --:--:-- 263k 5 [root@k8s-master ~]#
此时使用Jenkins实现k8s里面的项目版本更新,k8s中版本升级和回滚,首先实现手动实现项目版本升级和回滚。这里实现版本的升级并查看历史版本,如下所示:
手动做版本升级的时候,Vx这个版本和之前的不能重复的,不然回造成升级版本失败的,需要注意。
1 [root@k8s-master ~]# kubectl set image deploy feiji feiji=192.168.110.133:5000/feiji:v3 2 deployment "feiji" image updated 3 [root@k8s-master ~]# kubectl rollout history deployment feiji 4 deployments "feiji" 5 REVISION CHANGE-CAUSE 6 1 kubectl run feiji --image=192.168.110.133:5000/feiji:v1 --replicas=2 --record 7 2 kubectl set image deploy feiji feiji=192.168.110.133:5000/feiji:v3 8 9 [root@k8s-master ~]#
这里做版本的回滚到上一个版本,并查看历史版本,如下所示:
1 [root@k8s-master ~]# kubectl rollout undo deployment feiji 2 deployment "feiji" rolled back 3 [root@k8s-master ~]# kubectl rollout history deployment feiji 4 deployments "feiji" 5 REVISION CHANGE-CAUSE 6 2 kubectl set image deploy feiji feiji=192.168.110.133:5000/feiji:v3 7 3 kubectl run feiji --image=192.168.110.133:5000/feiji:v1 --replicas=2 --record 8 9 [root@k8s-master ~]#
可以回滚到指定的版本,如下所示:
1 [root@k8s-master ~]# kubectl rollout undo deployment feiji --to-revision=1
2、上面是手动做的版本更新,下面做自动版本的更新,首先需要在Jenkins进行配置。之前是直接将镜像推送到私有仓库就结束了,现在要使用kubectl进行更新。
由于k8s的Node节点不能执行kubectl set image deploy feiji feiji=192.168.110.133:5000/feiji:v$BUILD_ID,是执行不成功的,如果需要远程执行k8s的命令,先测试一下,-s执行api-server的命令,如下所示:
1 [root@k8s-node3 feiji]# kubectl -s 192.168.110.133:8080 get nodes 2 NAME STATUS AGE 3 k8s-master Ready 27d 4 k8s-node2 Ready 27d 5 k8s-node3 Ready 27d 6 [root@k8s-node3 feiji]#
此时为了方便观察,修改界面代码,方便观察效果,模拟提交代码,如下所示:
1 [root@k8s-node3 feiji]# vim index.html 2 [root@k8s-node3 feiji]# git add . 3 [root@k8s-node3 feiji]# git commit -m '2rd update feiji!' 4 [master f2722e1] 2rd update feiji! 5 1 file changed, 3 insertions(+), 3 deletions(-) 6 [root@k8s-node3 feiji]# git push -u origin master 7 Username for 'http://192.168.110.135': root 8 Password for 'http://root@192.168.110.135': 9 Counting objects: 5, done. 10 Delta compression using up to 2 threads. 11 Compressing objects: 100% (3/3), done. 12 Writing objects: 100% (3/3), 341 bytes | 0 bytes/s, done. 13 Total 3 (delta 2), reused 0 (delta 0) 14 To http://192.168.110.135/root/feiji.git 15 caba46a..f2722e1 master -> master 16 Branch master set up to track remote branch master from origin. 17 [root@k8s-node3 feiji]#
此时点击立即构建,然后观察是否已经正常升级了,如下所示:
然后查看控制台输出,发现已经更新了,如下所示:
此时访问地址,http://192.168.110.133:32283/。此项目是可以进行访问的。如果想要回滚,可以在Jenkins上面创建一个任务,然后在shell里面写好回滚的命令就行了额。