From 0608b6b368c521d649bfae1824fd9fab9d917adb Mon Sep 17 00:00:00 2001 From: plhw57tbe <2723863608@qq.com> Date: Sun, 19 Oct 2025 22:38:09 +0800 Subject: [PATCH] Update deployment.yaml --- .../deploy/k8s/deployment.yaml | 222 +++++++++++------- 1 file changed, 137 insertions(+), 85 deletions(-) diff --git a/src/DjangoBlog-master/deploy/k8s/deployment.yaml b/src/DjangoBlog-master/deploy/k8s/deployment.yaml index 414fdcc..c448b50 100644 --- a/src/DjangoBlog-master/deploy/k8s/deployment.yaml +++ b/src/DjangoBlog-master/deploy/k8s/deployment.yaml @@ -1,132 +1,161 @@ +# 第一部分:Django 博客应用部署配置 +# apiVersion 指定 Kubernetes API 版本,apps/v1 是 Deployment 资源的稳定版本 apiVersion: apps/v1 +# kind 定义资源类型为 Deployment(用于管理Pod的创建和扩展) kind: Deployment metadata: + # Deployment 的名称 name: djangoblog + # 部署所在的命名空间(用于资源隔离) namespace: djangoblog + # 为 Deployment 添加标签(用于筛选和关联资源) labels: app: djangoblog spec: + # 副本数:指定运行的 Pod 数量为 3 个(实现高可用) replicas: 3 + # 选择器:用于匹配要管理的 Pod 标签(必须与下面 template.metadata.labels 一致) selector: matchLabels: app: djangoblog + # Pod 模板:定义要创建的 Pod 的规格 template: metadata: + # Pod 的标签(与上面的 selector.matchLabels 对应) labels: app: djangoblog spec: + # 容器列表:一个 Pod 可以包含多个容器,这里定义应用容器 containers: - - name: djangoblog + - name: djangoblog # 容器名称 + # 容器使用的镜像(Django 博客应用镜像) image: liangliangyy/djangoblog:latest + # 镜像拉取策略:Always 表示每次都从仓库拉取最新镜像 imagePullPolicy: Always + # 容器暴露的端口(Django 应用默认运行在 8000 端口) ports: - containerPort: 8000 + # 从配置映射(ConfigMap)中注入环境变量 envFrom: - configMapRef: - name: djangoblog-env + name: djangoblog-env # 引用的 ConfigMap 名称 + # 就绪探针:判断容器是否已准备好接收请求(服务发现会依赖此状态) readinessProbe: - httpGet: - path: / - port: 8000 - initialDelaySeconds: 10 - periodSeconds: 30 + httpGet: # 通过 HTTP 请求检查就绪状态 + path: / # 检查的路径(应用根目录) + port: 8000 # 检查的端口 + initialDelaySeconds: 10 # 容器启动后延迟 10 秒开始首次检查 + periodSeconds: 30 # 每隔 30 秒检查一次 + # 存活探针:判断容器是否存活,若失败会重启容器 livenessProbe: - httpGet: - path: / - port: 8000 - initialDelaySeconds: 10 - periodSeconds: 30 + httpGet: # 通过 HTTP 请求检查存活状态 + path: / # 检查的路径 + port: 8000 # 检查的端口 + initialDelaySeconds: 10 # 容器启动后延迟 10 秒开始首次检查 + periodSeconds: 30 # 每隔 30 秒检查一次 + # 资源限制:控制容器对 CPU 和内存的使用 resources: - requests: - cpu: 10m - memory: 100Mi - limits: - cpu: "2" - memory: 2Gi + requests: # 资源请求(调度时的最小需求) + cpu: 10m # 10 毫核 CPU(1核=1000m) + memory: 100Mi # 100 兆内存 + limits: # 资源限制(容器最大可使用的资源) + cpu: "2" # 2 核 CPU + memory: 2Gi # 2 吉内存 + # 卷挂载:将持久卷挂载到容器内的指定路径 volumeMounts: - - name: djangoblog - mountPath: /code/djangoblog/collectedstatic - - name: resource - mountPath: /resource + - name: djangoblog # 引用下面 volumes 中定义的卷名称 + mountPath: /code/djangoblog/collectedstatic # 容器内的挂载路径(Django 静态文件目录) + - name: resource # 引用资源卷 + mountPath: /resource # 容器内的资源文件目录 + # 卷定义:声明需要挂载的持久卷 volumes: - - name: djangoblog - persistentVolumeClaim: - claimName: djangoblog-pvc - - name: resource + - name: djangoblog # 卷名称(与上面 volumeMounts.name 对应) + persistentVolumeClaim: # 使用持久卷声明(PVC) + claimName: djangoblog-pvc # 引用的 PVC 名称(需提前创建) + - name: resource # 资源卷名称 persistentVolumeClaim: - claimName: resource-pvc + claimName: resource-pvc # 资源对应的 PVC 名称 ---- + +# 第二部分:Redis 缓存服务部署配置 +--- # 分隔符:用于在一个文件中定义多个 Kubernetes 资源 apiVersion: apps/v1 kind: Deployment metadata: - name: redis - namespace: djangoblog + name: redis # Redis 部署名称 + namespace: djangoblog # 同属 djangoblog 命名空间 labels: - app: redis + app: redis # Redis 标签 spec: - replicas: 1 + replicas: 1 # Redis 单副本(简单部署,生产环境可能需要集群) selector: matchLabels: - app: redis + app: redis # 匹配 Redis Pod 标签 template: metadata: labels: - app: redis + app: redis # Pod 标签 spec: containers: - - name: redis - image: redis:latest + - name: redis # 容器名称 + image: redis:latest # Redis 官方最新镜像 + # 镜像拉取策略:IfNotPresent 表示本地有则使用本地镜像,否则拉取 imagePullPolicy: IfNotPresent ports: - - containerPort: 6379 + - containerPort: 6379 # Redis 默认端口 + # 资源限制(Redis 对资源需求较低) resources: requests: cpu: 10m memory: 100Mi limits: - cpu: 200m + cpu: 200m # 限制最大 200 毫核 CPU memory: 2Gi - + + +# 第三部分:MySQL 数据库部署配置 --- apiVersion: apps/v1 kind: Deployment metadata: - name: db + name: db # 数据库部署名称 namespace: djangoblog labels: - app: db + app: db # 数据库标签 spec: - replicas: 1 + replicas: 1 # 数据库单副本(生产环境需考虑主从或集群) selector: matchLabels: - app: db + app: db # 匹配数据库 Pod 标签 template: metadata: labels: - app: db + app: db # Pod 标签 spec: containers: - - name: db - image: mysql:latest + - name: db # 容器名称 + image: mysql:latest # MySQL 官方最新镜像 imagePullPolicy: IfNotPresent ports: - - containerPort: 3306 + - containerPort: 3306 # MySQL 默认端口 + # 从 ConfigMap 注入环境变量(如数据库密码、用户名等) envFrom: - configMapRef: - name: djangoblog-env + name: djangoblog-env # 复用 Django 应用的环境变量配置 + # 就绪探针:通过执行 mysqladmin ping 检查数据库是否就绪 readinessProbe: - exec: + exec: # 执行命令检查 command: - mysqladmin - ping - "-h" - - "127.0.0.1" + - "127.0.0.1" # 数据库主机(容器内本地) - "-u" - - "root" - - "-p$MYSQL_ROOT_PASSWORD" - initialDelaySeconds: 10 - periodSeconds: 10 + - "root" # 用户名 + - "-p$MYSQL_ROOT_PASSWORD" # 密码(从环境变量获取) + initialDelaySeconds: 10 # 延迟 10 秒检查 + periodSeconds: 10 # 每 10 秒检查一次 + # 存活探针:同就绪探针,确保数据库存活 livenessProbe: exec: command: @@ -139,6 +168,7 @@ spec: - "-p$MYSQL_ROOT_PASSWORD" initialDelaySeconds: 10 periodSeconds: 10 + # 资源限制(数据库对资源需求较高) resources: requests: cpu: 10m @@ -146,38 +176,42 @@ spec: limits: cpu: "2" memory: 2Gi + # 挂载数据库数据目录(持久化存储,避免数据丢失) volumeMounts: - - name: db-data - mountPath: /var/lib/mysql + - name: db-data # 引用数据卷 + mountPath: /var/lib/mysql # MySQL 数据存储路径 volumes: - - name: db-data + - name: db-data # 数据卷名称 persistentVolumeClaim: - claimName: db-pvc - + claimName: db-pvc # 数据库对应的 PVC 名称 + + +# 第四部分:Nginx 反向代理部署配置 --- apiVersion: apps/v1 kind: Deployment metadata: - name: nginx + name: nginx # Nginx 部署名称 namespace: djangoblog labels: - app: nginx + app: nginx # Nginx 标签 spec: - replicas: 1 + replicas: 1 # Nginx 单副本 selector: matchLabels: - app: nginx + app: nginx # 匹配 Nginx Pod 标签 template: metadata: labels: - app: nginx + app: nginx # Pod 标签 spec: containers: - - name: nginx - image: nginx:latest + - name: nginx # 容器名称 + image: nginx:latest # Nginx 官方最新镜像 imagePullPolicy: IfNotPresent ports: - - containerPort: 80 + - containerPort: 80 # Nginx 默认端口 + # 资源限制 resources: requests: cpu: 10m @@ -185,67 +219,82 @@ spec: limits: cpu: "2" memory: 2Gi + # 卷挂载:挂载配置文件和静态资源 volumeMounts: + # 挂载 Nginx 主配置文件(subPath 表示只挂载单个文件,而非目录) - name: nginx-config mountPath: /etc/nginx/nginx.conf subPath: nginx.conf + # 挂载默认站点配置 - name: nginx-config mountPath: /etc/nginx/conf.d/default.conf subPath: djangoblog.conf + # 挂载资源站点配置 - name: nginx-config mountPath: /etc/nginx/conf.d/resource.lylinux.net.conf subPath: resource.lylinux.net.conf + # 挂载额外的资源配置 - name: nginx-config mountPath: /etc/nginx/lylinux/resource.conf subPath: lylinux.resource.conf + # 挂载 Django 静态文件目录(与 Django 应用共享存储) - name: djangoblog-pvc mountPath: /code/djangoblog/collectedstatic + # 挂载资源文件目录 - name: resource-pvc mountPath: /resource volumes: + # Nginx 配置卷:通过 ConfigMap 挂载配置文件(避免在镜像中硬编码配置) - name: nginx-config configMap: - name: web-nginx-config + name: web-nginx-config # 引用的 ConfigMap 名称 + # 挂载 Django 静态文件对应的 PVC - name: djangoblog-pvc persistentVolumeClaim: claimName: djangoblog-pvc + # 挂载资源文件对应的 PVC - name: resource-pvc persistentVolumeClaim: claimName: resource-pvc + +# 第五部分:Elasticsearch 搜索引擎部署配置 --- apiVersion: apps/v1 kind: Deployment metadata: - name: elasticsearch + name: elasticsearch # ES 部署名称 namespace: djangoblog labels: - app: elasticsearch + app: elasticsearch # ES 标签 spec: - replicas: 1 + replicas: 1 # ES 单节点(生产环境需集群) selector: matchLabels: - app: elasticsearch + app: elasticsearch # 匹配 ES Pod 标签 template: metadata: labels: - app: elasticsearch + app: elasticsearch # Pod 标签 spec: containers: - - name: elasticsearch + - name: elasticsearch # 容器名称 + # 带 IK 分词器的 ES 镜像(适用于中文搜索) image: liangliangyy/elasticsearch-analysis-ik:8.6.1 imagePullPolicy: IfNotPresent + # ES 环境变量配置 env: - - name: discovery.type + - name: discovery.type # 单节点模式(无需集群发现) value: single-node - - name: ES_JAVA_OPTS + - name: ES_JAVA_OPTS # JVM 内存配置(根据需求调整) value: "-Xms256m -Xmx256m" - - name: xpack.security.enabled + - name: xpack.security.enabled # 关闭安全验证(简化部署) value: "false" - - name: xpack.monitoring.templates.enabled + - name: xpack.monitoring.templates.enabled # 关闭监控模板 value: "false" ports: - - containerPort: 9200 + - containerPort: 9200 # ES HTTP 接口端口 + # 资源限制(ES 对内存需求较高) resources: requests: cpu: 10m @@ -253,22 +302,25 @@ spec: limits: cpu: "2" memory: 2Gi + # 就绪探针:检查 ES 是否就绪 readinessProbe: httpGet: - path: / + path: / # ES 健康检查路径 port: 9200 - initialDelaySeconds: 15 + initialDelaySeconds: 15 # 延迟 15 秒(ES 启动较慢) periodSeconds: 30 + # 存活探针:检查 ES 是否存活 livenessProbe: httpGet: path: / port: 9200 initialDelaySeconds: 15 periodSeconds: 30 + # 挂载 ES 数据目录(持久化存储索引数据) volumeMounts: - name: elasticsearch-data - mountPath: /usr/share/elasticsearch/data/ + mountPath: /usr/share/elasticsearch/data/ # ES 数据存储路径 volumes: - - name: elasticsearch-data - persistentVolumeClaim: - claimName: elasticsearch-pvc + - name: elasticsearch-data + persistentVolumeClaim: + claimName: elasticsearch-pvc # ES 对应的 PVC 名称 \ No newline at end of file