Skip to content
ImagineWorld

Managing a Hybrid HA Environment

node_idnode-book_mastering-gitlab-12_chapter-20-managing-a-hybrid-ha-environment
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num20
chapter_titleManaging a Hybrid HA Environment
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

Chapter 20 — Managing a Hybrid HA Environment

Section titled “Chapter 20 — Managing a Hybrid HA Environment”

In the previous chapter, we created a basic scalable GitLab architecture using Terraform and Ansible. In this chapter, we will continue with the same structure, but we will change some things and create even more resiliency. 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. Network File System (NFS) is known to have some performance and file-locking problems; GitLab has developed Gitaly as a replacement, and we will install it in this chapter. Another issue is that there was no monitoring available in the basic scalable architecture. We will install that in this chapter as well.

In this chapter, we will cover the following topics:

  • The basic architecture of this solution
  • A renewed Terraform configuration
  • Splitting application components into frontend and middleware tiers
  • Connecting the shared filesystem
  • Changes in Ansible files
  • Script enhancements

For managing omnibus installs, there is one central configuration file called gitlab.rb. You need to create it or copy an example. A template is available from https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/files/gitlab-config-template/gitlab.rb.template. Upgrades don’t replace or edit this file. In large parts of this chapter, I quote and discuss parts of this file.

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

Terraform providers use the following:

  • 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 Starting with the code section of previous chapter).
  • provider.tls v1.2.0: This is automatically downloaded with the terraform init command (see Starting with the code section of previous chapter).

For automating our deployments, we are using 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 available on GitHub, at: https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter20.

What will be included in the second iteration of a horizontally scaled GitLab? We have chosen to split components even more and change the shared filesystem type. We will introduce two monitoring components.

The following diagram is of the second iteration of the High Availability (HA) solution for GitLab:

The frontend servers in the basic HA design run the Unicorn/NGINX Rails application, as well as the background jobs, via the Sidekiq component. These are all installed as part of the GitLab omnibus package. In the second iteration, you can see that the NFS server in the diagram has been replaced with a Gitaly server. Gitaly is an Remote Procedure Call (RPC) server that is specifically designed to handle Git traffic and has the ability to scale better than NFS servers, while using less bandwidth.

In the basic HA solution, the shared filesystem was a basic Linux NFS Version 4 server that was mounted on both frontend application servers. In the second iteration, we replaced this with a dedicated Gitaly server, which you can find in the previous diagram.

The monitoring components are a combination of a Prometheus server (to accumulate time series data) and a Grafana dashboard (to display metrics from the Prometheus database). Both components can be installed using the GitLab omnibus package.

In this new version of the solution, there are some changes in the Terraform part, mainly in the amount of instances. Let’s look at the file that contains the AWS EC2 instances. With Terraform, you can create very elaborate structures, but for this example, we only add instances to make it more understandable.

The first machine to add is the Sidekiq node. It will be placed in the frontend servers’ security group (SG-frontendservers). As you can see from the following code, the node uses an Amazon Machine Image (AMI), which is defined in the variable.tf file. The instance type is t2.medium; you can find a list of instances types at https://aws.amazon.com/ec2/instance-types/. The instance description mentions the subnet, the public key pair, and the security group to be used:

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

The key pair that is generated at install time is also inserted into this machine, and the subnet where it is created is the public frontend one.

Using only one machine would not be very sufficient for our example, so we create an identical one with the new name of SIDEKIQ_B. It is placed in another availability zone.

The following is the description of this second Sidekiq instance, which is almost similar to the first, but for the name and availability zone:

resource "aws_instance" "SIDEKIQ_B" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.medium"
tags {
Name = "${var.environment}-SIDEKIQ002"
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 create an instance for our monitoring dashboard:

resource "aws_instance" "GRAFANA_A" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.medium"
tags {
Name = "${var.environment}-GRAFANA001"
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}"]
}

Notice that the Grafana server is also placed in the frontend servers’ subnet and security group. In this example, we are not creating redundant Grafana dashboards. This is possible, but most companies run this on one machine, which we are also doing for this example.

