- 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.
How does Amazon EKS work?
- First, create an Amazon EKS cluster in the AWS Management Console or with the AWS CLI or one of the AWS SDKs.
- Then, launch worker nodes that register with the Amazon EKS cluster. We provide you with an AWS CloudFormation template that automatically configures your nodes.
- When your cluster is ready, you can configure your favorite Kubernetes tools (such as kubectl) to communicate with your cluster.
- Deploy and manage applications on your Amazon EKS cluster the same way that you would with any other Kubernetes environment.
For more information about creating your required resources and your first Amazon EKS cluster, see Getting started with Amazon EKS.
SOLUTION:-
In EKS service the Master Node of K8S will be managed by the AWS and we have to plan our slave Nodes . we are gonna create so many pods so in total i have used 3 Nodes you can plan yous structure according to your need and budget.
Step1 : First create IAM user with administartion access.
- Then for access AWS from command line use
aws configure
- And give your access key ,secret key and region name.
Step 2 : Create your EKS cluster
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfigmetadata:
name: ekscluster
region: ap-south-1nodeGroups:
- name: ng1
instanceType: t2.micro
desiredCapacity: 2
ssh:
publicKeyName: keycloudclass
- name: ng2
desiredCapacity: 1
instanceType: t2.small
ssh:
publicKeyName: keycloudclass
- This code will create a cluster having 2 Node groups and in total 3 Nodes.
eksctl create cluster -f cluster.yml
- After this you can see that 3 nodes are deployed on aws.
- You can also verify it using cmd
- After this yo 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 ekscluster
- It is always a good habit to crate a different namespace for every project, So I am creating a namespace for this;
kubectl create ns wp-mysql
- After this I am updating this namespace as my default namespace
kubectl config set-context --current --namespace=wp-mysql
Step 3 : Creation of PVC
- First create a File System so that you can use EFS as storage.
- For using amazon EFS as storage we have to just install amazon-efs-utils on all worker nodes.
sudo yum install amazon-efs-utils -y
- Now, create a provisioner for EFS.
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-3e42c8ef
- 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-3e42c8ef.efs.ap-south-1.amazonaws.com
path: /
Step 4 : After creation of provisioner , create a role binding.
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
- This will create a role binding . But we also needs PVCs so that data insides the pods can remain persistent so we have to create PVCs and as we will be using EFS so for this we need a storage class that support the EFS.
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: aws-efs
provisioner: eks-prov/aws-efs
Step 5:
- Now , I am creating mysql-deployment .
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 create your wordpess-deployment.
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
- Now , creating kustomization file.
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: mysql-pass
literals:
- password=redhat
resources:
- mysql-deployment.yaml
- wordpress-deployment.yaml
Step 6 :
- Now, apply your kustomization.
kubectl apply -k .
- Now for accessing your wordpres website go to ELB.