Install Nomad, Podman dan Traefik untuk Selfhosting Rumahan

Latar Belakang

Nomad adalah sebuah scheduler yang dikembangkan oleh Hashicorp, yang mana scheduler ini bisa digunakan untuk melakukan scheduling container, raw execution, dan aplikasi lainnya. Keunikan ini membuat Nomad bisa digunakan untuk menjalankan berbagai jenis aplikasi, tidak hanya container lewat konsep Task Driver.

Yang perlu disiapkan

Prasyarat Pengetahuan

Lingkungan yang dibutuhkan

Jadi yang perlu disiapkan antara lain:

Catatan Penting

Instalasi

Pastikan server tujuan sudah bisa diakses lewat ssh key. Kemudian jalankan perintah berikut:

1hashi-up nomad install \
2 --ssh-target-addr 192.168.1.10 \
3 --ssh-target-user ubuntu \
4 --ssh-target-key ~/.ssh/id_ed25519 \
5 --server

tolong pastikan untuk mengganti IP, user dan alamat ssh key sesuai dengan lingkunganmu.

Podman Task Driver

Podman adalah salah satu task driver yang didukung Nomad, kenapa memilih Podman? karena belakangan saya memang menggunakan Podman sebagai daily driver. Proses instalasi dapat mengikuti panduan dari dokumentasi resmi Nomad di halaman ini.

Selain itu, pastikan juga Podman sudah terinstal.

1sudo apt install podman

Selanjutnya perlu dipastikan agar Nomad mengenali plugin ini.

1sudo vim /etc/nomad.d/nomad.hcl

tambahkan baris berikut.

1# generated with hashi-up
2 
3datacenter = "dc1"
4data_dir = "/opt/nomad"
+plugin_dir = "/opt/nomad/data/plugins"
6server {
7 enabled = true
8 bootstrap_expect = 1
9}

Restart server Nomad.

1sudo systemctl restart nomad

Pastikan plugin sudah tertera pada bagian task driver.

1nomad node status -self -short | grep Drivers
2CSI Drivers = <none>
3Drivers = exec,podman

Nomad Single Node

Untuk menjalankan Nomad sebagai single-node, kita perlu enable client mode pada server Nomad ini. Edit lagi file /etc/nomad.d/nomad.hcl dan tambahkan baris berikut

1# generated with hashi-up
2 
3datacenter = "dc1"
4data_dir = "/opt/nomad"
5plugin_dir = "/opt/nomad/data/plugins"
6server {
7 enabled = true
8 bootstrap_expect = 1
9}
10 
+client {
+ enabled = true
+}

Jangan lupa restart service nomad

Traefik dan Nomad service discovery

Buka nomad webui melalui browser, biasanya di http://192.168.1.10:4646/ui. Kemudian klik tombol "Run job" dan tambahkan spesifikasi job berikut.

1job "traefik" {
2 datacenters = ["dc1"]
3 type = "service"
4 
5 group "traefik" {
6 count = 1
7 
8 network {
9 port "http" {
10 static = 8080
11 }
12 
13 port "admin" {
14 static = 8081
15 }
16 }
17 
18 service {
19 name = "traefik-http"
20 provider = "nomad"
21 port = "http"
22 }
23 
24 task "server" {
25 driver = "podman"
26 
27 config {
28 image = "docker.io/traefik:v2.11.20"
29 ports = ["admin", "http"]
30 
31 args = [
32 "--api.dashboard=true",
33 "--api.insecure=true", # Traefik akan diinstall dengan mode insecure, sebaiknya jangan di ekspose ke internet
34 "--entrypoints.web.address=:${NOMAD_PORT_http}",
35 "--entrypoints.traefik.address=:${NOMAD_PORT_admin}",
36 "--providers.nomad=true",
37 "--providers.nomad.endpoint.address=http://${NOMAD_IP_http}:4646",
38 "--providers.nomad.exposedByDefault=false"
39 ]
40 }
41 }
42 }
43}

Deploy demo aplikasi

Selanjutnya kita akan deploy sebuah aplikasi yang mendemokan load balancing sederhana melalui traefik yang sudah terkoneksi dengan Nomad service discovery.

1job "demo-webapp" {
2 datacenters = ["dc1"]
3 
4 group "demo" {
5 count = 3
6 
7 network {
8 port "http"{
9 to = -1
10 }
11 }
12 
13 service {
14 name = "demo-webapp"
15 port = "http"
16 provider = "nomad"
17 
18 tags = [
19 "traefik.enable=true",
20 "traefik.http.routers.demo-webapp-http.rule=Host(`demo-webapp-192-168-1-10.nip.io`)",
21 "traefik.http.routers.demo-webapp-http.tls=false",
22 ]
23 
24 check {
25 type = "http"
26 path = "/"
27 interval = "2s"
28 timeout = "2s"
29 }
30 }
31 
32 task "server" {
33 env {
34 PORT = "${NOMAD_PORT_http}"
35 NODE_IP = "${NOMAD_IP_http}"
36 }
37 
38 driver = "podman"
39 
40 config {
41 image = "docker.io/hashicorp/demo-webapp-lb-guide"
42 ports = ["http"]
43 }
44 }
45 }
46}

Setalah job di atas dijalankan, maka aplikasi demo-webapp akan melalui proses Load balancing oleh Traefik terlebih dahulu, setelah itu diarahkan ke masing-masing container.

1ubuntu@nomad01:~$ curl http://demo-webapp-192-168-1-10.nip.io:8080
2Welcome! You are on node 192.168.1.10:20190
3ubuntu@nomad01:~$ curl http://demo-webapp-192-168-1-10.nip.io:8080
4Welcome! You are on node 192.168.1.10:25458
5ubuntu@nomad01:~$ curl http://demo-webapp-192-168-1-10.nip.io:8080
6Welcome! You are on node 192.168.1.10:25482
7ubuntu@nomad01:~$ curl http://demo-webapp-192-168-1-10.nip.io:8080
8Welcome! You are on node 192.168.1.10:20190

Dapat kamu lihat kalau yang meresponse request kita ada beberapa container dengan masing-masing port yang berbeda, port tersebut dialokasikan oleh Nomad secara dinamis.

Demikian tutorial singkat ini, di artikel selanjutnya saya akan bahas cara menjalankan aplikasi stateful semacam database dengan host volume di Nomad.