In recent years, containers and container orchestration systems have become increasingly popular for deploying and managing applications. Amazon Web Services (AWS) offers the Elastic Kubernetes Service (EKS) as a managed Kubernetes service, making it easier to deploy, manage, and scale containerized applications using Kubernetes.
In this blog post, we will discuss how to use GitHub Actions to automate the deployment of an EKS cluster and an application on the cluster.
Before we begin, it is assumed that you have an AWS account and have already created an EKS cluster. If not, please refer to the AWS documentation for guidance.
Prerequisites
Before we get started, make sure you have the following installed:
Git
Docker
Minikube
kubectl
eksctl
aws cli
Overview
In this project, I have created a GitHub twin finder application using Django, which uses data from GitHub to identify users who have similar repositories and code contributions. After building the Docker image, I pushed it to the Docker Hub registry using the docker push
command. This made it accessible to the EKS cluster for deployment. After building and testing the application locally, I deployed it on the Amazon Elastic Kubernetes Service (EKS) to make it accessible to a wider audience.
The following steps will guide you through the end-to-end automation process:
Create an IAM role
Certainly. To enable GitHub Actions to access the EKS cluster, you must create an IAM role with the necessary permissions. The IAM role should have permissions to interact with the EKS cluster, as well as any other resources that your application needs.
Here are the steps to create an IAM role:
Go to the AWS Management Console and navigate to the IAM service.
Click on "Roles" in the left navigation menu and then click the "Create role" button.
Select "AWS service" as the type of trusted entity and choose "EKS" as the service that will use the role.
Choose the "EKS" use case and click "Next: Permissions".
Search for and select the necessary policies for your application, such as "AmazonEKSClusterPolicy" and "AmazonEKSServicePolicy".
Click "Next: Tags" and add any tags that you want to associate with the role.
Click "Next: Review" and review the role details.
Provide a name for the role and click "Create role".
Note: Make sure to note down the access key ID and secret access key of the IAM user with the necessary permissions to create and manage the EKS cluster.
To create the EKS cluster, we will be using the AWS CLI. You can install the AWS CLI on your Linux machine by following the instructions provided in this link: docs.aws.amazon.com/cli/latest/userguide/in...
In addition, we will be using eksctl to communicate with the EKS cluster. You can download and install eksctl by following the instructions provided in this link: eksctl.io/introduction/installation.
Once you have installed the AWS CLI and eksctl, you can use eksctl to create the EKS cluster by providing the necessary parameters such as region, cluster name, instance types, etc.
Create cluster, subnet and node
configure the aws in your terminal :
aws configure
Certainly. If you prefer to create the EKS cluster using the AWS CLI instead of eksctl, you can use the following command:
$ eksctl create cluster --name githubtw \
--version 1.26 \
--region us-east-1 \
--nodegroup-name linux-nodes\
--node-type t2.micro \
--nodes 2
After executing the command to create the EKS cluster using the AWS CLI, you can navigate to the Amazon EKS service dashboard. There you should be able to see the new cluster that has been created, along with the associated CloudFormation stack.
Once the node group is created, you can view the list of EC2 instances that have been provisioned as worker nodes by navigating to the EC2 service in the AWS Management Console. This will allow you to monitor the performance of the worker nodes and perform any necessary maintenance or updates.
Create deployment.yaml for deploying and connecting to the cluster
apiVersion: apps/v1
kind: Deployment
metadata:
name: first-deployment
labels:
app: first
spec:
replicas: 3
selector:
matchLabels:
app: first
template:
metadata:
labels:
app: first
spec:
containers:
- name: first
image: docker.io/vikash98yadav/githubtwin:latest
imagePullPolicy: "Always"
ports:
- containerPort: 8000
Create service.yaml for exposing the port
apiVersion: v1
kind: Service
metadata:
name: first-service
spec:
type: NodePort
selector:
app: first
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 8000
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
Create the GitHub action main.yaml for automating the eks creating process
name: Eks Deployment
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: docker login
env:
DOCKER_USER: vikash98yadav
DOCKER_PASSWORD: Vikash@17apr
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Build the Docker image
run: docker build . --file Dockerfile --tag vikash98yadav/githubtwin
- name: Install kubectl
uses: azure/setup-kubectl@v2.0
with:
version: 'v1.24.0' # default is latest stable
id: install
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Docker Push
run: docker push vikash98yadav/githubtwin
- name: Update kube config
run: aws eks update-kubeconfig --name githubtw
- name: Deploy to EKS
run: |
kubectl apply -f service.yaml
kubectl get svc
To run this application
- set up the security group in the Ecs node instances as shown below.
Get the node external IP and combine with the service exposed address
$ kubeclt get nodes -o wide $ kubectl get svc
After deploying your application to the EKS cluster, you can expose the application to the internet by creating a Kubernetes service of type LoadBalancer
and specifying the necessary parameters such as port and target port.
Once the service is created, you can view the external IP address that has been assigned to the service by running the command kubectl get services
. This IP address can be used to access the application from the internet.
For example, if the external IP address assigned to the service is 44.107.106.238
and the application is running on port 30007
, then the URL to access the application would be 44.107.106.238:30007
.
Some basic commands -
To get information of the
$ kubectl get nodes
To get the service information
$ kubectl get svc
To get the deployment information
$ kubectl get deployment
full wide information
$ kubectl get node -0 wide
There are many more code for this , refer this -> https://docs.aws.amazon.com/cli/latest/reference/eks/index.html
Github link -> https://github.com/Vikash-8090-Yadav/GithubTwin
๐ Congratulations on creating and deploying your Django application on EKS ! You should be proud of yourself for taking this step towards building scalable and resilient applications. ๐
๐ก If you found this blog helpful and learned something new, please do give it a thumbs up and consider following me for more such tutorials. ๐
โ If you have any doubts or questions, please feel free to leave them in the comments section below. I would be more than happy to help you out. ๐ฌ
๐ Don't forget to follow me for more exciting content and tutorials on Django, Kubernetes, and other interesting technologies. Let's keep learning and growing together! ๐