关于如何使用Jenkins+Gitlab实现可CI/CD,在此就不多做介绍了,本篇博文主要介绍Jenkins+Gitlab针对kubernetes集群实现CI/CD。由于Kubernetes近年来的火爆程度,掌握这项技能已经变得尤为重要!
一、环境准备
系统 | 主机名 | IP地址 | 所运行的服务 |
---|---|---|---|
Centos 7 | master | 192.168.1.1 | K8s集群的master节点 |
Centos 7 | node01 | 192.168.1.2 | K8s集群的node节点 |
Centos 7 | docker | 192.168.1.4 | Docker |
- K8s集群主要用于运行一个Nginx运行容器,模拟线上环境;
- master节点主要用于运行registry私有仓库;
- docker服务器运行Jenkins与Gitlab服务;
- 上述三台服务器全部加入registry私有仓库;
部署环境所需任何软件包,都可从以下链接中获取!
链接:https://pan.baidu.com/s/1pb4ZShK5MBxIj6xN-hMmoA
提取码:k66s
二、部署registry私有仓库
其实registry私有仓库在任何一台机器上部署都可以,这里就直接在master节点上部署了!
[root@master ~]# docker run -d --restart=always -p 5000:5000 registry:2
[root@master ~]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd --insecure-registry 192.168.1.1:5000
[root@master ~]# scp /usr/lib/systemd/system/docker.service 192.168.1.2:/usr/lib/systemd/system/docker.service
[root@master ~]# scp /usr/lib/systemd/system/docker.service 192.168.1.4:/usr/lib/systemd/system/docker.service
[root@master ~]# systemctl daemon-reload && systemctl restart docker
[root@node01 ~]# systemctl daemon-reload && systemctl restart docker
[root@docker ~]# systemctl daemon-reload && systemctl restart docker
三、master节点运行nginx容器
[root@master ~]# docker tag nginx:latest 192.168.1.1:5000/nginx:v1
[root@master ~]# docker push 192.168.1.1:5000/nginx:v1
[root@master ~]# vim nginx.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: 192.168.1.1:5000/nginx:v1
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
labels:
name: nginx
name: nginx
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 31234
selector:
name: nginx
[root@master ~]# kubectl apply -f nginx.yaml
[root@master ~]# kubectl get pod,svc | grep nginx
pod/nginx-64dfdff6cd-q24qn 1/1 Running 0 105s
pod/nginx-64dfdff6cd-vj9hm 1/1 Running 0 105s
service/nginx NodePort 10.97.11.24 80:31234/TCP 105s
四、部署Jenkins服务
在部署Jenkins服务之前需部署Tomcat服务,本次部署Jenkins采用war包的形式进行部署!
[root@docker ~]# tar zxf jdk-8u231-linux-x64.tar.gz
[root@docker ~]# mv jdk1.8.0_231/ /usr/java
[root@docker ~]# vim /etc/profile #末尾写入
export JAVA_HOME=/usr/java
export JRE_HOME=/usr/java/jre
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
[root@docker ~]# source /etc/profile
[root@docker ~]# java -version
java version "1.8.0_231"
Java(TM) SE Runtime Environment (build 1.8.0_231-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.231-b11, mixed mode)
[root@docker ~]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.31.tar.gz
[root@docker ~]# tar zxf apache-tomcat-9.0.31.tar.gz
[root@docker ~]# mv apache-tomcat-9.0.31 /usr/tomcat9
[root@docker ~]# cd /usr/tomcat9/webapps/
[root@docker webapps]# mv * /tmp
[root@docker webapps]# wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war
[root@docker webapps]# vim /usr/tomcat9/conf/server.xml
69
#在8443后添加URIEncoding="UTF-8",使tomcat支持utf-8字符集
[root@docker webapps]# cd /usr/tomcat9/bin/
[root@docker bin]# vim catalina.sh
1 #!/bin/sh
2 export CATALINA_OPTS="-DJENKINS_HOME=/data/jenkins"
3 export JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dhudson.ClassicPluginStrategy.noBytecodeTransformer=true"
#这两行特别重要,注意不要填写错误!
[root@docker bin]# ./catalina.sh start
#启动tomcat,tomcat自动解压war包,也就是说jenkins也就启动了
[root@docker bin]# netstat -antp | grep 8080
tcp6 0 0 :::8080 :::* LISTEN 3303/java
五、配置Jenkins
1)配置Jenkins web界面
注意访问的是Jenkins服务器的IP地址+8080端口+jenkins目录!
自行根据提示查看密码并粘贴到相应位置,如下,查看密码:
[root@docker bin]# cat /data/jenkins/secrets/initialAdminPassword
d2bc124dfcb44fc4bac0df7427f7b8c9
由于跳过了插件安装,所以需要使用网盘中提供的插件目录——plugins.tar.gz!
2)配置所需插件
[root@docker ~]# mv /data/jenkins/plugins/ /tmp/
[root@docker ~]# tar zxf plugins.tar.gz -C /data/jenkins/
[root@docker ~]# /usr/tomcat9/bin/catalina.sh stop
[root@docker ~]# /usr/tomcat9/bin/catalina.sh start
六、部署Gitlab服务
[root@docker ~]# yum -y install epel-release curl openssh-server openssh-clients postfix cronie policycoreutils-python patch
[root@docker ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
[root@docker ~]# yum -y localinstall gitlab-ce-12.3.5-ce.0.el7.x86_64.rpm
[root@docker ~]# vim /etc/gitlab/gitlab.rb
external_url 'http://192.168.1.4:90'
unicorn['listen'] = '192.168.1.4' #本机IP
unicorn['port'] = 3000
[root@docker ~]# gitlab-ctl reconfigure
[root@docker ~]# gitlab-ctl start
由于gitlab的操作过于简单,这里就不截图说明了,访问gitlab服务器的IP地址+90端口,配置gitlab服务器可以免密登录gitlab,创建一个仓库,保证可以克隆到本地即可!
[root@docker ~]# git clone [email protected]:root/test.git
[root@docker ~]# ls -d test
test
七、配置Jenkins创建一个任务
#!/bin/bash
backupcode="/data/backcode/$JOB_NAME/$BUILD_NUMBER" #这里引用了Jenkins的默认变量
mkdir -p $backupcode
chmod 644 "$JENKINS_HOME"/workspace/"$JOB_NAME"/*
rsync -acP "$JENKINS_HOME"/workspace/"$JOB_NAME"/* $backupcode
echo From 192.168.1.1:5000/nginx:v1 > "$JENKINS_HOME"/workspace/Dockerfile
echo COPY ./"$JOB_NAME"/* /usr/share/nginx/html/ >> "$JENKINS_HOME"/workspace/Dockerfile
docker rmi 192.168.1.1:5000/nginx:v1
docker build -t 192.168.1.1:5000/nginx:v2 /"$JENKINS_HOME"/workspace/.
docker push 192.168.1.1:5000/nginx:v2
ssh [email protected] sed -i 's/nginx:v1/nginx:v2/g' /root/nginx.yaml
ssh [email protected] kubectl delete deployment nginx
ssh [email protected] kubectl apply -f /root/nginx.yaml
#脚本中最后三行的IP地址是K8s集群的master节点
#除此之外的IP地址则是registry私有仓库的IP地址
脚本内容编写完成之后,先不要着急保存!还需进行以下配置:
脚本中涉及登录到K8s的master节点,所以要配置免密登录!
[root@docker ~]# ssh-copy-id [email protected]
八、开启Jenkins的匿名访问权限
九、Gitlab开启允许向自己发送web hook
十、测试持续CI/CD效果
1)在gitlab服务器上进行版本更新迭代测试
[root@docker ~]# cd test/
[root@docker test]# git config --global user.name "test"
[root@docker test]# git config --global user.email "[email protected]"
[root@docker test]# echo "hello world" > index.html
[root@docker test]# git add .
[root@docker test]# git commit -m "test CI/CD"
[root@docker test]# git push origin master