一、实施准备
登录 OpenStack 平台,使用提供的 CentOS_7.5_x86_64_XD.qcow2 镜像创建两台云主机,并使用提供的软件包部署好双节点 Kubernetes 集群。
项目目标:jenkins的离线安装步骤、gitlab的使用和管理、CICD的配置步骤和方法。
二、案例实施
1、安装Jenkins环境
# 查看k8s集群状态
[root@master jenkins]# kubectl get cs
Warning: v1 ComponentStatus is deprecated in v1.19+
NAME STATUS MESSAGE ERROR
etcd-0 Healthy {"health":"true","reason":""}
scheduler Healthy ok
controller-manager Healthy ok
# 查看k8s集群节点信息
[root@master jenkins]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane 9d v1.25.0
node NotReady <none> 9d v1.25.0
# 创建 Jenkins 目录
[root@master ~]# mkdir -p /root/cicd/jenkins && cd /root/cicd/jenkins
# 上传离线包到master节点 jenkins 目录,然后解压文件
[root@master jenkins]# tar -zxvf CICD_Offline.tar -C /opt/
# 导入镜像
[root@master jenkins]# cd /opt/
[root@master opt]# ls
apache-maven-3.6.3-bin.tar.gz cni containerd jenkins.tar plugins repository springcloud
[root@master opt]# docker load -i jenkins.tar
...略
Loaded image: jenkins/jenkins:2.262-centos
...略
Loaded image: gitlab/gitlab-ce:12.9.2-ce.0
...略
Loaded image: java:8-jre
# 编写jenkins编排文件
[root@master jenkins]# vi docker-compose.yml
version: '3.3'
services:
jenkins:
image: 'jenkins/jenkins:2.262-centos'
volumes:
- /home/jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
- /usr/bin/kubectl:/usr/local/bin/kubectl
- /root/.kube:/root/.kube
ports:
- "8080:8080"
expose:
- "8080"
- "50000"
privileged: true
user: root
restart: always
container_name: jenkins
# 启动jenkins
[root@master jenkins]# docker-compose -f docker-compose.yml up -d
[+] Running 2/2
⠿ Network jenkins_default Created 0.1s
⠿ Container jenkins Started 0.6s
[root@master jenkins]# docker-compose up -d
[+] Running 1/0
⠿ Container jenkins Running 0.0s
[root@master jenkins]# docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
jenkins "/sbin/tini -- /usr/…" jenkins running 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp
# 安装插件
[root@master jenkins]# cp -rfv /opt/plugins/* /home/jenkins_home/plugins/
[root@master jenkins]# docker restart jenkins
jenkins
在 web 端通过 http://10.104.43.110:8080 访问 Jenkins
根据上面的信息查看密码:
[root@master jenkins]# docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword
9dcd074ea5d645618314a5f68085a45e
输入密码并点击“继续”,进入jenkins欢迎页面。
依次点击“系统管理”、“管理用户”按钮进入用户列表界面,如图所示:
再点击“新建用户”,输入用户信息:
- 用户名:springcloud
- 密码/确认密码:1234
- 全名:springcloud-guo
- 电子邮箱:[email protected]
创建用户后,退出admin用户登录,使用新建用户'springcloud'登录Jenkins,依次点击“系统管理”——“系统配置”按钮进入系统配置界面,在 “Resource root URL”处配置 jenkins URL 为:http://10.104.43.110:8080/,然后点击“保存”。
2、部署Gitlab
GitLab 是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面进行访问公开的或者私人项目。与 Github 类似,GitLab 能够浏览源代码,管理缺陷和注释,可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库,团队成员可以利用内置的简单聊天程序(Wall)进行交流。Gitlab 还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。
(1)启动gitlab
[root@master cicd]# mkdir gitlab && cd gitlab/
[root@master gitlab]# pwd
/root/cicd/gitlab
# 编写gitlab编排文件
[root@master gitlab]# vi docker-compose.yml
version: '3.3'
services:
gitlab:
image: "gitlab/gitlab-ce:12.9.2-ce.0"
container_name: gitlab
restart: always
hostname: '10.104.43.110'
privileged: true
environment:
TZ: 'Asia/Shanghai'
ports:
- '81:80'
- '443:443'
- '1022:22'
volumes:
- /srv/gitlab/config:/etc/gitlab
- /srv/gitlab/gitlab/logs:/var/log/gitlab
- /srv/gitlab/gitlab/data:/var/opt/gitlab
# 启动gitlab
[root@master gitlab]# docker-compose up -d
[+] Running 2/2
⠿ Network gitlab_default Created 0.1s
⠿ Container gitlab Started 0.6s
[root@master gitlab]# docker-compose ps
NAME COMMAND SERVICE STATUS PORTS
gitlab "/assets/wrapper" gitlab running (starting) 0.0.0.0:1022->22/tcp, 0.0.0.0:81->80/tcp, 0.0.0.0:443->443/tcp, :::1022->22/tcp, :::81->80/tcp, :::443->443/tcp
启动完成后,在 web 端访问 Gitlab(http://10.104.43.110:81)。
设置 root 用户信息(密码:12345678),并使用 root 用户登录 Gitlab .
(2)创建项目
点击 “Create a project”,创建项目 springcloud ,可见等级选择 “public”,如下所示:
点击“创建项目”按钮后,进入项目页面。
push 源代码到 gitlab 的 springcloud 项目:
[root@master ~]# cd /opt/springcloud/
[root@master springcloud]# ls
account-service docker-compose.dev.yml LICENCE notification-service registry
auth-service docker-compose.yml mongodb pom.xml statistics-service
config gateway monitoring README.md yaml
[root@master springcloud]# yum install -y git
[root@master springcloud]# git config --global user.name "administrator"
[root@master springcloud]# git config --global user.email "[email protected]"
[root@master springcloud]# git remote remove origin
[root@master springcloud]# git remote add origin http://10.104.43.110:81/root/springcloud.git
[root@master springcloud]# git add .
[root@master springcloud]# git commit -m "initial commit"
# On branch master
nothing to commit, working directory clean
[root@master springcloud]# git push -u origin master
Username for 'http://10.104.43.110:81': root 《————git用户名
Password for 'http://[email protected]:81': 《————git密码:12345678
Counting objects: 3192, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (1428/1428), done.
Writing objects: 100% (3192/3192), 1.40 MiB | 1.48 MiB/s, done.
Total 3192 (delta 1233), reused 3010 (delta 1207)
remote: Resolving deltas: 100% (1233/1233), done.
To http://10.104.43.110:81/root/springcloud.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
刷新网页(http://10.104.43.110:81/root/springcloud),springcloud项目中文件已经更新了。