Jenkins+Gitlabs+Maven+Springcloud(piggy)基于k8s部署

Jenkins+Gitlabs+Maven+Springcloud(piggy)基于k8s部署

压缩包CICD_Offline.tar 上传到master 节点/root 目录下并解压到/opt 目录。

---起始步骤

1.kubectl get nodes

kubectl get cs

#检查各个节点的状态信息

0

2.tar -zxvf CICD_Offline.tar -C /opt/

#解压tar包到/opt目录下

0

3.cd /opt/ && docker load -i jenkins.tar

#导入镜像

---安装Jenkins

1.mkdir-p jenkins

cd jenkins/

vi docker-compose.yaml

#添加以下内容:

version: '3'
services:
  jenkins:
    container_name: jenkins
    image: jenkins/jenkins:2.262-centos
    volumes:
    - /home/jenkins_home:/var/jenkins_home
    - /var/run/docker.sock:/var/run/docker.sock       #docker引擎
    - /usr/bin/docker:/usr/bin/docker                    #docker命令行
    - /usr/bin/kubectl:/usr/local/bin/kubectl            #k8s命令行
    - /root/.kube:/root/.kube                                #k8s引擎
    ports:
    - 8080:8080                       #jenkins的端口号
    expose:
    - 8080
    - 50000
    privileged: true                   #同意共享卷组
    user: root                           #使用用户root去登录Jenkins
    restart: always

2.docker-compose up -d

#后台启动Jenkins

