1,jenkins+gitlab部署
https://mirrors.tuna.tsinghua.edu.cn/jenkins/debian-stable/
https://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat-stable/
https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/ubuntu/pool/bionic/main/g/gitlab-ce/
root@ubuntu20:~# cat /etc/apt/sources.list
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
#安装jdk环境
apt install openjdk-11-jdk
# /var/cache/apt/archives 默认保存地址,apt clean 清除下载的jdk包
#安装jenkins
dpkg -c jenkins_2.303.2_all.deb
dpkg -i jenkins_2.303.2_all.deb
vi /etc/default/jenkins 打开钩子
JAVA_ARGS="-Djava.awt.headless=true -Dhudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true"
JENKINS_USER=root
JENKINS_GROUP=root
root@ubuntu20:/var/lib/jenkins# du -sh plugins
218M plugins
#安装gitlab
dpkg -i gitlab-ce_14.2.5-ce.0_amd64.deb
vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.192.158'
gitlab-ctl reconfigure
gitlab-ctl stop start restart
cat /etc/gitlab/initial_root_password
root@jenkins-157:~# apt install git
root@jenkins-157:~# git clone http://192.168.192.158/chuan/app1.git
Cloning into 'app1'...
Username for 'http://192.168.192.158': user1
Password for 'http://user1@192.168.192.158':
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), 444 bytes | 444.00 KiB/s, done.
git config --global user.name "user1"
git config --global user.
git add .
git commit -m "v5"
git push
root@ubuntu20:/data/scripts# cat chuan-test-job1.sh
#!/bin/bash
#Author: Chuan
#Date: 2021-1-1
#Version: v1
#记录脚本开始执行时间
starttime=`date +'%Y-%m-%d %H:%M:%S'`
#变量
SHELL_DIR="/data/scripts/"
SHELL_NAME="$0"
K8S_CONTROLLER1="192.168.192.153"
K8S_CONTROLLER2="192.168.192.152"
DATE=`date +%Y-%m-%d_%H_%M_%S`
METHOD=$1
Branch=$2
#echo $METHOD
#echo $Branch
if test -z $Branch;then
Branch=develop
fi
function Code_Clone(){
Git_URL="git@192.168.192.158:chuan/app1.git"
DIR_NAME=`echo ${Git_URL} |awk -F "/" '{print $2}' | awk -F "." '{print $1}'`
DATA_DIR="/data/gitdata/chuan"
Git_Dir="${DATA_DIR}/${DIR_NAME}"
cd ${DATA_DIR} && echo "即将清空上一版本代码并获取当前分支最新代码" && sleep 1 && rm -rf ${DIR_NAME}
echo "即将开始从分支${Branch} 获取代码" && sleep 1
git clone -b ${Branch} ${Git_URL}
echo "分支${Branch} 克隆完成,即将进行代码编译!" && sleep 1
#cd ${Git_Dir} && mvn clean package
#echo "代码编译完成,即将开始将IP地址等信息替换为测试环境"
#####################################################
sleep 1
cd ${Git_Dir}
tar czf ${DIR_NAME}.tar.gz ./*
}
#将打包好的压缩文件拷贝到k8s 控制端服务器
function Copy_File(){
echo "压缩文件打包完成,即将拷贝到k8s 控制端服务器${K8S_CONTROLLER1}" && sleep 1
scp ${Git_Dir}/${DIR_NAME}.tar.gz root@${K8S_CONTROLLER1}:/opt/k8s-data/dockerfile/web/chuan/tomcat-app1/
echo "压缩文件拷贝完成,服务器${K8S_CONTROLLER1}即将开始制作Docker 镜像!" && sleep 1
}
#到控制端执行脚本制作并上传镜像
function Make_Image(){
echo "开始制作Docker镜像并上传到Harbor服务器" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/dockerfile/web/chuan/tomcat-app1 && bash build-command.sh ${DATE}"
echo "Docker镜像制作完成并已经上传到harbor服务器" && sleep 1
}
#到控制端更新k8s yaml文件中的镜像版本号,从而保持yaml文件中的镜像版本号和k8s中版本号一致
function Update_k8s_yaml(){
echo "即将更新k8s yaml文件中镜像版本" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/chuan/tomcat-app1 && sed -i 's/image: 192.168.192.*/image: 192.168.192.155:80\/chuan\/tomcat-app1:${DATE}/g' tomcat-app1.yaml"
echo "k8s yaml文件镜像版本更新完成,即将开始更新容器中镜像版本" && sleep 1
}
#到控制端更新k8s中容器的版本号,有两种更新办法,一是指定镜像版本更新,二是apply执行修改过的yaml文件
function Update_k8s_container(){
#第一种方法
ssh root@${K8S_CONTROLLER1} "kubectl set image deployment/chuan-tomcat-app1-deployment chuan-tomcat-app1-container=192.168.192.155:80/chuan/tomcat-app1:${DATE} -n chuan"
#第二种方法,推荐使用第一种
#ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/chuan/tomcat-app1 && kubectl apply -f tomcat-app1.yaml --record"
echo "k8s 镜像更新完成" && sleep 1
echo "当前业务镜像版本: 192.168.192.155:80/chuan/tomcat-app1:${DATE}"
#计算脚本累计执行时间,如果不需要的话可以去掉下面四行
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次业务镜像更新总计耗时:"$((end_seconds-start_seconds))"s"
}
#基于k8s 内置版本管理回滚到上一个版本
function rollback_last_version(){
echo "即将回滚之上一个版本"
ssh root@${K8S_CONTROLLER1} "kubectl rollout undo deployment/chuan-tomcat-app1-deployment -n chuan"
sleep 1
echo "已执行回滚至上一个版本"
}
#使用帮助
usage(){
echo "部署使用方法为 ${SHELL_DIR}/${SHELL_NAME} deploy "
echo "回滚到上一版本使用方法为 ${SHELL_DIR}/${SHELL_NAME} rollback_last_version"
}
#主函数
main(){
case ${METHOD} in
deploy)
Code_Clone;
Copy_File;
Make_Image;
Update_k8s_yaml;
Update_k8s_container;
;;
rollback_last_version)
rollback_last_version;
;;
*)
usage;
esac;
}
main $1 $2
控制台输出
Started by user jenkinsadmin
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/chuan-aa-job1
[chuan-aa-job1] $ /bin/sh -xe /tmp/jenkins10005038184895047881.sh
+ bash /data/scripts/chuan-test-job1.sh deploy main
即将清空上一版本代码并获取当前分支最新代码
即将开始从分支main 获取代码
Cloning into 'app1'...
分支main 克隆完成,即将进行代码编译!
压缩文件打包完成,即将拷贝到k8s 控制端服务器192.168.192.153
压缩文件拷贝完成,服务器192.168.192.153即将开始制作Docker 镜像!
开始制作Docker镜像并上传到Harbor服务器
Sending build context to Docker daemon 24.13MB
Step 1/8 : FROM 192.168.192.155:80/pub-images/tomcat-base:v8.5.43
---> 17a7ec7145c1
Step 2/8 : ADD catalina.sh /apps/tomcat/bin/catalina.sh
---> Using cache
---> 0910be8f535f
Step 3/8 : ADD server.xml /apps/tomcat/conf/server.xml
---> Using cache
---> a42f347ddde9
Step 4/8 : ADD app1.tar.gz /data/tomcat/webapps/myapp/
---> 2d07796bc414
Step 5/8 : ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
---> 82b29eda3234
Step 6/8 : RUN useradd nginx && chown -R nginx.nginx /data/ /apps/
---> Running in 116dfffa8743
Removing intermediate container 116dfffa8743
---> 3b4490ccd111
Step 7/8 : EXPOSE 8080 8443
---> Running in f6892b974b98
Removing intermediate container f6892b974b98
---> d28b9cd49be9
Step 8/8 : CMD ["/apps/tomcat/bin/run_tomcat.sh"]
---> Running in 52aad52658c6
Removing intermediate container 52aad52658c6
---> d623275137fd
Successfully built d623275137fd
Successfully tagged 192.168.192.155:80/chuan/tomcat-app1:2021-1-1_19_51_57
The push refers to repository [192.168.192.155:80/chuan/tomcat-app1]
ca943abb0f78: Preparing
7f201ddbef6e: Preparing
35fbd74deae2: Preparing
e567ecab90c7: Preparing
afc24a18a5db: Preparing
23ea9990b7cf: Preparing
765ee3b74553: Preparing
2e0e31327efc: Preparing
a9e916f2637f: Preparing
a7279fa62de8: Preparing
376c6da15644: Preparing
01d35c9611c5: Preparing
fb82b029bea0: Preparing
23ea9990b7cf: Waiting
765ee3b74553: Waiting
2e0e31327efc: Waiting
a9e916f2637f: Waiting
a7279fa62de8: Waiting
376c6da15644: Waiting
01d35c9611c5: Waiting
fb82b029bea0: Waiting
afc24a18a5db: Layer already exists
e567ecab90c7: Layer already exists
23ea9990b7cf: Layer already exists
765ee3b74553: Layer already exists
a9e916f2637f: Layer already exists
2e0e31327efc: Layer already exists
a7279fa62de8: Layer already exists
7f201ddbef6e: Pushed
01d35c9611c5: Layer already exists
376c6da15644: Layer already exists
fb82b029bea0: Layer already exists
35fbd74deae2: Pushed
ca943abb0f78: Pushed
2021-10-26_19_51_57: digest: sha256:73cd06bb213ca432276fc12b4b701d659fa848673fdb7978167c995f8ae12197 size: 3040
Docker镜像制作完成并已经上传到harbor服务器
即将更新k8s yaml文件中镜像版本
k8s yaml文件镜像版本更新完成,即将开始更新容器中镜像版本
deployment.apps/chuan-tomcat-app1-deployment image updated
k8s 镜像更新完成
当前业务镜像版本: 192.168.192.155:80/chuan/tomcat-app1:2021-1-1_19_51_57
本次业务镜像更新总计耗时:16s
Finished: SUCCESS