Skip to content

Use RDS Database for Workloads running on AWS EKS Cluster

Step-01: Introduction

  • What are the problems with MySQL Pod & EBS CSI?
  • How we are going to solve them using AWS RDS Database?

Kubernetes Manifests

#02-UserManagementMicroservice-Deployment-Service.yml
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: usermgmt-microservice
  labels:
    app: usermgmt-restapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: usermgmt-restapp
  template:  
    metadata:
      labels: 
        app: usermgmt-restapp
    spec:
      initContainers:
        - name: init-db
          image: busybox:1.31
          command: ['sh', '-c', 'echo -e "Checking for the availability of MySQL Server deployment"; while ! nc -z mysql 3306; do sleep 1; printf "-"; done; echo -e "  >> MySQL DB Server has started";']      
      containers:
        - name: usermgmt-restapp
          image: stacksimplify/kube-usermanagement-microservice:1.0.0  
          ports: 
            - containerPort: 8095           
          env:
            - name: DB_HOSTNAME
              value: "mysql"            
            - name: DB_PORT
              value: "3306"            
            - name: DB_NAME
              value: "usermgmt"            
            - name: DB_USERNAME
              value: "dbadmin"        # RDS DB Username is dbadmin     
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-db-password
                  key: db-password           
          livenessProbe:
            exec:
              command: 
                - /bin/sh
                - -c 
                - nc -z localhost 8095
            initialDelaySeconds: 60
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /usermgmt/health-status
              port: 8095
            initialDelaySeconds: 60
            periodSeconds: 10          
#03-UserManagement-Service.yml
apiVersion: v1
kind: Service
metadata:
  name: usermgmt-restapp-service
  labels: 
    app: usermgmt-restapp
spec:
  type: NodePort
  selector:
    app: usermgmt-restapp
  ports: 
    - port: 8095
      targetPort: 8095
      nodePort: 31231
#04-Kubernetes-Secrets.yml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-db-password
type: Opaque
data: 
  db-password: ZGJwYXNzd29yZDEx

Step-02: Create RDS Database

Review VPC of our EKS Cluster

  • Go to Services -> VPC
  • VPC Name: eksctl-eksdemo1-cluster/VPC

Pre-requisite-1: Create DB Security Group

  • Create security group to allow access for RDS Database on port 3306
  • Security group name: eks_rds_db_sg
  • Description: Allow access for RDS Database on Port 3306
  • VPC: eksctl-eksdemo1-cluster/VPC
  • Inbound Rules
  • Type: MySQL/Aurora
  • Protocol: TPC
  • Port: 3306
  • Source: Anywhere (0.0.0.0/0)
  • Description: Allow access for RDS Database on Port 3306
  • Outbound Rules
  • Leave to defaults

Pre-requisite-2: Create DB Subnet Group in RDS

  • Go to RDS -> Subnet Groups
  • Click on Create DB Subnet Group
  • Name: eks-rds-db-subnetgroup
  • Description: EKS RDS DB Subnet Group
  • VPC: eksctl-eksdemo1-cluster/VPC
  • Availability Zones: us-east-1a, us-east-1b
  • Subnets: 2 subnets in 2 AZs
  • Click on Create

Create RDS Database

  • Go to Services -> RDS
  • Click on Create Database
  • Choose a Database Creation Method: Standard Create
  • Engine Options: MySQL
  • Edition: MySQL Community
  • Version: 5.7.22 (default populated)
  • Template Size: Free Tier
  • DB instance identifier: usermgmtdb
  • Master Username: dbadmin
  • Master Password: dbpassword11
  • Confirm Password: dbpassword11
  • DB Instance Size: leave to defaults
  • Storage: leave to defaults
  • Connectivity
    • VPC: eksctl-eksdemo1-cluster/VPC
    • Additional Connectivity Configuration
    • Subnet Group: eks-rds-db-subnetgroup
    • Publicyly accessible: YES (for our learning and troubleshooting - if required)
    • VPC Security Group: Create New
    • Name: eks-rds-db-securitygroup
    • Availability Zone: No Preference
    • Database Port: 3306
  • Rest all leave to defaults
  • Click on Create Database

Edit Database Security to Allow Access from 0.0.0.0/0

  • Go to EC2 -> Security Groups -> eks-rds-db-securitygroup
  • Edit Inboud Rules
  • Source: Anywhere (0.0.0.0/0) (Allow access from everywhere for now)

AWS EKS - Elastic Kubernetes Service - Masterclass

Image

Step-03: Create Kubernetes externalName service Manifest and Deploy

  • Create mysql externalName Service
  • 01-MySQL-externalName-Service.yml
    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      type: ExternalName
      externalName: usermgmtdb.c7hldelt9xfp.us-east-1.rds.amazonaws.com
    
  • Deploy Manifest
    kubectl apply -f kube-manifests/01-MySQL-externalName-Service.yml
    

Step-04: Connect to RDS Database using kubectl and create usermgmt schema/db

kubectl run -it --rm --image=mysql:5.7.22 --restart=Never mysql-client -- mysql -h usermgmtdb.c7hldelt9xfp.us-east-1.rds.amazonaws.com -u dbadmin -pdbpassword11

mysql> show schemas;
mysql> create database usermgmt;
mysql> show schemas;
mysql> exit

Step-05: In User Management Microservice deployment file change username from root to dbadmin

  • 02-UserManagementMicroservice-Deployment-Service.yml
    # Change From
              - name: DB_USERNAME
                value: "root"
    
    # Change To
              - name: DB_USERNAME
                value: "dbadmin"            
    

Step-06: Deploy User Management Microservice and Test

# Deploy all Manifests
kubectl apply -f kube-manifests/

# List Pods
kubectl get pods

# Stream pod logs to verify DB Connection is successful from SpringBoot Application
kubectl logs -f <pod-name>

Step-07: Access Application

# Capture Worker Node External IP or Public IP
kubectl get nodes -o wide

# Access Application
http://<Worker-Node-Public-Ip>:31231/usermgmt/health-status

Step-08: Clean Up

# Delete all Objects created
kubectl delete -f kube-manifests/

# Verify current Kubernetes Objects
kubectl get all