測(cè)試代碼是否可以克隆
root@wang:~/3# git clone [email protected]:linux37/app.git
創(chuàng)建放置腳本的文件夾
root@jenkins:~# mkdir /root/scripts
創(chuàng)建打包目錄
root@jenkins:~/scripts# mkdir /data/gitdata/linux37/app -pv
創(chuàng)建代碼克隆腳本
root@jenkins:~/scripts# cat linux37-k8s-tomcat-app1.sh
#!/bin/bash
#Author:wyh
#Date: 2019-08-03
#Version: v1
#記錄腳本開(kāi)始執(zhí)行時(shí)間
starttime=`date +'%Y-%m-%d %H:%M:%S'`
#變量
SHELL_DIR="/root/scripts"
SHELL_NAME="$0"
K8S_CONTROLLER1="192.168.200.198"
K8S_CONTROLLER2="192.168.200.197"
DATE=`date +%Y-%m-%d_%H_%M_%S`
METHOD=$1
Branch=$2
if test -z $Branch;then
Branch=develop
fi
function Code_Clone(){
Git_URL="[email protected]:linux37/app.git"
DIR_NAME=`echo ${Git_URL} |awk -F "/" '{print $2}' | awk -F "." '{print $1}'`
DATA_DIR="/data/gitdata/linux37"
Git_Dir="${DATA_DIR}/${DIR_NAME}"
cd ${DATA_DIR} && echo "即將清空上一版本代碼并獲取當(dāng)前分支最新代碼" && sleep 1 && rm -rf ${DIR_NAME}
echo "即將開(kāi)始從分支${Branch} 獲取代碼" && sleep 1
git clone -b ${Branch} ${Git_URL}
echo "分支${Branch} 克隆完成,,即將進(jìn)行代碼編譯!" && sleep 1
#cd ${Git_Dir} && mvn clean package
#echo "代碼編譯完成,即將開(kāi)始將IP地址等信息替換為測(cè)試環(huán)境"
#####################################################
sleep 1 && echo "代碼編譯完成,,即將開(kāi)始打包"
cd ${Git_Dir}
tar czf ${DIR_NAME}.tar.gz ./*
}
#將打包好的壓縮文件拷貝到k8s 控制端服務(wù)器
function Copy_File(){
echo "壓縮文件打包完成,,即將拷貝到k8s 控制端服務(wù)器${K8S_CONTROLLER1}" && sleep 1
scp ${Git_Dir}/${DIR_NAME}.tar.gz root@${K8S_CONTROLLER1}:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1
echo "壓縮文件拷貝完成,服務(wù)器${K8S_CONTROLLER1}即將開(kāi)始制作Docker 鏡像!" && sleep 1
}
#到控制端執(zhí)行腳本制作并上傳鏡像
function Make_Image(){
echo "開(kāi)始制作Docker鏡像并上傳到Harbor服務(wù)器" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/dockerfile/web/linux37/tomcat-app1 && bash build-command.sh ${DATE}"
echo "Docker鏡像制作完成并已經(jīng)上傳到harbor服務(wù)器" && sleep 1
}
#到控制端更新k8s yaml文件中的鏡像版本號(hào),從而保持yaml文件中的鏡像版本號(hào)和k8s中版本號(hào)一致
function Update_k8s_yaml(){
echo "即將更新k8s yaml文件中鏡像版本" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/web/linux37/tomcat-app1 && sed -i 's/image: harbor.magedu.*/image: harbor.magedu.net\/linux37\/linux37-tomcat:${DATE}/g' tomcat-app1.yaml"
echo "k8s yaml文件鏡像版本更新完成,即將開(kāi)始更新容器中鏡像版本" && sleep 1
}
#到控制端更新k8s中容器的版本號(hào),有兩種更新辦法,,一是指定鏡像版本更新,二是apply執(zhí)行修改過(guò)的yaml文件
function Update_k8s_container(){
#第一種方法
ssh root@${K8S_CONTROLLER1} "kubectl set image deployment/linux37-tomcat-app1-deployment linux37-tomcat-app1-container=harbor.magedu.net/linux37/linux37-tomcat:${DATE} -n linux37"
#第二種方法,推薦使用第一種
#ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/web-test/tomcat-app1 && kubectl apply -f web-test.yam --record"
echo "k8s 鏡像更新完成" && sleep 1
echo "當(dāng)前業(yè)務(wù)鏡像版本: harbor.magedu.net/linux37/linux37-tomcat:${DATE}"
#計(jì)算腳本累計(jì)執(zhí)行時(shí)間,,如果不需要的話可以去掉下面四行
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次業(yè)務(wù)鏡像更新總計(jì)耗時(shí):"$((end_seconds-start_seconds))"s"
}
#基于k8s 內(nèi)置版本管理回滾到上一個(gè)版本
function rollback_last_version(){
echo "即將回滾之上一個(gè)版本"
ssh root@${K8S_CONTROLLER1} "kubectl rollout undo deployment/linux37-tomcat-app1-deployment -n linux37"
sleep 1
echo "已執(zhí)行回滾至上一個(gè)版本"
}
#使用幫助
usage(){
echo "部署使用方法為 ${SHELL_DIR}/${SHELL_NAME} deploy branch"
echo "回滾到上一版本使用方法為 ${SHELL_DIR}/${SHELL_NAME} rollback_last_version"
}
#主函數(shù)
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 $3
執(zhí)行打包腳本
root@jenkins:~/scripts# bash linux37-k8s-tomcat-app1.sh deploy master
即將清空上一版本代碼并獲取當(dāng)前分支最新代碼
即將開(kāi)始從分支master 獲取代碼
Cloning into 'app'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
分支master 克隆完成,,即將進(jìn)行代碼編譯!
代碼編譯完成,即將開(kāi)始打包
查看打包目錄
root@jenkins:/data/gitdata/linux37/app# ls
app.tar.gz index.html
root@jenkins:/data/gitdata/linux37/app# cp app.tar.gz /opt/
拷貝過(guò)去
root@master:/opt# scp -r k8s-data 192.168.200.197:/opt/
拷貝密鑰過(guò)去
root@jenkins:~/scripts# ssh-copy-id 192.168.200.198
把拷貝代碼的添加過(guò)去,,打包鏡像也添加上
root@jenkins:~/scripts# vim linux37-k8s-tomcat-app1.sh
95 Copy_File;
96 Make_Image;
97 Update_k8s_yaml;
98 Update_k8s_container;
修改build文件,,加上tag標(biāo)簽
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# vim build-command.sh
#/bin/bash
TAG=$1
docker build -t harbor.wyh.net/linux37/linux37-tomcat:${TAG} .
docker push harbor.wyh.net/linux37/linux37-tomcat:${TAG}
修改Dockerfile 解壓路徑
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# vim Dockerfile
ADD app.tar.gz /data/tomcat/webapps/app
執(zhí)行鏡像腳本
root@jenkins:~/scripts# bash linux37-k8s-tomcat-app1.sh deploy master
即將清空上一版本代碼并獲取當(dāng)前分支最新代碼
即將開(kāi)始從分支master 獲取代碼
Cloning into 'app'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
分支master 克隆完成,即將進(jìn)行代碼編譯!
代碼編譯完成,,即將開(kāi)始打包
壓縮文件打包完成,,即將拷貝到k8s 控制端服務(wù)器192.168.200.198
app.tar.gz 100% 139 193.2KB/s 00:00
壓縮文件拷貝完成,服務(wù)器192.168.200.198即將開(kāi)始制作Docker 鏡像!
開(kāi)始制作Docker鏡像并上傳到Harbor服務(wù)器
Sending build context to Docker daemon 39.94kB
Step 1/9 : FROM harbor.wyh.net/linux37/linux37-centos7.6-tomcat:8.5.45
---> 2bc25989d3eb
Step 2/9 : maintainer zhangshijie "[email protected]"
---> Running in 338f123645a9
Removing intermediate container 338f123645a9
---> cd52e0f15ef2
Step 3/9 : ADD app.tar.gz /data/tomcat/webapps/app
---> 41ebc73587b0
Step 4/9 : ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
---> 37452f5430b7
Step 5/9 : ADD server.xml /apps/tomcat/conf/server.xml
---> 9fa537c0e1ca
Step 6/9 : ADD catalina.sh /apps/tomcat/bin/catalina.sh
---> fc6bfedf1640
Step 7/9 : RUN chown www.www /apps/tomcat /data/tomcat /apps/apache-tomcat-8.5.45 -R
---> Running in ed1f3c01ddc3
Removing intermediate container ed1f3c01ddc3
---> 7e4194983f2b
Step 8/9 : EXPOSE 8080 8443
---> Running in 8d7350f4410c
Removing intermediate container 8d7350f4410c
---> 008fa5c88de5
Step 9/9 : CMD ["/apps/tomcat/bin/run_tomcat.sh"]
---> Running in 50c57445c8d0
Removing intermediate container 50c57445c8d0
---> 8a8f5c56e00a
Successfully built 8a8f5c56e00a
Successfully tagged harbor.wyh.net/linux37/linux37-tomcat:2019-12-01_09_10_49
The push refers to repository [harbor.wyh.net/linux37/linux37-tomcat]
Get https://harbor.wyh.net/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
Docker鏡像制作完成并已經(jīng)上傳到harbor服務(wù)器
查看鏡像
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
harbor.wyh.net/linux37/linux37-tomcat 2019-12-01_09_10_49 8a8f5c56e00a 2 minutes ago 977MB
啟動(dòng)容器
root@master:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1# docker run -it --rm -p 8081:8080 harbor.wyh.net/linux37/linux37-tomcat:2019-12-01_09_10_49
修改腳本主要是鏡像地址
root@jenkins:~/scripts# cat linux37-k8s-tomcat-app1.sh
#!/bin/bash
#Author: ZhangShiJie
#Date: 2019-08-03
#Version: v1
#記錄腳本開(kāi)始執(zhí)行時(shí)間
starttime=`date +'%Y-%m-%d %H:%M:%S'`
#變量
SHELL_DIR="/root/scripts"
SHELL_NAME="$0"
K8S_CONTROLLER1="192.168.200.198"
K8S_CONTROLLER2="192.168.200.197"
DATE=`date +%Y-%m-%d_%H_%M_%S`
METHOD=$1
Branch=$2
if test -z $Branch;then
Branch=develop
fi
function Code_Clone(){
Git_URL="[email protected]:linux37/app.git"
DIR_NAME=`echo ${Git_URL} |awk -F "/" '{print $2}' | awk -F "." '{print $1}'`
DATA_DIR="/data/gitdata/linux37"
Git_Dir="${DATA_DIR}/${DIR_NAME}"
cd ${DATA_DIR} && echo "即將清空上一版本代碼并獲取當(dāng)前分支最新代碼" && sleep 1 && rm -rf ${DIR_NAME}
echo "即將開(kāi)始從分支${Branch} 獲取代碼" && sleep 1
git clone -b ${Branch} ${Git_URL}
echo "分支${Branch} 克隆完成,即將進(jìn)行代碼編譯!" && sleep 1
#cd ${Git_Dir} && mvn clean package
#echo "代碼編譯完成,,即將開(kāi)始將IP地址等信息替換為測(cè)試環(huán)境"
#####################################################
sleep 1 && echo "代碼編譯完成,,即將開(kāi)始打包"
cd ${Git_Dir}
tar czf ${DIR_NAME}.tar.gz ./*
}
#將打包好的壓縮文件拷貝到k8s 控制端服務(wù)器
function Copy_File(){
echo "壓縮文件打包完成,即將拷貝到k8s 控制端服務(wù)器${K8S_CONTROLLER1}" && sleep 1
scp ${Git_Dir}/${DIR_NAME}.tar.gz root@${K8S_CONTROLLER1}:/opt/k8s-data/dockerfile/web/linux37/tomcat-app1
echo "壓縮文件拷貝完成,服務(wù)器${K8S_CONTROLLER1}即將開(kāi)始制作Docker 鏡像!" && sleep 1
}
#到控制端執(zhí)行腳本制作并上傳鏡像
function Make_Image(){
echo "開(kāi)始制作Docker鏡像并上傳到Harbor服務(wù)器" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/dockerfile/web/linux37/tomcat-app1 && bash build-command.sh ${DATE}"
echo "Docker鏡像制作完成并已經(jīng)上傳到harbor服務(wù)器" && sleep 1
}
#到控制端更新k8s yaml文件中的鏡像版本號(hào),從而保持yaml文件中的鏡像版本號(hào)和k8s中版本號(hào)一致
function Update_k8s_yaml(){
echo "即將更新k8s yaml文件中鏡像版本" && sleep 1
ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/web/linux37/tomcat-app1 && sed -i 's/image: harbor.wyh.*/image: harbor.wyh.net\/linux37\/linux37-tomcat:${DATE}/g' tomcat-app1.yaml"
echo "k8s yaml文件鏡像版本更新完成,即將開(kāi)始更新容器中鏡像版本" && sleep 1
}
#到控制端更新k8s中容器的版本號(hào),有兩種更新辦法,,一是指定鏡像版本更新,,二是apply執(zhí)行修改過(guò)的yaml文件
function Update_k8s_container(){
#第一種方法
ssh root@${K8S_CONTROLLER1} "kubectl set image deployment/linux37-tomcat-app1-deployment linux37-tomcat-app1-container=harbor.wyh.net/linux37/linux37-tomcat:${DATE} -n linux37"
#第二種方法,推薦使用第一種
#ssh root@${K8S_CONTROLLER1} "cd /opt/k8s-data/yaml/web-test/tomcat-app1 && kubectl apply -f web-test.yam --record"
echo "k8s 鏡像更新完成" && sleep 1
echo "當(dāng)前業(yè)務(wù)鏡像版本: harbor.wyh.net/linux37/linux37-tomcat:${DATE}"
#計(jì)算腳本累計(jì)執(zhí)行時(shí)間,,如果不需要的話可以去掉下面四行
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "本次業(yè)務(wù)鏡像更新總計(jì)耗時(shí):"$((end_seconds-start_seconds))"s"
}
#基于k8s 內(nèi)置版本管理回滾到上一個(gè)版本
function rollback_last_version(){
echo "即將回滾之上一個(gè)版本"
ssh root@${K8S_CONTROLLER1} "kubectl rollout undo deployment/linux37-tomcat-app1-deployment -n linux37"
sleep 1
echo "已執(zhí)行回滾至上一個(gè)版本"
}
#使用幫助
usage(){
echo "部署使用方法為 ${SHELL_DIR}/${SHELL_NAME} deploy branch"
echo "回滾到上一版本使用方法為 ${SHELL_DIR}/${SHELL_NAME} rollback_last_version"
}
#主函數(shù)
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 $3
root@jenkins:~/scripts# bash linux37-k8s-tomcat-app1.sh deploy master
把nginx修改成v3
root@master:/opt/k8s-data/yaml/web/linux37/nginx# vim nginx.yaml
9 replicas: 1
改為1個(gè)
20 image: harbor.wyh.net/linux37/nginx:v3
執(zhí)行
root@master:/opt/k8s-data/yaml/web/linux37/nginx# kubectl apply -f nginx.yaml
克隆
root@jenkins:/3# git clone [email protected]:linux37/app.git
修改代碼為v2
root@jenkins:/3/app# vim index.html
linux37 app1 k8s v2
root@jenkins:/3/app# mkdir wyh
root@jenkins:/3/app# vim wyh/index.html
wyh
添加到本地倉(cāng)庫(kù)
root@jenkins:/3/app# git add ./*
提交到gitlab上
root@jenkins:/3/app# git commit -m "v2"