kubernetes云平台管理实战:jenkins自动化构建docker镜像并部署到k8s集群(二十四)

一、构建docker镜像并测试

1、在node2上编辑dockerfile

[root@node02 opt]# cd xiaoniao/
[root@node02 xiaoniao]# ls
2000.png 21.js icon.png img index.html sound1.mp3
[root@node02 xiaoniao]# vim dockerfile
[root@node02 xiaoniao]# vi .dockerignore
[root@node02 xiaoniao]# cat dockerfile 
FROM 192.168.118.188:5000/nginx:1.13
ADD . /usr/share/nginx/html
[root@node02 xiaoniao]# cat .dockerignore
dockerfile

2、jenkins web页面配置

配置截图

控制台输出代码

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao
using credential gitlab
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a770b526b3931f687970283349f710469ad6a222 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a770b526b3931f687970283349f710469ad6a222 # timeout=10
Commit message: "install commit"
 > git rev-list --no-walk a770b526b3931f687970283349f710469ad6a222 # timeout=10
[xiaoniao] $ /bin/sh -xe /app/apache-tomcat-8.0.27/temp/jenkins4656512328641218353.sh
+ docker build -t 192.168.118.188:5000/xiaoniao:v1 .
Sending build context to Docker daemon 332.3 kB

Step 1/2 : FROM 192.168.118.18:5000/nginx:1.13
 ---> ae513a47849c
Step 2/2 : ADD . /usr/share/nginx/html
 ---> Using cache
 ---> 292d37a1ec49
Successfully built 292d37a1ec49
+ docker push 192.168.118.188:5000/xiaoniao:v1
The push refers to a repository [192.168.118.188:5000/xiaoniao]
beaaf3962106: Preparing
7ab428981537: Preparing
82b81d779f83: Preparing
d626a8ad97a1: Preparing
beaaf3962106: Pushed
7ab428981537: Pushed
82b81d779f83: Pushed
d626a8ad97a1: Pushed
v1: digest: sha256:c25a7cf4e2180abfc4f814fc44c6ec55b35e9cee6d8e398c7885c42f176ea0ee size: 1158
Finished: SUCCESS

浏览器访问截图

二、自动化构建docker镜像问题

1、自动化构建docker镜像版本问题

使用jenkins环境变量

BUILD_ID 

查看可用变量

http://192.168.118.19:8080/env-vars.html/

BUILD_ID
The current build ID, identical to BUILD_NUMBER for builds created in 1.597+, but a YYYY-MM-DD_hh-mm-ss timestamp for older builds

配置截图

控制台输出

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao
using credential gitlab
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a770b526b3931f687970283349f710469ad6a222 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a770b526b3931f687970283349f710469ad6a222 # timeout=10
Commit message: "install commit"
 > git rev-list --no-walk a770b526b3931f687970283349f710469ad6a222 # timeout=10
[xiaoniao] $ /bin/sh -xe /app/apache-tomcat-8.0.27/temp/jenkins588157755746945636.sh
+ docker build -t 192.168.118.188:5000/xiaoniao:v9 .
Sending build context to Docker daemon 332.3 kB

Step 1/2 : FROM 192.168.118.18:5000/nginx:1.13
 ---> ae513a47849c
Step 2/2 : ADD . /usr/share/nginx/html
 ---> Using cache
 ---> 292d37a1ec49
Successfully built 292d37a1ec49
+ docker push 192.168.118.188:5000/xiaoniao:v9
The push refers to a repository [192.168.118.188:5000/xiaoniao]
beaaf3962106: Preparing
7ab428981537: Preparing
82b81d779f83: Preparing
d626a8ad97a1: Preparing
beaaf3962106: Layer already exists
d626a8ad97a1: Layer already exists
82b81d779f83: Layer already exists
7ab428981537: Layer already exists
v9: digest: sha256:c25a7cf4e2180abfc4f814fc44c6ec55b35e9cee6d8e398c7885c42f176ea0ee size: 1158
Finished: SUCCESS

重点看版本:+ docker push 192.168.118.188:5000/xiaoniao:v9

