目录
3.K8S集群部署 nginx-ingress-controller
4. Jenkins 通过GitLab共享库 实现前端项目镜像构建
一、实验
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)查看镜像
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.5
(2)修改node节点docker配置文件,指定私有镜像仓库地址
浏览器访问 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拉取镜像
# 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) 查看集群状态
# kubectl get nodes
(2)下载 nginx-ingress-controller 和 ingress-nginx 暴露端口配置文件
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 目录,并加载镜像
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
kubectl apply -f mandatory.yaml
kubectl apply -f service-nodeport.yaml
(5)Ingress HTTP 代理访问
创建 deployment、Service、Ingress Yaml 资源
# vim devops03-devops-ui.yaml
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)创建并查看命名空间
# kubectl create ns devops03
# kubectl get ns
(7)生成资源并查看
# kubectl apply -f devops03-devops-ui.yaml -n devops03
# kubectl -n devops03 get all
(8)内部测试访问
# curl 10.109.26.174
(9)外部测试访问
# vim /etc/hosts
# kubectl get svc -n ingress-nginx
# 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查看镜像
192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.6
5.K8S node节点拉取镜像
(1)node节点连接Harbor拉取镜像
# 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节点更新文件
# vim devops03-devops-ui.yaml
更新前:
更新后:
(2)另开一个终端用watch命令观察pod变化
# watch -n 1 "kubectl get pod -n devops03"
(3)生成资源
# kubectl apply -f devops03-devops-ui.yaml -n devops03
(4)观察pod变化
(5)外部访问测试
# curl http://devops03-devops-ui.devops.com:31291
7.前端项目应用回滚
(1)查看历史
# kubectl rollout history deployment/devops03-devops-ui -n devops03
(2)查看具体某一个历史版本信息
# kubectl rollout history deployment/devops03-devops-ui --revision=1 -n devops03
# kubectl rollout history deployment/devops03-devops-ui --revision=2 -n devops03
(3)回滚上个版本
# kubectl rollout undo deployment/devops03-devops-ui -n devops03
观察变化
外部访问测试
# curl http://devops03-devops-ui.devops.com:31291
(4)回滾指定版本
# kubectl rollout undo deployment/devops03-devops-ui --to-revision=2 -n devops03
观察变化
外部访问测试
# curl http://devops03-devops-ui.devops.com:31291