持续集成交付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
相关推荐
全能全知者26 分钟前
docker快速安装与配置mongoDB
mongodb·docker·容器
景天科技苑7 小时前
【云原生开发】K8S多集群资源管理平台架构设计
云原生·容器·kubernetes·k8s·云原生开发·k8s管理系统
wclass-zhengge7 小时前
K8S篇(基本介绍)
云原生·容器·kubernetes
颜淡慕潇7 小时前
【K8S问题系列 |1 】Kubernetes 中 NodePort 类型的 Service 无法访问【已解决】
后端·云原生·容器·kubernetes·问题解决
川石课堂软件测试9 小时前
性能测试|docker容器下搭建JMeter+Grafana+Influxdb监控可视化平台
运维·javascript·深度学习·jmeter·docker·容器·grafana
昌sit!15 小时前
K8S node节点没有相应的pod镜像运行故障处理办法
云原生·容器·kubernetes
A ?Charis18 小时前
Gitlab-runner running on Kubernetes - hostAliases
容器·kubernetes·gitlab
wclass-zhengge18 小时前
Docker篇(Docker Compose)
运维·docker·容器
北漂IT民工_程序员_ZG19 小时前
k8s集群安装(minikube)
云原生·容器·kubernetes
IT-民工2111021 小时前
CI/CD 实践总结
运维·ci/cd·自动化