You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SoftwareMethodology/src/DjangoBlog-master/docs/k8s-en.md

7.7 KiB

Deploying DjangoBlog with Kubernetes

This document guides you through deploying the DjangoBlog application on a Kubernetes (K8s) cluster. We provide a complete set of .yaml configuration files in the deploy/k8s directory to deploy a full service stack, including the DjangoBlog application, Nginx, MySQL, Redis, and Elasticsearch.

Architecture Overview

This deployment utilizes a microservices-based, cloud-native architecture:

  • Core Components: Each core service (DjangoBlog, Nginx, MySQL, Redis, Elasticsearch) runs as a separate Deployment.
  • Configuration Management: Nginx configurations and Django application environment variables are managed via ConfigMap. Note: For sensitive information like passwords, using Secret is highly recommended.
  • Service Discovery: All services are exposed internally within the cluster as ClusterIP type Service, enabling communication via service names.
  • External Access: An Ingress resource is used to route external HTTP traffic to the Nginx service, which acts as the single entry point for the entire blog application.
  • Data Persistence: A local-storage solution based on node-local paths is used. This requires you to manually create storage directories on a specific K8s node and statically bind them using PersistentVolume (PV) and PersistentVolumeClaim (PVC).

1. Prerequisites

Before you begin, please ensure you have the following:

  • A running Kubernetes cluster.
  • The kubectl command-line tool configured to connect to your cluster.
  • An Nginx Ingress Controller installed and configured in your cluster.
  • Filesystem access to one of the nodes in your cluster (defaulted to master in the configs) to create local storage directories.

2. Deployment Steps

Step 1: Create a Namespace

We recommend deploying all DjangoBlog-related resources in a dedicated namespace for better management.

# 创建一个名为'djangoblog'的命名空间namespace
# 命名空间用于在 Kubernetes 集群中隔离不同的应用或环境(如开发、测试、生产)
# 此处创建'djangoblog'命名空间,通常用于部署与 djangoblog 应用相关的资源
# 后续部署的 Pod、Service 等资源可指定在此命名空间下,避免与其他应用资源冲突
kubectl create namespace djangoblog

Step 2: Configure Persistent Storage

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).

# 通过SSH登录到主节点master node
# user为登录用户名master-node为主节点的地址可是IP或域名
ssh user@master-node

# 创建所需的存储目录使用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

# 从节点退出,返回本地终端
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.

After creating the directories, apply the storage-related configurations:

# 应用StorageClass配置存储类用于定义持久化存储的类型和属性
# StorageClass用于动态供应持久卷PV简化存储管理
kubectl apply -f deploy/k8s/storageclass.yaml

# 应用持久卷PersistentVolumes, PVs配置
# PV是集群中的一块存储资源由管理员预先创建或通过StorageClass动态生成
kubectl apply -f deploy/k8s/pv.yaml

# 应用持久卷声明PersistentVolumeClaims, PVCs配置
# PVC是用户对存储资源的请求用于绑定PV并为Pod提供持久化存储
kubectl apply -f deploy/k8s/pvc.yaml

Step 3: Configure the Application

Before deploying the application, you need to edit the deploy/k8s/configmap.yaml file to modify sensitive information and custom settings.

It is strongly recommended to change the following fields:

  • DJANGO_SECRET_KEY: Change to a random, complex string.
  • DJANGO_MYSQL_PASSWORD and MYSQL_ROOT_PASSWORD: Change to your own secure database password.
# 使用vim编辑器编辑ConfigMap配置文件
# ConfigMap用于存储非敏感的配置数据供Pod中的容器使用
vim deploy/k8s/configmap.yaml

# 应用ConfigMap配置到Kubernetes集群
# 执行后配置数据将被创建或更新供相关资源如Pod引用
kubectl apply -f deploy/k8s/configmap.yaml

Step 4: Deploy the Application Stack

Now, we can deploy all the core services.

# 部署 Deployment 资源包含DjangoBlog应用、MySQL数据库、Redis缓存、Nginx服务器、Elasticsearch搜索引擎
# Deployment 用于定义Pod的期望状态负责创建和管理Pod支持滚动更新等功能
kubectl apply -f deploy/k8s/deployment.yaml

# 部署 Service 资源为上述Deployment创建内部访问端点
# Service 提供固定的访问地址实现Pod的负载均衡和服务发现即使Pod重建IP变化也能通过Service稳定访问
kubectl apply -f deploy/k8s/service.yaml

The deployment may take some time. You can run the following command to check if all Pods are running successfully (STATUS should be Running):

kubectl get pods -n djangoblog -w

Step 5: Expose the Application Externally

Finally, expose the Nginx service to external traffic by applying the Ingress rule.

# 应用Ingress规则配置
# Ingress用于管理Kubernetes集群外部访问集群内部服务的规则如HTTP/HTTPS路由
# 该配置文件deploy/k8s/gateway.yaml定义了外部请求如何映射到集群内的服务如通过域名路由到对应的Service
# 执行后Ingress控制器将根据规则处理外部流量实现对集群内服务的访问
kubectl apply -f deploy/k8s/gateway.yaml

Once deployed, you can access your blog via the external IP address of your Ingress Controller. Use the following command to find the address:

kubectl get ingress -n djangoblog

Step 6: First-Time Initialization

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.

# 首先获取djangoblog相关Pod的名称
# -n djangoblog指定在'djangoblog'命名空间中查询
# grep djangoblog筛选出包含'djangoblog'关键词的Pod即目标应用的Pod
kubectl get pods -n djangoblog | grep djangoblog

# 进入其中一个Pod的交互式终端将[pod-name]替换为上一步获取的Pod名称
# -it以交互式终端模式进入Pod
# -n djangoblog指定目标Pod所在的命名空间
# -- bash在Pod内启动bash shell
kubectl exec -it [pod-name] -n djangoblog -- bash

# 在Pod内部运行以下命令

# 创建超级用户账号(按照提示输入用户名、邮箱和密码)
# 超级用户用于登录Django管理后台拥有最高权限
python manage.py createsuperuser

# (可选)创建一些测试数据
# 用于快速填充数据库,方便测试应用功能
python manage.py create_testdata

# 可选如果启用了Elasticsearch创建搜索索引
# 初始化或更新Elasticsearch的搜索索引确保搜索功能可用
python manage.py rebuild_index

# 退出Pod的终端返回本地命令行
exit

Congratulations! You have successfully deployed DjangoBlog on your Kubernetes cluster.