AWS EKS(Integration of kubernetes and AWS Cloud).

Anish Garg
7 min readJul 12, 2020

What is AWS EKS?

EKS stands for Elastic Kubernetes Service, which is an Amazon offering that helps in running the Kubernetes on AWS without requiring the user to maintain their own Kubernetes control plane. It is a fully managed service by Amazon.
Amazon EKS helps run Kubernetes control plane instances over multiple Availability Zones which makes sure that they are highly available and it also can be integrated with other Amazon service in order to provide scalability and security for user applications.

EKS creates master and slave nodes and set up entire Kubernetes cluster.

What is Kubernetes?

Kubernetes is a open-source,extensible and portable platform for container-orchestration system that is used for automating the process of deploying, scaling, and managing computer applications.It has a large, rapidly growing ecosystem. It keeps on monitoring the containers, if any container goes down, behind the scenes it requests docker to create another container with the same data.

what is AWS?

Amazon Web Services (AWS) is a subsidiary of Amazon that provides
On-Demand Cloud Computing Platform and APIs to individuals, companies and goverments on a Pay-as-you-go basis.AWS services provides a real CPUs, GPUs, RAM, HDD/SDD(unlimited storage), Operating System image , databases and many more services. AWS is a platform that offers flexible, reliable, scalable, easy-to-use and cost-effective cloud computing solutions.The platform is developed with a combination of infrastructure as a service (IaaS), platform as a service (PaaS) and packaged software as a service (SaaS) offerings.

How does AWS EKS work?🤔

  1. First, create an Amazon EKS cluster in the AWS Management Console or with the AWS CLI or one of the AWS SDKs.
  2. Then, launch worker nodes that register with the Amazon EKS cluster. We provide you with an AWS CloudFormation template that automatically configures your nodes.
  3. When your cluster is ready, you can configure your favorite Kubernetes tools (such as kubectl) to communicate with your cluster.
  4. Deploy and manage applications on your Amazon EKS cluster the same way that you would with any other Kubernetes environment.

What is eksctl?

Eksctl is a weave community slack command. It launch the cluster for us and lot’s of customisation we can do with this command.It is a simple cli tool for creating clusters on EKS. For setup click on this

Our Aim:

To Create a Kubernetes cluster using AWS EKS and integrate with EC2 , ELB , EBS , EFS. Then launch two pods on that cluster, one containing WordPress image and other containing MYSQL image for database, connecting this pod to a Load Balancer provided by the ELB service of AWS to expose this pod and provide these pods persistent volume through EFS storage.

Practical Time:

Step 1: Create an IAM user with administration Access.

If you want to login with CLI then use the aws configurecommand and give the access key, secret key from credential file and region name.

Step 2: Create EKS Cluster

I am creating cluster having 2 node groups. First node group have the 2 nodes and second have the 1 node with the help of yaml code.

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: anish-cluster
region: ap-south-1
nodeGroups:
- name: ng-1
desiredCapacity: 2
instanceType: t2.micro
ssh:
publicKeyName: key-eks
- name: ng-2
desiredCapacity: 1
instanceType: t2.small
ssh:
publicKeyName: key-eks

Now, Run this command to create cluster

eksctl create cluster -f cluster.yml

Now, you can see 3 nodes are deployed in aws

  • Now , we need to update our kubeconfig file so that kubectl command can work and we can connect to cluster from outside world.

aws eks update-kubeconfig --name anish-cluster

Step 3: Create a storage using EFS(Elastic File Storage)

EFS is a service of AWS which is used for providing persistent storage to our pods. For using aws EFS as storage we have to just install amazon-efs-utils on all worker nodes.While creating the EFS storage we have to create it in same VPC in which our nodes has been created so that they can connect to the EFS storage.

sudo yum install amazon-efs-utils 

Now, we are creating a provisioner for EFS, so that kubectl can connect to the EFS storage that is running on the cloud. So, we are writing a yaml for it.

apiVersion: apps/v1
kind: Deployment
metadata:
name: efs-provisioner
spec:
selector:
matchLabels:
app: efs-provisioner
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: efs-provisioner
spec:
containers:
- name: efs-provisioner
image: quay.io/external_storage/efs-provisioner:v0.1.0
env:
- name: FILE_SYSTEM_ID
value: fs-747cf6a5
- name: AWS_REGION
value: ap-south-1
- name: PROVISIONER_NAME
value: eks-prov/aws-efs
volumeMounts:
- name: pv-volume
mountPath: /persistentvolumes
volumes:
- name: pv-volume
nfs:
server: fs-747cf6a5.efs.ap-south-1.amazonaws.com
path: /

Now create a namespace and set name space as default

kubectl create ns wp-mysql  # To create nskubectl config set-context  --current --namespace=wp-mysql

Now create a RBAC file:
Role-based access control (RBAC) is a method of regulating access to a computer or network resources based on the roles of individual users within your organization.
RBAC authorization uses the rbac.authorization.k8s.io API group to drive authorization decisions, allowing you to dynamically configure policies through the Kubernetes API.

apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: nfs-prov-role-binding
subjects:
- kind: ServiceAccount
name: default
namespace: wp-mysql
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io

Now, we need PVC as storage class. But why?
If we write anything in our pod , and restart the pod, we will lose entire data . so if we attach PVC then, we can retrieve our data even if we delete or restart the pods.

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: aws-efs
provisioner: eks-prov/aws-efs

Step 4: Create MYSQL Deployment

Now, we are launching a MYSQL database which will be connected to WordPress. For this, I have launched a pod using MYSQL version 5.7 image. I have picked the environment variables form the pre-created secret and I have also created a service type- ClusterIP which would be connected to this pod.

apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: efs-mysql
annotations:
volume.beta.kubernetes.io/storage-class: "aws-efs"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: mysql
spec:
containers:
- image: mysql:5.6
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: efs-mysql

Now I have launched a pod using WordPress version 4.8-apache image and provided the environment variables and I have created a service type-Load Balancer which would help us to expose this pod to the outside world so that we could connect to the WordPress.

apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: efs-wordpress
annotations:
volume.beta.kubernetes.io/storage-class: "aws-efs"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 20Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: wordpress
labels:
app: wordpress
spec:
selector:
matchLabels:
app: wordpress
tier: frontend
strategy:
type: Recreate
template:
metadata:
labels:
app: wordpress
tier: frontend
spec:
containers:
- image: wordpress:4.8-apache
name: wordpress
env:
- name: WORDPRESS_DB_HOST
value: wordpress-mysql
- name: WORDPRESS_DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-pass
key: password
ports:
- containerPort: 80
name: wordpress
volumeMounts:
- name: wordpress-persistent-storage
mountPath: /var/www/html
volumes:
- name: wordpress-persistent-storage
persistentVolumeClaim:
claimName: efs-wordpress

Create Kustomization file:

Kustomization file declares the customization provided by the kustomize program.

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: mysql-pass
literals:
- password=redhat
resources:
- mysql-deployment.yaml
- wordpress-deployment.yaml

Now, apply your kustomization:

kubectl apply -k

After creating this file we can check using WebUI that a Load balancer is created. Now we can use the DNS provided by the ELB to connect to the WordPress.

  • This is the how my Web-UI look of my WordPress site and now we can login into the WordPress and create some websites or posts
First WordPress Page
Give Your details here

Yeahhh, our Wordpress is successfully launched.🥳🎉

It’s Done😎. Thanks For Reading🙏.

Any Suggestions and queries are most welcome .Feel free to comment below for any query or suggestions !!!

--

--

Anish Garg

Computer related tech, Cyber Security, Cloud Computing