It is essential to make your application/product available on the internet. This is generally achieved through deployment or hosting your application through any hosting platform or deploying your application to a server. In this article, I will be going over how to deploy your Django application with Elastic Kubernetes Service (EKS), but we need first to understand what EKS is, and why we should use it.
Let’s get right into it.
What is EKS
EKS stands for Elastic Kubernetes Service, it is a managed Kubernetes service built by Amazon. It is used for running Kubernetes clusters on AWS.
This means that when you create a new cluster, AWS helps you manage the master nodes, the scalability, and backup, with any other pre-installed apps on your cluster.
The only thing you need to create for your cluster is worker nodes.
There are reasons and situations when you might consider using EKS to deploy your applications, some of these reasons include:
- Managed Kubernetes control plane
- Managed Kubernetes data plane
- Managed node groups
- Integration with AWS
Now that we have gone through what EKS is and why we use EKS. Let us go through how we will deploy our Django app to EKS.
How to Deploy Django App With EKS
In this section, we will be going over how to deploy a Django application with EKS.
The following steps must be taken in order to deploy a Django app using Amazon Elastic Kubernetes Service (EKS:
1. Create an Amazon EKS cluster and set up kubectl to control it
To create an EKS cluster, kindly follow the steps outlined in the AWS documentation.
After the cluster is created, you will need to set up kubectl to gain control of the cluster, first, install kubectl on your local machine if it is not already installed. You can follow this brief tutorial on how to do so.
Then, run the following command to retrieve the cluster’s configuration:
aws eks --region region update-kubeconfig --name cluster_name
Replace region with the region where your cluster is located, and cluster_name with the name of your cluster. This command will update your kubeconfig file with the necessary information to access your cluster.
You can then use kubectl to control your EKS cluster. For example, you can use the following command to view the nodes in your cluster:
kubectl get nodes
You can also use
kubectl to deploy applications to your cluster, view and manage cluster resources, and perform other tasks.
2. Create a Docker image of your Django app
Create a Docker image of your Django app and push it to a Docker registry, such as Amazon Elastic Container Registry (ECR) or Docker Hub. You can do this by creating a Dockerfile that specifies the base image, such as Python, and the commands to install your app’s dependencies and run the app.
Here is an example of how you can create a Docker image of your Django app:
First, create a Dockerfile in the root directory of your Django project. A Dockerfile is a text file that contains the instructions for building a Docker image.
In the Dockerfile, specify the base image that you want to use for your Docker image. For Django, you can use a base image that includes Python and Django, such as python:3.8-slim.
Next, copy your Django app files into the Docker image. You can do this using the COPY command. For example:
COPY . /app
Change to the
/app directory and install any necessary Python dependencies using the
pip command. For example:
WORKDIR /app RUN pip install -r requirements.txt
Set the default command to run the Django development server. You can do this using the
CMD command. For example:
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]
Save the Dockerfile.
To build the Docker image, navigate to the root directory of your Django project in a terminal and run the following command:
docker build -t django-app .
Replace django-app with the desired name for your Docker image.
This will build the Docker image and save it to your local machine. You can then run the image using the docker run command.
For example, to run the image on port 8000:
docker run -p 8000:8000 django-app
This will start the Django development server and you should be able to access your Django app at http://localhost:8000.
3. Create a Kubernetes deployment:
Create a Kubernetes deployment to manage the deployment and scaling of your Django app. The deployment should specify the Docker image, number of replicas, and any environment variables or secrets needed by the app.
After you have set up your EKS cluster and created your Django app’s Docker image, you can create a deployment and service to deploy the app.
First, create a deployment for your app. The deployment should specify the Docker image to use, the number of replicas to run, and any environment variables or secrets that your app needs.
To create the deployment, you can use the kubectl apply command:
kubectl apply -f deployment.yaml
This will create a deployment named django-app with two replicas running the specified Docker image.
Next, create a service to expose the deployment to the internet. The service should specify the type of service (such as LoadBalancer) and the port on which your app is running.
4. Create a Kubernetes service:
To make your app accessible to the internet, create a Kubernetes service. The type of service (such as load balancer) and port number where your app is executing should be specified by the service.
Here is an example of a service for a Django app:
apiVersion: v1 kind: Service metadata: name: django-app spec: type: LoadBalancer ports: - port: 80 targetPort: 8000 selector: app: django-app
To create the service, you can use the
kubectl apply command:
This will create a service named django-app that exposes the django-app deployment on port 80.
5. Control and manage the deployment:
You may use kubectl to control the deployment and inspect the app’s logs after the service and deployment have been built.
Once the deployment and service are created, you can use kubectl to manage the deployment and view the app’s logs.
kubectl get deployment kubectl logs deployment/django-app
You can also use the kubectl commands to scale the deployment and update the app’s image or configuration.
Once you have deployed your Django app with EKS, you can access it by using the public DNS name or IP address of the service.
To view the public DNS name or IP address of the service, you can use the kubectl command:
kubectl get service django-app
This will output the public DNS name or IP address of the service, which you can use to access your app in a web browser.
You can also use the kubectl command to view the status of the deployment and the app’s logs:
kubectl get deployment kubectl logs deployment/django-app
These commands can help you monitor the health of your app and troubleshoot any issues that may arise.
Additionally, you can use the kubectl commands to scale the deployment and update the app’s image or configuration. For example, you can use the kubectl scale command to increase or decrease the number of replicas in the deployment:
kubectl scale deployment/django-app --replicas=4
This will increase the number of replicas in the django-app deployment to four.
You can also use the kubectl set image command to update the Docker image used by the deployment:
kubectl set image deployment/django-app django-app=<new Docker image>
This will update the django-app deployment to use the specified Docker image.
Overall, deploying a Django app with EKS involves setting up an EKS cluster, creating a Docker image of the app, creating a deployment and service, and using kubectl to manage and monitor the app.
When deploying a Django app with EKS, you may need to configure certain aspects of the app or the deployment to ensure it runs correctly.
Handling Secrets in Kubernetes
One important consideration is how to handle secrets, such as database passwords or API keys. In Kubernetes, it is recommended to use secrets to store sensitive information, rather than storing it in plaintext in the deployment configuration.
To use secrets in a Django app deployment, you can create Kubernetes secrets and reference them in the deployment configuration. For example, here is a deployment configuration that uses a secret for the SECRET_KEY environment variable:
apiVersion: apps/v1 kind: Deployment metadata: name: django-app spec: replicas: 2 selector: matchLabels: app: django-app template: metadata: labels: app: django-app spec: containers: - name: django-app image: <Docker image> env: - name: SECRET_KEY valueFrom: secretKeyRef: name: django-app-secrets key: secret-key ports: - containerPort: 8000
This deployment configuration references a secret named django-app-secrets, which is assumed to exist in the same namespace as the deployment. The SECRET_KEY environment variable is set to the value of the secret-key key in the secret.
To create the secret, you can use the kubectl command:
kubectl create secret generic django-app-secrets \ --from-literal=secret-key=<secret value>
This will create a secret named django-app-secrets with a secret-key key and the specified secret value.
Another important consideration is how to handle database migrations when deploying a Django app. By default, Django will run any pending migrations when the app is started, which can cause issues if the migration schema is not compatible with the existing database schema.
To avoid this issue, you can use the migrate management command to run migrations before starting the app. For example, here is a Dockerfile that runs migrations before starting the app:
FROM python:3.8 # Install app dependencies COPY requirements.txt /app/ RUN pip install -r /app/requirements.txt # Copy app code COPY . /app/ # Run migrations RUN python /app/manage.py migrate # Start app CMD ["python", "/app/manage.py", "runserver", "0.0.0.0:8000"]
This Dockerfile installs the app’s dependencies, copies the app code, runs the migrate management command, and starts the app.
Once you have configured secrets and migrations in your deployment, you can use kubectl to manage and monitor the deployment and app as described previously.
In conclusion, using Amazon Elastic Kubernetes Service (EKS) to deploy a Django app enables you to execute the app in a scalable and reliable environment under Kubernetes management.
You can build up an EKS cluster, generate a Docker image of your app, and deploy the app using a Kubernetes deployment and service by following the procedures outlined in this topic.
Additionally, you may update the app by changing the Docker image, deployment configuration, and secrets, redeploying the deployment, and managing the deployment with kubectl.
Overall, setting up a Django project to be deployed with EKS is a simple procedure that can assist you in running and managing your app in the cloud.