Microservices architectures are famous for building scalable, flexible, and maintainable applications. Combining this architecture with cloud-native technologies like AWS EKS, Jenkins, and Docker can unlock robust CI/CD pipelines, provide high availability, and provide robust monitoring. In this post, we’ll walk through deploying a multi-tier microservices architecture using AWS EKS with Jenkins and Docker, ensuring your setup is efficient, scalable, and automated.

1. Introduction to AWS EC2 and Initial Setup

Before deploying a microservices architecture on AWS EKS, it’s essential to begin with the basics: setting up AWS EC2. AWS EC2 provides the compute power for your infrastructure. Start by launching an EC2 instance that will serve as your control plane. Choose an instance type suitable for your workload, ensure it has SSH access, and configure security groups to allow inbound traffic from specific IP ranges.

Steps:

  • Log into your AWS console and navigate to EC2.
  • Launch an EC2 instance with a secure key pair for SSH access.
  • Install necessary packages like git and docker.

2. Installing Necessary Tools: AWS CLI, Jenkins, kubectl, and eksctl

For efficient orchestration and management of Kubernetes clusters, we need specific tools.

  • AWS CLI: Manage AWS services from your terminal.
  • Jenkins: Automates your CI/CD pipeline.
  • kubectl: Command-line tool for interacting with Kubernetes clusters.
  • eksctl: Simplifies the creation and management of EKS clusters.

To install these:

sudo apt-get update

sudo apt-get install awscli

sudo snap install kubectl –classic

curl –silent –location “https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz” | tar xz -C /usr/local/bin

sudo apt-get install openjdk-11-jdk

wget -q -O – https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add –

sudo apt-get install jenkins

3. Configuring Docker and Exposing Services

Docker enables packaging applications into containers, simplifying deployment. We’ll configure Docker on your EC2 instance and expose services for Jenkins and your microservices.

  • Install Docker:

sudo apt-get install docker.io

sudo systemctl start docker

sudo usermod -aG docker $(whoami)

  • Dockerize your services by creating Dockerfiles for each microservice. Push these Docker images to the Amazon Elastic Container Registry (ECR).

4. Establishing an EKS Cluster and Node Group

Amazon Elastic Kubernetes Service (EKS) handles Kubernetes operations, allowing you to focus on managing workloads. We use eksctl to provision the cluster and configure node groups for application hosting.

  • Create an EKS Cluster:

eksctl create cluster –name my-cluster –region us-west-2 –nodegroup-name standard-workers –node-type t3.medium –nodes 3

This command provisions an EKS cluster and sets up worker nodes to host your microservices.

5. Integrating Jenkins with SonarQube for Quality Assurance

Integrating Jenkins with SonarQube ensures code quality and continuous testing during deployment.

  • Install SonarQube and integrate it with Jenkins.
  • Add the SonarQube plugin in Jenkins.
  • In your Jenkins pipeline configuration, add steps for code analysis:

stage(‘SonarQube Analysis’) {

    steps {

        script {

            def scannerHome = tool ‘SonarQubeScanner’

            withSonarQubeEnv(‘SonarQube’) {

                sh “${scannerHome}/bin/sonar-scanner”

            }

        }

    }

}

6. Creating a Jenkins Pipeline for Continuous Integration and Deployment

Create a Jenkins pipeline that automates the CI/CD process, pulling code from your Git repository, building Docker images, pushing them to ECR, and deploying them to EKS.

  • Example Jenkins pipeline script:

pipeline {

    agent any

    stages {

        stage(‘Build Docker Image’) {

            steps {

                script {

                    docker.build(‘my-app:latest’)

                }

            }

        }

        stage(‘Push to ECR’) {

            steps {

                script {

                    docker.withRegistry(‘https://1234567890.dkr.ecr.us-west-2.amazonaws.com’, ‘ecr:us-west-2:aws-ecr-credentials’) {

                        docker.image(‘my-app:latest’).push(‘latest’)

                    }

                }

            }

        }

        stage(‘Deploy to EKS’) {

            steps {

                sh ‘kubectl apply -f kubernetes/deployment.yaml’

            }

        }

    }

}

7. Implementing RBAC and Service Accounts in Kubernetes

Implement role-based access control (RBAC) to enhance security in your Kubernetes environment. This ensures that services and users only have access to resources that they are authorized to interact with.

  • Example YAML file for an RBAC policy:

apiVersion: v1

kind: ServiceAccount

metadata:

  name: my-service-account

apiVersion: rbac.authorization.k8s.io/v1

kind: Role

metadata:

  name: pod-reader

rules:

– apiGroups: [“”]

  resources: [“pods”]

  verbs: [“get”, “watch”, “list”]

apiVersion: rbac.authorization.k8s.io/v1

kind: RoleBinding

metadata:

  name: read-pods

subjects:

– kind: ServiceAccount

  name: my-service-account

  namespace: default

roleRef:

  kind: Role

  name: pod-reader

  apiGroup: rbac.authorization.k8s.io

8. Deploying Microservices to EKS: A Detailed Walkthrough

Deploy your microservices to the EKS cluster using Kubernetes manifests. Here’s an example of a Kubernetes Deployment manifest for a simple microservice:

apiVersion: apps/v1

kind: Deployment

metadata:

  name: my-app

spec:

  replicas: 3

  selector:

    matchLabels:

      app: my-app

  template:

    metadata:

      labels:

        app: my-app

    spec:

      containers:

      – name: my-app-container

        image: 1234567890.dkr.ecr.us-west-2.amazonaws.com/my-app:latest

        ports:

        – containerPort: 80

Apply the deployment using:

kubectl apply -f deployment.yaml

9. Verifying Deployment and Monitoring Performance

Verify that your microservices have been deployed successfully by checking the pods’ status:

kubectl get pods

To monitor performance, integrate AWS CloudWatch and Prometheus for real-time metrics. For essential pod performance, use:

kubectl top pods

Conclusion

Deploying a multi-tier microservices architecture on AWS EKS using Jenkins and Docker provides flexibility, scalability, and automation for continuous delivery. By leveraging tools like SonarQube, RBAC, and AWS services, you can ensure your system is secure, resilient, and efficient.

References

Deploy a sample Java microservice on Amazon EKS and expose the microservice using an Application Load Balancer.

Orchestrate Jenkins Workloads using Dynamic Pod Autoscaling with Amazon EKS