Jenkins on EKS: A Step-by-Step Guide to Mounting EFS in DIND
Image by Lonee - hkhazo.biz.id

Jenkins on EKS: A Step-by-Step Guide to Mounting EFS in DIND

Posted on

Are you tired of dealing with storage issues in your Jenkins pipeline on EKS? Do you want to ensure that your build process is efficient and reliable? Look no further! In this article, we’ll show you how to mount EFS in DIND (Docker-in-Docker) on Jenkins running on EKS. Buckle up, folks, and let’s dive in!

What is Jenkins on EKS?

Jenkins is a popular open-source automation server that helps you automate your build, test, and deployment process. EKS (Elastic Container Service for Kubernetes) is a managed container service offered by AWS that allows you to run Kubernetes on AWS without managing the underlying infrastructure. When you combine Jenkins with EKS, you get a robust and scalable CI/CD pipeline.

What is DIND (Docker-in-Docker)?

DIND is a Docker image that allows you to run Docker inside a Docker container. It’s commonly used in CI/CD pipelines to provide a isolated environment for building and testing applications. In the context of Jenkins on EKS, DIND enables you to run Docker builds and deploy applications to your EKS cluster.

Why Mount EFS in DIND?

EFS (Elastic File System) is a fully managed file system offered by AWS that provides a shared storage solution for your applications. Mounting EFS in DIND provides several benefits, including:

  • Persistent storage: EFS provides persistent storage that survives even if your container restarts or fails.
  • Shared storage: EFS allows multiple containers to access the same file system, making it easier to share data between containers.
  • Scalability: EFS scales with your application, so you don’t have to worry about running out of storage space.

Prerequisites

Before we dive into the tutorial, make sure you have the following:

  • An EKS cluster with at least one node.
  • Jenkins installed on your EKS cluster.
  • Docker installed on your Jenkins node.
  • An EFS file system created and mounted on your Jenkins node.

Step 1: Create an EKS Cluster and Deploy Jenkins

If you haven’t already, create an EKS cluster using the AWS Management Console or the AWS CLI. Make sure to choose a node type that has enough resources to run Jenkins.

Once your cluster is created, deploy Jenkins using the following YAML file:


apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins
spec:
  replicas: 1
  selector:
    matchLabels:
      app: jenkins
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
      - name: jenkins
        image: jenkins/jenkins:lts
        ports:
        - containerPort: 8080
        volumeMounts:
        - name: jenkins-home
          mountPath: /var/jenkins_home
      volumes:
      - name: jenkins-home
        emptyDir: {}

Apply the YAML file using the following command:


kubectl apply -f jenkins-deployment.yaml

Step 2: Create an EFS File System and Mount it on the Jenkins Node

Create an EFS file system using the AWS Management Console or the AWS CLI. Make sure to choose a file system type that meets your needs.

Once your EFS file system is created, mount it on your Jenkins node using the following command:


sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport .efs..amazonaws.com:/ /efs

Make sure to replace `` and `` with your actual EFS file system ID and AWS region, respectively.

Step 3: Create a DIND Image with EFS Mounted

Create a new Dockerfile that mounts the EFS file system and installs the necessary dependencies:


FROM docker:latest

# Mount the EFS file system
RUN mkdir /efs
RUN mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport .efs..amazonaws.com:/ /efs

# Install necessary dependencies
RUN apt-get update && apt-get install -y git openssh-client

# Set the working directory to /efs
WORKDIR /efs

Build the Docker image using the following command:


docker build -t my-dind-image .

Step 4: Configure Jenkins to Use the DIND Image

Create a new Jenkins pipeline and add the following configuration:


pipeline {
  agent {
    docker {
      image 'my-dind-image'
      args '-v /efs:/efs'
    }
  }
  stages {
    stage('Build') {
      steps {
        sh 'mkdir -p /efs/my-repo'
        sh 'git clone https://github.com/my-repo.git /efs/my-repo'
        sh 'cd /efs/my-repo && ./build.sh'
      }
    }
  }
}

This configuration uses the `my-dind-image` Docker image and mounts the EFS file system at `/efs`. The pipeline then clones a Git repository, builds the application, and stores the build artifacts in the EFS file system.

Conclusion

Mounting EFS in DIND on Jenkins running on EKS provides a scalable and reliable storage solution for your CI/CD pipeline. By following this tutorial, you can ensure that your build process is efficient and reliable, and that your build artifacts are safely stored in a persistent file system.

Benefits Description
Persistent storage EFS provides persistent storage that survives even if your container restarts or fails.
Shared storage EFS allows multiple containers to access the same file system, making it easier to share data between containers.
Scalability EFS scales with your application, so you don’t have to worry about running out of storage space.

We hope you found this tutorial helpful! If you have any questions or need further assistance, please don’t hesitate to reach out.

  1. Learn more about Jenkins on EKS: https://aws.amazon.com/eks/
  2. Learn more about DIND: https://github.com/docker-library/docker
  3. Learn more about EFS: https://aws.amazon.com/efs/

Frequently Asked Questions

Get clarity on running Jenkins on Amazon Elastic Container Service for Kubernetes (EKS) and mounting Elastic File System (EFS) in Docker-in-Docker (DIND) environments.

What is the main advantage of running Jenkins on EKS?

Running Jenkins on EKS provides a highly scalable and managed Kubernetes environment, allowing you to focus on Jenkins pipeline management while leveraging the scalability, security, and reliability of EKS.

Why do I need to mount EFS in my DIND Jenkins environment?

Mounting EFS in your DIND Jenkins environment provides a persistent and shared storage solution for your Jenkins jobs, allowing you to store and retrieve artifacts, configurations, and other data across container restarts and node instances.

How do I configure my Jenkins EKS cluster to use EFS for persistent storage?

Create an EFS file system and mount it to your EKS cluster nodes. Then, update your Jenkins configuration to use the EFS mount point as the Jenkins home directory or a custom directory for persistent storage.

What are some common challenges when running DIND on EKS with EFS?

Common challenges include ensuring correct EFS mount permissions, managing DIND container storage, and troubleshooting Kubernetes pod and container issues. Additionally, you may encounter issues with Jenkins pipeline performance and Artifact storage.

What are some best practices for securing my Jenkins EKS environment with EFS?

Implement identity and access management (IAM) roles for EKS and EFS, use Kubernetes network policies to restrict access, and configure Jenkins with encryption and secure plugins. Regularly monitor and update your environment to ensure compliance with security best practices.

Leave a Reply

Your email address will not be published. Required fields are marked *