欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jenkins + Docker + SpringCloud 微服务持续集成(一)

程序员文章站 2024-02-03 19:12:10
...

Jenkins + Docker + SpringCloud 微服务持续集成环境部署,docker 测试

本章内容不适合 初学者看,也看不懂


一、jenkins + Docker + SpringCloud CI\CD\CD 流程

Jenkins + Docker + SpringCloud 微服务持续集成(一)

流程简介:

  1. 开发人员将代码上传至 Gitlab仓库

  2. jenkins 从 Gitlab 中利用webhook拉取源码,编译打成 jar 包,然后构建成 docker 镜像,将镜像上传到 Harbor仓库。

  3. jenkins 发送 SSH 远程命令,让生产服务器到 Harbor 私有仓库拉取镜像到本地,然后创建容器。

  4. 最后,用户可以访问到容器。


二、Jenkins + Docker + SpringCloud 环境

服务器名称 IP地址 软件环境
代码服务器(GitLab) 192.168.168.12 git,python
持续集成服务器(jenkins) 192.168.168.13 maven+JDK+Docker
Docker仓库(Harbor) 192.168.168.14 Docker+Harbor
生产部署服务器(SpringCloud) 192.168.168.15 Docker+springcloud

JDK1.8

java development kit java 开发工具包
[aaa@qq.com ~]# tar -zxvf jdk-8u201-linux-x64.tar.gz -C /usr/src
[aaa@qq.com ~]# mv /usr/src/jdk1.8.0_201/  /usr/local/java 
[aaa@qq.com ~]# echo 'export JAVA_HOME=/usr/local/java
export JRE_HOME=/usr/local/java/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin' >> /etc/profile
[aaa@qq.com ~]# rm -rf /usr/bin/java
[aaa@qq.com ~]# source /etc/profile
[aaa@qq.com ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

maven

[aaa@qq.com ~]# tar -zxvf apache-maven-3.6.0-bin.tar.gz -C /usr/src/
[aaa@qq.com ~]# mv /usr/src/apache-maven-3.6.0/  /usr/local/maven
[aaa@qq.com ~]# echo 'export PATH=$PATH:/usr/local/maven/bin' >> /etc/profile
[aaa@qq.com ~]# source /etc/profile
[aaa@qq.com ~]# mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/local/java/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-862.el7.x86_64", arch: "amd64", family: "unix"


Ⅰ、设置 GitLab 代码服务器

[aaa@qq.com ~]# yum -y install git
[aaa@qq.com ~]# yum -y install policycoreutils-python 
[aaa@qq.com ~]# rpm -ivh gitlab-ce-12.3.0-ce.0.el7.x86_64.rpm 
[aaa@qq.com ~]# vim /etc/gitlab/gitlab.rb
---
    external_url 'http://192.168.168.10'
[aaa@qq.com ~]# gitlab-ctl reconfigure    
[aaa@qq.com ~]# gitlab-ctl start    

创建一个用户组:itheima_group

创建微服务项目(私有):tensquare_back

创建前端 web 项目(私有):tensquare_front
Jenkins + Docker + SpringCloud 微服务持续集成(一)
Jenkins + Docker + SpringCloud 微服务持续集成(一)


Ⅱ、设置 Harbor 企业级仓库

搭建 Harbor 需要 docker-compose:搭建Harbor链接

docker

yum -y install yum-utils device-mapper-persistent-data lvm2

yum-config-manager --add-repo=http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
 
yum makecache fast
yum -y install docker-ce

mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://m41udh8b.mirror.aliyuncs.com"]
}
EOF

Harbor

[aaa@qq.com ~]# tar zxvf harbor-offline-installer-v1.6.2.tgz -C /usr/local/
[aaa@qq.com ~]# mv docker-compose  /usr/local/bin/
[aaa@qq.com ~]# chmod +x /usr/local/bin/docker-compose 
[aaa@qq.com ~]# cd /usr/local/harbor/
[aaa@qq.com harbor]# vim harbor.cfg 
---
hostname = 192.168.168.14   	#修改成自己的本机IP地址

[aaa@qq.com]# ./install.sh

启动 harbor:docker-compose start
访问 harbor -web :192.168.168.14

创建私有项目"tensquare",创建并设置管理员用户

用户名:shm

密码:QQ1136265636.com

Jenkins + Docker + SpringCloud 微服务持续集成(一)


Ⅲ、设置 jenkins 服务器

JDK 1.8 ln -s /usr/local/java/bin/java /usr/bin

maven

[aaa@qq.com ~]# vim /usr/local/maven/conf/settings.xml
---在 159 行 </mirrors>上添加以下内容:
        <mirror>
         <id>aliyun maven</id>
         <name>aliyun</name>
         <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
         <mirrorOf>central</mirrorOf>
        </mirror>
  </mirrors>

SonarQube7.8 + MySQL5.7.29搭建 SonarQube代码审查链接

docker

[aaa@qq.com ~]# vim /etc/yum.repos.d/ali-docker.repo
[ali.docker]
name=mirrors.aliyun.com.docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[aaa@qq.com ~]# yum -y install docker-ce
[aaa@qq.com ~]# mkdir -p /etc/docker
[aaa@qq.com ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://m41udh8b.mirror.aliyuncs.com"]
}
EOF

[aaa@qq.com ~]# vim /usr/lib/systemd/system/docker.service  
---
	ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.168.14

[aaa@qq.com ~]# systemctl start docker &&  systemctl enable docker

连接 Harbor

docker pull hello-world
docker tag hello-world:latest  192.168.168.14/tensquare/hello-world:v1
docker login -u shm -p QQ1136265636.com 192.168.168.14
docker push 192.168.168.14/tensquare/hello-world


Ⅳ、设置生产 docker 服务器

JDK + MAVEN

[aaa@qq.com ~]# java -version
java version "1.8.0_201"
Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

[aaa@qq.com ~]# mvn -v
Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-25T02:41:47+08:00)
Maven home: /usr/local/maven
Java version: 1.8.0_201, vendor: Oracle Corporation, runtime: /usr/local/java/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1062.el7.x86_64", arch: "amd64", family: "unix"

docker

[aaa@qq.com ~]# vim /etc/yum.repos.d/ali-docker.repo
[ali.docker]
name=mirrors.aliyun.com.docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg

[aaa@qq.com ~]# yum -y install docker-ce
[aaa@qq.com ~]# mkdir -p /etc/docker
[aaa@qq.com ~]# tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://m41udh8b.mirror.aliyuncs.com"]
}
EOF

[aaa@qq.com ~]# vim /usr/lib/systemd/system/docker.service  
---
	ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 192.168.168.14

[aaa@qq.com ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf 
[aaa@qq.com ~]# sysctl -p

[aaa@qq.com ~]# systemctl start docker &&  systemctl enable docker

连接 Harbor

docker login 192.168.168.14
用户:shm
密码:QQ1136265636.com
docker pull 192.168.168.14/tensquare/hello-world:v1


三、测试 整个 docker 持续集成 环境

测试 能否生成 docker 镜像,上传到 harbor 仓库,再下载到 docker 生产服务器。


1、spring cloud 设置 eureka注册中心,并生成 jar 包

安装一个测试用的 spring cloude

[aaa@qq.com ~]# yum -y install  git
[aaa@qq.com ~]# yum makecache fast
[aaa@qq.com ~]# git clone https://github.com/luojunyong/spring-cloud-examples.git

[aaa@qq.com src]# mv spring-cloud-examples/spring-cloud-eureka/   /usr/local/eureka
[aaa@qq.com src]# cd /usr/local/eureka/spring-cloud-eureka

1、#修改 eureka 注册中心配置文件
[aaa@qq.com eureka]# vim src/main/resources/application.properties 
--- 修改成以下内容:
spring.application.name=spring-cloud-eureka	#名字

server.port=8000				#端口
eureka.client.register-with-eureka=true		#当前应用是否要进行注册至注册中心
eureka.client.fetch-registry=true		#当前应用是否从注册中心获取信息

eureka.client.serviceUrl.defaultZone=http://192.168.168.13:8000/eureka/
#本机IP地址和端口号

2、#编译打包,生成 jar 包。
[aaa@qq.com spring-cloud-eureka]# mvn clean package
[aaa@qq.com spring-cloud-eureka]# ls target/
---
spring-cloud-eureka-0.0.1-SNAPSHOT.jar


2、用 dockerfile 将 jar包 生成镜像

[aaa@qq.com target]# mkdir /dockerfile
[aaa@qq.com target]# cp spring-cloud-eureka-0.0.1-SNAPSHOT.jar /dockerfile/

制作 jar 包的 dockerfile文件

FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} eureka.jar
EXPOSE 8000
ENTRYPOINT ["java","-jar","/eureka.jar"]

构建镜像的同时,为 JAR_FILE 赋值:
[aaa@qq.com dockerfile]# docker build --build-arg JAR_FILE=spring-cloud-eureka-0.0.1-SNAPSHOT.jar -t eureka:v1 .

docker build: 构建
--build-arg:赋值
JAR_FILE:引用变量=变量值(eureka的 jar包)
-t :指定构建镜像的版本(tag标签) eureka:v1 
.  :在当前目录下构建

查看本地仓库:docker images

运行此镜像:docker run -itd -p 8000:8000 eureka:v1

查看容器日志,通过 容器ID :docker logs -f ID号

Jenkins + Docker + SpringCloud 微服务持续集成(一)


3、推送 Harbor 仓库

①、给镜像打上标签

docker tag eureka:v1 192.168.168.14/tensquare/eureka:v1

②、推送镜像

docker login -u 用户名 -p 密码 192.168.168.14:85 先登录 Harbor

docker push 192.168.168.14/tensquare/eureka:v1

4、docker 生产服务器从 Harbor 下载

docker login -u 用户名 -p 密码 192.168.168.14:85 先登录 Harbor

docker pull 192.168.168.14/tensquare/eureka:v1

5、测试 gitlab - jenkins 拉取代码

这一步可以不做,没什么意义

gitlab

Jenkins + Docker + SpringCloud 微服务持续集成(一)

pipeline - SCM - Jenkinsfile

pipeline {
    agent any      

    stages {        			//项目构建
        stage('pull code') {    	//拉取代码 
            steps {             	//具体实施步骤
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '218fb747-c605-4fa0-80d3-c322cd42d7a9', url: 'aaa@qq.com:itheima_group/test.git']]])
            }
        }
    }
}

Jenkins + Docker + SpringCloud 微服务持续集成(一)

生产环境 完整持续化集成步骤

Pipeline 流水线:

GitLab

  1. 项目上传到 Gitlab

jenkins集成步骤

  1. 从 Gitlab拉取项目源代码
  2. 提交到 SonarQube 代码审查
  3. 生成 jar包
  4. 使用 Dockerfile 将 jar 包制作成镜像
  5. 上传到 Harbor 仓库

部署上线步骤

  1. 生产服务器拉取镜像和发布应用
  2. 部署前端服务