Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed Kubernetes service.
For creating a cluster on Kubernetes and launching WordPress and MYSQL on top of it tools required are as follows:
- aws cli
- kubectl
- eksctl
Creating a Kubernetes cluster :
First, an amazon IAM account is created with full Administration power. Download the credentials file and then use it to configure in CLI.
For launching a cluster we need to write a YML file with name cluster.yml as follows:
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig
metadata:
name: eks-cluster
region: ap-south-1nodeGroups:
- name: ng-1
instanceType: t2.micro
desiredCapacity: 2
ssh:
publicKeyName: mykey1122
- name: ng-2
instanceType: t2.small
desiredCapacity: 1
ssh:
publicKeyName: mykey1122
- name: ng-mixed
minSize: 2
maxSize: 5
instancesDistribution:
maxPrice: 0.017
instanceTypes: ["t3.small", "t3.medium"] # At least one instance type should be specified
onDemandBaseCapacity: 0
onDemandPercentageAboveBaseCapacity: 50
spotInstancePools: 2
ssh:
publicKeyName: mykey1122
Use the following command to create this cluster. The AWS UI shows the launched cluster.
eksctl create cluster -f cluster.yml
Next update the kubeconfig file by command:
aws eks --region ap-south-1 update-kubeconfig --name eks-cluster
To attach EFS we need to create it from UI in the same VPC as of our node so that they can connect.
We create a new namespace for provisioner.
kubectl create ns provisionerns
We need to create efs-provisioner.yml and update the FILE_SYSTEM_ID , server & your provisioner _name in it.
kind: Deployment
apiVersion: apps/v1
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-a1058f70
- name: AWS_REGION
value: ap-south-1
- name: PROVISIONER_NAME
value: isha/aws-efs
volumeMounts:
- name: pv-volume
mountPath: /persistentvolumes
volumes:
- name: pv-volume
nfs:
server: fs-a1058f70.efs.ap-south-1.amazonaws.com
path: /
create using:
kubectl create - f create-efs-provisioner.yml -n provisionerns
For some extra security, we need to create the Cluster Role Binding File.
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: nfs-provisioner-role-binding
subjects:
- kind: ServiceAccount
name: default
namespace: provisionerns
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
To run it use :
kubectl create - f create-rbac.yml -n provisionerns
Next, we create our own storage class.
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: aws-efs
provisioner: isha/aws-efs
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: efs-wordpress
annotations:
volume.beta.kubernetes.io/storage-class: "aws-efs"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: efs-mysql
annotations:
volume.beta.kubernetes.io/storage-class: "aws-efs"
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 10Gi
For creating it use:
kubectl create - f create-storage.yml -n provisionerns
For deploying WordPress and MySQL database using EFS as storage we will use the following yml files:
deploy-mysql.yml
apiVersion: v1
kind: Service
metadata:
name: wordpress-mysql
labels:
app: wordpress
spec:
ports:
- port: 3306
selector:
app: wordpress
tier: mysql
clusterIP: None
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
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
deploy-wordpress.yml
apiVersion: v1
kind: Service
metadata:
name: wordpress
labels:
app: wordpress
spec:
ports:
- port: 80
selector:
app: wordpress
tier: frontend
type: LoadBalancer
---
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
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
kustomization.yml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- name: mysql-pass
literals:
- password= redhat
resources:
- deploy-mysql.yaml - deploy-wordpress.yaml
By running the kustomization file the deploy-mysql.yml and deploy-wordpress.yml are also created. Use command:
kubectl create -k .
We get an IP of WordPress open it in the browser and set up your website and you are good to go.