How to use AWS instance as Runner in Gitlab?
ec2 with gitlab runner

How to use AWS instance as Runner in Gitlab?

Hello,

This is Ritesh,

Today we will see how to add the Ec2 instance as a runner in your GitLab so that we can install the software according to our need for learning CI/CD and other things.

Maybe this will be so much introductory for some people but sometimes when we start learning something new we face some problems. So for that, I am writing this for those who just started learning and want to face a similar type of problem.

In this, we will be performing some small tasks so to get a better understanding I want to say.

So let's get started !!

First, we will understand what is GitLab.

Gitlab is an open source DevOps software package that helps to develop, secure, and operate software in a single application (near about similar to Jenkins )

Now, let's understand what is Gitlab runner.

GitLab Runner is an application that works with GitLab CI/CD to run jobs in a pipeline.

You can get a detailed idea about it from the below official document of Gitlab


So let's move to the task

1)The pipeline should automatically build and create a Docker image using the?Dockerfile?already available in the project and push the Docker image to the GitLab integrated Docker container registry.

2)The Docker image that is pushed to the GitLab container registry should be used for deployment using Docker

3)Create a Revery proxy for the server .

Let's start with how to install GitLab runner on the AWS instance

Step 1:- Login to AWS console and start an ec2 instance and log in to the instance.

Step 2:- Install the required software which you required to execute the pipeline

Step3:- install git in your instance it is required to install the GitLab runner.

Step 4:-

# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64        

Step 5:-

# Give it permission to execute
sudo chmod +x /usr/local/bin/gitlab-runner        

Step 6:-

# Create a GitLab Runner user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash        

Step 7:-

# Install and run as a service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start        

Step 8:-

#Token registration 
sudo gitlab-runner register --url https://gitlab.com/ --registration-token $REGISTRATION_TOKEN        

You will get all these steps in your GitLab account Setting--> CICD--> runner

Step 9:- change the permission for the user

usrrmod -aG docker gitlab-runner        

Step 10(optional):- If you face that runner never contacted you can use this command

gitlab-runner verify        

That's it you can now use your was ec2 instance as a runner for your pipeline if you want to install any software and just restart the runner so it configures properly.

Now let's see the script and task which we will be performing

Gitlab pipline script in .yml format

Here you can see I am using the Docker file to create an image after that pushing that image into GitLab internal repository so that we can use it .

Here in the tags section, we are using the runner which we have created in the above steps I tagged it to Redhat so that my pipeline will use only that instance you can give any name to it and use that tag here. so that script will detect the runner and perform the task on a specific one.

And last step performing reverse proxy

A?reverse proxy server?is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server

For this, you need to install the apache or Nginx server to perform the task using the command

After installing we have to make the changes in the given file

Vim /etc/apache2/sites-enabled/000-default.conf        

Update the above file with below contain port number may vary according to your configuration/availability of port

    <VirtualHost *:*
        ProxyPreserveHost On
        ProxyPass / https://0.0.0.0:8080/
        ProxyPassReverse / https://0.0.0.0:8080/
        ServerName localhost
    </VirtualHost>>        

After this just restart the service using the command

systemctl restart service name        


So this is how we can create a runner using AWS instance and use it for our use case or testing we can use multiple runners according to our need

I know this I too much introductory but I thought that so many students start learning new things so it will help them

Hope you like it

For any suggestions or mistakes in the above blog please DM me.

Thank you!!??

Duranta Patra

DevOps Engineer @Eventify AWS ?Terraform ? Kubernetes ? Ansible ? Jenkins ? Zabbix ? Prometheus ? Grafana

2 年

great work bro

要查看或添加评论,请登录

Ritesh Chaudhari的更多文章

社区洞察

其他会员也浏览了