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, usingSecretis highly recommended. - Service Discovery: All services are exposed internally within the cluster as
ClusterIPtypeService, enabling communication via service names. - External Access: An
Ingressresource 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-storagesolution based on node-local paths is used. This requires you to manually create storage directories on a specific K8s node and statically bind them usingPersistentVolume(PV) andPersistentVolumeClaim(PVC).
1. Prerequisites
Before you begin, please ensure you have the following:
- A running Kubernetes cluster.
- The
kubectlcommand-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
masterin 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_PASSWORDandMYSQL_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.