标签:work dep 远程推送 查看 验证 部署 图片 ima mamicode
1.jenkins环境准备并启动jenkins服务[root@centos7 ~]# ll /app/
[root@centos7 ~]# cat /etc/profile | tail -n3 #jdk的环境变量
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
URL=git@k8s.gitlab.cn: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"`
#代码克隆至jenkins后端
clone_code(){
cd /root/.jenkins/workspace/jenkins_project && git clone -b $Branch ${URL}&& echo "Clone Finished"
}
#代码打包压缩并远程推送至k8s-master-1的nginx镜像制作目录
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‘
}
#远程操作k8s-master-1节点,进行镜像制作并推送至harbor镜像仓库
build_iamge(){
ssh root@k8s-master-1 "cd /data/Dockerfile/nginx/ && ./build.sh ${Starttime} && echo ‘build_image and push_harbor success!‘"
}
#对k8s集群中的nginx的pod应用进行升级
app_update(){
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`
total_time=$((end_T-start_T))
echo "deploy success,it has been spent ${total_time} seconds"
}
#k8s集群中的pod应用进行回滚
app_rollback(){
ssh root@k8s-master-1 ‘kubectl rollout undo deployment/nginx-deployment -n default‘
}
#进行k8s集群自动部署的主函数
main(){
case $Method in
deploy)
clone_code
Pack_scp
build_iamge
app_update
;;
rollback)
app_rollback
;;
esac
}
#执行主函数命令
main $1 $2
[root@centos7 ~]# cat .ssh/id_rsa.pub
[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
[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:.*"
标签:work dep 远程推送 查看 验证 部署 图片 ima mamicode
原文地址:https://blog.51cto.com/14234542/2495985