3.cp -rvf /opt/plugins/* /home/jenkins_home/plugins/

#将离线包提供的Jenkins插件复制到Jenkins中!!!(不添加的话会一直报错,无法启动Jenkins)

4.docker restart jenkins

#重启Jenkins容器,使它的插件能够启动

5.浏览器登录Jenkins(宿主机IP:8080端口)

6.cat /home/jenkins_home/secrets/initialAdminPassword

#查看管理员密码

7.输入密码后来到此界面


8.依次点击“系统管理”的“管理用户”按钮进入用户列表界面

并输入用户信息,点击“新建用户”

9.创建完用户后,退出admin用户,使用新创建的用户进行登录Jenkins

然后点击“系统管理”的“全局安全配置”,在授权策略中,允许匿名用户具有可读权限

10.依次点击 “系统配置”按钮进入系统配置界面,在“Resource root URL”处配置Jenkins URL,完成后点击保存!

---安装gitlab

1.mkdir -p gitlab

cd gitlab/

vi docker-compose.yaml

#添加以下内容:

version: '3'
services: 
  gitlab: 
    container_name: gitlab
    image: gitlab/gitlab-ce:12.9.2-ce.0
    volumes: 
    - /srv/gitlab/config:/etc/gitlab                     #映射配置文件
    - /srv/gitlab/gitlab/logs:/var/log/gitlab          #映射日志文件
    - /srv/gitlab/gitlab/data:/var/opt/gitlab         #映射数据文件
    ports: 
    - 81:80
    - 443:443
    - 1022:22
    privileged: true
    restart: always
    hostname: 192.168.200.131
    environment: 
      TZ: 'Asia/Shanghai'

2.docker-compose up -d

#后台启动gitlab

3.浏览器登录gitlab=宿主机IP:8080端口(Gitlab启动较慢,可以通过 docker logs 查看启动状态)

密码自定义

设置为不小于八位数的

#并且用root用户登录

4.点击“Create a project”,创建项目 springcloud,可见等级选择“Public”

5.push源代码到 gitlab 的 springcloud 项目

yum install -y git
cd /opt/springcloud
rm -rf .git*                #删除git项目
git config --global user.name "Administrator"           #设置全局用户
git config --global user.name "Administrator"
git init                              #初始化git仓库
git remote add origin http://192.168.200.131:81/root/springcloud.git           #添加远程分支
git add .                  #添加当前文件
git commit -m "Initial commit"               #给此仓库打上标签
git push -u origin master                    #推送分支

---配置Jenkins连接gitlab

1.点击管理区域的扳手图标

点击左侧导航栏的“Settings”→“Network”,设置“Outbound requests”,勾选”Allowrequests to the local network fromweb hooks and services“

并且勾选Save changes

2.创建 Gitlab API Token

点击 Gitlab 用户头像图标,并点击Settings

点击左侧导航栏的“Access Tokens”添加token,点击“Create personal accesstoken”生成 Token

复制 Token,后面配置 Jenkins 时会用到


3.登录 Jenkins 首页,点击“系统管理”→“系统配置”,配置 Gitlab 信息,取消勾选“Enable authentication for'/project' end-point”

点击“添加”→“Jenkins”添加认证信息,将Gitlab API Token 填入

点击“Test Connection”进行测试

---配置Jenkins连接maven

1.将maven包复制到Jenkins容器中,并解压配置

#容器内部
cp -rf /opt/apache-maven-3.6.3-bin.tar.gz /home/jenkins_home/
docker exec -it jenkins bash

#容器内部
tar -zxvf /var/jenkins_home/apache-maven-3.6.3-bin.tar.gz -C .
mv apache-maven-3.6.3/ /usr/local/maven
vi/etc/profile
#在最后一行添加:
export M2_HOME=/usr/local/maven
export PATH=$PATH:$M2_HOME/bin
 
vi /root/.bashrc
#./etc/bashrc在这一行的下面添加:
source /etc/profile

#验证:                         
退出容器重新进入后:mvn -n(验证maven安装是否成功)

2.登录 Jenkins 首页,点击“系统管理”→“全局工具配置”

点击“新增 Maven”,如图所示。取消勾选“自动安装”,填入maven 名称和安装路径,配置完成后点击“保存”

---配置CI/CD

1.登录 Jenkins 首页,点击左侧导航栏“新建任务”,如图所示,选择构建一个流水线,并选择触发器

2.在流水线中,先去点击“流水线语法”,去生成对应的凭证出来

选择git:Git类型


3.写好流水线脚本,并点击“保存”

🔺脚本内容如下:

本内容如下:

node{
	stage("git clone"){
	    git credentialsId: '2a133168-04fe-4ec7-9d41-6e876146d3bd', url: 'http://192.168.200.131:81/root/springcloud.git'      #去Jenkins中获取(git:Git流水线语法)
	}

	stage("maven build"){
		sh '''/usr/local/maven/bin/mvn clean package -DskipTests -f /var/jenkins_home/workspace/springcloud'''
	}

	stage("image build"){
		sh '''echo $BUILD_ID
		docker build -t 192.168.200.131/springcloud/gateway:$BUILD_ID -f /var/jenkins_home/workspace/springcloud/gateway/Dockerfile /var/jenkins_home/workspace/springcloud/gateway
		
		docker build -t 192.168.200.131/springcloud/config:$BUILD_ID -f /var/jenkins_home/workspace/springcloud/config/Dockerfile /var/jenkins_home/workspace/springcloud/config
		'''
	}

	stage("upload image"){
		sh '''docker login 192.168.200.131 -uadmin -pHarbor12345
		docker push 192.168.200.131/springcloud/gateway:$BUILD_ID
		docker push 192.168.200.131/springcloud/config:$BUILD_ID
		'''
	}

	stage("deploy"){
		sh 'sed -i "s/sqshq\\/piggymetrics-gateway/192.168.200.131\\/springcloud\\/gateway:$BUILD_ID/g" /var/jenkins_home/workspace/springcloud/yaml/deployment/gateway-deployment.yaml'
		
		sh 'sed -i "s/sqshq\\/piggymetrics-config/192.168.200.131\\/springcloud\\/config:$BUILD_ID/g" /var/jenkins_home/workspace/springcloud/yaml/deployment/config-deployment.yaml'
		
		sh 'kubectl create ns springcloud'
		
		sh 'kubectl apply -f /var/jenkins_home/workspace/springcloud/yaml/deployment/gateway-deployment.yaml --kubeconfig=/root/.kube/config'
		
		sh 'kubectl apply -f /var/jenkins_home/workspace/springcloud/yaml/deployment/config-deployment.yaml --kubeconfig=/root/.kube/config'
		
		sh 'kubectl apply -f /var/jenkins_home/workspace/springcloud/yaml/svc/gateway-svc.yaml --kubeconfig=/root/.kube/config'
		
		sh 'kubectl apply -f /var/jenkins_home/workspace/springcloud/yaml/svc/config-svc.yaml --kubeconfig=/root/.kube/config'
	}
}

4.去Harbor仓库新建一个springcloud项目,并且公开

5.配置Webhook(去springcloud项目的Setting配置)用于推送测试

URL为http://192.168.200.131:8080/project/springcloud/

并且取消勾选SSL选项!!!


6.点击“Add webhook”,然后去测试“Push events”

---触发CI/CD

1.docker cp /opt/repository/ jenkins:/root/.m2

docker restart jenkins

#先在Jenkins容器内部创建一个/root/.m2 的目录

2.cd /opt/springcloud/

git add .

git commit -m "Intiial commit"

git push -u origin master #重新推送本地的文件!!!

🔺总结:

开始自动构建:

访问:宿主机IP:30010

posted @ 2023-08-04 17:08  Ghost3  阅读(125)  评论(1编辑  收藏  举报