Install Docker Swarm Cluster and Deploy a Service on AlmaLinux 8

Install Docker Swarm Cluster dan Deploy a Service pada AlmaLinux 8

  • Minimum Requirement: 2 CPU & 1GB RAM
  • Node Controller: 192.168.88.100
  • Node Worker01: 192.168.88.11
  • Node Worker02: 192.168.88.12

Install Docker CE pada Semua Node

  • Update repository almalinux dan install dependencies
yum update -y && yum install epel-release -y
yum install -y yum-utils lvm2 device-mapper device-mapper-persistent-data device-mapper-event device-mapper-libs device-mapper-event-libs net-tools wget htop nano
  • Install Docker CE
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && yum makecache
yum install docker-ce docker-ce-cli containerd.io -y
  • Jalankan service docker
systemctl enable docker
systemctl start docker
systemctl status docker
  • Cek versi docker
docker version
---<output>---
Client: Docker Engine - Community
 Version:           23.0.1
 API version:       1.42
 Go version:        go1.19.5
 Git commit:        a5ee5b1
 Built:             Thu Feb  9 19:49:07 2023
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          23.0.1
  API version:      1.42 (minimum version 1.12)
  Go version:       go1.19.5
  Git commit:       bc3805a
  Built:            Thu Feb  9 19:46:47 2023
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.18
  GitCommit:        2456e983eb9e37e47538f59ea18f2043c9a73640
 runc:
  Version:          1.1.4
  GitCommit:        v1.1.4-0-g5fd4c4d
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Install Docker Compose CE pada Semua Node

  • Download docker compose versi terbaru
curl -s https://api.github.com/repos/docker/compose/releases/latest | grep browser_download_url  | grep docker-compose-linux-x86_64 | cut -d '"' -f 4 | wget -qi -
  • Tambahkan permission execute dan copy file ke directory /usr/local/bin
chmod +x docker-compose-linux-x86_64
sudo cp -R docker-compose-linux-x86_64 /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  • Cek versi docker compose
docker-compose version
---<output>---
Docker Compose version v2.16.0

Setting Firewalld pada Semua Node

  • Firewalld untuk Node Controller
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=2377/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload
  • Firewalld untuk Node Worker
firewall-cmd --permanent --add-port=2376/tcp
firewall-cmd --permanent --add-port=7946/tcp
firewall-cmd --permanent --add-port=7946/udp
firewall-cmd --permanent --add-port=4789/udp
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

Install Docker Swarm pada Node Controller

  • Install Docker Swarm
docker swarm init --advertise-addr 192.168.88.100
---<output>---
Swarm initialized: current node (u84mzdp4gqqi0ay75hyoii04d) is now a manager.
To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-4l29ny2l6u0s6fhujwxrxypzwy73a1u3cwxykac0s00vtpijib-1mgwa9pw2j3fijatvbe4gmrsb 192.168.88.100:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  • Join Docker Swarm
  • Copy perintah docker swarm join –token * pada semua Node Worker
docker swarm join --token SWMTKN-1-4l29ny2l6u0s6fhujwxrxypzwy73a1u3cwxykac0s00vtpijib-1mgwa9pw2j3fijatvbe4gmrsb 192.168.88.100:2377

  • Cek join-token manager untuk menambah node controller
docker swarm join-token manager
  • Cek join-token worker untuk menambah node worker
docker swarm join-token worker

  • Cek status docker swarm pada Node Controller
docker node ls
---<output>---
ID                            HOSTNAME     STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
u84mzdp4gqqi0ay75hyoii04d *   controller   Ready     Active         Leader           23.0.1
luutqruwq1yh4zd441hlr5y8j     worker1      Ready     Active                          23.0.1
q6rkgrtqkaislbppzmh75gk5q     worker2      Ready     Active                          23.0.1
  • Cek docker swarm info
