1.生成安全证书,放到/usr/local/jenkins/data里面
mkdir -p /opt/cert/docker
cd /opt/cert/docker
#逐行执行下面的脚本($HOST替换成你的域名)
openssl genrsa -aes256 -out ca-key.pem 4096
openssl req -new -x509 -days 365 -key ca-key.pem -sha256 -out ca.pem
openssl genrsa -out server-key.pem 4096
openssl req -subj "/CN=$HOST" -sha256 -new -key server-key.pem -out server.csr
# 配置白名单,推荐配置0.0.0.0,允许所有IP连接但只有证书才可以连接成功
echo subjectAltName = DNS:$HOST,IP:0.0.0.0 > extfile.cnf
openssl x509 -req -days 365 -sha256 -in server.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out server-cert.pem -extfile extfile.cnf
openssl genrsa -out key.pem 4096
openssl req -subj '/CN=client' -new -key key.pem -out client.csr
echo extendedKeyUsage = clientAuth > extfile.cnf
openssl x509 -req -days 365 -sha256 -in client.csr -CA ca.pem -CAkey ca-key.pem -CAcreateserial -out cert.pem -extfile extfile.cnf
rm -v client.csr server.csr
chmod -v 0400 ca-key.pem key.pem server-key.pem
chmod -v 0444 ca.pem server-cert.pem cert.pem
2.安装docker
apt update
apt install docker.io
3.修改配置
vi /usr/lib/systemd/system/docker.service
# 注释掉其中的ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
# 添加如下
ExecStart=/usr/bin/dockerd \
--tlsverify --tlscacert=/opt/cert/docker/ca.pem \
--tlscert=/opt/cert/docker/server-cert.pem \
--tlskey=/opt/cert/docker/server-key.pem \
-H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock
# 重启docker
systemctl daemon-reload && systemctl restart docker
# 测试2375端口,直接访问报错
curl https://域名:2375/info
# 指定证书访问成功
curl --insecure https://域名:2375/info --cert /opt/cert/docker/cert.pem --key /opt/cert/docker/key.pem --cacert /opt/cert/docker/ca.pem
4.安装gogs
docker pull gogs/gogs
docker run -d -p 10022:22 -p 3000:3000 --name=gogs --restart=always gogs/gogs
3000端口访问:
![](https://i-blog.csdnimg.cn/blog_migrate/85d3afda0a2c26113aaa9d393efa7952.png)
localhost换成公网ip:
![](https://i-blog.csdnimg.cn/blog_migrate/28f72a3908e3dfe571b50a378f0f70dc.png)
接着登录注册、上传仓库:
![](https://i-blog.csdnimg.cn/blog_migrate/97a4ee04d9eed711142b7345b2ffa9d9.png)
5.安装jenkins
mkdir -p /usr/local/jenkins/data
chmod -R a+w /usr/local/jenkins/data/
docker pull jenkins/jenkins
docker run --privileged=true -u=root -itd --name jenkins -v /usr/local/jenkins/data:/var/jenkins_home/ -p 8080:8080 -p 50000:50000 --restart=always jenkins/jenkins
![](https://i-blog.csdnimg.cn/blog_migrate/e7bc8875c4cb15542215a423911d0382.png)
docker logs jenkins
# 输入密码
6.安装java和maven
![](https://i-blog.csdnimg.cn/blog_migrate/3e3382c07b935f812c042a40cb7ee04b.png)
# ca.pem cert.pem key.pem也移到这里来
![](https://i-blog.csdnimg.cn/blog_migrate/f9293948e00dd555c5573872312c8988.png)
![](https://i-blog.csdnimg.cn/blog_migrate/cc0d04c798b6a92ec59317bbea879dc7.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2d37a132607e61ff23dd5cfb1d8bea88.png)
7.配置jenkins(这块非常难)
先添加插件publish over ssh,不会可以百度,很简单。
1.项目根目录添加jenkins.sh
#!/bin/bash
if docker ps | grep gateway; then
docker stop gateway
docker rm gateway
docker rmi flow/gateway:latest
fi
if docker ps | grep auth; then
docker stop auth
docker rm auth
docker rmi flow/auth:latest
fi
if docker ps | grep video; then
docker stop video
docker rm video
docker rmi flow/video:latest
fi
if docker ps | grep userinfo; then
docker stop userinfo
docker rm userinfo
docker rmi flow/userinfo:latest
fi
if docker ps | grep comment; then
docker stop comment
docker rm comment
docker rmi flow/comment:latest
fi
![](https://i-blog.csdnimg.cn/blog_migrate/d0f26bb699ef815db99d2079c5927b8f.png)
2.安装插件Publish Over SSH并全局配置
![](https://i-blog.csdnimg.cn/blog_migrate/8133d9c638472dc5df0c3094848b5a55.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7c6ab031e8b74172e6d44efec5f056e8.png)
3.第一步当然是拷贝jenkins.sh到根目录下,并执行,目的是清除镜像和容器
![](https://i-blog.csdnimg.cn/blog_migrate/38e97ca0a01748139afad7a7cc82b7ac.png)
4.执行打包镜像的命令
![](https://i-blog.csdnimg.cn/blog_migrate/8c8376f45fd61b6b8c27c457ef40cb77.png)
4.2023.4.3修改如下
mvn clean install
echo "开始构建Las Vegas的微服务群"
cd gateway
mvn docker:build
cd ../auth
mvn docker:build
cd ../video
mvn docker:build
cd ../userInfo
mvn docker:build
cd ../comment
mvn docker:build
echo "Las Vegas的微服务群构建完毕!"
cd ..
echo "开始更改文件"
cd gateway
sed -i 's/alexmisko.com/cl.alexmisko.com/g' pom.xml
sed -i 's#jenkins_home#jenkins_home/cert#g' pom.xml
cd src/main/resources
sed -i '0,/66\.187\.4\.234/s//38.34.245.48/' application.yml
cd ../../../../auth
sed -i 's/alexmisko.com/cl.alexmisko.com/g' pom.xml
sed -i 's#jenkins_home#jenkins_home/cert#g' pom.xml
cd src/main/resources
sed -i '0,/66\.187\.4\.234/s//38.34.245.48/' application.yml
cd ../../../../video
sed -i 's/alexmisko.com/cl.alexmisko.com/g' pom.xml
sed -i 's#jenkins_home#jenkins_home/cert#g' pom.xml
cd src/main/resources
sed -i '0,/66\.187\.4\.234/s//38.34.245.48/' application.yml
cd ../../../../userInfo
sed -i 's/alexmisko.com/cl.alexmisko.com/g' pom.xml
sed -i 's#jenkins_home#jenkins_home/cert#g' pom.xml
cd src/main/resources
sed -i '0,/66\.187\.4\.234/s//38.34.245.48/' application.yml
cd ../../../../comment
sed -i 's/alexmisko.com/cl.alexmisko.com/g' pom.xml
sed -i 's#jenkins_home#jenkins_home/cert#g' pom.xml
cd src/main/resources
sed -i '0,/66\.187\.4\.234/s//38.34.245.48/' application.yml
echo "更改文件完毕"
cd ../../../../
mvn clean install
echo "开始构建HK的微服务群"
cd gateway
mvn docker:build
cd ../auth
mvn docker:build
cd ../video
mvn docker:build
cd ../userInfo
mvn docker:build
cd ../comment
mvn docker:build
echo "HK的微服务群构建完毕!"
5.运行镜像为容器
![](https://i-blog.csdnimg.cn/blog_migrate/ccb0d19522d31f024573e7d42de635ae.png)
5.2023.4.3增加香港机器
![](https://i-blog.csdnimg.cn/blog_migrate/eae22630d6d51a85f4d2d2d696e49b62.png)
6.贴个图
![](https://i-blog.csdnimg.cn/blog_migrate/ea1a862b1e0860befdf027ae90968d2a.png)
结语:
人生就是一场Mortal Combat,不服?Fight!
![](https://i-blog.csdnimg.cn/blog_migrate/0a85c230b90e84e674781dc28855d339.png)