Skip to content
ImagineWorld

Making Your Environment Fully Distributed

node_idnode-book_mastering-gitlab-12_chapter-21-making-your-environment-fully-distributed
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num21
chapter_titleMaking Your Environment Fully Distributed
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 21 — Making Your Environment Fully Distributed

Section titled “Chapter 21 — Making Your Environment Fully Distributed”

In Chapter 18, Monitoring CI Metrics, and Chapter 19, Creating a Basic HA Architecture Using Horizontal Scaling, we created a basic scalable GitLab architecture using Terraform and Ansible. In this chapter, we will create even more nodes and, by doing so, strengthen the High Availability (HA) capabilities of GitLab. One of the weak points of the architecture we first designed was that the frontend servers had more than one GitLab component installed, which could prove to be a problem if one component breaks down. We will run the Sidekiq component on another server to mitigate this risk. Another weakness was the shared filesystem.

In this chapter, we will cover the following topics:

  • The basic architecture of this solution
  • The changes to the Terraform configuration
  • Splitting more application components
  • The clustered Redis/Sentinel configuration
  • Changes in Ansible files
  • Script enhancements

To manage omnibus installs, there is one central configuration file called gitlab.rb. You need to create it or copy an example. There is a template available and you can find it here: https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template. It is not updated after upgrades. In large parts of this chapter, I will quote and discuss parts of this file.

To create our virtual infrastructure, we are going to use Terraform (≥ v0.11.12). You can download it at https://www.terraform.io. Terraform is a multiplatform binary.

The following Terraform providers will be used:

  • provider.ansible v0.0.4: Get it from https://github.com/nbering/terraform-inventory.
  • provider.aws v2.1.0: This is automatically downloaded with the terraform init command (see Chapter 19, Creating a Basic HA Architecture Using Horizontal Scaling, particularly the Preparing to run Terraform to deploy the virtual hardware section).
  • provider.tls v1.2.0: This is automatically downloaded with the terraform init command (see Chapter 19, Creating a Basic HA Architecture Using Horizontal Scaling, particularly the Preparing to run Terraform to deploy the virtual hardware section). To automate our deployments, we’ll use Ansible (≥ version 2.7). You can download it from https://github.com/ansible/ansible or use a package manager such as brew or pip to install it.

To follow along with the instructions in this chapter, please download the Git repository with examples, which is available on GitHub: https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter21.

We already created a more scalable combination of GitLab components in the previous chapter. But, of course, there are even more options. If you wanted go for a 100% breakdown into smaller, more cloud-native components, you would have to stop using the omnibus-gitlab package. If you did that, an elastic, fault-tolerant database service such as Aurora could be used from Amazon itself and other Amazon-specific services such as ElastiCache. It would mean much more management overhead, but it could be the way to go if you have the time and money to build big production sites. It would be harder to switch to another cloud vendor, though.

Because we like to use an example that can be deployed on another cloud and because the omnibus package is used throughout this book, we will use that again.

In the following diagram, you will find the third iteration of the HA solution for GitLab:

There is no change in the bastion hosts part (SG-bastionhosts) or the backend part (SG-backendservers). That is still equipped with one master database (sqla) and two read-only replicas (sqlb and sqlc). The cluster of consul agents and the nodes keep an eye on the database and notifies the pgbouncer node to switch to the active database node. This pgbouncer node also multiplexes PostgreSQL client connections. On the frontend side (SG-frontendservers), there are more changes. The Sidekiq component was installed alongside other components on the frontend servers in earlier chapters but is now split into a new layer: a middleware layer (sidekiqa-d). There are four dedicated nodes for Sidekiq and they have different functions. The first one handles near real-time background jobs such as merge requests. The second one handles jobs that cannot wait too long (such as commits and deployments). The third node is responsible for the queues handling CI pipeline jobs, and the last node handles the other background jobs from the Ruby on Rails GitLab web application.

The two Redis nodes that existed in a master-slave configuration in the earlier chapters have now increased to three (another slave has been added). The Sentinel HA component of Redis is also installed. It can detect failures in your Redis cluster and can handle automatic failover. It runs next to Redis on the same node.