Like the Grafana instance, the Prometheus server is again placed in the frontend servers subnet and security group:

resource "aws_instance" "PROMETHEUS_A" {
ami = "${lookup(var.aws_ubuntu_amis,var.region)}"
instance_type = "t2.medium"
tags {
Name = "${var.environment}-PROMETHEUS001"
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}"]
}

The other main configuration change is in the Ansible host definition.

We create another Ansible host called SIDEKIQ001, which is coupled to SIDEKIQ_A in AWS. The extra attribute we give is the group, which we call middleware. This attribute is used in Ansible scripts to determine which hosts should be processed in a playbook. A custom attribute we create is role. In a clustered situation, we always designate the server to be either a slave or a master:

resource "ansible_host" "SIDEKIQ001" {
inventory_hostname = "${aws_instance.SIDEKIQ_A.private_dns}"
groups = ["middleware"]
vars
{
ansible_user = "ubuntu"
role = "slave"
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_A.public_dns}\""
proxy = "${aws_instance.BASTIONHOST_A.private_ip}"
subnet = "${aws_subnet.public-frontend_az-a.cidr_block}"
}
}

We also create a second host here, which has the role of master:

resource "ansible_host" "SIDEKIQ002" {
inventory_hostname = "${aws_instance.SIDEKIQ_B.private_dns}"
groups = ["middleware"]
vars
{
ansible_user = "ubuntu"
role = "master"
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 monitoring components are also created as Ansible hosts. For Grafana, we create an Ansible group called monitoring-dashboard:

resource "ansible_host" "GRAFANA001" {
inventory_hostname = "${aws_instance.GRAFANA_A.private_dns}"
groups = ["monitoring-dashboard"]
vars
{
ansible_user = "ubuntu"
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_A.public_dns}\""
proxy = "${aws_instance.BASTIONHOST_A.private_ip}"
subnet = "${aws_subnet.public-frontend_az-a.cidr_block}"
}
}

For the Prometheus server, we create a group called monitoring-server:

resource "ansible_host" "PROMETHEUS001" {
inventory_hostname = "${aws_instance.PROMETHEUS_A.private_dns}"
groups = ["monitoring-server"]
vars
{
ansible_user = "ubuntu"
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_A.public_dns}\""
proxy = "${aws_instance.BASTIONHOST_A.private_ip}"
subnet = "${aws_subnet.public-frontend_az-a.cidr_block}"
}
}

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

terraform apply

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

We have shown you the new parts of the Terraform configuration, and you have applied them to your Amazon AWS configuration. Now you can execute Ansible deployment scripts.

The split has already been prepared on the Terraform infrastructure level by creating extra EC2 instances in the correct subnets. Now it is time to configure those virtual machines. With the creation of extra instances, we basically defined a split in functionality, which means we have a frontend tier, a backend tier, and now also a middleware tier. Now we have to run the Ansible scripts to make the application use that infrastructure. We describe the function and the name of the script. We mention the Jinja template that is used in the deployment, and the run command to use is the ansible-playbook command.

In the former chapter all application services were running on the frontend. In this chapter we create a new middleware layer by splitting the frontend into more layers. Our current middleware is Sidekiq background processing, let’s put that functionality on separate nodes. The deployment is contained in the following playbook and template and you can apply the playbook using the Run command:

  • Script: install-middleware-services.yml
  • Jinja template: gitlab.rb.middleware.j2
  • Run command:ansible-playbook -i /usr/local/bin/terraform.py deploy/install-middleware-services.yml

The GitLab URL or endpoint will be the address of the load balancer. The value of this address can be extracted from our dynamic Ansible inventory command, /usr/local/bin/terraform.py. In the Ansible scripts, we can refer to a specific host variable:

external_url 'http://{{ hostvars[groups['lb'][0]]['inventory_hostname'] }}'

Sidekiq uses GitLab Shell, which needs to authenticate to the frontend servers as follows:

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

This server should have the application role:

roles ['application_role']

We specifically want to enable Sidekiq on this machine:

## Enable sidekiq
sidekiq['enable'] = true

Disable components that will not be on the GitLab application server:

