基于Kubernetes的Jenkins持续部署

示例代码
提取码:c7pn

一、部署

1. 构建流程

2. 部署服务

step 1 上传代码

[root@k8s-registry ~]# cd /mnt/
[root@k8s-registry /mnt]# rz -E
rz waiting to receive.
[root@k8s-registry /mnt]# tar xf yiliao.tar.gz 
[root@k8s-registry /mnt]# ls
yiliao  yiliao.tar.gz
[root@k8s-registry /mnt]# cd yiliao/
[root@k8s-registry /mnt/yiliao]# ls -a
.           article_detail.html  css         product_detail.html
..          article.html         images      product.html
about.html  comment.html         index.html
album.html  contact.html         js

step 2 准备dockerfile

dockerfile制作请参照Docker镜像构建章节

[root@k8s-registry /mnt]# cd yiliao/
[root@k8s-registry /mnt/yiliao]# cat dockerfile 
[root@k8s-registry /mnt/yiliao]# docker build -t demo:v1 .
Sending build context to Docker daemon 2.239 MB
Step 1/2 : FROM 10.0.0.140:5000/nginx:1.15
 ---> be1f31be9a87
Step 2/2 : ADD . /usr/share/nginx/html
 ---> e14f88f4d4c5
Removing intermediate container a53c977ba934
Successfully built e14f88f4d4c5
FROM 10.0.0.140:5000/nginx:1.15
ADD . /usr/share/nginx/html

step 3 测试dockerfile

[root@k8s-registry /mnt/yiliao]# docker run -d -p 8000:80 demo:v1  
639ff48f296008686d3e5ca22278f239d45dc206df7e12174f5e65bfd93f4b43
[root@k8s-registry /mnt/yiliao]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
639ff48f2960        demo:v1             "nginx -g 'daemon ..."   About a minute ago   Up About a minute   0.0.0.0:8000->80/tcp     jolly_euler
254f479a25d5        registry            "/entrypoint.sh /e..."   12 months ago        Up 2 hours          0.0.0.0:5000->5000/tcp   registry

step 4 将代码同步到代码仓库

Git相关操作请参照版本控制章节

[root@k8s-registry /mnt/yiliao]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
.....
[root@k8s-registry /mnt/yiliao]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCmaKUBEGbgieALryYBcYsWg5gEnV7CYQ1rF7mlKnnbj6u6nONWnBCsWcCTb66B/kCE9jOrXdoSebC0fCUk/PReHJho1WtSonOsfZK6oOcyG5tEgZsh9U9y46Wfbn73OPwq7jU3W2xGyxXMJFeRrvEhNFAn1F3E+6Z17KSgQLh83ZXct1MWJY0rsl+H4/EpNvHWfRUddzho1Z+1AhaM7GvxY40KNETgOTSEC+I71lp80TjcqrgHhQMQyrAlDsh//r4s0Y7zqyH6GguCuOSTAYtRVnlgcXcWKTvGmvnuUxDu2XrfQccNcA/ARHuqNZslLqu1eGnzTr62Tj/ofqtqqKzR root@k8s-registry
[root@k8s-registry /mnt/yiliao]# git config --global user.name "Administrator"
[root@k8s-registry /mnt/yiliao]# git config --global user.email "admin@example.com"
[root@k8s-registry /mnt/yiliao]# git init 
Initialized empty Git repository in /mnt/yiliao/.git/
[root@k8s-registry /mnt/yiliao]# git add .
[root@k8s-registry /mnt/yiliao]# git commit -m "First Commit include DockerFile"
[master (root-commit) 121b049] First Commit include DockerFile
 83 files changed, 18416 insertions(+)
 create mode 100644 about.html
......
[root@k8s-registry /mnt/yiliao]# git remote add origin git@10.0.0.140:root/demo.git
[root@k8s-registry /mnt/yiliao]# git push -u origin master
The authenticity of host '10.0.0.140 (10.0.0.140)' can't be established.
ECDSA key fingerprint is SHA256:4O+/HRUt2Qwcz4xXk3y+Y5It07gqAUNy//ju/dZH2Vc.
ECDSA key fingerprint is MD5:5b:e2:99:8c:b6:d6:88:85:2c:4a:84:65:4a:74:78:75.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.140' (ECDSA) to the list of known hosts.
Counting objects: 91, done.
Compressing objects: 100% (91/91), done.
Writing objects: 100% (91/91), 1.48 MiB | 0 bytes/s, done.
Total 91 (delta 12), reused 0 (delta 0)
To git@10.0.0.140:root/demo.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

step 5 准备java环境

[root@k8s-master ~]# yum install -y java
......
  xorg-x11-font-utils.x86_64 1:7.5-21.el7                                                                
  xorg-x11-fonts-Type1.noarch 0:7.5-9.el7                                                                

Complete!
[root@k8s-master ~]# java -version
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)

step 6 准备jenkins环境

jenkins相关操作请参照Jenkins章节

