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
[root@centos7 war]# ll
[root@centos7 war]# cat start.sh
[root@centos7 war]# ./start.sh && tailf test.log
[root@centos7 ~]# cat .jenkins/hudson.model.UpdateCenter.xml
[root@centos7 ~]# cat /etc/hosts
[root@centos7 ~]# cat /data/script/k8s_auto_depoy.sh #!/bin/bash [email protected]:middle-supoort-project/middle-compose-service.git Starttime=`date +"%Y-%m-%d_%H-%M-%S"` Method=$1 Branch=$2 t1=`date +"%Y-%m-%d %H:%M:%S"` #Code cloned to jenkins backend clone_code(){ cd /root/.jenkins/workspace/jenkins_project && git clone -b $Branch $&& echo "Clone Finished" } #Code packaging is compressed and pushed remotely to the nginx mirroring directory of k8s-master-1 Pack_scp(){ 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 build_iamge(){ ssh root@k8s-master-1 "cd /data/Dockerfile/nginx/ && ./build.sh $ && echo 'build_image and push_harbor success!'" } #Upgrade pod application of nginx in k8s cluster app_update(){ ssh root@k8s-master-1 "sed -ri 's@image: .*@image: k8s.harbor.cn/base_application/nginx:$@g' /data/mainfest/nginx.yaml" ssh root@k8s-master-1 "kubectl set image deployment/nginx-deployment nginx-container=k8s.harbor.cn/base_application/nginx:$ -n default --record=true" t2=`date +"%Y-%m-%d %H:%M:%S"` start_T=`date --date="$" +%s` end_T=`date --date="$" +%s` total_time=$((end_T-start_T)) echo "deploy success,it has been spent $ seconds" } #Rollback of pod application in k8s cluster app_rollback(){ ssh root@k8s-master-1 'kubectl rollout undo deployment/nginx-deployment -n default' } #Main functions for automatic deployment of k8s clusters main(){ case $Method in deploy) clone_code Pack_scp build_iamge app_update ;; rollback) app_rollback ;; esac } #Execute main function command main $1 $210) 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
[root@k8s-master-1 ~]# ll /etc/docker/certs.d/k8s.harbor.cn/harbor_ca.crt
[root@k8s-master-1 data]# vim /data/mainfest/nginx.yaml
[root@k8s-master-1 ~]# kubectl apply -f /data/mainfest/nginx.yaml
[root@k8s-master-1 ~]# kubectl get pod -n default
[root@k8s-master-1 ~]# kubectl get service -n default
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 branch3. 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[root@k8s-master-1 ~]# kubectl get pod -n default
[root@k8s-master-1 ~]# kubectl describe pod nginx-deployment-b856886fb-9mzrm -n default
[root@k8s-master-1 ~]# cat /data/mainfest/nginx.yaml | grep "image:.*"