Skip to content

Commit

Permalink
Merge pull request #11 from khushmeeet/nomad
Browse files Browse the repository at this point in the history
Setup Nomad cluster
  • Loading branch information
khushmeeet authored Sep 15, 2023
2 parents ef1d291 + 2744a76 commit b5d2443
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .ansible-lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
skip_list:
- 'fqcn-builtins'
20 changes: 20 additions & 0 deletions .github/workflows/hetzner_create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,23 @@ jobs:
run: sleep 2m
shell: bash
- run: echo "Job is ${{ job.status }}."

ansible:
name: Setup Nomad & Consul
needs: terraform
runs-on: ubuntu-latest
steps:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.11"

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y ansible
python -m pip install --upgrade pip
- name: Run Ansible Playbook
run: |
ansible-playbook ansible/setup-server.yml
12 changes: 12 additions & 0 deletions ansible/data-files/consul.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
datacenter = "dc-1"
data_dir = "/opt/consul"
bind_addr = "0.0.0.0"
bind_addr = "[::]"
client_addr = "0.0.0.0"

ui_config {
enabled = true
}

server = true
bootstrap_expect=1
21 changes: 21 additions & 0 deletions ansible/data-files/nomad.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
datacenter = "dc-1"
data_dir = "/opt/nomad/data"
bind_addr = "0.0.0.0"

server {
enabled = true
bootstrap_expect = 1
}

client {
enabled = true
servers = [ "127.0.0.1" ]
}

telemetry {
collection_interval = "1s"
disable_hostname = true
prometheus_metrics = true
publish_allocation_metrics = true
publish_node_metrics = true
}
6 changes: 6 additions & 0 deletions ansible/data-files/nomad_client.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
datacenter = "dc1"
data_dir = "/opt/nomad"

client {
enabled = true
}
2 changes: 2 additions & 0 deletions ansible/inventory.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[axion]
axion-1
114 changes: 112 additions & 2 deletions ansible/setup-server.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
---
- name: Axion server setup
hosts: all
vars_files:
- vars.yml
tasks:
- name: Update APT repo and cache
apt:
Expand All @@ -27,8 +29,9 @@

- name: Download and dearmor the Docker GPG key
command:
cmd: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg"
cmd: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg"
warn: false
changed_when: false

- name: Set permissions for docker.gpg
file:
Expand All @@ -38,8 +41,9 @@

- name: Add Docker linux repo
command:
cmd: echo "deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
cmd: echo "deb [arch=\"$(dpkg --print-architecture)\" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo \"$VERSION_CODENAME\") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
warn: false
changed_when: false

- name: Update APT cache
apt:
Expand All @@ -60,11 +64,13 @@
command:
cmd: wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
warn: false
changed_when: false

- name: Add official Hashicorp linux repo
command:
cmd: echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
warn: false
changed_when: false

- name: Update APT cache
apt:
Expand All @@ -84,3 +90,107 @@
pip:
name: awscli
executable: pip3

- name: Setup Nomad cluster
block:
- name: Create nomad config dir
file:
path: "{{ nomad_config_dir }}"
state: directory
mode: '0755'

- name: Create nomad data dir
file:
path: "{{ nomad_data_dir }}"
state: directory
mode: '0755'

- name: Copy nomad config file
copy:
src: data-files/nomad.hcl
dest: "{{ nomad_config_dir }}/nomad.hcl"
owner: axn
group: axn
mode: '0644'

- name: Uncomment line Wants=consul.service
lineinfile:
path: /usr/lib/systemd/system/nomad.service
regexp: '^#(Wants=consul.service)'
line: 'Wants=consul.service'
state: present

- name: Uncomment line After=consul.service
lineinfile:
path: /usr/lib/systemd/system/nomad.service
regexp: '^#(After=consul.service)'
line: 'After=consul.service'
state: present

- name: Start Nomad cluster
block:
- name: Enable nomad service
systemd:
name: nomad
enabled: true

- name: Start nomad service
systemd:
name: nomad
state: started

- name: Check nomad service status
command:
cmd: systemctl status nomad
register: service_status
changed_when: false
ignore_errors: true

- name: Display nomad service status
debug:
var: service_status.stdout_lines

- name: Setup Consul
block:
- name: Create nomad config dir
file:
path: "{{ consul_config_dir }}"
state: directory
mode: '0755'

- name: Create nomad data dir
file:
path: "{{ consul_data_dir }}"
state: directory
mode: '0755'

- name: Copy consul config file
copy:
src: data-files/consul.hcl
dest: "{{ consul_config_dir }}/consul.hcl"
owner: axn
group: axn
mode: '0644'

- name: Start Consul
block:
- name: Enable consul
systemd:
name: consul
enabled: true

- name: Start consul
systemd:
name: consul
state: started

- name: Check consul status
command:
cmd: systemctl status consul
register: service_status
changed_when: false
ignore_errors: true

- name: Display consul service status
debug:
var: service_status.stdout_lines
4 changes: 4 additions & 0 deletions ansible/vars.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
nomad_config_dir: /etc/nomad.d/
nomad_data_dir: /opt/nomad/
consul_config_dir: /etc/consul.d/
consul_data_dir: /opt/consul/
1 change: 1 addition & 0 deletions cloud-init/cloud-init.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ packages:
- gnupg-agent
- software-properties-common
- fail2ban
- unzip

runcmd:
- |
Expand Down

0 comments on commit b5d2443

Please sign in to comment.