文章目录
- 一、企业业务代码发布方式
-
- [1.1 传统方式](#1.1 传统方式)
- [1.2 容器化方式](#1.2 容器化方式)
- 二、企业业务代码发布逻辑图
- 三、企业业务代码发布工具及流程图
-
- [3.1 工具](#3.1 工具)
- [3.2 流程图](#3.2 流程图)
- 四、企业业务代码发布系统环境部署
-
- [4.1 主机规划](#4.1 主机规划)
- [4.2 主机准备](#4.2 主机准备)
-
- [4.2.1 主机名配置](#4.2.1 主机名配置)
- [4.2.2 主机IP地址配置](#4.2.2 主机IP地址配置)
- [4.2.3 主机名与IP地址解析配置](#4.2.3 主机名与IP地址解析配置)
- [4.2.4 主机安全设置](#4.2.4 主机安全设置)
- [4.2.5 主机时间同步](#4.2.5 主机时间同步)
- [4.3 主机中工具安装](#4.3 主机中工具安装)
-
- [4.3.1 dev主机](#4.3.1 dev主机)
- [4.3.2 gitlab-server主机](#4.3.2 gitlab-server主机)
-
- [4.3.2.1 获取YUM源](#4.3.2.1 获取YUM源)
- [4.3.2.2 gitlab-ce安装](#4.3.2.2 gitlab-ce安装)
- [4.3.2.3 gitlab-ce配置](#4.3.2.3 gitlab-ce配置)
- [4.3.2.4 启动gitlab-ce](#4.3.2.4 启动gitlab-ce)
- [4.3.2.5 访问gitlab-ce](#4.3.2.5 访问gitlab-ce)
- [4.3.3 jenkins-server主机](#4.3.3 jenkins-server主机)
-
- [4.3.3.1 jdk安装](#4.3.3.1 jdk安装)
- [4.3.3.2 jenkins安装](#4.3.3.2 jenkins安装)
-
- [4.3.3.2.1 安装](#4.3.3.2.1 安装)
- [4.3.3.2.2 jenkins配置](#4.3.3.2.2 jenkins配置)
- [4.3.3.2.3 jenkins启动](#4.3.3.2.3 jenkins启动)
- [4.3.3.2.4 jenkins访问](#4.3.3.2.4 jenkins访问)
- [4.3.3.2.5 jenkins初始化配置](#4.3.3.2.5 jenkins初始化配置)
- [4.3.3.2.6 数据清除并备份](#4.3.3.2.6 数据清除并备份)
- [4.3.3.3 git安装](#4.3.3.3 git安装)
- [4.3.3.4 maven安装](#4.3.3.4 maven安装)
-
- [4.3.3.4.1 获取maven安装包](#4.3.3.4.1 获取maven安装包)
- [4.3.3.4.2 maven安装](#4.3.3.4.2 maven安装)
- [4.3.3.5 docker安装](#4.3.3.5 docker安装)
- [4.3.4 harbor-server主机](#4.3.4 harbor-server主机)
-
- [4.3.4.1 docker安装](#4.3.4.1 docker安装)
- [4.3.4.2 docker-compose安装](#4.3.4.2 docker-compose安装)
-
- [4.3.4.2.1 获取docker-compose文件](#4.3.4.2.1 获取docker-compose文件)
- [4.3.4.2.2 docker-compose安装及测试](#4.3.4.2.2 docker-compose安装及测试)
- [4.3.4.3 harbor部署](#4.3.4.3 harbor部署)
-
- [4.3.4.3.1 harbor部署文件获取](#4.3.4.3.1 harbor部署文件获取)
- [4.3.4.3.2 harbor部署](#4.3.4.3.2 harbor部署)
- [4.3.5 web-server](#4.3.5 web-server)
- [4.4 工具集成配置](#4.4 工具集成配置)
-
- [4.4.1 配置docker主机使用harbor](#4.4.1 配置docker主机使用harbor)
-
- [4.4.1.1 jenkins-server](#4.4.1.1 jenkins-server)
- [4.4.1.2 harbor-server](#4.4.1.2 harbor-server)
- [4.4.1.3 web-server](#4.4.1.3 web-server)
- [4.4.2 配置jenkins使用docker](#4.4.2 配置jenkins使用docker)
- [4.4.3 密钥配置](#4.4.3 密钥配置)
-
- [4.4.3.1 dev主机至gitlab-ce](#4.4.3.1 dev主机至gitlab-ce)
-
- [4.4.3.1.1 dev主机生成密钥对](#4.4.3.1.1 dev主机生成密钥对)
- [4.4.3.1.2 添加公钥至gitlab-ce](#4.4.3.1.2 添加公钥至gitlab-ce)
- [4.4.3.2 jenkins-server主机至gitlab-ce](#4.4.3.2 jenkins-server主机至gitlab-ce)
-
- [4.4.3.2.1 在jenkins-server生成密钥对](#4.4.3.2.1 在jenkins-server生成密钥对)
- [4.4.3.2.2 添加公钥至gitlab-ce](#4.4.3.2.2 添加公钥至gitlab-ce)
- [4.4.3.3 配置jenkins-sever主机的私钥到凭据列表](#4.4.3.3 配置jenkins-sever主机的私钥到凭据列表)
- [4.5 jenkins插件安装](#4.5 jenkins插件安装)
-
- [4.5.1 maven integration](#4.5.1 maven integration)
- [4.5.2 git parameter](#4.5.2 git parameter)
- [4.5.3 gitlab](#4.5.3 gitlab)
- [4.5.4 Generic Webhook Trigger](#4.5.4 Generic Webhook Trigger)
- [4.5.5 ssh](#4.5.5 ssh)
- [4.6 jenkins全局工具配置](#4.6 jenkins全局工具配置)
-
- [4.6.1 JDK配置](#4.6.1 JDK配置)
- [4.6.2 Git配置](#4.6.2 Git配置)
- [4.6.3 Maven配置](#4.6.3 Maven配置)
- [4.7 jenkins系统配置](#4.7 jenkins系统配置)
-
- [4.7.1 添加jenkins-server访问web-server凭据](#4.7.1 添加jenkins-server访问web-server凭据)
- [4.7.2 配置ssh协议连接主机](#4.7.2 配置ssh协议连接主机)
- 五、企业业务代码项目发布
-
- [5.1 数据库管理系统部署 mariadb及创建项目数据库](#5.1 数据库管理系统部署 mariadb及创建项目数据库)
- [5.2 项目代码获取](#5.2 项目代码获取)
- [5.3 项目代码修改](#5.3 项目代码修改)
- [5.4 项目代码上传到gitlab](#5.4 项目代码上传到gitlab)
- [5.5 构建项目运行基础应用容器镜像](#5.5 构建项目运行基础应用容器镜像)
-
- [5.5.1 创建项目目录](#5.5.1 创建项目目录)
- [5.5.2 生成Dockerfile文件](#5.5.2 生成Dockerfile文件)
- [5.5.3 使用docker build构建容器镜像](#5.5.3 使用docker build构建容器镜像)
- [5.5.4 推送容器镜像至harbor容器镜像仓库](#5.5.4 推送容器镜像至harbor容器镜像仓库)
- [5.5.5 验证容器镜像可用性](#5.5.5 验证容器镜像可用性)
- [5.6 项目构建及发布](#5.6 项目构建及发布)
-
- [5.6.1 项目构建及发布步骤](#5.6.1 项目构建及发布步骤)
- [5.6.2 创建项目任务](#5.6.2 创建项目任务)
- [5.7 存在的问题及解决](#5.7 存在的问题及解决)
一、企业业务代码发布方式
1.1 传统方式
- 以物理机或虚拟机为颗粒度部署
- 部署环境比较复杂,需要有先进的自动化运维手段
- 出现问题后重新部署成本大,一般采用集群方式部署
- 部署后以静态方式展现
1.2 容器化方式
- 以容器为颗粒度部署
- 部署方式简单,启动速度快
- 一次构建可到处运行
- 出现故障后,可随时恢复
- 可同时部署多套环境(测试、预发布、生产环境等)
二、企业业务代码发布逻辑图
三、企业业务代码发布工具及流程图
3.1 工具
序号 | 工具 | 工具用途 |
---|---|---|
1 | git | 用于提交业务代码或克隆业务代码仓库 |
2 | gitlab | 用于存储业务代码 |
3 | jenkins | 用于利用插件完成业务代码编译、构建、推送至Harbor容器镜像仓库及项目部署 |
4 | tomcat | 用于运行JAVA业务代码 |
5 | maven | 用于编译业务代码 |
6 | harbor | 用于存储业务代码构建的容器镜像存储 |
7 | docker | 用于构建容器镜像,部署项目 |
3.2 流程图
本次部署Java代码包。
四、企业业务代码发布系统环境部署
4.1 主机规划
序号 | 主机名 | 主机IP | 主机功能 | 软件 |
---|---|---|---|---|
1 | dev | 192.168.150.113 | 开发者 项目代码 solo | git |
2 | gitlab-server | 192.168.150.114 | 代码仓库 | gitlab-ce |
3 | jenkins-server | 192.168.150.115 | 编译代码、打包镜像、项目发布 | jenkins、docker、git |
4 | web-server | 192.168.150.116 | 运行容器,项目上线 | docker |
5 | harbor-server | 192.168.150.117 | 存储容器镜像 | harbor、docker |
4.2 主机准备
4.2.1 主机名配置
shell
hostnamectl set-hostname xxx
根据主机规划实施配置
4.2.2 主机IP地址配置
编辑网卡固定IP地址vim /etc/sysconfig/network-scripts/ifcfg-ens33
:
powershell
TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="none" # 配置为静态IP
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="ec87533a-8151-4aa0-9d0f-1e970affcdc6"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.150.113" # 替换成对应的IP地址
PREFIX="24"
GATEWAY="192.168.150.2"
DNS1="192.168.150.2"
4.2.3 主机名与IP地址解析配置
设置域名解析vim /etc/hosts
,添加如下内容
powershell
192.168.150.113 dev
192.168.150.114 gitlab-server
192.168.150.115 jenkins-server
192.168.150.116 web-server
192.168.150.117 harobr-server
4.2.4 主机安全设置
关闭防火墙
shell
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --state
关闭SELinux
shell
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
reboot # 重启主机
sestatus
4.2.5 主机时间同步
设置定时任务同步时间
shell
crontab -e
添加如下内容
shell
0 */1 * * * ntpdate time1.aliyun.com
查看任务
shell
crontab -l
4.3 主机中工具安装
4.3.1 dev主机
下载项目及上传代码至代码仓库
shell
yum -y install git
4.3.2 gitlab-server主机
4.3.2.1 获取YUM源
添加镜像仓库
shell
vim /etc/yum.repos.d/gitlab.repo
添加如下内容
powershell
[gitlab]
name=gitlab-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
enabled=1
gpgcheck=0
4.3.2.2 gitlab-ce安装
安装
shell
yum -y install gitlab-ce
4.3.2.3 gitlab-ce配置
修改配置文件
shell
vim /etc/gitlab/gitlab.rb
改成自己的IP地址能访问就可以了
powershell
external_url 'http://192.168.150.114'
4.3.2.4 启动gitlab-ce
重新配置启动并查看状态
shell
gitlab-ctl reconfigure
gitlab-ctl status
4.3.2.5 访问gitlab-ce
查看生成的初始密码
powershell
[root@gitlab-server ~]# cat /etc/gitlab/initial_root_password | grep "Password:"
Password: WLf1lFd6JIGHru6o87T+H9xORMtagUGoDmjzIjODQV4=
4.3.3 jenkins-server主机
4.3.3.1 jdk安装
准备的安装包
powershell
[root@jenkins-server ~]# ls
jdk-17.0.13_linux-x64_bin.rpm
直接rpm安装,默认存放路径为/usr/lib/jvm/jdk-17.0.13-oracle-x64
powershell
rpm -ivh jdk-17.0.13_linux-x64_bin.rpm
验证
powershell
[root@jenkins-server ~]# java -version
java version "17.0.13" 2024-10-15 LTS
Java(TM) SE Runtime Environment (build 17.0.13+10-LTS-268)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.13+10-LTS-268, mixed mode, sharing)
4.3.3.2 jenkins安装
4.3.3.2.1 安装
相关手册:https://www.jenkins.io/doc/book/installing/linux/
按照提示操作
shell
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
觉得慢使用阿里云镜像仓库
shell
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装Jenkins
shell
yum -y install fontconfig # Jenkins的一个所需依赖,用于配置和管理字体
yum -y install jenkins # 安装Jenkins
4.3.3.2.2 jenkins配置
如果是使用以下命令直接安装的JDK是能被Jenkins直接找到的
shell
sudo yum install java-17-openjdk
如果我们使用的是自定义下载的jdk需要对Jenkins中的配置作替换
编辑配置文件
shell
vim /lib/systemd/system/jenkins.service
对如下内容进行修改:
shell
# The Java home directory. When left empty, JENKINS_JAVA_CMD and PATH are consulted.
Environment="JAVA_HOME=/usr/lib/jvm/jdk-17.0.13-oracle-x64"
# The Java executable. When left empty, JAVA_HOME and PATH are consulted.
Environment="JENKINS_JAVA_CMD=/usr/lib/jvm/jdk-17.0.13-oracle-x64/bin/java"
4.3.3.2.3 jenkins启动
启动并设置开机自启动
shell
sudo systemctl enable jenkins
sudo systemctl start jenkins
如果由于端口正在使用而导致 Jenkins 启动失败,请运行systemctl edit jenkins
并添加以下内容:
powershell
[Service]
Environment="JENKINS_PORT=8081"
4.3.3.2.4 jenkins访问
Jenkins主页会提示为了确保管理员安全地安装 Jenkins,密码已写入到日志中,让我们使用如下命令查看
shell
cat /var/lib/jenkins/secrets/initialAdminPassword
4.3.3.2.5 jenkins初始化配置
4.3.3.2.6 数据清除并备份
如果出现问题可以直接清除数据并备份让服务重新可用
shell
# 停止服务运行
sudo systemctl stop jenkins
# 备份
sudo tar -czf jenkins-backup.tar.gz /var/lib/jenkins
# 清除数据目录,包括插件和自定义配置
sudo rm -rf /var/lib/jenkins
# 重新创建
sudo mkdir /var/lib/jenkins
sudo chown jenkins:jenkins /var/lib/jenkins
# 启动服务
sudo systemctl start jenkins
4.3.3.3 git安装
安装git
shell
yum -y install git
4.3.3.4 maven安装
4.3.3.4.1 获取maven安装包
下载
shell
wget https://dlcdn.apache.org/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.tar.gz
4.3.3.4.2 maven安装
查看下载好的安装包
powershell
[root@jenkins-server ~]# ls
apache-maven-3.9.9-bin.tar.gz
解压
powershell
tar xf apache-maven-3.9.9-bin.tar.gz
移动到对应目录下
shell
mv apache-maven-3.9.9 /usr/local/mvn
配置环境变量
powershell
[root@jenkins-server ~]# vim /etc/profile
......
export JAVA_HOME=/usr/lib/jvm/jdk-17.0.13-oracle-x64
export MAVEN_HOME=/usr/local/mvn
export PATH=${JAVA_HOME}/bin:${MAVEN_HOME}/bin:$PATH
使其生效
shell
source /etc/profile
验证
powershell
[root@jenkins-server ~]# mvn -v
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /usr/local/mvn
Java version: 17.0.13, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-17.0.13-oracle-x64
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.119.1.el7.x86_64", arch: "amd64", family: "unix"
4.3.3.5 docker安装
如果网速较慢可以使用阿里云镜像
shell
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装
shell
yum -y install docker-ce
启动并设置开机自启
shell
systemctl enable docker
systemctl start docker
4.3.4 harbor-server主机
4.3.4.1 docker安装
使用阿里云镜像
shell
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
安装
shell
yum -y install docker-ce
启动并设置开机自启
shell
systemctl enable docker
systemctl start docker
4.3.4.2 docker-compose安装
4.3.4.2.1 获取docker-compose文件
下载
shell
wget https://github.com/docker/compose/releases/download/v2.31.0/docker-compose-linux-x86_64
4.3.4.2.2 docker-compose安装及测试
查看安装包
powershell
[root@harbor-server ~]# ls
docker-compose-linux-x86_64
授予执行权限
shell
chmod +x docker-compose-linux-x86_64
添加到快捷目录下
shell
mv docker-compose-linux-x86_64 /usr/bin/docker-compose
验证
powershell
[root@harbor-server ~]# docker-compose version
Docker Compose version v2.31.0
不用下,docker自带的也行。
4.3.4.3 harbor部署
4.3.4.3.1 harbor部署文件获取
shell
wget https://github.com/goharbor/harbor/releases/download/v2.11.2/harbor-offline-installer-v2.11.2.tgz
4.3.4.3.2 harbor部署
powershell
[root@harbor-server ~]# ls
anaconda-ks.cfg harbor-offline-installer-v2.11.2.tgz
解压到/home
目录下
powershell
tar xf harbor-offline-installer-v2.11.2.tgz -C /home
切换到home
目录:
shell
cd /home
查看资源文件
powershell
[root@harbor-server ~]# cd /home
[root@harbor-server home]# ls
harbor
[root@harbor-server home]# cd harbor/
[root@harbor-server harbor]# ls
common.sh harbor.v2.11.2.tar.gz harbor.yml.tmpl install.sh LICENSE prepare
创建配置文件
shell
mv harbor.yml.tmpl harbor.yml
将harbor.yml
文件进行如下修改,禁用HTTPS服务(仅限测试环境)
yaml
# Configuration file of Harbor
# The IP address or hostname to access admin UI and registry service.
# DO NOT use localhost or 127.0.0.1, because Harbor needs to be accessed by external clients.
hostname: 192.168.150.117 修改
# http related config
http:
# port for http, default is 80. If https enabled, this port will redirect to https port
port: 80
# https related config
# https: 注释
# https port for harbor, default is 443
# port: 443 注释
# The path of cert and key files for nginx
# certificate: /your/certificate/path 注释
# private_key: /your/private/key/path 注释
执行预备脚本
shell
./prepare
执行安装脚本
shell
./install.sh
查看运行的harbor容器
powershell
[root@web-server harbor]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
56aefa69d1f4 goharbor/harbor-jobservice:v2.11.2 "/harbor/entrypoint...." 12 minutes ago Up 12 minutes (healthy) harbor-jobservice
225d0d1d54e8 goharbor/nginx-photon:v2.11.2 "nginx -g 'daemon of..." 12 minutes ago Up 12 minutes (healthy) 0.0.0.0:80->8080/tcp, :::80->8080/tcp nginx
e27e5723b62c goharbor/harbor-core:v2.11.2 "/harbor/entrypoint...." 12 minutes ago Up 12 minutes (healthy) harbor-core
8c43713fabd1 goharbor/redis-photon:v2.11.2 "redis-server /etc/r..." 12 minutes ago Up 12 minutes (healthy) redis
c464cafafca9 goharbor/harbor-registryctl:v2.11.2 "/home/harbor/start...." 12 minutes ago Up 12 minutes (healthy) registryctl
838c0be0311c goharbor/harbor-db:v2.11.2 "/docker-entrypoint...." 12 minutes ago Up 12 minutes (healthy) harbor-db
b2e0b134d7df goharbor/registry-photon:v2.11.2 "/home/harbor/entryp..." 12 minutes ago Up 12 minutes (healthy) registry
78e02bbd2f13 goharbor/harbor-portal:v2.11.2 "nginx -g 'daemon of..." 12 minutes ago Up 12 minutes (healthy) harbor-portal
5f21015e1d3a goharbor/harbor-log:v2.11.2 "/bin/sh -c /usr/loc..." 12 minutes ago Up 12 minutes (healthy) 127.0.0.1:1514->10514/tcp harbor-log
4.3.5 web-server
docker安装,不再解释
shell
wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce
systemctl enable docker
systemctl start docker
4.4 工具集成配置
4.4.1 配置docker主机使用harbor
4.4.1.1 jenkins-server
vim /etc/docker/daemon.json
添加如下配置
json
[root@jenkins-server ~]# cat /etc/docker/daemon.json
{
"insecure-registries": ["http://192.168.150.117"]
}
启动docker
powershell
[root@jenkins-server ~]# systemctl restart docker
测试
powershell
[root@jenkins-server ~]# docker login 192.168.150.117
Username: admin
Password: Harbor12345
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
4.4.1.2 harbor-server
harbor-server主机进行同样配置
powershell
[root@harbor-server harbor]# cat /etc/docker/daemon.json
{
"insecure-registries": ["http://192.168.150.117"]
}
重启docker
powershell
[root@harbor-server harbor]# systemctl restart docker
docker重启后harbor也需要重新启动,注意需要再harbor
目录下
shell
docker compose down
docker compose up -d
4.4.1.3 web-server
同样操作
powershell
[root@web-server ~]# vim /etc/docker/daemon.json
[root@web-server ~]# cat /etc/docker/daemon.json
{
"insecure-registries": ["http://192.168.150.117"]
}
[root@web-server ~]# systemctl restart docker
验证
powershell
[root@web-server ~]# docker login 192.168.150.117
Username: admin
Password:
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
4.4.2 配置jenkins使用docker
在jenkins-server主机上配置,验证系统中是否有jenkins用户
powershell
[root@jenkins-server ~]# grep jenkins /etc/passwd
jenkins:x:997:995:Jenkins Automation Server:/var/lib/jenkins:/bin/false
验证系统中是否有docker用户及用户组
powershell
[root@jenkins-server ~]# grep docker /etc/group
docker:x:993:
添加jenkins用户到docker用户组
powershell
[root@jenkins-server ~]# usermod -G docker jenkins
[root@jenkins-server ~]# grep docker /etc/group
docker:x:993:jenkins
重启jenkins服务
shell
systemctl restart jenkins
4.4.3 密钥配置
4.4.3.1 dev主机至gitlab-ce
4.4.3.1.1 dev主机生成密钥对
powershell
[root@dev ~]# ssh-keygen
4.4.3.1.2 添加公钥至gitlab-ce
powershell
[root@dev ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDc7vWyCdumWNmWOcEv/Z8flvR2A+bWEjqHvX4ugfD4ukH1f7mPSThT3Jb898fnv/pUEpZgvYDz3eNtw8+U5SpEvKJpsynUNL7vpbbViydgUB/QQqhh09wuASlR3CwtQ5YzfvDcggIZqyK2vZC4B/p0840pVBfQANucktAcQJ+sYppKXA+gIn5HW1Eg+3kI2OrIJXQ1FvAhIGstxF/Y/l0VT4ZJ2rIbHRMBVaafNIRa5SWnHh4TYLB3OG2tlhvuwGrXtXbqLipnqHxXTNsKrJW+tOlK1H2JVe2cXTNHpBBUVkExe2iYtojEWy3I2CrLN67QNPfrcK8LS01wILrnij0n root@dev
4.4.3.2 jenkins-server主机至gitlab-ce
4.4.3.2.1 在jenkins-server生成密钥对
powershell
[root@jenkins-server ~]# ssh-keygen
4.4.3.2.2 添加公钥至gitlab-ce
powershell
[root@jenkins-server ~]# cat /root/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCp17Q93yvEXLDMCRHlQopZ3TdFommg5mhe3gb5YSkZfSPfRRqRWEfxIBO8eyEqWyFa5NR60yr6GB4MFyewmjrbRF2pavEsN0W9AMapaA7Hggkfxf/eBygOfCVrWY8UVQ4V9GueWr+VUVeXD4yLGrgwH8XoKV/ahXcoi9Pba9maObtKeKfj3wfYRukwO9BmzAcjO3j5IVL9QWCiPTExlg1r8xFHKVpPkggmpt+mWeuO64GIhEhLoELtM8WwazxVGsa5RKTcupgZKzTUCn7P3E4faULaWAVpxfZy5gJEL0DsNxLCTY1Z/u3txeJiY+g9ia1EyytNoNMotahAWcPUVd0v root@jenkins-server
4.4.3.3 配置jenkins-sever主机的私钥到凭据列表
powershell
[root@jenkins-server ~]# cat /root/.ssh/id_rsa
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAqde0Pd8rxFywzAkR5UKKWd03RaJpoOZoXt4G+WEpGX0j30Ua
kVhH8SATvHshKlshWuTUetMq+hgeDBcnsJo620RdqWrxLDdFvQDGqWgOx4IJH8X/
3gcoDnwla1mPFFUOFfRrnlq/lVFXlw+Mixq4MB/F6Clf2oV3KIvT22vZmjm7Snin
498H2EbpMDvQZswHIzt4+SFS/UFgoj0xMZYNa/MRRylaT5IIJqbfplnrjuuBiIRI
S6BC7TPFsGs8VRrGuUSk3LqYGSs01Ap+z9xOH2lC2lgFacX2cuYCRC9A7DcSwk2N
Wf7t7cXiYmPoPYmtRMsrTaDTKLWoQFnD1FXdLwIDAQABAoIBACS6PIPUqsY/tQuq
OnB1Py3HpJguUuE0cgVxxdhPpm+YnBVAAErF6GjAIwRzK+zY4BGjwj79QyHp7TV4
HGEYOPoJgqeMNLL3EOGaNVuU4P1LpWV1j6sXqRXYys64AzTGX9FnlY4c2akUefh0
qFO8Z3Agrs9RgyTiD6547ktYuymaUBdgIJo0fFgMJgisXvLIWHunxty9+RricNcK
pQe05SoU/0YPJzuO6Rrh5PeNJCmP9739QodhLJs2mmOlKQCRpBb31gZ34ADZWfG3
m7Ivxj1FbxOZXVCO5+GQbG2Z2DF+xT3TqbRwOho+hoQ/hy7I8lJpsjzSx4rnAIS8
FOKL2MECgYEA2wBNT9q2shV3IYSTW+1y5lP28mTVQLfQfMC0mf1Fm4XkzpQJ1RDp
FchtJL6j4SPJkDY3S+/bCAcFFMCR1q+HCeqKXMDIWaYpe+pgBz4SFwfarnIq3Cly
ivrgyrPWtA+1Ug/cs6JoDhVOpRYWDGhPgDiFZXLNcNlOcgHWOMHHmCcCgYEAxolQ
/T29WfEwx/HYum6qyiz6WLAZrRk6oasifileclnE+CZBBgbXGxZsBbw/ZyUv78TO
mib1EDB5lz7njCCm8gxvVcvH7haj4nnubwLOeHbLjokd5hds6v2dWbQ3NUzPYKi+
BwTwkcPvGNTUVfl/O120Kr/traRUSp/5prLgb7kCgYEAuHSNqYeev2ni+EEw2Imr
cCIiadDR6PupR5oLw3OWUEK7D9+SVHo0x9sTSpKjdzpsR+HSO0xfoGhBOFRlwXv/
8jMwrKTXWuLgJ/WlQhReqjU8VBfmz569Uh+yfChy2yAr6V1g15AfslTx84whr/uf
knCIsoS6UqVFNDMl2pIkaZsCgYAbMqCHsLeJ5MK8AWOhVpvLCpJZJ/a8geJIV3to
MR6hJ68WF5d8zd8V9OJjfsBozu1cOWfcMk4MI7fkgleSGL8GFbIC3OHMuglFc1K4
nnfOhOep5RrVdIwVPR0bNtcBVD5sjXFK4OBb961/h1h3VrCZX2LZIGXdDdAGURgQ
QB0C8QKBgCqAdGk/3BlgAvCTt4BqP9cJ812uliQAUjcT/dhjQW1msB4GEpv//j0B
eEjXqS6k7mcQ6SGiA8KJ8n4Jw5zbUyz6FMUlqS1orlS29+g6J0PubBcEeCXI6STN
vrk2unTNFdyJbOOs+BHwC9utvvYJITzdC+PjGKhryUN4d9cgcJqb
-----END RSA PRIVATE KEY-----
有了这个凭据后Jenkins就可以访问到gitlab-server了
4.5 jenkins插件安装
4.5.1 maven integration
用于编译JAVA项目
4.5.2 git parameter
用于基于git版本提交进行参数构建项目
4.5.3 gitlab
用于jenkins-server拉取项目
4.5.4 Generic Webhook Trigger
用于项目自动化构建
4.5.5 ssh
用于jenkins-server对web-server实施项目部署
安装后访问192.168.150.115:8080/safeRestart
进行重启。
4.6 jenkins全局工具配置
4.6.1 JDK配置
配置并安装JDK8,因为我们的javademo需要JDK8来启动
shell
[root@jenkins-server ~]# ls
jdk-8u431-linux-x64.rpm
[root@jenkins-server ~]# rpm -ivh jdk-8u431-linux-x64.rpm
4.6.2 Git配置
powershell
[root@jenkins-server ~]# git version
git version 1.8.3.1
4.6.3 Maven配置
powershell
[root@jenkins-server ~]# mvn --version
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /usr/local/mvn
Java version: 17.0.13, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-17.0.13-oracle-x64
Default locale: zh_CN, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-1160.119.1.el7.x86_64", arch: "amd64", family: "unix"
[root@jenkins-server ~]# echo $MAVEN_HOME
/usr/local/mvn
4.7 jenkins系统配置
主要配置jenkins-server通过ssh协议连接web-server
4.7.1 添加jenkins-server访问web-server凭据
4.7.2 配置ssh协议连接主机
别忘了保存
五、企业业务代码项目发布
5.1 数据库管理系统部署 mariadb及创建项目数据库
下载安装
powershell
[root@web-server ~]# yum -y install mariadb mariadb-server
启动设置服务启动时启动
powershell
[root@web-server ~]# systemctl enable mariadb
[root@web-server ~]# systemctl start mariadb
设置账户和密码
powershell
[root@web-server ~]# mysqladmin -uroot password 'abc123'
登录
powershell
[root@web-server ~]# mysql -uroot -pabc123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
创建数据库solo
powershell
MariaDB [(none)]> create database if not exists solo default charset utf8 collate utf8_general_ci;
授权
powershell
MariaDB [(none)]> grant all on solo.* to 'root'@'%' identified by "123456";
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> grant all on solo.* to 'root'@'localhost' identified by "123456";
Query OK, 0 rows affected (0.00 sec)
5.2 项目代码获取
开发服务器尝试下载
shell
[root@dev ~]# git clone --recurse-submodules https://gitee.com/dl88250/solo.git
5.3 项目代码修改
查看项目代码
powershell
[root@dev ~]# ls
solo
修改项目配置,将数据库的地址修改为web-server
地址即可
powershell
#
# Solo - A small and beautiful blogging system written in Java.
# Copyright (c) 2010-present, b3log.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
#
# Description: Solo local environment configurations.
# Version: 1.1.3.15, Mar 17, 2019
# Author: Liang Ding
#
#### MySQL runtime ####
runtimeDatabase=MYSQL
jdbc.username=root
jdbc.password=123456
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.URL=jdbc:mysql://192.168.150.116:3306/solo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
#### H2 runtime ####
#runtimeDatabase=H2
#jdbc.username=root
#jdbc.password=
#jdbc.driver=org.h2.Driver
#jdbc.URL=jdbc:h2:~/solo_h2/db;MODE=MYSQL
# The minConnCnt MUST larger or equal to 3
jdbc.minConnCnt=5
jdbc.maxConnCnt=10
# The specific table name prefix
jdbc.tablePrefix=b3_solo
5.4 项目代码上传到gitlab
设置账户和邮箱
powershell
[root@dev ~]# git config --global user.name "dev"
[root@dev ~]# git config --global user.email "dev@163.com"
删除远程仓库(如果是git clone
克隆)
powershell
[root@dev solo]# git remote remove origin
初始化仓库
powershell
[root@dev ~]# cd solo
[root@dev solo]# git init
添加远程仓库
powershell
[root@dev solo]# git remote add origin git@192.168.150.114:root/solo.git
添加到暂存区然后提交到本地仓库
powershell
[root@dev solo]# git add -A .
[root@dev solo]# git commit -m "new"
[master 3e39b0a] new
1 file changed, 1 insertion(+), 1 deletion(-)
打上标记
powershell
[root@dev solo]# git tag 1.0.0
推送到远程仓库
powershell
[root@dev solo]# git push origin 1.0.0
推送所有,防止看不到代码
powershell
[root@dev solo]# git push -u origin --all
5.5 构建项目运行基础应用容器镜像
在harbor-server主机上操作
5.5.1 创建项目目录
创建项目目录
powershell
[root@harbor-server ~]# mkdir tomcatdir
[root@harbor-server ~]# cd tomcatdir
5.5.2 生成Dockerfile文件
创建首页文件
powershell
[root@harbor-server tomcatdir]# echo "tomcat is running" >> index.html
创建Dockerfile文件
powershell
[root@harbor-server tomcatdir]# vim Dockerfile
[root@harbor-server tomcatdir]# cat Dockerfile
FROM centos:centos7
LABEL maintainer="sy<sy@163.com>"
ENV BIG_VERSION=9
ENV VERSION=9.0.97
ENV JAVA_HOME=/usr/local/jdk
ENV TOMCAT_HOME=/usr/local/tomcat
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
RUN yum -y install wget
RUN wget https://dlcdn.apache.org/tomcat/tomcat-${BIG_VERSION}/v${VERSION}/bin/apache-tomcat-${VERSION}.tar.gz
RUN tar xf apache-tomcat-${VERSION}.tar.gz
RUN mv apache-tomcat-${VERSION} /usr/local/tomcat
RUN rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/*
RUN mkdir /usr/local/tomcat/webapps/ROOT
ADD ./index.html /usr/local/tomcat/webapps/ROOT/
ADD ./jdk /usr/local/jdk
RUN echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
RUN echo "export JAVA_HOME=/usr/local/jdk" >> /etc/profile
RUN echo "export PATH=${TOMCAT_HOME}/bin:${JAVA_HOME}/bin:$PATH" >> /etc/profile
RUN echo "export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar" >> /etc/profile
RUN source /etc/profile
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
查看准备文件
powershell
[root@harbor-server tomcatdir]# ls
Dockerfile index.html jdk
5.5.3 使用docker build构建容器镜像
构建容器镜像
powershell
[root@harbor-server tomcatdir]# docker build -t 192.168.150.117/library/tomcat:9097 .
5.5.4 推送容器镜像至harbor容器镜像仓库
查看
powershell
[root@harbor-server tomcatdir]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.150.117/library/tomcat 9097 8c4ff853836f 9 seconds ago 792MB
登录harbor
powershell
[root@harbor-server tomcatdir]# docker login 192.168.150.117
Username: admin
Password:
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
推送镜像
powershell
[root@harbor-server tomcatdir]# docker push 192.168.150.117/library/tomcat:9097
5.5.5 验证容器镜像可用性
运行容器
powershell
[root@harobr-server ~]# docker run -d 192.168.150.117/library/tomcat:9097
91e1ca591ee9c51286570ce25c46e0ac18f66f2aca03c6a77fc6ec2a082711f3
查看容器运行状态
powershell
[root@harobr-server ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91e1ca591ee9 192.168.150.117/library/tomcat:9097 "/usr/local/tomcat/b..." 14 seconds ago Up 13 seconds 8080/tcp determined_hodgkin
查看内网IP
powershell
[root@harobr-server ~]# docker inspect 91e1c | grep "IPAddress"
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
获得:172.17.0.2,进行测试
powershell
[root@harbor-server ~]# curl http://172.17.0.2:8080
tomcat is running
5.6 项目构建及发布
5.6.1 项目构建及发布步骤
第一步:jenkins获取项目代码
第二步:jenkins对项目代码编译,由maven完成
第三步:jenkins使用docker对编译完成的项目代码进行打包,打包成容器应用镜像
第四步:jenkins把打包的容器应用镜像上传到harbor
第五步:jenkins通过ssh插件完成对web-server进行运行容器应用镜像的操作
5.6.2 创建项目任务
shell
mvn clean package -Dmaven.test.skip=true
shell
docker image rm 192.168.150.117/library/tomcat:9097 | true
docker pull 192.168.150.117/library/tomcat:9097
Dockerfile:
powershell
REPOSITORY=192.168.150.117/library/solo:${Tag}
# 构建镜像
cat > Dockerfile << EOF
FROM 192.168.150.117/library/tomcat:9097
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY target/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
EOF
docker build -t $REPOSITORY .
# 上传镜像
docker login 192.168.150.117 -u admin -p Harbor12345
docker push $REPOSITORY
docker logout 192.168.150.117
shell script:
powershell
REPOSITORY=192.168.150.117/library/solo:${Tag}
# 部署
docker rm -f blog-solo | true
docker image rm $REPOSITORY | true
docker container run -d --name blog-solo -p 80:8080 $REPOSITORY
5.7 存在的问题及解决
问题
解决