From cc830e805054dbad670be81646d5f7cb437dc23d Mon Sep 17 00:00:00 2001 From: plhw57tbe <2723863608@qq.com> Date: Sun, 19 Oct 2025 22:47:02 +0800 Subject: [PATCH] Update service.yaml --- src/DjangoBlog-master/deploy/k8s/service.yaml | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/src/DjangoBlog-master/deploy/k8s/service.yaml b/src/DjangoBlog-master/deploy/k8s/service.yaml index 4ef2931..c361b9f 100644 --- a/src/DjangoBlog-master/deploy/k8s/service.yaml +++ b/src/DjangoBlog-master/deploy/k8s/service.yaml @@ -1,80 +1,93 @@ -apiVersion: v1 -kind: Service +# 第一部分:Django 应用服务(Service) +# Service 用于为集群内的 Pod 提供稳定网络访问地址,实现 Pod 访问的负载均衡和服务发现 +apiVersion: v1 # Service 资源对应的 Kubernetes API 版本 +kind: Service # 资源类型为 Service metadata: - name: djangoblog - namespace: djangoblog + name: djangoblog # Service 名称,需与其他组件(如 Nginx 配置)中引用的服务名一致 + namespace: djangoblog # 所属命名空间,与 Django Deployment、其他组件保持一致(资源隔离) labels: - app: djangoblog + app: djangoblog # 服务标签,用于筛选和管理服务资源 spec: - selector: + selector: # 标签选择器:通过标签匹配要管理的 Pod(必须与 Django Pod 的标签一致) app: djangoblog - ports: - - protocol: TCP - port: 8000 - targetPort: 8000 - type: ClusterIP ---- + ports: # 端口配置:定义服务暴露的端口与 Pod 端口的映射关系 + - protocol: TCP # 网络协议,默认 TCP(常用还有 UDP) + port: 8000 # 服务暴露给集群内部的端口(其他组件通过此端口访问该服务) + targetPort: 8000 # 服务转发请求到 Pod 的目标端口(需与 Django 容器暴露的端口一致) + type: ClusterIP # 服务类型:ClusterIP 表示仅在集群内部暴露服务,外部无法直接访问(适合内部组件通信) + + +# 第二部分:Nginx 服务(Service) +--- # 资源分隔符,用于在单个文件中定义多个 Kubernetes 资源 apiVersion: v1 kind: Service metadata: - name: nginx + name: nginx # Service 名称,需与 Ingress 配置中引用的服务名一致 namespace: djangoblog labels: app: nginx spec: selector: - app: nginx + app: nginx # 匹配 Nginx Pod 的标签 ports: - protocol: TCP - port: 80 - targetPort: 80 - type: ClusterIP + port: 80 # 服务暴露的端口(Ingress 转发请求到该端口) + targetPort: 80 # 转发到 Nginx 容器暴露的 80 端口 + type: ClusterIP # 集群内部访问(外部通过 Ingress 间接访问 Nginx 服务) + + +# 第三部分:Redis 缓存服务(Service) --- apiVersion: v1 kind: Service metadata: - name: redis + name: redis # Service 名称,需与 Django 应用配置中访问 Redis 的服务名一致 namespace: djangoblog labels: app: redis spec: selector: - app: redis + app: redis # 匹配 Redis Pod 的标签 ports: - protocol: TCP - port: 6379 - targetPort: 6379 - type: ClusterIP + port: 6379 # 服务暴露的端口(Redis 默认端口) + targetPort: 6379 # 转发到 Redis 容器暴露的 6379 端口 + type: ClusterIP # 仅集群内部访问(缓存服务无需外部暴露) + + +# 第四部分:MySQL 数据库服务(Service) --- apiVersion: v1 kind: Service metadata: - name: db + name: db # Service 名称,需与 Django 应用配置中访问数据库的服务名一致 namespace: djangoblog labels: app: db spec: selector: - app: db + app: db # 匹配 MySQL Pod 的标签 ports: - protocol: TCP - port: 3306 - targetPort: 3306 - type: ClusterIP + port: 3306 # 服务暴露的端口(MySQL 默认端口) + targetPort: 3306 # 转发到 MySQL 容器暴露的 3306 端口 + type: ClusterIP # 仅集群内部访问(数据库服务禁止外部直接访问,保障安全) + + +# 第五部分:Elasticsearch 搜索引擎服务(Service) --- apiVersion: v1 kind: Service metadata: - name: elasticsearch + name: elasticsearch # Service 名称,需与 Django 应用配置中访问 ES 的服务名一致 namespace: djangoblog labels: app: elasticsearch spec: selector: - app: elasticsearch + app: elasticsearch # 匹配 Elasticsearch Pod 的标签 ports: - protocol: TCP - port: 9200 - targetPort: 9200 - type: ClusterIP - + port: 9200 # 服务暴露的端口(ES HTTP 接口默认端口) + targetPort: 9200 # 转发到 ES 容器暴露的 9200 端口 + type: ClusterIP # 仅集群内部访问(搜索引擎无需外部直接暴露) \ No newline at end of file