目录
一、实验
1.环境
2.Harbor查看镜像与连接K8S节点
3.K8S集群部署 nginx-ingress-controller
4. Jenkins 通过GitLab共享库 实现前端项目镜像构建
5.K8S node节点拉取镜像
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)查看镜像
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/dockertee /etc/docker/daemon.json <<-'EOF'{"registry-mirrors": ["https://XXXXXXXX.mirror.aliyuncs.com"] ,"insecure-registries": ["http://192.168.204.15"]}EOFsystemctl daemon-reload #重新加载systemctl restart docker #重启dockerdocker 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-nodeporttar 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/v12 kind: Deployment3 metadata:4 labels:5 k8s-app: devops03-devops-ui6 name: devops03-devops-ui7 namespace: devops038 spec:9 replicas: 210 revisionHistoryLimit: 1011 selector:12 matchLabels:13 k8s-app: devops03-devops-ui14 template:15 metadata:16 labels:17 k8s-app: devops03-devops-ui18 namespace: devops0319 name: devops03-devops-ui20 spec:21 containers:22 - name: devops03-devops-ui23 image: 192.168.204.15/devops03/devops03-devops-ui:RELEASE-1.1.524 imagePullPolicy: IfNotPresent25 ports:26 - containerPort: 8027 name: web28 ---29 apiVersion: v130 kind: Service31 metadata:32 name: devops03-devops-ui33 namespace: devops0334 spec:35 ports:36 - name: web37 port: 8038 targetPort: 8039 selector:40 k8s-app: devops03-devops-ui41 ---42 apiVersion: networking.k8s.io/v143 kind: Ingress44 metadata:45 name: devops03-devops-ui46 namespace: devops0347 spec:48 rules:49 - host: devops03-devops-ui.devops.com50 http:51 paths:52 - path: /53 pathType: Prefix54 backend:55 service:56 name: devops03-devops-ui57 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