gitaly['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

We specify that this will use the central Gitaly server:

# Gitaly
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

We also have to specify a database connection to pgbouncer:

gitlab_rails['db_adapter'] = 'postgresql'
gitlab_rails['db_encoding'] = 'unicode'
gitlab_rails['db_password'] = 'packtpub'
gitlab_rails['db_host'] = '{{ hostvars[groups['pgbouncer'][0]]['inventory_hostname'] }}'
gitlab_rails['db_port'] = 6432

Sidekiq also connects to Redis, so we need the relevant connection details:

gitlab_rails['redis_port'] = '6379'
gitlab_rails['redis_host'] = '{{ hostvars[groups['redis'][1]]['inventory_hostname'] }}'
gitlab_rails['redis_password'] = 'packtpub'

We only want a master server to execute migrations:

{% if hostvars[inventory_hostname].role == "slave" %}
gitlab_rails['initial_root_password'] = "packtpub"
# Make sure only 1 GitLab server executes migrations
gitlab_rails['auto_migrate'] = false
{% endif %}

We definitely want to disable letsencrypt on this server:

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

After successfully running this script, the middleware tier should exist and we should have succeeded in transferring Sidekiq from the frontend servers to the middleware servers.

In the basic setup, there was no active monitoring installed. For an HA cluster, this is not something we want. In Chapter 12, Monitoring with Prometheus, we discussed the ways we can use Prometheus to monitor GitLab instances. We can install it as well using Ansible in our infrastructure. In the following Ansible playbook script, we install in the cluster:

  • Sc ript: install-prometheus.yml
  • Jinja template: gitlab.rb.prometheus.j2
  • Run command:ansible-playbook -i /usr/local/bin/terraform.py deploy/install-prometheus.yml

To create a Prometheus server, enable it in the omnibus configuration file:

prometheus_monitoring['enable'] = true

The server should listen on every interface, as follows:

prometheus['listen_address'] = '0.0.0.0:9090'

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

After running this playbook, we have a Prometheus instance running in our cluster.

The Prometheus server provides the datastore to save metrics. There is a simple admin-interface which can produce graphs, but for decent monitoring you need a product which can create better ones. Grafana allows you to create fancy monitoring dashboards with data from Prometheus. The following Ansible playbook installs and configures this:

  • Script: install-grafana-dashboard.yml
  • Jinja template:gitlab.rb.grafana.j2
  • Run command:ansible-playbook -i /usr/local/bin/terraform.py deploy/install-grafana-dashboard.yml

The URL of our GitLab instance is as follows:

external_url 'http://{{ hostvars[groups['lb'][0]]['inventory_hostname'] }}'

Set the following to true to enable Grafana:

grafana['enable'] = true

The default admin password is admin; change it here:

grafana['admin_password'] = 'admin'

After running this playbook, the Grafana dashboard is available under the following URL, at /grafafa.

In the former chapter we used NFS as the location to store git repositories inside of GitLab. In this chapter we switch to Gitaly, the new application layer that eliminates the need for NFS. The Ansible playbook for installing Gitaly is new, and is basically a GitLab omnibus install that only activates the Gitaly service. The playbook, the accompanying template and the command to run it are:

  • Script: install-gitaly.yml
  • Jinja template: gitlab.rb.gitaly.j2
  • Run command: ansible-playbook -i /usr/local/bin/terraform.py deploy/install-gitaly.yml

Avoid running unnecessary services on the Gitaly server:

postgresql['enable'] = false
redis['enable'] = false
nginx['enable'] = false
prometheus['enable'] = false
unicorn['enable'] = false
sidekiq['enable'] = false
gitlab_workhorse['enable'] = false

Prevent database connections during gitlab-ctl reconfigure and disable migrations as follows:

gitlab_rails['rake_cache_clear'] = false
gitlab_rails['auto_migrate'] = false

Configure the gitlab-shell API callback URL:

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

Make Gitaly accept connections on all network interfaces:

gitaly['listen_addr'] = "0.0.0.0:8075"
gitaly['auth_token'] = 'abc123secret'

Define the storage locations to use:

gitaly['storage'] = [
{ 'name' => 'default', 'path' => '/mnt/gitlab/default/repositories' },
{ 'name' => 'storage1', 'path' => '/mnt/gitlab/storage1/repositories' },
]

After running this Ansible playbook, you should have a working Gitaly server that can be used to store GitLab items.

Of course, there have not only been changes in the Jinja templates, but playbooks have aslo been added. You can see the new list here:

.
├── files
├── install-bastion-hosts.yml
├── install-consul.yml
├── install-frontend-services.yml
├── install-gitaly.yml
├── install-grafana-dashboard.yml
├── install-middleware-services.yml
├── install-pgbouncer.yml
├── install-postgres-core.yml
├── install-postgres-slaves.yml
├── install-prometheus.yml
├── install-redis.yml
└── templates
├── databases.ini.j2
├── gitlab.rb.consul.j2
├── gitlab.rb.frontend.j2
├── gitlab.rb.gitaly.j2
├── gitlab.rb.grafana.j2
├── gitlab.rb.middleware.j2
├── gitlab.rb.pgbouncer.j2
├── gitlab.rb.postgres.j2
├── gitlab.rb.prometheus.j2
├── gitlab.rb.redis.j2
└── pgpass.j2

The newly added playbooks can be executed in the following fashion:

  1. Install the Gitaly server as follows:
ansible-playbook -i /usr/local/bin/terraform.py deploy/install-gitaly.yml
  1. Install the GitLab middleware servers as follows:
ansible-playbook -i /usr/local/bin/terraform.py deploy/install-middleware-services.yml
  1. Install the Prometheus monitoring server as follows:
ansible-playbook -i /usr/local/bin/terraform.py deploy/install-prometheus.yml
  1. Install the Grafana monitoring dashboard server as follows:
ansible-playbook -i /usr/local/bin/terraform.py deploy/install-grafana-dashboard.yml

Now that we have shown you all the changes in the Terraform code and Ansible playbooks, you should have a new, more HA-capable infrastructure. These changes also impact the scripts that are part of the Git repository.

The directory in the repository (https://github.com/PacktPublishing/Mastering-GitLab-12/tree/master/Chapter20) contains a script to connect to the instances that you first create with Terraform.

The connect_ssh.sh script is changed for the situation in this chapter, so you can connect to the new instances after they are created. You can execute the script with show_host_codes to check which hosts you can connect to:

$scripts/connect_ssh.sh show_host_codes
hostcode: pg0 -- hostname: "ip-10-0-2-195.eu-central-1.compute.internal"
hostcode: gitaly0 -- hostname: "ip-10-0-11-29.eu-central-1.compute.internal"
hostcode: middleware1 -- hostname: "ip-10-0-11-160.eu-central-1.compute.internal"
hostcode: middleware0 -- hostname: "ip-10-0-12-252.eu-central-1.compute.internal"
hostcode: frontend1 -- hostname: "ip-10-0-11-76.eu-central-1.compute.internal"
hostcode: frontend0 -- hostname: "ip-10-0-12-164.eu-central-1.compute.internal"
...

The new servers for Gitaly are visible in the previous output, and the rest of the output is truncated for better readability.

Remember, once you are connected, you can switch to root with the sudo -i command, check all log files, and debug the machine.

In this chapter, we created a second iteration of an HA architecture of GitLab. The frontend tier was split into frontend and backend, and a middleware tier containing Sidekiq nodes was created. The shared filesystem was changed from NFS to Gitaly, and we created nodes for a monitoring server (Prometheus) and a dashboard (Grafana) to connect to it. We changed the Terraform files, the Ansible playbooks and templates, and the shell scripts.

In the next chapter, we will go even further into splitting up nodes, and introduce a new middleware tier, which runs several background nodes that handle different kinds of traffic.

  1. Why is NFS not always a good choice to use in the cloud?
  2. What component is split from the frontend servers in the enhanced architecture laid out in this chapter?
  3. What file describes Ansible hosts?
  4. How many Grafana dashboards are in this architecture?
  5. What is the name of the Ansible group used for Sidekiq?
  • 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: