From 502d9389a65b4e2fd6423f2fac8bdf0782e3b7c1 Mon Sep 17 00:00:00 2001 From: pig6z2klp <431960330@qq.com> Date: Mon, 13 Oct 2025 17:22:41 +0800 Subject: [PATCH] Update k8s-en.md --- src/DjangoBlog-master/docs/k8s-en.md | 70 ++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 19 deletions(-) diff --git a/src/DjangoBlog-master/docs/k8s-en.md b/src/DjangoBlog-master/docs/k8s-en.md index 20e9527..3a29655 100644 --- a/src/DjangoBlog-master/docs/k8s-en.md +++ b/src/DjangoBlog-master/docs/k8s-en.md @@ -28,7 +28,10 @@ Before you begin, please ensure you have the following: We recommend deploying all DjangoBlog-related resources in a dedicated namespace for better management. ```bash -# Create a namespace named 'djangoblog' +# 创建一个名为'djangoblog'的命名空间(namespace) +# 命名空间用于在 Kubernetes 集群中隔离不同的应用或环境(如开发、测试、生产) +# 此处创建'djangoblog'命名空间,通常用于部署与 djangoblog 应用相关的资源 +# 后续部署的 Pod、Service 等资源可指定在此命名空间下,避免与其他应用资源冲突 kubectl create namespace djangoblog ``` @@ -37,16 +40,26 @@ kubectl create namespace djangoblog This setup uses Local Persistent Volumes. You need to create the data storage directories on a node within your cluster (the default is the `master` node in `pv.yaml`). ```bash -# Log in to your master node +# 通过SSH登录到主节点(master node) +# user为登录用户名,master-node为主节点的地址(可是IP或域名) ssh user@master-node -# Create the required storage directories +# 创建所需的存储目录(使用sudo获取管理员权限) +# -p 选项确保在父目录不存在时自动创建,避免报错 + +# 用于数据库(如MySQL)的本地存储目录 sudo mkdir -p /mnt/local-storage-db + +# 用于djangoblog应用的本地存储目录 sudo mkdir -p /mnt/local-storage-djangoblog + +# 通用资源存储目录 sudo mkdir -p /mnt/resource/ + +# 用于Elasticsearch的本地存储目录 sudo mkdir -p /mnt/local-storage-elasticsearch -# Log out from the node +# 从节点退出,返回本地终端 exit ``` **Note**: If you wish to store data on a different node or use different paths, you must modify the `nodeAffinity` and `local.path` settings in the `deploy/k8s/pv.yaml` file. @@ -54,13 +67,16 @@ exit After creating the directories, apply the storage-related configurations: ```bash -# Apply the StorageClass +# 应用StorageClass配置(存储类,用于定义持久化存储的类型和属性) +# StorageClass用于动态供应持久卷(PV),简化存储管理 kubectl apply -f deploy/k8s/storageclass.yaml -# Apply the PersistentVolumes (PVs) +# 应用持久卷(PersistentVolumes, PVs)配置 +# PV是集群中的一块存储资源,由管理员预先创建或通过StorageClass动态生成 kubectl apply -f deploy/k8s/pv.yaml -# Apply the PersistentVolumeClaims (PVCs) +# 应用持久卷声明(PersistentVolumeClaims, PVCs)配置 +# PVC是用户对存储资源的请求,用于绑定PV并为Pod提供持久化存储 kubectl apply -f deploy/k8s/pvc.yaml ``` @@ -73,10 +89,12 @@ Before deploying the application, you need to edit the `deploy/k8s/configmap.yam - `DJANGO_MYSQL_PASSWORD` and `MYSQL_ROOT_PASSWORD`: Change to your own secure database password. ```bash -# Edit the ConfigMap file +# 使用vim编辑器编辑ConfigMap配置文件 +# ConfigMap用于存储非敏感的配置数据,供Pod中的容器使用 vim deploy/k8s/configmap.yaml -# Apply the configuration +# 应用ConfigMap配置到Kubernetes集群 +# 执行后,配置数据将被创建或更新,供相关资源(如Pod)引用 kubectl apply -f deploy/k8s/configmap.yaml ``` @@ -85,10 +103,12 @@ kubectl apply -f deploy/k8s/configmap.yaml Now, we can deploy all the core services. ```bash -# Deploy the Deployments (DjangoBlog, MySQL, Redis, Nginx, ES) +# 部署 Deployment 资源(包含DjangoBlog应用、MySQL数据库、Redis缓存、Nginx服务器、Elasticsearch搜索引擎) +# Deployment 用于定义Pod的期望状态,负责创建和管理Pod,支持滚动更新等功能 kubectl apply -f deploy/k8s/deployment.yaml -# Deploy the Services (to create internal endpoints for the Deployments) +# 部署 Service 资源(为上述Deployment创建内部访问端点) +# Service 提供固定的访问地址,实现Pod的负载均衡和服务发现,即使Pod重建IP变化,也能通过Service稳定访问 kubectl apply -f deploy/k8s/service.yaml ``` @@ -103,7 +123,10 @@ kubectl get pods -n djangoblog -w Finally, expose the Nginx service to external traffic by applying the `Ingress` rule. ```bash -# Apply the Ingress rule +# 应用Ingress规则配置 +# Ingress用于管理Kubernetes集群外部访问集群内部服务的规则(如HTTP/HTTPS路由) +# 该配置文件(deploy/k8s/gateway.yaml)定义了外部请求如何映射到集群内的服务(如通过域名路由到对应的Service) +# 执行后,Ingress控制器将根据规则处理外部流量,实现对集群内服务的访问 kubectl apply -f deploy/k8s/gateway.yaml ``` @@ -118,23 +141,32 @@ kubectl get ingress -n djangoblog Similar to the Docker deployment, you need to get a shell into the DjangoBlog application Pod to perform database initialization and create a superuser on the first run. ```bash -# First, get the name of a djangoblog pod +# 首先,获取djangoblog相关Pod的名称 +# -n djangoblog:指定在'djangoblog'命名空间中查询 +# grep djangoblog:筛选出包含'djangoblog'关键词的Pod(即目标应用的Pod) kubectl get pods -n djangoblog | grep djangoblog -# Exec into one of the Pods (replace [pod-name] with the name from the previous step) +# 进入其中一个Pod的交互式终端(将[pod-name]替换为上一步获取的Pod名称) +# -it:以交互式终端模式进入Pod +# -n djangoblog:指定目标Pod所在的命名空间 +# -- bash:在Pod内启动bash shell kubectl exec -it [pod-name] -n djangoblog -- bash -# Inside the Pod, run the following commands: -# Create a superuser account (follow the prompts) +# 在Pod内部,运行以下命令: + +# 创建超级用户账号(按照提示输入用户名、邮箱和密码) +# 超级用户用于登录Django管理后台,拥有最高权限 python manage.py createsuperuser -# (Optional) Create some test data +# (可选)创建一些测试数据 +# 用于快速填充数据库,方便测试应用功能 python manage.py create_testdata -# (Optional, if ES is enabled) Create the search index +# (可选,如果启用了Elasticsearch)创建搜索索引 +# 初始化或更新Elasticsearch的搜索索引,确保搜索功能可用 python manage.py rebuild_index -# Exit the Pod +# 退出Pod的终端,返回本地命令行 exit ```