构建版本查询

[root@master _manifests]# ls /opt/myregistry/docker/registry/v2/repositories/xiaoniao/_manifests/tags/
v1  v10  v9

2、防止误操作立即构建

配置截图

#!/bin/bash
if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ];then
     echo "no change,skip build"
     exit 0
else
     docker build  -t 192.168.118.18:5000/xiaoniao:v$BUILD_ID  .
	 docker push 192.168.118.18:5000/xiaoniao:v$BUILD_ID 
fi

测试代码

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao
using credential gitlab
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a770b526b3931f687970283349f710469ad6a222 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a770b526b3931f687970283349f710469ad6a222 # timeout=10
Commit message: "install commit"
 > git rev-list --no-walk a770b526b3931f687970283349f710469ad6a222 # timeout=10
[xiaoniao] $ /bin/bash /app/apache-tomcat-8.0.27/temp/jenkins4961049993779057712.sh
no change,skip build
Finished: SUCCESS

三、k8s集群部署项目

1、部署项目

[root@master _manifests]# kubectl run xiaoniao --image=192.168.118.188:5000/xiaoniao:v1 --replicas=2 --record
deployment "xiaoniao" created
[root@master _manifests]# kubectl rollout history deployment xiaoniao
deployments "xiaoniao"
REVISION	CHANGE-CAUSE
1	kubectl run xiaoniao --image=192.168.118.188:5000/xiaoniao:v1 --replicas=2 --record

2、设置对外访问

[root@master _manifests]# kubectl expose deployment xiaoniao --port=80 --type=NodePort
service "xiaoniao" exposed
[root@master _manifests]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
glusterfs 10.254.122.179 <none> 49152/TCP 2d
kubernetes 10.254.0.1 <none> 443/TCP 9d
mysql 10.254.168.177 <none> 3306/TCP 5d
myweb 10.254.202.136 <nodes> 8080:30008/TCP 5d
readiness 10.254.199.105 <none> 80/TCP 3d
xiaoniao 10.254.22.194 <nodes> 80:32392/TCP 9s

3、测试访问:

[root@master _manifests]# curl 192.168.118.19:32392|grep '小鸟'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3049 100 3049 0 0 707k 0 --:--:-- --:--:-- --:--:-- 992k
<title>小鸟飞飞飞-文章库小游戏</title>
"tTitle": "小鸟飞飞飞-文章库小游戏",
document.title = "我玩小鸟飞飞飞过了"+t+"关!你能超过洒家我吗?";

4、测试截图

 

 

 

 四、k8s 版本升级和回滚

1、版本更新

[root@master _manifests]# ls /opt/myregistry/docker/registry/v2/repositories/xiaoniao/_manifests/tags/
v1 v10 v13 v9
[root@master _manifests]# kubectl set image deploy xiaoniao xiaoniao=192.168.118.188:5000/xiaoniao:v13
deployment "xiaoniao" image updated
[root@master _manifests]# curl 192.168.118.19:32392|grep '活的' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3036 100 3036 0 0 658k 0 --:--:-- --:--:-- --:--:-- 741k <title>活的潇洒的游戏</title> [root@master _manifests]# kubectl set image deploy xiaoniao xiaoniao=192.168.118.188:5000/xiaoniao:v10 deployment "xiaoniao" image updated
[root@master _manifests]# curl 192.168.118.19:32392|grep '小鸟' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3049 100 3049 0 0 706k 0 --:--:-- --:--:-- --:--:-- 992k <title>小鸟飞飞飞-文章库小游戏</title> "tTitle": "小鸟飞飞飞-文章库小游戏", document.title = "我玩小鸟飞飞飞过了"+t+"关!你能超过洒家我吗?";

2、回滚指定版本

