1.jenkins环境准备并启动jenkins服务 1)jdk环境配置 [root@centos7 ~]# ll /app/ [root@centos7 ~]# cat /etc/profile | tail -n3 #jdk的环境变量export JAVA_HOME=/app/jdkexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHexport C
1.jenkins环境准备并启动jenkins服务
1)jdk环境配置
[root@centos7 ~]# ll /app/[root@centos7 ~]# cat /etc/profile | tail -n3 #jdk的环境变量export JAVA_HOME=/app/jdkexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATHexport CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
2)准备好jenkins启动的war包
[root@centos7 war]# ll3)查看jenkins的启动脚本
[root@centos7 war]# cat start.sh4)启动jenkins
[root@centos7 war]# ./start.sh && tailf test.log5)在数据目录修改hudson更新url,改为国内url,避免jenkins无法启动
[root@centos7 ~]# cat .jenkins/hudson.model.UpdateCenter.xml6)准备好主机解析或添加到DNS服务器
[root@centos7 ~]# cat /etc/hosts7)web端访问jenkins界面
8)创建一个jenkins_project项目,并编辑配置
9)在jenkins的后端准备好自动化部署脚本k8s_auto_deploy.sh,事先做好公私钥认证,使得jenkins能够访问k8s-master-1
[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 $210)将jenkins的公钥配置到gitlab服务的web端,使得jenkins可以直接拉取代码无需认证
[root@centos7 ~]# cat .ssh/id_rsa.pub
2.k8s的master节点的准备
1)准备存放nginx应用镜像制作的路径,当前目录为空,其代码配置文件均已放在gitlab服务器中
[root@k8s-master-1 ~]# ll /data/Dockerfile/nginxtotal 0
2)拉取harbor中的镜像的认证文件
[root@k8s-master-1 ~]# ll /etc/docker/certs.d/k8s.harbor.cn/harbor_ca.crt