docker info
---<output>---
 Swarm: active
  NodeID: u84mzdp4gqqi0ay75hyoii04d
  Is Manager: true
  ClusterID: po81mltcbwgwfaq1q9lqdqg5c
  Managers: 1
  Nodes: 3
  Default Address Pool: 10.0.0.0/8
  SubnetSize: 24
  Data Path Port: 4789
  Orchestration:
   Task History Retention Limit: 5
  Raft:
   Snapshot Interval: 10000
   Number of Old Snapshots to Retain: 0
   Heartbeat Tick: 1
   Election Tick: 10
  Dispatcher:
   Heartbeat Period: 5 seconds
  CA Configuration:
   Expiry Duration: 3 months
   Force Rotate: 0
  Autolock Managers: false
  Root Rotation In Progress: false
  Node Address: 192.168.88.100
  Manager Addresses:
   192.168.88.100:2377
 Runtimes: io.containerd.runc.v2 runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 2456e983eb9e37e47538f59ea18f2043c9a73640
 runc version: v1.1.4-0-g5fd4c4d
 init version: de40ad0
 Security Options:
  seccomp
   Profile: builtin
 Kernel Version: 4.18.0-425.13.1.el8_7.x86_64
 Operating System: AlmaLinux 8.7 (Stone Smilodon)
 OSType: linux
 Architecture: x86_64
 CPUs: 3
 Total Memory: 1.77GiB
 Name: controller
 ID: 89c0a7dc-79a0-4fe5-9867-f5e0ef0504d8
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Registry: https://index.docker.io/v1/
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Deploy Service pada Docker Swarm Cluster

  • Deploy service nginx+php dengan nama web-test menggunakan image sysopsid/nginx-php dengan publish port 8080 ke port 8080 yang ada pada service.
docker service create --publish 8080:8080 --name web-test sysopsid/nginx-php
---<output>---
4hpwcw3zd9na2foiksjc2t83w
overall progress: 1 out of 1 tasks
1/1: running   [==================================================>]
verify: Service converged
  • Cek docker service
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                       PORTS
4hpwcw3zd9na   web-test   replicated   1/1        sysopsid/nginx-php:latest   *:8080->8080/tcp
  • Cek docker service task
docker service ps web-test
---<output>---
ID             NAME         IMAGE                       NODE      DESIRED STATE   CURRENT STATE           ERROR     PORTS
mxqach63nvzq   web-test.1   sysopsid/nginx-php:latest   worker1   Running         Running 2 minutes ago
  • Hapus docker service
docker service rm web-test

  • Deploy service nginx+php dengan nama nginx-web, jumlah replikasi container 5 dan menggunakan image sysopsid/nginx-php dengan publish port 8080 ke port 8080 yang ada pada local service.
docker service create --publish 8080:8080 --name nginx-web --replicas 5 sysopsid/nginx-php
---<output>---
lxol8uq4036qwa0oe19w8esxq
overall progress: 5 out of 5 tasks
1/5: running   [==================================================>]
2/5: running   [==================================================>]
3/5: running   [==================================================>]
4/5: running   [==================================================>]
5/5: running   [==================================================>]
verify: Service converged
  • Akses docker service lewat browser menggunakan IP Node dengan url: http://192.168.88.100:8080
  • Berikut merupakan tampilan default image sysopsid/nginx-php.
  • Tambahkan/kurangi service (scale), replikasi 7
docker service scale nginx-web=7
---<output>---
nginx-web scaled to 7
overall progress: 7 out of 7 tasks
1/7: running   [==================================================>]
2/7: running   [==================================================>]
3/7: running   [==================================================>]
4/7: running   [==================================================>]
5/7: running   [==================================================>]
6/7: running   [==================================================>]
7/7: running   [==================================================>]
verify: Service converged
  • Cek docker service task
docker service ls
---<output>---
ID             NAME        MODE         REPLICAS   IMAGE                       PORTS
lxol8uq4036q   nginx-web   replicated   7/7        sysopsid/nginx-php:latest   *:8080->8080/tcp

-------------------------------------------------------------
docker service ps nginx-web
---<output>---
ID             NAME          IMAGE                       NODE         DESIRED STATE   CURRENT STATE                ERROR     PORTS
mp6gztdflzum   nginx-web.1   sysopsid/nginx-php:latest   controller   Running         Running 8 minutes ago
edyyqam154lv   nginx-web.2   sysopsid/nginx-php:latest   worker2      Running         Running 8 minutes ago
049rs89070ok   nginx-web.3   sysopsid/nginx-php:latest   controller   Running         Running 8 minutes ago
v27nvbeupp2z   nginx-web.4   sysopsid/nginx-php:latest   worker1      Running         Running 8 minutes ago
tnk12x7rfe0t   nginx-web.5   sysopsid/nginx-php:latest   worker2      Running         Running 8 minutes ago
ij39vu5smhl8   nginx-web.6   sysopsid/nginx-php:latest   worker1      Running         Running about a minute ago
4a342ulolzna   nginx-web.7   sysopsid/nginx-php:latest   worker1      Running         Running about a minute ago

  • Deploy service nginx+php dengan nama nginx-web, jumlah replikasi container 8, maksimal jumlah container per node ada 4, hanya deploy pada node berdasarkan role worker saja dan menggunakan image sysopsid/nginx-php dengan publish port 8080 ke port 8080 yang ada pada local service.
