Automating Deployment Using Amazon Elastic Kubernetes Service
Chetan Vyas
MLOps | DevOps | Hybrid MultiCLoud | Ansible | Flutter | RedHat Linux | Openstack
In this article, you will find out how to use EKS to automate Deployment, scaling, and management of containerized applications. In this article, we will deploy WordPress with MySQL on the top of EKS.
What is Kubernetes :
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.
What is EKS :
Amazon Elastic Kubernetes Service (Amazon EKS) is a managed service that makes it easy for you to run Kubernetes on AWS without needing to stand up or maintain your own Kubernetes control plane. Kubernetes is an open-source system for automating the deployment, scaling, and management of containerized applications.
Amazon EKS runs Kubernetes control plane instances across multiple Availability Zones to ensure high availability. Amazon EKS automatically detects and replaces unhealthy control plane instances, and it provides automated version upgrades and patching for them.
Why EKS :
You can build web applications that automatically scale up and down and run in a highly available configuration across multiple Availability Zones. By running on EKS, your web applications benefit from the performance, scale, reliability, and availability of the AWS. Additionally, your services get out-of-the-box integrations with AWS networking and security services, such as Application Load Balancers for load distribution of your web application and VPC for networking.
Amazon EKS is also integrated with many AWS services to provide scalability and security for your applications, including the following:
- Amazon ECR for container images
- Elastic Load Balancing for load distribution
- IAM for authentication
- Amazon VPC for isolation
Pre-Requisites:
- You need an AWS account and an IAM user account with Administrator access power.
- Download and configure AWS cli command
- Install kubectl -The kubectl command-line tool lets you control Kubernetes clusters.
- Install eksctl - It is a simple CLI tool for creating clusters on EKS - Amazon's new managed Kubernetes service for EC2.
Let's Start -
Creating Cluster :
First, we have to create or set up the EKS cluster. And while creating cluster we have to define Availability Zone, node groups or the number of slave nodes and instance type according to our requirement.
The numbers of pods that we can launch inside a particular node depend on the instance type example: in the node of t2.micro instance type we can launch 4 pods.
Now I am going to create YAML code to create cluster :
You can see I am going to launch the cluster in ap-south-1 [Mumbai] region with 3 slave nodes of t2.micro instance type. And don't forget to add ssh public key otherwise you can't able to login inside nodes.
Just run following command to launch cluster :
eksctl create cluster -f cluster.yml
we will use kubectl to manage our kubernetes cluster and to configure kubectl run following command.
aws eks update-kubeconfig --name ekscluster
after this, you can check whether the cluster is created or not using CLI and GUI
Creating EFS Provisioner for PVC :
In EKS we can use EBS for Persistent Volume because EKS already integrated with EBS and we have provisioner for EBS, but we have some issues with EBS. We can't attach EBS to multiple instances [node] at a time. So for Persistent Volume we will use EFS because :
We can attach EFS to multiple instances at a time and an EFS file system can be accessed from multiple availability zones and it is valuable for a multi-AZ cluster.
But for EFS we don't have a provisioner, so to use EFS for Persistent Volume we have to first create provisioner. To create EFS provisioner follow the steps :
Go to AWS and create EFS :
Then we have to install amazon-efs-utils in our slave nodes for login to node using ssh:
ssh -i myclusterkey.pem -l ec2-user 35.154.201.155
and then use yum command to install EFS client software
yum install amazon-efs-utils -y
now I am creating YAML to for EFS provisioner :
run below command to create provisioner
kubectl create ns myns kubectl create -f efs_provisioner.yml -n myns
now we have to do Role Binding, just follow steps:
YAML code for cluster role binding
then run create command
kubectl create -f rabc.yml -n myns
Done
Creating Persistent Volume :
A PersistentVolume (PV) is a piece of storage in the cluster that has been manually provisioned by an administrator, or dynamically provisioned by Kubernetes using a StorageClass. A PersistentVolumeClaim (PVC) is a request for storage by a user that can be fulfilled by a PV. PersistentVolumes and PersistentVolumeClaims are independent from Pod lifecycles and preserve data through restarting, rescheduling, and even deleting Pods.
To create PVC first we have to create Storage Class [sc]. so let's create code for SC :
we will deploy WordPress with MySQL database so we have to create different PVC for WordPress and MySQL :
Run create command to create SC and PVC :
kubectl create -f mypvc.yml -n myns
You can check whether it created or not using kubectl get command
Launching WordPress and MySQL by creating Deployment and Services :
WordPress (WP, WordPress.org) is a free and open-source content management system (CMS) written in PHP[4] and paired with MySQL or MariaDB database.WordPress is the simplest, most popular way to create your own website or blog. In fact, WordPress powers over 37.6% of all the websites on the Internet. Yes – more than one in four websites that you visit are likely powered by WordPress.
MySQL is a database management system that allows you to manage relational databases.If you develop websites or web applications, MySQL is a good choice. MySQL is an essential component of the LAMP stack, which includes Linux, Apache, MySQL, and PHP.MySQL can run on various platforms UNIX, Linux, Windows, etc. You can install it on a server or even on a desktop. Besides, MySQL is reliable, scalable, and fast.
Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. In this way, Deployments help ensure that one or more instances of your application are available to serve user requests. Deployments are managed by the Kubernetes Deployment controller.
Service is an abstract way to expose an application running on a set of Pods as a network service. With Kubernetes you don't need to modify your application to use an unfamiliar service discovery mechanism. Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods and can load-balance across them.
Hence we will create a deployment for WordPress so it will provide facilities like replicas, scaling, if pod goes down it will automatically launch new pod, we can do Rolling Updates
We also use one more Kubernetes Resource that is known as services for a Load balancer so it will manage ingress traffic over pods and we can expose our pods.
create a secret where we can store our credential or password
ubectl create -f secret.yml -n myns
so let's create code,
- MySQL :
Here we attach PCV that we created already. And for the password we used our secret mysql-pass.
then run create command and launch MySQL
kubectl create -f deploy-mysql.yml -n myns
- WordPress :
Similarly, create code for WordPress again here we will attach PVC and here we use the environment variable to attach WordPress with MySQL.
Run create command and launch WordPress too :
kubectl create -f deploy-wordpress.yml -n myns
Done
Now you can see our pods are launched . Just use Load Balancer IP to access WordPress and do the initial setup :