The shared storage in use is still Gitaly in this architecture but can be swapped for NFS as well. Unfortunately, there are no simple solutions to make the shared storage HA-capable. There are several options, but they are not created out of the box and require tuning.

Some examples include the following:

The monitoring components are still present in this architecture and can provide valuable information about your components.

Adding more nodes to the infrastructure, as proposed in the basic architecture of this solution, means we have to change some Terraform files. The first one we will change is the one where the EC2 instances are defined.

The functional composition of the frontend servers is going to change a lot in this version of the infrastructure. The first instance will serve regular web UI traffic, the second will serve only API traffic, and a third instance will be created to handle Git-SSH traffic:

resource "aws_instance" "FRONTEND_C" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
subnet_id = "${aws_subnet.public-frontend_az-b.id}"
key_name = "${aws_key_pair.keypair.key_name}"
vpc_security_group_ids = ["${aws_security_group.SG-frontendservers.id}"]
instance_type = "t2.medium"
tags {
Name = "${var.environment}-FRONTEND003"
Environment = "${var.environment}"
sshUser = "ubuntu"
}
}

On the middleware side of things, the amount of Sidekiq nodes is growing. The third node, which is going to be responsible for CI pipeline jobs, has to be instantiated:

resource "aws_instance" "SIDEKIQ_C" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.micro"
tags {
Name = "${var.environment}-SIDEKIQ003"
Environment = "${var.environment}"
sshUser = "ubuntu"
}
subnet_id = "${aws_subnet.public-frontend_az-b.id}"
key_name = "${aws_key_pair.keypair.key_name}"
vpc_security_group_ids = ["${aws_security_group.SG-frontendservers.id}"]
}

A fourth Sidekiq node is created to handle real-time jobs that cannot wait:

resource "aws_instance" "SIDEKIQ_D" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.micro"
tags {
Name = "${var.environment}-SIDEKIQ004"
Environment = "${var.environment}"
sshUser = "ubuntu"
}
subnet_id = "${aws_subnet.public-frontend_az-b.id}"
key_name = "${aws_key_pair.keypair.key_name}"
vpc_security_group_ids = ["${aws_security_group.SG-frontendservers.id}"]
}

We will need a third Redis node, as the new situation will require a Redis cluster with one master and two slave nodes, which will be watched by a sentinel cluster:

resource "aws_instance" "REDISC" {
subnet_id = "${aws_subnet.public-frontend_az-b.id}"
key_name = "${aws_key_pair.keypair.key_name}"
vpc_security_group_ids = ["${aws_security_group.SG-frontendservers.id}"]
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.micro"
tags {
Name = "${var.environment}-REDIS003"
Environment = "${var.environment}"
sshUser = "ubuntu"
}
}

Now that we have created our AWS instance, we will also define our Ansible host definitions, which use AWS instances.

The first new Sidekiq host is the pipeline background job handler, which will get the ci_pipeline role in the Ansible definition:

resource "ansible_host" "SIDEKIQ003" {
inventory_hostname = "${aws_instance.SIDEKIQ_C.private_dns}"
groups = ["middleware_pipeline"]
vars
{
ansible_user = "ubuntu"
role = "ci_pipeline"
ansible_ssh_private_key_file="/tmp/mykey.pem"
ansible_python_interpreter="/usr/bin/python3"
ansible_ssh_common_args= " -o ProxyCommand=\"ssh -o StrictHostKeyChecking=no -i /tmp/mykey.pem -W %h:%p -q ubuntu@${aws_instance.BASTIONHOST_B.public_dns}\""
proxy = "${aws_instance.BASTIONHOST_B.private_ip}"
subnet = "${aws_subnet.public-frontend_az-b.cidr_block}"
}
}

The second new Sidekiq host is the real-time background job handler, which will get the realtime role in the Ansible definition:

resource "ansible_host" "SIDEKIQ004" {
inventory_hostname = "${aws_instance.SIDEKIQ_D.private_dns}"
groups = ["middleware_realtime"]
vars
{
ansible_user = "ubuntu"
role = "realtime"
ansible_ssh_private_key_file="/tmp/mykey.pem"
ansible_python_interpreter="/usr/bin/python3"
ansible_ssh_common_args= " -o ProxyCommand=\"ssh -o StrictHostKeyChecking=no -i /tmp/mykey.pem -W %h:%p -q ubuntu@${aws_instance.BASTIONHOST_B.public_dns}\""
proxy = "${aws_instance.BASTIONHOST_B.private_ip}"
subnet = "${aws_subnet.public-frontend_az-b.cidr_block}"
}
}

As you can see in the following, we also define the added Redis server with the slave role:

resource "ansible_host" "REDIS003" {
inventory_hostname = "${aws_instance.REDIS_C.private_dns}"
groups = ["redis"]
vars
{
ansible_user = "ubuntu"
role = "slave"
ansible_ssh_common_args= " -o ProxyCommand=\"ssh -o StrictHostKeyChecking=no -i /tmp/mykey.pem -W %h:%p -q ubuntu@${aws_instance.BASTIONHOST_B.public_dns}\""
ansible_ssh_private_key_file="/tmp/mykey.pem"
ansible_python_interpreter="/usr/bin/python3"
proxy = "${aws_instance.BASTIONHOST_B.private_ip}"
}
}

These are all of the changes needed for Terraform to create the Amazon EC2 instances. You can deploy them using this:

terraform apply

After successful creation, you should find all instances in the AWS web console:

There are now three extra nodes in the infrastructure. In the next section, we will execute the Ansible deployment scripts to configure them.

The first node we will configure is the frontend server, which is added especially to handle Git-SSH traffic. First, we mention the Ansible script name. The second item is the Jinja template that is used to configure the omnibus-gitlab package on that host. The last item is the command to execute the Ansible script.

The Ansible playbook that is used to configure the node consists of different parts. It consists of the following:

  • Script: frontend-services-ssh.yml
  • Template: gitlab.rb.frontend_ssh.j2
  • Run command: ansible-playbook -i /usr/local/bin/terraform.py deploy/frontend-services.yml

This part of the deployment is the installation of the Git SSH frontend application servers.

We will have to define an internal URL, which is used to authenticate users:

gitlab_rails['internal_api_url'] = "http://{{ hostvars[groups['frontend'][0]]['inventory_hostname'] }}:8080"

We disable all other services on this node:

## Disable components that will not be on the GitLab application server
roles ['application_role']
nginx['enable'] = false
sidekiq['enable'] = false
unicorn['enable'] = false

The repositories should be reachable for Git SSH:

# Gitaly
gitlab_rails['gitaly_token'] = 'abc123secret'
git_data_dirs({
'default' => { 'path' => '/var/opt/gitlab/git-data','gitaly_address' => 'tcp://{{ hostvars[groups['gitaly'][0]]['inventory_hostname'] }}:8075' }
})
gitlab_rails['gitaly_token'] = 'abc123secret'
gitaly['enable'] = false

Let’s keep the user IDs the same everywhere:

# Ensure UIDs and GIDs match between servers for permissions via shared filesystem
user['uid'] = 9000
user['gid'] = 9000
web_server['uid'] = 9001
web_server['gid'] = 9001
registry['uid'] = 9002
registry['gid'] = 9002

Also, here, don’t run migrations, because only one node should do this:

gitlab_rails['auto_migrate'] = false

We don’t want this node to generate SSL certificates:

#disable letsencrypt for demo purposes
letsencrypt['enable'] = false

As explained in the The basic architecture of this solution section of this chapter, we created four separate Sidekiq nodes. They were created using Terraform. Now, we have to configure them with four different Ansible scripts.

The Ansible playbook that is used to configure the node consists of different parts. It consists of the following:

  • Script: install-sidekiq-asap.yml
  • Template: gitlab.rb.sidekiq_asap.j2
  • Run command: ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-asap.yml

This part of the deployment is the installation of the Sidekiq node for background jobs that need to run as soon as possible.

We set up specific queues for this sidekiq-cluster, the realtime instance:

sidekiq_cluster['enable'] = false
sidekiq_cluster['ha'] = true
sidekiq_cluster['max_concurrency'] = 15 # The maximum number of threads each Sidekiq process should run
sidekiq_cluster['queue_groups'] = [
"update_merge_requests,auto_merge",
"process_commit",
"deployment"
]