docker service create --publish 8080:8080 --name nginx-web --replicas 8 --replicas-max-per-node 4 --constraint node.role==worker sysopsid/nginx-php
---<output>---
mdkndiplnh5vvva5ispjmkph5
overall progress: 8 out of 8 tasks
1/8: running   [==================================================>]
2/8: running   [==================================================>]
3/8: running   [==================================================>]
4/8: running   [==================================================>]
5/8: running   [==================================================>]
6/8: running   [==================================================>]
7/8: running   [==================================================>]
8/8: running   [==================================================>]
verify: Service converged
  • Cek docker service task
  • Semua container berada pada node worker
docker service ps nginx-web
---<output>---
ID             NAME          IMAGE                       NODE      DESIRED STATE   CURRENT STATE                ERROR     PORTS
dhc97cb2e18o   nginx-web.1   sysopsid/nginx-php:latest   worker1   Running         Running about a minute ago
m1nmqy04esjm   nginx-web.2   sysopsid/nginx-php:latest   worker2   Running         Running about a minute ago
yqsw8axo32xe   nginx-web.3   sysopsid/nginx-php:latest   worker1   Running         Running about a minute ago
wktvkzd4ymrx   nginx-web.4   sysopsid/nginx-php:latest   worker2   Running         Running about a minute ago
se41q5sbe0kl   nginx-web.5   sysopsid/nginx-php:latest   worker1   Running         Running about a minute ago
d6puftef1shx   nginx-web.6   sysopsid/nginx-php:latest   worker2   Running         Running about a minute ago
cwg63qzwtqrh   nginx-web.7   sysopsid/nginx-php:latest   worker1   Running         Running about a minute ago
bculf0bjsqra   nginx-web.8   sysopsid/nginx-php:latest   worker2   Running         Running about a minute ago

  • Deploy service berdasarkan label pada node
  • Tambahkan label dengan nama WEB-NODE pada semua node
docker node update --label-add WEB-NODE=yes controller
docker node update --label-add WEB-NODE=yes worker1
docker node update --label-add WEB-NODE=yes worker2
  • Deploy service nginx+php dengan nama nginx-web menggunakan mode global (akan di replikasi per node 1 container) dengan label WEB-NODE
docker service create --name nginx-web --mode global --publish 8080:8080 --constraint node.labels.WEB-NODE==yes sysopsid/nginx-php
---<output>---
cre0c2momf4q6hvhhc7li67gv
overall progress: 3 out of 3 tasks
u84mzdp4gqqi: running   [==================================================>]
luutqruwq1yh: running   [==================================================>]
q6rkgrtqkais: running   [==================================================>]
verify: Service converged

  • Deploy service nginx+php dengan nama nginx-web, jumlah replikasi container 2, dengan jumlah cpu 2 dan memory 256 MB, hanya deploy pada node berdasarkan role worker saja dan menggunakan image sysopsid/nginx-php dengan publish port 8080 ke port 8080 yang ada pada local service.
docker service create --publish 8080:8080 --name nginx-web --replicas 2 --limit-cpu 2 --limit-memory 256M --constraint node.role==worker sysopsid/nginx-php
---<output>---
v40gbbkujphu8jmw4si0z2u98
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged
  • Update docker service publish port
docker service update --publish-add 443:443 nginx-web
---<output>---
nginx-web
overall progress: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged

-------------------------------------------------------------
docker service ls
---<output>---
ID             NAME        MODE         REPLICAS   IMAGE                       PORTS
v40gbbkujphu   nginx-web   replicated   2/2        sysopsid/nginx-php:latest   *:443->443/tcp, *:8080->8080/tcp
  • Roll back docker service
docker service update --rollback nginx-web
---<output>---
nginx-web
rollback: manually requested rollback
overall progress: rolling back update: 2 out of 2 tasks
1/2: running   [==================================================>]
2/2: running   [==================================================>]
verify: Service converged

-------------------------------------------------------------
docker service ls
---<output>---
ID             NAME        MODE         REPLICAS   IMAGE                       PORTS
v40gbbkujphu   nginx-web   replicated   2/2        sysopsid/nginx-php:latest   *:8080->8080/tcp

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.