k8s+jenkins for Automated Deployment and application to k8s cluster

1. The jenkins environment prepares and starts the jenkins service

1) jdk environment configuration

[root@centos7 ~]# ll /app/

[root@centos7 ~]# cat/etc/profile | tail-n3 #jdk environment variable
export JAVA_HOME=/app/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

2) Prepare the war package for jenkins startup

[root@centos7 war]# ll

3) View the jenkins startup script

[root@centos7 war]# cat start.sh

4) Start jenkins

[root@centos7 war]# ./start.sh && tailf test.log

5) Modify hudson update url in data directory to domestic url to avoid jenkins being unable to start

[root@centos7 ~]# cat .jenkins/hudson.model.UpdateCenter.xml

6) Prepare hosts to resolve or add to DNS servers

[root@centos7 ~]# cat /etc/hosts

7) web-side access to the jenkins interface

8) Create a jenkins_project project project and edit the configuration

9) Prepare the automated deployment script k8s_auto_deploy.sh at the back end of jenkins, and certify the public and private keys in advance so that Jenkins can access k8s-master-1

[root@centos7 ~]# cat /data/script/k8s_auto_depoy.sh
Starttime=`date +"%Y-%m-%d_%H-%M-%S"`
t1=`date +"%Y-%m-%d %H:%M:%S"`

     #Code cloned to jenkins backend
    cd /root/.jenkins/workspace/jenkins_project && git clone -b $Branch ${URL}&& echo "Clone Finished"

 #Code packaging is compressed and pushed remotely to the nginx mirroring directory of k8s-master-1
    cd /root/.jenkins/workspace/jenkins_project/middle-compose-service/ && tar cvzf nginx.tar.gz * && echo Package Finished
    scp nginx.tar.gz root@k8s-master-1:/data/Dockerfile/nginx/ && ssh root@k8s-master-1 'cd /data/Dockerfile/nginx/ && tar xvf nginx.tar.gz &&rm -f nginx.tar.gz' 

 #Operate k8s-master-1 node remotely to mirror and push to harbor mirror warehouse
    ssh root@k8s-master-1 "cd /data/Dockerfile/nginx/ && ./build.sh ${Starttime} && echo 'build_image and push_harbor success!'"

    #Upgrade pod application of nginx in k8s cluster
    ssh root@k8s-master-1 "sed -ri 's@image: .*@image: k8s.harbor.cn/base_application/nginx:${Starttime}@g'  /data/mainfest/nginx.yaml"
    ssh root@k8s-master-1 "kubectl set image deployment/nginx-deployment nginx-container=k8s.harbor.cn/base_application/nginx:${Starttime} -n default --record=true"
                t2=`date +"%Y-%m-%d %H:%M:%S"`
    start_T=`date --date="${t1}" +%s`
    end_T=`date --date="${t2}" +%s`
    echo "deploy success,it has been spent ${total_time} seconds"   

    #Rollback of pod application in k8s cluster
    ssh root@k8s-master-1 'kubectl rollout undo deployment/nginx-deployment  -n default'

    #Main functions for automatic deployment of k8s clusters
    case $Method in 

#Execute main function command
main $1 $2

10) Configure the public key of jenkins to the web side of the gitlab service, enabling jenkins to pull code directly without authentication

[root@centos7 ~]# cat .ssh/id_rsa.pub

Preparation of master node for 2.k8s

1) Prepare to store the path for nginx application mirroring. The current directory is empty, and the code profiles are all placed on the gitlab server

[root@k8s-master-1 ~]# ll /data/Dockerfile/nginx
total 0

2) Pull the authentication file for the mirror in harbor

[root@k8s-master-1 ~]# ll /etc/docker/certs.d/k8s.harbor.cn/harbor_ca.crt

3) yaml manifest file of nginx in k8s cluster

[root@k8s-master-1 data]# vim /data/mainfest/nginx.yaml

4) Start pod resource and service resource through nginx.yaml file

[root@k8s-master-1 ~]# kubectl apply -f /data/mainfest/nginx.yaml

5) Check the pod application of nginx and whether the service resource is started

[root@k8s-master-1 ~]# kubectl get pod -n default

6) View service resources

[root@k8s-master-1 ~]# kubectl get service -n default

7) web-side access

3. Upload nginx's image building and other related configuration files to gitlab

1) The following are the mirror profiles for nginx, all under the develop ment branch

2) Dockerfile file for nginx mirroring

3) build.sh Mirror and Auto-Push to harbor Script

4) index.html page file

5) Main configuration file for nginx

6) server access path profile for nginx

3. Build a jenkins project immediately to automate the deployment of nginx applications to the k8s cluster

1) All above are ready to start building the jenkins project

2) Select the appropriate branch and then build the method

3) Start building the project

4) View Control Output

5) View the pod container where k8s is currently running nginx

[root@k8s-master-1 ~]# kubectl get pod -n default 

6) Look again at the mirror source address of the currently running pod and see that it is updated automatically

[root@k8s-master-1 ~]# kubectl describe pod nginx-deployment-b856886fb-9mzrm -n default

7) View the yaml manifest file of nginx, which is also automatically modified

[root@k8s-master-1 ~]# cat /data/mainfest/nginx.yaml | grep "image:.*"

8) Check the harbor mirror warehouse to see if the new image you built was successfully pushed into this harbor warehouse

9) Visit the updated version of nginx on the web side to see automated updates

10) Suppose the developer updates the version of index.html in gitlab to version 1.13

11) Rebuild Publication Updates

12) View console output

13) web-side access to nginx applications

4. If there is a problem with the current version, you need to roll back. Do the following rollback

1) jenkins console chooses rollback rollback

2) Log view of rollback process

3) web-side authentication access, visible rollback success

Tags: Linux Nginx jenkins ssh GitLab

Posted on Sun, 17 May 2020 13:45:59 -0400 by conor.higgins