【Kubernetes】第二十七篇 - 布署前端项(下)
创始人
2025-05-28 03:21:02

一,前言

上一篇,介绍了前端项目的部署:项目的创建和 jenkins 配置;

本篇,创建 Deployment、Service,完成前端项目的部署;


二,创建 Deployment

创建 Deployment 配置文件:deployment-cicd-frontend.yaml

[root@k8s-master cicd]# vi deployment-cicd-frontend.yamlapiVersion: apps/v1
kind: Deployment
metadata:name: cicd-frontend
spec:selector:matchLabels: #匹配标签名app: cicd-frontendreplicas: 1template: #模板metadata:labels:app: cicd-frontend #对应selector-matchLabels名称spec: #规格imagePullSecrets: #拉取私服镜像所需要的信息- name: private-registrycontainers: #容器- name: cicd-frontendimage: 47.94.92.122:8082/cicd-frontend:20220112231457

生效配置:

[root@k8s-master cicd]# kubectl apply -f deployment-cicd-frontend.yaml
deployment.apps/cicd-frontend created

查看 pod:cicd-frontend 已完成 1 个副本的部署

[root@k8s-master cicd]# kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
cicd-backend-98b5d4f57-ftrdk     1/1     Running   0          34h
cicd-frontend-77447bfdb4-bgt6t   1/1     Running   0          90s
cicd-mysql-745975859b-c4b6p      1/1     Running   4          34h

三,创建 Service

创建 Service 配置文件:service-cicd-frontend.yaml

[root@k8s-master cicd]# vi service-cicd-frontend.yamlapiVersion: v1
kind: Service
metadata:name: service-cicd-frontend
spec:selector:app: cicd-frontendports:- protocol: TCPport: 80targetPort: 80type: NodePort

备注:

1,spec 下的 app,这里的 cicd-frontend 是需要和 Deployment 部署配置文件的名字一致;
2,port: 80,指前端构建 nginx 容器的端口号,集群内部服务之间项目访问的端口号是 80;
3,targetPort: 80:指容器内部启动之后,对外暴露的端口号是 80;

生效配置:

[root@k8s-master cicd]# kubectl apply -f  service-cicd-frontend.yaml
service/service-cicd-frontend created

查看服务

[root@k8s-master cicd]# kubectl get svc
NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubernetes              ClusterIP   10.96.0.1                443/TCP          21d
service-cicd-backend    NodePort    10.97.144.175            7001:30174/TCP   34h
service-cicd-frontend   NodePort    10.103.232.242           80:30339/TCP     2m21s
service-cicd-mysql      NodePort    10.108.224.96            8899:32154/TCP   5d13h

可以看到,service-cicd-frontend 对外部暴露的端口号是 30339

测试访问服务

查看 host,部署在 k8s-node 节点上,使用内网 ip:172.17.178.106

// 查看 host
[root@k8s-master cicd]# cat /etc/hosts
::1	localhost	localhost.localdomain	localhost6	localhost6.localdomain6
127.0.0.1	localhost	localhost.localdomain	localhost4	localhost4.localdomain4172.17.178.106	k8s-node172.17.178.105	k8s-master
172.17.178.105	k8s-master	k8s-master

curl 访问页面地址,返回 html 代码,说明部署成功且能够正常提供服务

// 测试访问
[root@k8s-master cicd]# curl http://172.17.178.106:30339/
React App

但是,由于阿里云服务器的防火墙,使用外网 ip,直接在浏览器访问:
http://47.93.9.45:30339 暂时还访问不了,需要配置安全组开放端口:


本篇,创建了前端项目的 Deployment、Service,完成了前端项目的部署和访问;

下一篇,集成 jenkins 与 k8s,实现提交代码自动触发构建部署;

相关内容

热门资讯

【文化中国行】“圈”出幸福感!... (来源:工人日报) 阅读提示 近年来,嘉兴市总工会以全总“559”部署和省总工会“137...
特色养殖“驮”起增收梦 投喂黑驴。本报记者 栾雨嘉 摄本报记者 栾雨嘉12月12日,黄南藏族自治州尖扎县措周乡俄什加村的肉驴...
内蒙古粮食产量达840.7亿斤 (来源:内蒙古日报)转自:内蒙古日报本报12月13日讯  (记者  韩雪茹)据12月12日国家统计局...
北疆楷模黄启东先进事迹报告会举... (来源:内蒙古日报)转自:内蒙古日报本报呼和浩特12月13日讯  (记者  皇甫秀玲)12月11日,...
全球最大储能电站在巴彦淖尔投运 (来源:内蒙古日报)转自:内蒙古日报本报巴彦淖尔12月13日电  (记者  薄金凤)12月12日,内...