Deployment of Wordpress & MySql on the top of K8S Cluster through AMAZON EKS
What is EKS?
Amazon Elastic Kubernetes Service (Amazon EKS) is a fully managed Kubernetes service. Customers such as Intel, Snap, Intuit, GoDaddy, and Autodesk trust EKS to run their most sensitive and mission critical applications because of its security, reliability, and scalability.
EKS runs upstream Kubernetes and is certified Kubernetes conformant so you can leverage all benefits of open source tooling from the community. You can also easily migrate any standard Kubernetes application to EKS without needing to refactor your code.
Amazon Elastic File System (Amazon EFS) provides a simple, scalable, fully managed elastic NFS file system for use with AWS Cloud services and on-premises resources.
Pre-requisites:-
- AWS account
- AWS CLI configured in your device
- eksctl downloaded and path set
- kubectl downloaded and path set
Step-1
First we need to install AWS CLI and then create an IAM user with admin powers and configure AWS:
We create an EKS cluster from CLI using YML format:
apiVersion: eksctl.io/v1alpha5 kind: ClusterConfig metadata: name: cluster region: ap-south-1 nodeGroups: - name: ng1 desiredCapacity: 2 instanceType: t2.micro ssh: publicKeyName: key11 - name: ng2 desiredCapacity: 1 instanceType: t2.small ssh: publicKeyName: key11 - 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: key11
Execute below command to run above file:
eksctl create cluster -f cluster.yml
Output:-
wait until the cluster is launched successfully;
And the EC2 instances to see all the instances are running
Now for connecting to the master, kubectl command require config file (.kube) having IP of cluster, Username and Password.
To update config file to allow kubectl to send instructions to master node.
aws eks update-kubeconfig --name cluster
Output:
Now we create an EFS storage and choose EKS cluster VPC and Security group.
Creating a namespace so that everything is stored in a single plane
kubectl create namespace abns
Creating an EFS provisioner that allows us to mount EFS storage as PersistentVolumes in kubernetes. The container reads a configmap which contains the EFS filesystem ID, the AWS region and the name you want to use for your efs-provisioner.
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-b61f9567 - name: AWS_REGION value: ap-south-1 - name: PROVISIONER_NAME value: lw-course/aws-efs volumeMounts: - name: pv-volume mountPath: /persistentvolumes volumes: - name: pv-volume nfs: server: fs-b61f9567.efs.ap-south-1.amazonaws.com path: /
run the command in CMD to execute above file:
Then we provide rbac permissions . ClusterRole can be used to grant the same permissions as a Role.
--- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRoleBinding metadata: name: nfs-provisioner-role-binding subjects: - kind: ServiceAccount name: default namespace: abns roleRef: kind: ClusterRole name: cluster-admin apiGroup: rbac.authorization.k8s.io
run the below given command in CMD to create rbac permissions:
kubectl create -f create-rbac.yaml
Creating a secret for mysql password:
kubectl create secret generic mysql-pass --from-literal=password=akanksha -n abns
Creating a storage class so that we enable data persistancy through EFS. Provision PVC for both MySQL and word press deployments.
kind: StorageClass apiVersion: storage.k8s.io/v1 metadata: name: aws-efs provisioner: lw-course/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: mysql-pvc annotations: volume.beta.kubernetes.io/storage-class: "aws-efs" spec: accessModes: - ReadWriteMany resources: requests: storage: 10Gi
Run the command to execute above file:
kubectl create -f storage.yaml
Now create a ELB service to allow WordPress to access MySQL DB and deploy MySQL
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
Run this command in CMD to execute above file:
kubectl create -f mysql.yaml
Creating a ELB service to allow clients to access WordPress and deploy WordPress.
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
Run the command in CMD to execute the above:
kubectl create -f wordpress.yaml
Checking if all the resources are running use the command:
kubectl get all -n abns -o wide
Thanks!!
Software Engineer
4 年Nice work ??