[root@master _manifests]# kubectl rollout undo deployment xiaoniao --to-revision=1
deployment "xiaoniao" rolled back
[root@master _manifests]# curl 192.168.118.19:32392|grep '小鸟' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3049 100 3049 0 0 744k 0 --:--:-- --:--:-- --:--:-- 992k <title>小鸟飞飞飞-文章库小游戏</title> "tTitle": "小鸟飞飞飞-文章库小游戏", document.title = "我玩小鸟飞飞飞过了"+t+"关!你能超过洒家我吗?";
[root@master _manifests]# kubectl rollout undo deployment xiaoniao deployment "xiaoniao" rolled back
[root@master _manifests]# curl 192.168.118.19:32392|grep '小鸟' % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3049 100 3049 0 0 849k 0 --:--:-- --:--:-- --:--:-- 992k <title>小鸟飞飞飞-文章库小游戏</title> "tTitle": "小鸟飞飞飞-文章库小游戏", document.title = "我玩小鸟飞飞飞过了"+t+"关!你能超过洒家我吗?";

3、回滚到上一个版本

[root@master _manifests]# kubectl rollout history deployment xiaoniao
deployments "xiaoniao"
REVISION	CHANGE-CAUSE
1	kubectl run xiaoniao --image=192.168.118.188:5000/xiaoniao:v1 --replicas=2 --record
2	kubectl set image deploy xiaoniao xiaoniao=192.168.118.188:5000/xiaoniao:v13
3	kubectl set image deploy xiaoniao xiaoniao=192.168.118.188:5000/xiaoniao:v10

[root@master _manifests]# kubectl rollout

history View rollout history
pause Mark the provided resource as paused
resume Resume a paused resource
status Show the status of the rollout
undo Undo a previous rollout

[root@master _manifests]# kubectl rollout undo deployment xiaoniao --to-revision=1
deployment "xiaoniao" rolled back

五、jenkins实现一键k8s版本升级

1、一键升级

jenkins web页面配置

 

 控制台输出

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao
using credential gitlab
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url git@192.168.118.20:root/xiaoniao.git # timeout=10
Fetching upstream changes from git@192.168.118.20:root/xiaoniao.git
 > git --version # timeout=10
using GIT_SSH to set credentials 
 > git fetch --tags --progress git@192.168.118.20:root/xiaoniao.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision 4190fdf06bb26390c97b5d772e2c09ad1b5100d6 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f 4190fdf06bb26390c97b5d772e2c09ad1b5100d6 # timeout=10
Commit message: "5rd commit"
 > git rev-list --no-walk af1a63adecbaf53517fb590bb718a2cc032a630c # timeout=10
[xiaoniao] $ /bin/bash /app/apache-tomcat-8.0.27/temp/jenkins2728768869418033481.sh
git pull commmit id not equals to current commit id trigger build
Sending build context to Docker daemon 352.8 kB

Step 1/2 : FROM 192.168.118.188:5000/nginx:1.13
 ---> ae513a47849c
Step 2/2 : ADD . /usr/share/nginx/html
 ---> 67da545f2503
Removing intermediate container cf183aeb58bb
Successfully built 67da545f2503
The push refers to a repository [192.168.118.188:5000/xiaoniao]
8f72175a5946: Preparing
7ab428981537: Preparing
82b81d779f83: Preparing
d626a8ad97a1: Preparing
82b81d779f83: Layer already exists
d626a8ad97a1: Layer already exists
7ab428981537: Layer already exists
8f72175a5946: Pushed
v19: digest: sha256:8537562cb2a083953fb6f41c74facdfa515e405bf58c53096eb33b8409527011 size: 1158
deployment "xiaoniao" image updated
Finished: SUCCESS

浏览器测试截图

2、一键回滚

jenkins web页面配置

控制台输出

Started by user luoahong
Running as SYSTEM
Building in workspace /root/.jenkins/workspace/xiaoniao-rollout
[xiaoniao-rollout] $ /bin/sh -xe /app/apache-tomcat-8.0.27/temp/jenkins2984771295776282597.sh
+ kubectl -s 192.168.118.188:8080 rollout undo deployment xiaoniao
deployment "xiaoniao" rolled back
Finished: SUCCESS

测试截图

整体项目截图

posted @ 2020-05-26 22:30  活的潇洒80  阅读(1460)  评论(0编辑  收藏  举报