Mac M1 Parallels CentOS7.9 Rancher + K8S + Gitlab + Jenkins +Harbor CICD

一、资源清单

机器名称IP地址角色
k8s+rancher高可用部署: https://blog.csdn.net/qq_41594280/article/details/135312148
rancher10.211.55.200管理K8S集群
k8svip10.211.55.199K8S VIP
master0110.211.55.201K8S集群主节点
master0210.211.55.202K8S集群主节点
master0310.211.55.203K8S集群主节点
node0110.211.55.211K8S集群从节点
node0210.211.55.212K8S集群从节点
Harbor容器私服安装部署: https://blog.csdn.net/qq_41594280/article/details/135447118
harbor10.211.55.20docker容器私服
Gitlab代码仓库安装部署: https://blog.csdn.net/qq_41594280/article/details/135491802
gitlab10.211.55.33代码仓库
Jenkins安装部署: https://blog.csdn.net/qq_41594280/article/details/135442317
jenkins10.211.55.34持续集成工具

二、Jenkins与GitLab集成实现自动构建(可忽略)

2.0 安装Git和Maven

# 安装Git
[root@jenkins ~]# yum install -y git
[root@jenkins ~]# git --version
git version 1.8.3.1

# 安装maven,地址: https://archive.apache.org/dist/maven
wget https://archive.apache.org/dist/maven/maven-3/3.8.6/binaries/apache-maven-3.8.6-bin.tar.gz
mkdir /opt/maven
tar xzvf apache-maven-3.8.6-bin.tar.gz -C /opt/maven

vim ~/.bash_profile
# 内容 BEGIN
export M2_HOME=/opt/maven/apache-maven-3.8.6
export PATH=$M2_HOME/bin:$PATH
# 内容 END
source ~/.bash_profile

[root@jenkins ~]# mvn -v
Apache Maven 3.8.6 (84538c9988a25aec085021c365c560670ad80f63)
Maven home: /opt/maven/apache-maven-3.8.6
Java version: 11.0.21, vendor: Oracle Corporation, runtime: /opt/java/jdk-11.0.21
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "5.11.12-300.el7.aarch64", arch: "aarch64", family: "unix"

vim /opt/maven/apache-maven-3.8.6/conf/settings.xml
<localRepository>/opt/maven/repo</localRepository>

<mirrors>
	<!--<mirror>
	      <id>maven-default-http-blocker</id>
	      <mirrorOf>external:http:*</mirrorOf>
	      <name>Pseudo repository to mirror external repositories initially using HTTP.</name>
      <url>http://0.0.0.0/</url>
      <blocked>true</blocked>
    </mirror>-->
    <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>

2.1 下载安装Pipeline插件

下载安装Pipeline插件

2.2 Jenkins 安装插件 GitLab Plugin、Maven Integration Plugin

GitLab Plugin
在这里插入图片描述

2.3 服务器编写测试脚本

[root@jenkins ~]# echo "echo "hello world"" >demo.sh
[root@jenkins ~]# chmod +x demo.sh
[root@jenkins ~]# ll demo.sh
-rwxr-xr-x. 1 root root 16 113 16:28 demo.sh

2.4 Jenkins创建自由风格项目并填写构建脚本

创建Jenkins项目
立即构建

2.5 配置钩子脚本

在这里插入图片描述

# 服务器执行安装openssl
yum install -y openssl
# 生成ssl令牌 -> d22f1292ecf0a36007ab
openssl rand -hex 10

peizhi

2.6 GitLab创建项目添加钩子脚本

在这里插入图片描述
在这里插入图片描述
保存报错
在这里插入图片描述
解决:Settings → Network → Outbound requests
在这里插入图片描述
再次添加hooks
在这里插入图片描述

在这里插入图片描述
报错
在这里插入图片描述
解决
添加token
在这里插入图片描述
还不行,去掉Jenkins系统设置里的 Enable authentication for ‘/project’ end-point
ok
自动构建成功
在这里插入图片描述

三、基于Jenkins、Harbor、GitLab自动部署SpringBoot项目到K8S

3.1 创建项目

springboot-demo

3.2 配置Jenkins

参考2.4~2.6配置即可,最终验证钩子返回200 OK
Manage Jenkins → Tools
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

3.3 配置Harbor

添加用户Jenkins
在这里插入图片描述
添加项目
在这里插入图片描述

项目添加成员
在这里插入图片描述

3.4 Jenkins和K8S Master做免密

# jenkins, 若有可不执行
ssh-keygen -t rsa -P "" -f ~/.ssh/id_rsa

[root@jenkins ~]# cat ~/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAwfUH1Oa1e3e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TsYx
SDZ52rszxIboaqQMTkhQUXndyVg6S37xkIf3RUhG8bnsIhvzU3M99ZnzJ1ZtR8Fs
N5dmQ5OuxHq1n1u3T5ztQCc5Kv3ktGGsyBvqe6xvx3Yl4Boj63CXrRpHPmSwFH1y
VTXjbU7QQBN/WhQZuShtWQKBgFasGVo9h7hiDA0oGJLxkOgNFdZTmjX85jUIA5bl
f06DE6HtnLtIWNLOjJkNk9jxw06kkNrZFr5tuLykAK5CC3oDgjzUBC6sJSWpGmHl
lFUKQ8Yz+FJuMtuumEaO5oq+xK37r337v1RZwU//F2FPRGHdkWyfwP4RjkWD5Kgb
2eppAoGADtbLzvT4YGMGBSnW2fpROI1sy7xIhMyzyVlHj9Il79qGZIZF5mkzt6io
vVm9f4Jgssn1PPSsKuUSN8HjVYMn12/+eGQB0S1P1dgRSs+RIdbnSe0ZWvy9lup4
Ytgzg5pfy+8i/PuodwYg/5AfQrxziSzvdFgWiWlCfgm566005Ws=
-----END RSA PRIVATE KEY-----

[root@jenkins ~]# ssh-copy-id 10.211.55.201
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '10.211.55.201 (10.211.55.201)' can't be established.
ECDSA key fingerprint is SHA256:ZAzP64mdrYfOM1o3bwXlqY1JFOiNsbAbiHnWKv1ogLo.
ECDSA key fingerprint is MD5:d9:12:05:84:0e:79:7c:5f:6b:13:a2:97:6f:cb:a7:72.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@10.211.55.201's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '10.211.55.201'"
and check to make sure that only the key(s) you wanted were added.

# 验证
[root@jenkins ~]# ssh root@10.211.55.201
Last login: Sun Jan  7 13:03:41 2024 from 10.211.55.2
[root@master01 ~]#

3.5 Jenkins登录Harbor

# 修改docker配置文件,添加Harbor地址
vim /etc/docker/daemon.json

# 内容BEGIN
#insecure-registries表示后面的registry地址为非安全的.在master和node都要加,不然拉取不了镜像
"insecure-registries":["10.211.55.20"]
# 内容END

sudo systemctl daemon-reload && sudo systemctl restart docker

# 测试登录, harbor关闭防火墙 systemctl stop firewalld && systemctl disable firewalld
docker login harbor.zhinian.com -u jenkins -p Harbor@123456

遇到问题
在这里插入图片描述
解决

[root@jenkins ~]# docker login harbor.zhinian.com -u jenkins -p Harbor@123456
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

3.6 Jnekins配置-继续

问题:控制台输出报错

Started by GitLab push by 王小虎
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/springboot-demo
[springboot-demo] $ /bin/sh -xe /tmp/jenkins7820913899171498991.sh
+ pwd
/var/lib/jenkins/workspace/springboot-demo
+ ls
+ mvn clean package -Dmaven.test.skip=true
/tmp/jenkins7820913899171498991.sh:行4: mvn: 未找到命令
Build step 'Execute shell' marked build as failure
Finished: FAILURE

解决:

ln -s /opt/maven/apache-maven-3.8.6/bin/mvn /usr/bin/mvn

Build Steps

pwd
ls
git --version
rm -rf springboot-demo && git clone git@gitlab.zhinian.com:opit/springboot-demo.git
cd springboot-demo && mvn clean package -Dmaven.test.skip=true
docker login harbor.zhinian.com -u jenkins -p Harbor@123456
docker build -t springboot-demo:latest .
docker tag springboot-demo:latest harbor.zhinian.com/opit/springboot-demo:latest
docker push harbor.zhinian.com/opit/springboot-demo:latest && docker rmi springboot-demo:latest && docker rmi harbor.zhinian.com/opit/springboot-demo:latest
scp -r script/springboot-demo.yaml root@10.211.55.201:~/kube-conf/
ssh 10.211.55.201 'kubectl apply -f /root/kube-conf/springboot-demo.yaml'
ssh 10.211.55.201 'kubectl get pod -n prod'

3.7 提交代码进行测试

# 提交并Push到仓库
git commit && git push ... after

# 自动构建部署
1.jenkins auto build
2.maven build success
3.docker build success
4.docker push harbor success
5.kubectl apply success

在这里插入图片描述
在这里插入图片描述

[root@master01 kube-conf]# kubectl get po springboot-demo-deploy-c4c5cf4d8-bs6k7 -n prod
NAME                                     READY   STATUS    RESTARTS   AGE
springboot-demo-deploy-c4c5cf4d8-bs6k7   1/1     Running   1          6m7s

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、基于GitLab的CICD自动构建部署

Waiting…


  • https://cloud.tencent.com/developer/article/1444269
  • https://zhuanlan.zhihu.com/p/583345352
  • https://blog.csdn.net/littlehaes/article/details/104694375
# https://hub.docker.com/_/openjdk/tags?page=6
docker pull openjdk:22-ea-jdk-oraclelinux8
docker tag openjdk:22-ea-jdk-oraclelinux8 java:8
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

只年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值