DEV Community

Cover image for Deploy and Manage GitLab Runners on Amazon EC2
Fernando Muller Junior
Fernando Muller Junior

Posted on

Deploy and Manage GitLab Runners on Amazon EC2

If you, like me, work daily with CI/CD, you’ve probably encountered the need to set up GitLab Runners. These tools are essential for automating pipelines efficiently. Recently, I had to scale them for an application with continuous growth, and Amazon EC2 emerged as the ideal solution.

In this article, I’ll share my experience deploying and managing GitLab Runners using AWS EC2 instances. If you want to optimize your infrastructure, reduce costs, and ensure high availability, stay with me!

GitLab Runners on Amazon EC2

Why Choose Amazon EC2 for GitLab Runners?

Hosting your GitLab Runners on Amazon EC2 provides several significant advantages:

  • Scalability: Automatically adjust resources to meet demand.
  • Flexibility: Support for various hardware configurations and operating systems.
  • Cost-effectiveness: Options like reserved and spot instances help cut costs.
  • AWS Integration: Services like CloudWatch and Auto Scaling simplify management.

In my experience, this setup allowed the team to focus on development while the infrastructure handled the heavy lifting.

Setting Up GitLab Runners on EC2

Step 1: Prepare the AWS Environment

First, you’ll need to configure your AWS account and prepare the environment. Here’s a summary:

  1. Create an EC2 Instance:

    • Choose a compatible AMI (Amazon Machine Image), like Amazon Linux or Ubuntu.
    • Configure the instance sizes based on expected workloads.
  2. Set Up Security Groups:

    • Allow traffic on port 22 (SSH) and port 2376 (Docker).
  3. Install Dependencies:

    • Connect to the instance via SSH and run:
sudo yum update -y
sudo yum install -y docker git
Enter fullscreen mode Exit fullscreen mode

🚀 Unlock the secrets of lean, powerful containers with our ultimate guide to multistage docker build techniques - transform your deployment workflow and cut image sizes by up to 80%!


Step 2: Register the GitLab Runner

Once your EC2 instance is ready, you can register the GitLab Runner:

  1. Install GitLab Runner:
    • Use the following command to install:
curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
chmod +x /usr/local/bin/gitlab-runner
Enter fullscreen mode Exit fullscreen mode
  1. Start Docker:
sudo systemctl start docker
sudo systemctl enable docker
Enter fullscreen mode Exit fullscreen mode
  1. Register the Runner:
    • Obtain the registration token from GitLab (Settings > CI/CD > Runners).
    • Execute:
sudo gitlab-runner register
Enter fullscreen mode Exit fullscreen mode
  • Provide the requested information, such as the URL and token.

Step 3: Advanced Configuration with Docker

If you use Docker to isolate jobs, configure the Runner to use the Docker executor:

sudo nano /etc/gitlab-runner/config.toml
Enter fullscreen mode Exit fullscreen mode

Add the following:

[[runners]]
  name = "Docker Runner"
  executor = "docker"
  [runners.docker]
    image = "docker:stable"
    privileged = true
    volumes = ["/var/run/docker.sock:/var/run/docker.sock", "/cache"]
Enter fullscreen mode Exit fullscreen mode

Restart the Runner:

sudo gitlab-runner restart
Enter fullscreen mode Exit fullscreen mode

Management and Scalability

Auto Scaling GitLab Runners

With Auto Scaling, you can adjust the number of EC2 instances based on demand. Here’s how:

  1. Create a Launch Template:

    • Include the instance configuration, such as type and AMI.
  2. Set Up an Auto Scaling Group:

    • Define the minimum, maximum, and desired capacity of instances.
  3. Integrate with CloudWatch:

    • Set metrics, like CPU or memory usage, to trigger scaling.

From my experience, configuring Auto Scaling was essential to handle demand spikes, such as during large application deployments.

Monitoring with CloudWatch

CloudWatch
Monitoring helps identify bottlenecks and optimize performance. Configure alarms for:

  • CPU Usage: Ensure jobs don’t overload the instances.
  • Job Latency: Detect delays and adjust infrastructure as needed.

Also, configure logs to debug failures directly in CloudWatch Logs.

Use Cases and Best Practices

Real-World Examples

One example that always impresses me is large teams using GitLab Runners to process massive Docker builds. By integrating EC2 with Auto Scaling, it’s possible to reduce job wait times by up to 50%.

Another practice I recommend is using spot instances for jobs that don’t require high availability. I’ve saved up to 70% on costs this way.

Quick Tips

  • Always tag EC2 instances: Simplifies identification and management.
  • Automate Runner Registration: Use scripts to register new Runners on automatically created instances.
  • Configure Backups: Prevent data loss by storing important configurations in S3.

Conclusion

Deploying and managing GitLab Runners on Amazon EC2 may seem challenging, but with the right tools, it becomes straightforward. The scalability, flexibility, and integration with other AWS services make this solution indispensable for teams seeking performance and cost savings.

If this guide was helpful, leave a comment or share your experiences with me. Let’s exchange ideas and keep learning together!

Top comments (0)