HighSkill

How to Deploy Docker Containers to AWS EKS Using GitLab CI CD

How to Deploy Docker Containers to AWS EKS Using GitLab CI CD

Are you ready to deploy your Docker containers to AWS EKS straight from GitLab CI/CD? This guide will walk you through every step from start to finish. By the end of this post, you'll have a fully functional CI/CD pipeline, allowing you to deploy your applications seamlessly.

Why Use AWS EKS and GitLab CI/CD?

Deploying Docker containers to AWS EKS (Elastic Kubernetes Service) using GitLab CI/CD brings together the benefits of container orchestration and powerful automation.

  • Efficiency: Automate your deployment process, reducing manual intervention.
  • Scalability: AWS EKS automatically scales your applications based on demand.
  • Integration: Direct integration between GitLab CI/CD and AWS EKS ensures smooth operation.
  • Security: Utilize the security features of both AWS and GitLab to protect your application.

Prerequisites

Before we dive in, ensure you have the following prerequisites:

  • AWS account with permissions to create and manage EKS clusters.
  • GitLab account with a repository.
  • Docker installed on your local machine.
  • kubectl CLI installed.
  • aws-cli installed and configured.

Setting Up Your AWS Environment

First things first, set up your AWS EKS environment.

  1. Create an EKS Cluster:
  • Navigate to EKS on the AWS Management Console.
  • Click "Create Cluster."
  • Configure your cluster settings, name, and networking.
  1. Install eksctl:
brew tap weaveworks/tap
brew install weaveworks/tap/eksctl
  1. Create the Cluster via eksctl:
eksctl create cluster --name my-cluster --region us-west-2 --nodes 3

Configuring Your GitLab Repository

Configure your GitLab repository to enable CI/CD.

  1. Create a GitLab Project:
  • Log in to GitLab.
  • Click "New Project" and fill out the required details.
  1. Configure CI/CD Variables:
  • Navigate to "Settings" -> "CI/CD" -> "Variables".
  • Add AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  • Add other necessary variables such as DOCKER_IMAGE, AWS_REGION, and CLUSTER_NAME.

Creating Your Dockerfile

Your Dockerfile is crucial for defining your application's container.

  1. Create a Dockerfile in Your Root Directory:
FROM node:14
WORKDIR /app
COPY . .
RUN npm install
CMD ["npm", "start"]
  1. Build and Push Docker Image (Locally):
docker build -t myapp:latest .
docker tag myapp:latest <your-docker-repo>/myapp:latest
docker push <your-docker-repo>/myapp:latest

Setting Up GitLab CI/CD Pipeline

Now, set up your CI/CD pipeline on GitLab.

  1. Create .gitlab-ci.yml:
stages:
   - build
   - deploy

build:
   stage: build
   script:
      - docker build -t myapp:$CI_COMMIT_SHA .
      - docker tag myapp:$CI_COMMIT_SHA $DOCKER_IMAGE:$CI_COMMIT_SHA
      - docker push $DOCKER_IMAGE:$CI_COMMIT_SHA

deploy:
   stage: deploy
   script:
      - aws eks update-kubeconfig --name $CLUSTER_NAME --region $AWS_REGION
      - sed "s|DOCKER_IMAGE|$DOCKER_IMAGE:$CI_COMMIT_SHA|g" k8s_deployment.yaml | kubectl apply -f -
  1. Create k8s_deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
   name: myapp-deployment
spec:
   replicas: 3
   selector:
      matchLabels:
      app: myapp
   template:
      metadata:
      labels:
         app: myapp
      spec:
      containers:
         - name: myapp
            image: DOCKER_IMAGE
            ports:
            - containerPort: 3000

Deploying to AWS EKS

Once your pipeline is set up, your Docker containers will be deployed to AWS EKS automatically upon each commit.

  1. Trigger a Pipeline: Commit and push changes to your GitLab repository. This action triggers the pipeline.
  2. Monitor Pipeline Progress: Navigate to your GitLab project, and then click on "CI/CD" -> "Pipelines" to monitor the progress.

Monitoring and Management

Monitoring and managing your deployment is crucial to ensure it runs smoothly.

  1. Using kubectl:
kubectl get deployments
kubectl get pods
kubectl logs <pod-name>
  1. AWS CloudWatch: Utilize AWS CloudWatch for detailed monitoring and alerting.

Conclusion

Deploying Docker containers to AWS EKS from GitLab CI/CD might seem daunting, but by following these straightforward steps, you can streamline your development and deployment process. This setup not only enhances efficiency but also ensures that your applications are scalable and secure.

If you found this guide helpful, please leave a comment below or share it on social media. Your feedback helps us improve!

Happy deploying! 🚀