Containerized Application Deployment on Amazon EKS with Jenkins Pipeline
Tags: cloud, aws, docker, tutorial
Published: 2024-01-15 10:55:56

## Introduction

Continuing with Devops articles, today we will see how to deploy a containerized application on Amazon EKS using Jenkins Pipeline.

Jenkins Pipeline provides a simple and efficient way to define and automate the deployment of containerized applications to Amazon EKS. This article covers the basics of setting up a Jenkins pipeline for EKS deployment, customizing the deployment process, and improving the pipeline for more advanced workflows.

## What is Jenkins Pipeline?

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline DSL. The definition of a Jenkins Pipeline is typically written into a text file (called a Jenkinsfile) which in turn is checked into a project’s source control repository.

## What is Amazon 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.

## Why use Jenkins Pipeline for EKS?

Jenkins Pipeline is a powerful tool for automating EKS deployments. It provides a simple way to define and execute a series of steps, either manually or automatically. This makes it easy to automate the deployment process and ensure consistency across environments.

## High-level overview

The following diagram shows the high-level architecture of the Jenkins Pipeline for EKS:

![Jenkins Pipeline for EKS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l4c5q5vtzwfcr4482qex.png)

The Jenkins Pipeline for EKS consists of the following components:

- Jenkins - The Jenkins instance that runs the pipeline.
- Jenkinsfile - The pipeline definition file.
- Docker - The containerized application.
- Docker Registry - The Docker registry where the image is pushed.
- Amazon EKS - The Kubernetes cluster where the application is deployed.
- Amazon ECR - The Docker registry where the image is pulled from.
- Kubernetes - The Kubernetes cluster where the application is deployed.
- Deployment Manifest - The Kubernetes deployment manifest for the application.
- AWS Credentials - The AWS credentials used by Jenkins to access EKS.
- Kubernetes Server - The Kubernetes server used by Jenkins to access EKS.
- Kubernetes Continuous Deploy Plugin - The Jenkins plugin used to deploy to Kubernetes.
- CloudBees AWS Credentials Plugin - The Jenkins plugin used to manage AWS credentials.

## Prerequisites

Before you begin, make sure you have the following prerequisites:

- A running Jenkins instance
- An AWS account with access to Amazon EKS
- A Kubernetes cluster running on Amazon EKS
- A containerized application in Docker format
  
## Configuring Jenkins for EKS

Before creating the Jenkinsfile pipeline, Jenkins needs to be configured with the proper plugins and credentials to interface with EKS:

- Install the CloudBees AWS Credentials Plugin to manage AWS access keys
- Install the Kubernetes Continuous Deploy Plugin for deploying to Kubernetes
- Add AWS credentials to Jenkins with access to EKS. The credentials should be added as an AWS Access Key ID and Secret Access Key pair. 
  
```bash
// Sample AWS credentials in Jenkins
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
```

- Configure a Kubernetes server in Jenkins pointing to the EKS cluster
  
## Jenkinsfile

Create a **Jenkinsfile** in the root directory of your project. The Jenkinsfile should contain the following code:

```groovy
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'docker build -t my-image .' 
      }
    }
    stage('Push') {
      steps {
        sh 'docker push my-image'
      }
    }
    stage('Deploy') {
      steps {
        sh 'kubectl apply -f my-deployment.yaml'
      }
    }
  }
}

```

This Jenkinsfile defines three stages:

- Build - Builds the Docker image
- Push - Pushes the image to a Docker registry
- Deploy - Deploys the image to EKS
  
## Using a Docker Registry

For the Push stage, you'll need a Docker registry to push the image to. Options include:

- Docker Hub - Public registry hosted by Docke.
For Docker Hub, this can be done by adding the following to the Push stage:

```groovy
stage('Push') {
  steps {
 	sh 'docker login -u $DOCKER_USERNAME -p $DOCKER_PASSWORD'
 	sh 'docker push my-image'
  }
}
```

- Amazon ECR - Managed AWS Docker registry.
- Self-hosted registry - Run your own Docker registry server.
- Other public registries - There are many other public Docker registries available. Feel free to use any of them.
The Jenkinsfile will need to authenticate with the registry before pushing.

## Deploying to Amazon EKS

To deploy the containerized application to Amazon EKS using the Jenkinsfile pipeline:

1. Log in to the Jenkins instance.
2. Click on the "Build Now" button to trigger the pipeline.
3. Wait for the pipeline to complete.
4. Check logs/dashboard to verify deployment.

## Customizing the Deployment

The sample Deploy stage does a basic kubectl apply. For more customization you can:

- Parameterize the deployment manifest
- Perform a rolling update with kubectl rollout
  
```bash
kubectl set image deployment/my-app my-app=my-image:latest
kubectl rollout status deployment/my-app
```

- Check rollout status before continuing.
- Rollback on failure.
- Use a Helm chart for more advanced deployments.
- Use a Jenkinsfile library to share common deployment logic across projects.

## Improving the Pipeline

To improve the pipeline:

- Add stages for testing, security scanning, etc.
- Use a Jenkins Shared Library for reusable functions.
- Integrate pipeline with GitHub, GitLab, etc.
- Send notifications on pipeline status. Automate notifications with Slack, email, etc.
- Visualize stages on the Jenkins UI.
There are many ways to extend the example pipeline to fit your workflow. Jenkins Pipeline is flexible and can be customized to suit complex deployment needs.

## Conclusion

Jenkins Pipeline provides a simple yet powerful way to automate deploying containerized applications to Amazon EKS. With Jenkins handling the process through code, you can achieve consistent and repeatable EKS deployments with minimal effort and maximum flexibility.

in this article, we have seen how to deploy a containerized application on Amazon EKS using Jenkins Pipeline. I hope you have enjoyed this article. If you have any questions, please feel free to reach out to me on [Twitter](https://twitter.com/KenMwaura1). or [LinkedIn](https://www.linkedin.com/in/kennedy-mwaura/). You can also check out my other articles on [Dev.to](https://dev.to/ken_mwaura1). Thanks for reading!

Buy me a coffee [here](https://www.buymeacoffee.com/kenmwaura1).

![next-time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mthxo12c3pejx4nz6dkq.gif)

## References

- [Jenkins Pipeline](https://www.jenkins.io/doc/book/pipeline/)
- [Amazon EKS](https://aws.amazon.com/eks/)
- [Amazon ECR](https://aws.amazon.com/ecr/)
- [Docker Hub](https://hub.docker.com/)
- [Kubernetes](https://kubernetes.io/)