[root@k8s-master ~]# cd /tmp/
[root@k8s-master /tmp]# rz -E
rz waiting to receive
[root@k8s-master /tmp]# mkdir /application 
[root@k8s-master /tmp]# tar xf apache-tomcat-8.5.69.tar.gz -C /application/
[root@k8s-master /tmp]# cd /application/
[root@k8s-master /application]# ls
apache-tomcat-8.5.69
[root@k8s-master /application]# ln -s apache-tomcat-8.5.69/ apache-tomcat
[root@k8s-master /application]# ll
total 0
lrwxrwxrwx 1 root root  21 Jul 22 14:07 apache-tomcat -> apache-tomcat-8.5.69/
drwxr-xr-x 9 root root 220 Jul 22 14:06 apache-tomcat-8.5.69
[root@k8s-master /application]# rm -rf apache-tomcat/webapps/*
[root@k8s-master /application]# mv /tmp/jenkins.war apache-tomcat/webapps/ROOT.war
[root@k8s-master /application]# ls apache-tomcat/webapps/
ROOT.war
[root@k8s-master /application]# sed -n 294,296p /application/apache-tomcat/bin/catalina.sh
if [ -z "$UMASK" ]; then
    UMASK="0022"
fi
[root@k8s-master /application]# /application/apache-tomcat/bin/startup.sh 
Using CATALINA_BASE:   /application/apache-tomcat
Using CATALINA_HOME:   /application/apache-tomcat
Using CATALINA_TMPDIR: /application/apache-tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /application/apache-tomcat/bin/bootstrap.jar:/application/apache-tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:   
Tomcat started.
[root@k8s-master /application]# cat /root/.jenkins/secrets/initialAdminPassword
c0c3c4476ccf44d49803901f34da2b66
[root@k8s-master /application]# ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_dsa.
Your public key has been saved in /root/.ssh/id_dsa.pub.
......

step 7 构建并测试Job

Jenkins构建Docker镜像需使用docker命令,因此需要安装Docker服务。
[root@k8s-master /application]# docker images 
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
demo_healthy                   v1                  69d21747afda        27 minutes ago      113 MB
10.0.0.140:5000/demo_healthy   v2                  69d21747afda        27 minutes ago      113 MB
10.0.0.140:5000/nginx          1.15                be1f31be9a87        2 years ago         109 MB

step 8 对接K8s,发布代码

[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 create namespace healthy
namespace "healthy" created
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 run demo-healthy -n healthy --image=10.0.0.140:5000/demo_healthy:v2 --replicas=3 --record
deployment "demo-healthy" created
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 rollout history deployment -n healthy demo-healthy
deployments "demo-healthy"
REVISION    CHANGE-CAUSE
1       kubectl -s http://10.0.0.110:10000 run demo-healthy -n healthy --image=10.0.0.140:5000/demo_healthy:v2 --replicas=3 --record
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 expose -n healthy deployment demo-healthy  --type=NodePort --port=80 --target-port=80 
service "demo-healthy" exposed
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 get svc -n healthy
NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
demo-healthy   10.100.240.83   <nodes>       80:35376/TCP   6m
[root@k8s-master /application]# curl -sI 10.0.0.120:35376
HTTP/1.1 200 OK
Server: nginx/1.15.5
Date: Fri, 23 Jul 2021 02:53:08 GMT
Content-Type: text/html
Content-Length: 29077
Last-Modified: Fri, 23 Jul 2021 01:57:08 GMT
Connection: keep-alive
ETag: "60fa21f4-7195"
Accept-Ranges: bytes
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 delete namespace healthy
namespace "healthy" deleted
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000 -n healthy get all
No resources found.
[root@k8s-master /application]# kubectl -s http://10.0.0.110:10000  get namespace 
NAME          STATUS        AGE
default       Active        1y
healthy       Terminating   18h
kube-system   Active        1y
tomcat        Active        11d

step 9 优化与完善

#!/bin/bash
if [ -f /tmp/demo_healthy.lock ]; then
    docker build -t 10.0.0.140:5000/demo_healthy:$version .
    docker push 10.0.0.140:5000/demo_healthy:$version
    kubectl -s http://10.0.0.110:10000 -n healthy set image deployment demo-healthy demo-healthy=10.0.0.1
40:5000/demo_healthy:$version
    Port=`kubectl -s http://10.0.0.110:10000 -n healthy get svc demo-healthy | awk -F "[:/]" 'NR==2{print
 $2}'`
    echo "The project has updated successfully. You could access the project through below address:"
    echo "http://10.0.0.120:$Port"
    exit
else
    docker build -t 10.0.0.140:5000/demo_healthy:$version .
    docker push 10.0.0.140:5000/demo_healthy:$version
    kubectl -s http://10.0.0.110:10000 create namespace healthy
    kubectl -s http://10.0.0.110:10000 -n healthy run demo-healthy --image=10.0.0.140:5000/demo_healthy:$
version --replicas=3 --record
    kubectl -s http://10.0.0.110:10000 expose -n healthy deployment demo-healthy --type=NodePort --port=8
0 --target-port=80
    Port=`kubectl -s http://10.0.0.110:10000 -n healthy get svc demo-healthy | awk -F "[:/]" 'NR==2{print
 $2}'`
    echo "The project has deployed successfully. You could access the project through below address:"
    echo "http://10.0.0.120:$Port"
    touch /tmp/demo_healthy.lock && chattr +i /tmp/demo_healthy.lock
fi