持续集成交付CICD:K8S 手动完成前端项目应用发布与回滚

目录

一、实验

1.环境

2.Harbor查看镜像与连接K8S节点

[3.K8S集群部署 nginx-ingress-controller](#3.K8S集群部署 nginx-ingress-controller)

[4. Jenkins 通过GitLab共享库 实现前端项目镜像构建](#4. Jenkins 通过GitLab共享库 实现前端项目镜像构建)

[5.K8S node节点拉取镜像](#5.K8S node节点拉取镜像)

[6.K8S master节点更新部署文件](#6.K8S master节点更新部署文件)

7.前端项目应用回滚


一、实验

1.环境

(1)主机

表1 主机

|---------|--------------|----------|---------------------|---------------------|
| 主机 | 架构 | 版本 | IP | 备注 |
| master1 | K8S master节点 | 1.20.6 | 192.168.204.180 | |
| node1 | K8S node节点 | 1.20.6 | 192.168.204.181 | |
| node2 | K8S node节点 | 1.20.6 | 192.168.204.182 | |
| jenkins | jenkins 主节点 | 2.414.2 | 192.168.204.15:8080 | gitlab runner (从节点) |
| | harbor私有仓库 | 1.2.2 | 192.168.204.15 | |
| gitlab | gitlab 主节点 | 12.10.14 | 192.168.204.8:82 | jenkins slave (从节点) |
| | sonarqube | 9.6 | 192.168.204.8:9000 | |

2.Harbor查看镜像与连接K8S节点

(1)查看镜像

bash 复制代码
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.5

(2)修改node节点docker配置文件,指定私有镜像仓库地址

bash 复制代码
 
 浏览器访问 https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors 获取镜像加速器配置
 ​
 mkdir -p /etc/docker
 tee /etc/docker/daemon.json <<-'EOF'
 {
   "registry-mirrors": ["https://XXXXXXXX.mirror.aliyuncs.com"]  ,
   "insecure-registries": ["http://192.168.204.15"]
 
 }
 EOF
 ​
 systemctl daemon-reload      #重新加载
 systemctl restart docker     #重启docker
 ​
 docker info   #查看是否启用了镜像加速

(3)node节点连接Harbor拉取镜像

bash 复制代码
# docker login -u admin -p Harbor12345 192.168.204.15

# docker pull 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.5

# docker logout 192.168.204.15

node1节点:

node2节点:

3.K8S集群部署 nginx-ingress-controller

(1) 查看集群状态

bash 复制代码
# kubectl get nodes

(2)下载 nginx-ingress-controller 和 ingress-nginx 暴露端口配置文件

bash 复制代码
mkdir /opt/ingress-nodeport
cd /opt/ingress-nodeport
 
官方下载地址:
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/mandatory.yaml
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml
 
国内 gitee 资源地址:
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/provider/baremetal/service-nodeport.yaml

(3)在所有 node 节点上传镜像包 ingress-controller-0.30.0.tar 到 /opt/ingress-nodeport 目录,并加载镜像

bash 复制代码
mkdir -p /opt/ingress-nodeport
cd /opt/ingress-nodeport
 
 
tar zxvf ingree.contro-0.30.0.tar.gz
docker load -i ingree.contro-0.30.0.tar

node1节点:

node2节点:

(4)启动 nginx-ingress-controller

bash 复制代码
kubectl apply -f mandatory.yaml
kubectl apply -f service-nodeport.yaml

(5)Ingress HTTP 代理访问

创建 deployment、Service、Ingress Yaml 资源

bash 复制代码
# vim devops03-devops-ui.yaml
bash 复制代码
  1 apiVersion: apps/v1
  2 kind: Deployment
  3 metadata:
  4   labels:
  5     k8s-app: devops03-devops-ui
  6   name: devops03-devops-ui
  7   namespace: devops03
  8 spec:
  9   replicas: 2
 10   revisionHistoryLimit: 10
 11   selector:
 12     matchLabels:
 13       k8s-app: devops03-devops-ui
 14   template:
 15     metadata:
 16       labels:
 17         k8s-app: devops03-devops-ui
 18       namespace: devops03
 19       name: devops03-devops-ui
 20     spec:
 21       containers:
 22         - name: devops03-devops-ui
 23           image: 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.5
 24           imagePullPolicy: IfNotPresent
 25           ports:
 26             - containerPort: 80
 27               name: web
 28 ---
 29 apiVersion: v1
 30 kind: Service
 31 metadata:
 32   name: devops03-devops-ui
 33   namespace: devops03
 34 spec:
 35   ports:
 36   - name: web
 37     port: 80
 38     targetPort: 80
 39   selector:
 40     k8s-app: devops03-devops-ui
 41 ---
 42 apiVersion: networking.k8s.io/v1
 43 kind: Ingress
 44 metadata:
 45     name: devops03-devops-ui
 46     namespace: devops03
 47 spec:
 48     rules:
 49     - host: devops03-devops-ui.devops.com
 50       http:
 51         paths:
 52         - path: /
 53           pathType: Prefix
 54           backend:
 55             service:
 56               name: devops03-devops-ui
 57               port:
 58                 number: 80

(6)创建并查看命名空间

bash 复制代码
# kubectl create ns devops03

# kubectl get ns

(7)生成资源并查看

bash 复制代码
# kubectl apply -f devops03-devops-ui.yaml -n devops03

# kubectl -n devops03 get all

(8)内部测试访问

bash 复制代码
# curl 10.109.26.174

(9)外部测试访问

bash 复制代码
# vim /etc/hosts
bash 复制代码
# kubectl get svc -n ingress-nginx
bash 复制代码
# curl http://devops03-devops-ui.devops.com:31291

4. Jenkins 通过GitLab共享库 实现前端项目镜像构建

(1)GitLab前端项目新建RELEASE-1.1.6分支

(2)Jenkins手动构建前端项目CI流水线

(3)日志显示SUCCESS

(4)查看Blue Ocean

(5) Harbor查看镜像

bash 复制代码
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.6

5.K8S node节点拉取镜像

(1)node节点连接Harbor拉取镜像

bash 复制代码
​
# docker login -u admin -p Harbor12345 192.168.204.15

# docker pull 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.6

# docker logout 192.168.204.15

node1节点:

node2节点:

6.K8S master节点更新部署文件

(1)master节点更新文件

bash 复制代码
# vim devops03-devops-ui.yaml

更新前:

更新后:

(2)另开一个终端用watch命令观察pod变化

bash 复制代码
# watch -n 1 "kubectl get pod -n devops03"


(3)生成资源

bash 复制代码
# kubectl apply -f devops03-devops-ui.yaml -n devops03

(4)观察pod变化

(5)外部访问测试

bash 复制代码
# curl http://devops03-devops-ui.devops.com:31291

7.前端项目应用回滚

(1)查看历史

bash 复制代码
# kubectl rollout history deployment/devops03-devops-ui -n devops03

(2)查看具体某一个历史版本信息

bash 复制代码
# kubectl rollout history deployment/devops03-devops-ui --revision=1 -n devops03

# kubectl rollout history deployment/devops03-devops-ui --revision=2 -n devops03

(3)回滚上个版本

bash 复制代码
# kubectl rollout undo deployment/devops03-devops-ui -n devops03

观察变化


外部访问测试

bash 复制代码
# curl http://devops03-devops-ui.devops.com:31291

(4)回滾指定版本

bash 复制代码
# kubectl rollout undo deployment/devops03-devops-ui --to-revision=2 -n devops03

观察变化

外部访问测试

bash 复制代码
# curl http://devops03-devops-ui.devops.com:31291
相关推荐
cv-daily2 小时前
通过docker overlay2目录名查找容器名和容器ID
运维·docker·容器
明月与玄武3 小时前
放弃使用Dockerfiles 平替 docker init
docker·容器
moton20174 小时前
云原生:构建现代化应用的基石
后端·docker·微服务·云原生·容器·架构·kubernetes
一个假的前端男6 小时前
Windows Docker Desktop安装及使用 Docker 运行 MySQL
windows·docker·容器
小马爱打代码6 小时前
125个Docker的常用命令
运维·docker·容器
xiao-xiang6 小时前
jenkins-k8s pod方式动态生成slave节点
java·kubernetes·jenkins
胡八一7 小时前
解决docker: ‘buildx‘ is not a docker command.
运维·docker·容器
小张认为的测试7 小时前
Jenkins邮件通知的详细配置含邮件通知模板!
java·servlet·ci/cd·jenkins·邮件通知
QQ_7781329749 小时前
在K8S中使用Values文件定制不同环境下的应用配置详解
kubernetes
小张认为的测试9 小时前
Liunx上Jenkins 持续集成 Java + Maven + TestNG + Allure + Rest-Assured 接口自动化项目
java·ci/cd·jenkins·maven·接口·testng