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

一,前言

上一篇,介绍了前端项目的部署:项目的创建和 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,实现提交代码自动触发构建部署;

相关内容

热门资讯

中证A500ETF摩根(560... 8月22日,截止午间收盘,中证A500ETF摩根(560530)涨1.19%,报1.106元,成交额...
A500ETF易方达(1593... 8月22日,截止午间收盘,A500ETF易方达(159361)涨1.28%,报1.104元,成交额1...
何小鹏斥资约2.5亿港元增持小... 每经记者|孙磊    每经编辑|裴健如 8月21日晚间,小鹏汽车发布公告称,公司联...
中证500ETF基金(1593... 8月22日,截止午间收盘,中证500ETF基金(159337)涨0.94%,报1.509元,成交额2...
中证A500ETF华安(159... 8月22日,截止午间收盘,中证A500ETF华安(159359)涨1.15%,报1.139元,成交额...
科创AIETF(588790)... 8月22日,截止午间收盘,科创AIETF(588790)涨4.83%,报0.760元,成交额6.98...
创业板50ETF嘉实(1593... 8月22日,截止午间收盘,创业板50ETF嘉实(159373)涨2.61%,报1.296元,成交额1...
港股异动丨航空股大幅走低 中国... 港股航空股大幅下跌,其中,中国国航跌近7%表现最弱,中国东方航空跌近5%,中国南方航空跌超3%,美兰...
电网设备ETF(159326)... 8月22日,截止午间收盘,电网设备ETF(159326)跌0.25%,报1.198元,成交额409....
红利ETF国企(530880)... 8月22日,截止午间收盘,红利ETF国企(530880)跌0.67%,报1.034元,成交额29.0...