1.CICD #

2. 服务器 #

配置 技术栈 类型 标签
2核4G Jenkins + Nexus + Docker Cloud 构建机
2核4G Docker + Kubernetes Cloud kubernetes Master
1核1G Docker + Kubernetes Cloud kubernetes Node

3. 构建机CI #

0cafb0ad2ba216ef1c566ea98ce8cb30

3.1 安装docker #

yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable docker

配置阿里云镜像源

sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://fwvjnv59.mirror.aliyuncs.com"]
}
EOF
# 重载所有修改过的配置文件
sudo systemctl daemon-reload
sudo systemctl restart docker

3.2 安装 git #

yum install git -y

3.3 安装 Jenkins #

3.3.1 安装java #

yum install -y java

3.3.2 安装jenkins #

sudo wget -O /etc/yum.repos.d/jenkins.repo https://img.zhufengpeixun.com/jenkins.repo
sudo rpm --import https://img.zhufengpeixun.com/jenkins.io.key
yum install jenkins -y

3.2.3 启动 Jenkins #

 systemctl start jenkins.service

3.3.4 开放端口 #

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=50000/tcp --permanent
systemctl reload firewalld

3.3.5 打开浏览器访问 #

http://8.136.218.128:8080/

3.3.6 查看密码 #

cat /var/lib/jenkins/secrets/initialAdminPassword

3.3.7 修改插件镜像 #

sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' /var/lib/jenkins/updates/default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' /var/lib/jenkins/updates/default.json

3.3.8 添加到docker用户组里 #

sudo gpasswd -a jenkins docker  #将当前用户添加至docker用户组
newgrp docker                 #更新docker用户组

3.3.9 新建任务 #

docker -v
docker pull node:latest

3.3 安装Nodejs #

默认会拉取这个地址的安装包,但有可能会失败,失败之后可以重复,这个只需要执行一次就可以了

Unpacking https://nodejs.org/dist/v15.11.0/node-v15.11.0-linux-x64.tar.gz to /var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/nodejs15.11.0 on Jenkins

3.4 集成 Git 仓库 #

3.4.1 生成公钥私钥 #

ssh-keygen -t rsa -C "zhufengnodejs@126.com"

3.4.2 Gitee 配置公钥 #

cat ~/.ssh/id_rsa.pub 

3.4.3 在Jenkins 配置私钥 #

3.5 构建镜像 #

3.5.1 编写 Dockerfile #

Dockerfile

FROM nginx:1.15
COPY build /etc/nginx/html
COPY conf /etc/nginx/
WORKDIR /etc/nginx/html

conf\site

server {
    listen       80;
    server_name  _;
    root         /etc/nginx/html;
}

3.5.2 Jenkins配置脚本 #

#!/bin/sh

npm install --registry=https://registry.npm.taobao.org
npm run build
docker build -t react-project .

3.5.3 执行任务 #

3.6 上传私有镜像库 #

3.6.1 部署 Nexus 服务 #

cd /usr/local
wget https://dependency-fe.oss-cn-beijing.aliyuncs.com/nexus-3.29.0-02-unix.tar.gz
tar -zxvf ./nexus-3.29.0-02-unix.tar.gz
cd nexus-3.29.0-02/bin
./nexus start

firewall-cmd --zone=public --add-port=8081/tcp --permanent
firewall-cmd --zone=public --add-port=8082/tcp --permanent

http://8.136.218.128:8081/

nexus 还支持停止,重启等命令。可以在 bin 目录下执行 ./nexus help 查看更多命令

3.6.2 配置 Nexus #

cat /root/sonatype-work/nexus3/admin.password 

3.6.3 创建Docker私服 #

3.6.4 添加访问权限 #

3.6.5 登录制品库 #

vi /etc/docker/daemon.json

{
  "insecure-registries" : [
    "8.136.218.128:8082"
  ],
  "registry-mirrors": ["https://fwvjnv59.mirror.aliyuncs.com"]
}
systemctl restart docker
docker login 8.136.218.128:8082 //注意此处要和insecure-registries里的地址一致
Username: admin
Password: 123456

3.6.6 推送镜像到制品库 #

#!/bin/sh -l

npm install --registry=https://registry.npm.taobao.org
npm run build
docker build -t 8.136.218.128:8082/react-project .
docker login -u $DOCKER_LOGIN_USERNAME -p $DOCKER_LOGIN_PASSWORD 8.136.218.128:8082
docker push 8.136.218.128:8082/react-project