Disable other services that start by default on an omnibus-gitlab installation:

## Disable all other services
gitlab_workhorse['enable'] = false
unicorn['enable'] = false
postgresql['enable'] = false
nginx['enable'] = false
prometheus['enable'] = false
alertmanager['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_monitor['enable'] = false
sidekiq['enable'] = false
gitaly['enable'] = false

This Sidekiq instance now only handles background jobs for commits and deployments.

The Ansible playbook that is used to configure the node consists of different parts. It consists of the following:

  • Script*:* install-sidekiq-realtime.yml
  • Template: gitlab.rb.sidekiq_realtime.j2
  • Run command: ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-realtime.yml

This part of the deployment is the installation of the Sidekiq node that handles real-time communications.

The first settings enable the cluster and define HA settings:

sidekiq_cluster['enable'] = false
sidekiq_cluster['ha'] = true
sidekiq_cluster['max_concurrency'] = 20 # The maximum number of threads each Sidekiq process should run

We set up specific queues for this sidekiq-cluster, the realtime instance:

sidekiq_cluster['queue_groups'] = [
"merge",
"pipeline_processing"
]

Disable other services:

## Disable all other services
gitlab_workhorse['enable'] = false
unicorn['enable'] = false
postgresql['enable'] = false
nginx['enable'] = false
prometheus['enable'] = false
alertmanager['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_monitor['enable'] = false
sidekiq['enable'] = false
gitaly['enable'] = false

This Sidekiq instance only handles background jobs that need to happen in real time; otherwise, users have a hanging web interface. The queues that are processed contain jobs that perform merges and pipeline processing tasks.

The Ansible playbook that is used to configure the node consists of different parts. It consists of the following:

  • Script: install-sidekiq-pipeline.yml
  • Template: gitlab.rb.sidekiq_pipeline.j2
  • Run command:ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-pipeline.yml

This part of the deployment is the installation of the Sidekiq node that handles jobs in CI pipelines.

The first settings enable the cluster and define HA settings:

sidekiq_cluster['enable'] = false
sidekiq_cluster['ha'] = true
sidekiq_cluster['max_concurrency'] = 10 #

Again, we set up specific queues for this sidekiq-cluster, the pipeline instance:

sidekiq_cluster['queue_groups'] = [
"pipeline_processing",
"pipeline_creation",
"pipeline_default,pipeline_cache,pipeline_hooks,pipeline_background"
]

This Sidekiq node is dedicated to run tasks that have to do with the CI pipeline functionality in GitLab. The reason to create a specific one is that it guarantees that a very busy build pipeline does not slow your whole GitLab application down.

You probably expect the fourth Sidekiq node to also have a special function. It doesn’t. It’s just the same Sidekiq node as in the previous architecture, and it runs all other jobs.

The Ansible playbook that is used to configure the node consists of different parts. It consists of the following:

  • Script: install-redis-cluster.yml
  • Template: gitlab.rb.redis-cluster.j2
  • Run command: ansible-playbook -i /usr/local/bin/terraform.py deploy/install-redis-cluster.yml

This part of the deployment is the installation of the extra Redis slave and Sentinel node.

We have to enable Redis:

## Enable Redis
redis['enable'] = true

Disable all other services because omnibus-gitlab defaults to starting them all:

sidekiq['enable'] = false
gitlab_workhorse['enable'] = false
unicorn['enable'] = false
postgresql['enable'] = false
nginx['enable'] = false
prometheus['enable'] = false
alertmanager['enable'] = false
pgbouncer_exporter['enable'] = false
gitlab_monitor['enable'] = false
gitaly['enable'] = false

Check the role of the Redis server:

{% if hostvars[inventory_hostname].role == "master" %}
roles ['redis_sentinel_role', 'redis_master_role']
{% else %}
roles ['redis_sentinel_role', 'redis_slave_role']
redis['master_ip'] = '{{ hostvars[groups['redis'][1]]['ansible_eth0']['ipv4']['address'] }}'
{% endif %}

Make sure Redis listens on all interfaces:

redis['bind'] = '0.0.0.0'

Make sure sentinel listens on all interfaces:

# General sentinel settings
sentinel['bind'] = '0.0.0.0'
sentinel['quorum'] = 2

Define a port so Redis can listen for TCP requests, which will allow other machines to connect to it:

redis['port'] = 6379

Set up password authentication for Redis (use the same password in all nodes):

redis['password'] = 'packtpub'

Don’t run database migrations:

gitlab_rails['auto_migrate'] = false

The four-node Sidekiq configuration should be running and makes sure every background job receives the right priority.

Of course, there have not only been changes in the Jinja; templates playbooks has also been added. You can see the added files in a list here:

.
├── install-frontend-services-ssh.yml
├── install-sidekiq-asap.yml
├── install-sidekiq-pipeline.yml
├── install-sidekiq-realtime.yml
├── install-redis-cluster.yml
└── templates
├── gitlab.rb.frontend_ssh.j2
├── gitlab.rb.redis-cluster.j2
├── gitlab.rb.sidekiq_asap.j2
├── gitlab.rb.sidekiq_pipeline.j2
├── gitlab.rb.sidekiq_realtime.1.j2

The newly added or changed playbooks can be executed in the following fashion.

Installing the frontend services (web) server can be done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-frontend-services.yml

Installing the frontend services (SSH) server can be done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-frontend-services-ssh.yml

Installing the GitLab sidekiq-asap servers can be done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-asap.yml

Installing the GitLab sidekiq-pipeline servers can be done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-pipeline.yml

Installing the GitLab sidekiq-realtime servers is done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-sidekiq-realtime.yml

Installing the Redis cluster can be done as follows:

ansible-playbook -i /usr/local/bin/terraform.py deploy/install-redis-cluster.yml

The templates and scripts could also have been designed by creating Ansible roles and by introducing a more modular approach, but handling changes in Ansible scripts makes the example easier to read, especially in combination with the Terraform files.

The connect_ssh.sh script is changed as well (following added hosts can be seen as an example), so you can connect to the new instances after they are created:

$scripts/connect_ssh.sh show_host_codes
hostcode: middleware1 -- hostname: "ip-10-0-11-160.eu-central-1.compute.internal"
hostcode: middleware2 -- hostname: "ip-10-0-11-160.eu-central-1.compute.internal"
hostcode: middleware3 -- hostname: "ip-10-0-12-252.eu-central-1.compute.internal"

The new server for the Git SSH connections is as follows:

hostcode: frontend2 -- hostname: "ip-10-0-12-164.eu-central-1.compute.internal"

The Redis cluster is as follows:

hostcode: red0 -- hostname: "ip-10-0-12-240.eu-central-1.compute.internal"
hostcode: red1 -- hostname: "ip-10-0-11-54.eu-central-1.compute.internal"
hostcode: red2 -- hostname: "ip-10-0-11-54.eu-central-1.compute.internal"

The new servers for the frontend (Git SSH), and middleware (extra Sidekiq nodes), and Redis are listed in the preceding code.

In this chapter, we created a third iteration of an HA architecture of GitLab. The frontend tier was increased with one server and, in the middleware tier, extra Sidekiq nodes were created. Also, we created an extra Redis node. We changed the Terraform files, the Ansible playbooks and templates, and the shell scripts. In the next chapter, we are going to test Geo, for replication across data centers.

  1. What three frontend services were split in this chapter?
  2. How many Sidekiq nodes are created?
  3. How many Redis nodes are created?
  4. What is watching Redis and signals downtime?
  5. How can you reach Grafana after installing this infrastructure?
  • Mastering Ansible – Third Edition by James Freeman and Jesse Keating:
  • Getting Started with Terraform – Second Edition by Kirill Shirinkin:
  • AWS Certified Developer – Associate Guide – Second Edition by Vipul Tankariya and Bhavin Parmar:
  • Learn Linux Shell Scripting – Fundamentals of Bash 4.4 by Sebastiaan Tammer:
  • Advanced Solutions in Go - Testing and Distributed Systems [Video] by Aaron Torres: