持续集成交付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
相关推荐
蜜獾云4 分钟前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器
年薪丰厚1 小时前
如何在K8S集群中查看和操作Pod内的文件?
docker·云原生·容器·kubernetes·k8s·container
zhangj11251 小时前
K8S Ingress 服务配置步骤说明
云原生·容器·kubernetes
岁月变迁呀1 小时前
kubeadm搭建k8s集群
云原生·容器·kubernetes
墨水\\1 小时前
二进制部署k8s
云原生·容器·kubernetes
Source、1 小时前
k8s-metrics-server
云原生·容器·kubernetes
上海运维Q先生1 小时前
面试题整理15----K8s常见的网络插件有哪些
运维·网络·kubernetes
颜淡慕潇1 小时前
【K8S问题系列 |19 】如何解决 Pod 无法挂载 PVC问题
后端·云原生·容器·kubernetes
大熊程序猿4 小时前
K8s证书过期
云原生·容器·kubernetes
摸鱼也很难7 小时前
Docker 镜像加速和配置的分享 && 云服务器搭建beef-xss
运维·docker·容器