Install Docker Swarm Cluster and Deploy a Service on Ubuntu 24.04

Install Docker Swarm Cluster dan deploy service pada Ubuntu 24.04

  • Minimum Requirement: 2 CPU & 1GB RAM
  • Node Controller: node01.sys-ops.id – 192.168.10.101
  • Node Worker01: node02.sys-ops.id – 192.168.10.102
  • Node Worker02: node03.sys-ops.id – 192.168.10.103

Install Docker CE pada semua Node

  • Update repository ubuntu dan install dependencies
apt update && apt upgrade -y
apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
  • Tambahkan repository Docker official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • Set repository stable docker ce
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • Install Docker CE versi terbaru
apt update && apt install docker-ce docker-ce-cli containerd.io -y

Install Docker Compose pada semua Node

  • Install 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 -
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 dan docker-compose
docker version
---<output>---
Client: Docker Engine - Community
 Version:           26.1.4
 API version:       1.45
 Go version:        go1.21.11
 Git commit:        5650f9b
 Built:             Wed Jun  5 11:28:57 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          26.1.4
  API version:      1.45 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       de5c9cf
  Built:            Wed Jun  5 11:28:57 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.33
  GitCommit:        d2d58213f83a351ca8f528a95fbd145f5654e957
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

============================================================
docker-compose version
---<output>---
Docker Compose version v2.27.1

Install Docker Swarm pada Node Controller

  • Install docker swarm
docker swarm init --advertise-addr 192.168.10.101
---<output>---
Swarm initialized: current node (qxhbqokwtpzawyyglua5gepk8) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-47wgtx0xx43tukzq1iz8hpdk48bvto1lr35kuah0bn3b4cxpvq-4dphfvl1xwnb8kgsxk7frp9x9 192.168.10.101:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
  • Copy perintah docker swarm join –token ******* pada semua node worker
docker swarm join --token SWMTKN-1-5shwu7ld887dwsn4dd29sim2g1q6itzs455st83xow8p4a7igy-2w57ifo1ms452yggxek6u94kp 129.168.10.101: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 node
docker node ls
---<output>---
ID                            HOSTNAME            STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
qxhbqokwtpzawyyglua5gepk8 *   node01.sys-ops.id   Ready     Active         Leader           26.1.4
v2qp2k4clgc6jdaa4qdey7zi1     node02.sys-ops.id   Ready     Active                          26.1.4
8p4pq8w155ujpbqqoy72n6b27     node03.sys-ops.id   Ready     Active                          26.1.4

Deploy Service pada Docker Swarm Cluster di node Controller

  • Deploy service dengan nama web-test dan menggunakan image sysopsid/web-test
docker service create --name web-test sysopsid/web-test
---<output>---
rzmrhqxw0f036yl7tlgjjbzpd
overall progress: 1 out of 1 tasks 
1/1: running   
verify: Service rzmrhqxw0f036yl7tlgjjbzpd converged 
  • Cek statu docker service
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
rzmrhqxw0f03   web-test   replicated   1/1        sysopsid/web-test:latest   
  • Remove docker service
docker service rm web-test

  • Deploy service dengan nama web-test dan menggunakan image sysopsid/web-test dengan jumlah replica 3
docker service create --replicas 3 --name web-test sysopsid/web-test
---<output>---
sgefmr3joxuxbh3u2vc716m2u
overall progress: 3 out of 3 tasks 
1/3: running   
2/3: running   
3/3: running   
verify: Service sgefmr3joxuxbh3u2vc716m2u converged
  • Cek status docker service, replica 3/3
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
sgefmr3joxux   web-test   replicated   3/3        sysopsid/web-test:latest   
  • Cek status container service
docker service ps web-test
---<output>---
ID             NAME         IMAGE                      NODE                DESIRED STATE   CURRENT STATE            ERROR     PORTS
d65kh6ga8lqu   web-test.1   sysopsid/web-test:latest   node01.sys-ops.id   Running         Running 30 seconds ago             
c1kxxx0tln52   web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 31 seconds ago             
w86y88irvmrz   web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 31 seconds ago   
  • Tambahkan/kurangi service (scale), scale 2 (sesuaikan dengan kebutuhan dan jumlah node)
docker service scale web-test=6
---<output>---
web-test scaled to 6
overall progress: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service web-test converged 
  • Cek status container service
docker service ps web-test
---<output>---
ID             NAME         IMAGE                      NODE                DESIRED STATE   CURRENT STATE                ERROR     PORTS
d65kh6ga8lqu   web-test.1   sysopsid/web-test:latest   node01.sys-ops.id   Running         Running about a minute ago             
c1kxxx0tln52   web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running about a minute ago             
w86y88irvmrz   web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running about a minute ago             
ch2qwqhv8jw9   web-test.4   sysopsid/web-test:latest   node01.sys-ops.id   Running         Running 22 seconds ago                 
oflbz7cwqazy   web-test.5   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 22 seconds ago                 
t3wpke9n695u   web-test.6   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 22 seconds ago  
  • Update image service container pada docker service web-test dari sysopsid/web-test:latest menjadi sysopsid/web-test:v4
docker service update --image sysopsid/web-test:v4 web-test
---<output>---
web-test
overall progress: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service web-test converged 
  • Cek status docker service dan container service, image sudah di update
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                  PORTS
sgefmr3joxux   web-test   replicated   6/6        sysopsid/web-test:v4   
=====================================================================================================
docker service ps web-test
---<output>---
ID             NAME             IMAGE                      NODE                DESIRED STATE   CURRENT STATE             ERROR     PORTS
nl5yg5azaa0f   web-test.1       sysopsid/web-test:v4       node01.sys-ops.id   Running         Running 47 seconds ago              
d65kh6ga8lqu    \_ web-test.1   sysopsid/web-test:latest   node01.sys-ops.id   Shutdown        Shutdown 47 seconds ago             
mjsiohht2oz1   web-test.2       sysopsid/web-test:v4       node02.sys-ops.id   Running         Running 52 seconds ago              
c1kxxx0tln52    \_ web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Shutdown        Shutdown 53 seconds ago             
s4pin3syrd76   web-test.3       sysopsid/web-test:v4       node03.sys-ops.id   Running         Running 38 seconds ago              
w86y88irvmrz    \_ web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Shutdown        Shutdown 39 seconds ago             
nzcjnn12ke8q   web-test.4       sysopsid/web-test:v4       node01.sys-ops.id   Running         Running 49 seconds ago              
ch2qwqhv8jw9    \_ web-test.4   sysopsid/web-test:latest   node01.sys-ops.id   Shutdown        Shutdown 50 seconds ago             
1xtm528u45t0   web-test.5       sysopsid/web-test:v4       node02.sys-ops.id   Running         Running 44 seconds ago              
oflbz7cwqazy    \_ web-test.5   sysopsid/web-test:latest   node02.sys-ops.id   Shutdown        Shutdown 44 seconds ago             
n7oft2jr2vlg   web-test.6       sysopsid/web-test:v4       node03.sys-ops.id   Running         Running 41 seconds ago              
t3wpke9n695u    \_ web-test.6   sysopsid/web-test:latest   node03.sys-ops.id   Shutdown        Shutdown 42 seconds ago    
  • Roll back docker service web-test
docker service update --rollback web-test
---<output>---
web-test
rollback: manually requested rollback 
overall progress: rolling back update: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service web-test converged 
  • Cek status docker service dan container service, image sudah di roll back
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
sgefmr3joxux   web-test   replicated   6/6        sysopsid/web-test:latest   
=====================================================================================================
docker service ps web-test
---<output>---
ID             NAME             IMAGE                      NODE                DESIRED STATE   CURRENT STATE             ERROR     PORTS
n0ellzfbvjqk   web-test.1       sysopsid/web-test:latest   node01.sys-ops.id   Running         Running 46 seconds ago              
nl5yg5azaa0f    \_ web-test.1   sysopsid/web-test:v4       node01.sys-ops.id   Shutdown        Shutdown 46 seconds ago             
d65kh6ga8lqu    \_ web-test.1   sysopsid/web-test:latest   node01.sys-ops.id   Shutdown        Shutdown 3 minutes ago              
06p51nvuwb84   web-test.2       sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 51 seconds ago              
mjsiohht2oz1    \_ web-test.2   sysopsid/web-test:v4       node02.sys-ops.id   Shutdown        Shutdown 51 seconds ago             
c1kxxx0tln52    \_ web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Shutdown        Shutdown 3 minutes ago              
0ip2u9ifacnp   web-test.3       sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 40 seconds ago              
s4pin3syrd76    \_ web-test.3   sysopsid/web-test:v4       node03.sys-ops.id   Shutdown        Shutdown 41 seconds ago             
w86y88irvmrz    \_ web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Shutdown        Shutdown 2 minutes ago              
3jj0slzm12i0   web-test.4       sysopsid/web-test:latest   node01.sys-ops.id   Running         Running 48 seconds ago              
nzcjnn12ke8q    \_ web-test.4   sysopsid/web-test:v4       node01.sys-ops.id   Shutdown        Shutdown 49 seconds ago             
ch2qwqhv8jw9    \_ web-test.4   sysopsid/web-test:latest   node01.sys-ops.id   Shutdown        Shutdown 3 minutes ago              
p3w5tgnepnxj   web-test.5       sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 54 seconds ago              
1xtm528u45t0    \_ web-test.5   sysopsid/web-test:v4       node02.sys-ops.id   Shutdown        Shutdown 54 seconds ago             
oflbz7cwqazy    \_ web-test.5   sysopsid/web-test:latest   node02.sys-ops.id   Shutdown        Shutdown 3 minutes ago              
kg3q1c03a0x0   web-test.6       sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 43 seconds ago              
n7oft2jr2vlg    \_ web-test.6   sysopsid/web-test:v4       node03.sys-ops.id   Shutdown        Shutdown 43 seconds ago             
t3wpke9n695u    \_ web-test.6   sysopsid/web-test:latest   node03.sys-ops.id   Shutdown        Shutdown 2 minutes ago
  • Update docker service add publish port 80:80 pada service web-test
docker service update --publish-add 80:80 web-test
---<output>---
web-test
overall progress: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service web-test converged 
  • Cek status docker service, publish port 80:80 sudah di tambahkan pada service web-test
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
sgefmr3joxux   web-test   replicated   6/6        sysopsid/web-test:latest   *:80->80/tcp
  • Update docker service remove publish port 80:80 pada service web-test
docker service update --publish-rm 80:80 web-test
---<output>---
web-test
overall progress: 6 out of 6 tasks 
1/6: running   
2/6: running   
3/6: running   
4/6: running   
5/6: running   
6/6: running   
verify: Service web-test converged
  • Cek status docker service, publish port 80:80 sudah di remove dari service web-test
docker service ls
---<output>---
ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
sgefmr3joxux   web-test   replicated   6/6        sysopsid/web-test:latest  
  • Remove docker service
docker service rm web-test

    • Deploy service dengan nama web-test, jumlah replikasi container 3 dan menggunakan image sysopsid/web-test dengan publish port 80 ke port 80 yang ada pada container service
    docker service create --publish 80:80 --name web-test --replicas 3 sysopsid/web-test
    ---<output>---
    q5le1vtxnrcq4pkwsf73j340o
    overall progress: 3 out of 3 tasks 
    1/3: running   
    2/3: running   
    3/3: running   
    verify: Service q5le1vtxnrcq4pkwsf73j340o converged
    • Cek status docker service
    docker service ls
    ---<output>---
    ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
    q5le1vtxnrcq   web-test   replicated   3/3        sysopsid/web-test:latest   *:80->80/tcp
    • Akses docker service lewat browser menggunakan url: http://ip_node_atau_domain
    • Remove docker service
    docker service rm web-test

    • Deploy service dengan nama web-test, jumlah replikasi container 6, maksimal jumlah container per node ada 3, hanya deploy pada node berdasarkan role worker saja dan menggunakan image sysopsid/web-test dengan publish port 80 ke port 80 yang ada pada container service
    docker service create --publish 80:80 --name web-test --replicas 6 --replicas-max-per-node 3 --constraint node.role==worker sysopsid/web-test
    ---<output>---
    no6ns6nmkk8tiftyug7z1mv63
    overall progress: 6 out of 6 tasks 
    1/6: running   
    2/6: running   
    3/6: running   
    4/6: running   
    5/6: running   
    6/6: running   
    verify: Service no6ns6nmkk8tiftyug7z1mv63 converged 
    • Cek status docker service dan container service, container hanya di deploy pada node dengan role worker saja yaitu node02 dan node03
    docker service ls
    ---<output>---
    ID             NAME       MODE         REPLICAS               IMAGE                      PORTS
    no6ns6nmkk8t   web-test   replicated   6/6 (max 3 per node)   sysopsid/web-test:latest   *:80->80/tcp
    =====================================================================================================
    docker service ps web-test
    ---<output>---
    ID             NAME         IMAGE                      NODE                DESIRED STATE   CURRENT STATE                ERROR     PORTS
    v030jwppxs2i   web-test.1   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running about a minute ago             
    2ooznzb9e05e   web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running about a minute ago             
    o8gujbzqttqu   web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running about a minute ago             
    l7do4n0yle29   web-test.4   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running about a minute ago             
    trqpjz7mmks6   web-test.5   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running about a minute ago             
    1g32fxi35cme   web-test.6   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running about a minute ago
    • Remove docker service
    docker service rm web-test

    • Deploy service berdasarkan label pada node dan mode global
    • Tambahkan label dengan nama WEB-NODE pada semua node
    docker node update --label-add WEB-NODE=yes node01.sys-ops.id
    docker node update --label-add WEB-NODE=yes node02.sys-ops.id
    docker node update --label-add WEB-NODE=yes node03.sys-ops.id
    • Deploy service dengan nama web-test menggunakan mode global (replikasi per node 1 container) dengan label WEB-NODE menggunakan image sysopsid/web-test dengan publish port 80 ke port 80 yang ada pada container service
    docker service create --name web-test --mode global --publish 80:80 --constraint node.labels.WEB-NODE==yes sysopsid/web-test
    ---<output>---
    qv6gtu6cvkaac30fqtw5ve6jk
    overall progress: 3 out of 3 tasks 
    8p4pq8w155uj: running   
    v2qp2k4clgc6: running   
    qxhbqokwtpza: running   
    verify: Service qv6gtu6cvkaac30fqtw5ve6jk converged 
    • Cek status docker service dan container service
    docker service ls
    ---<output>---
    ID             NAME       MODE      REPLICAS   IMAGE                      PORTS
    qv6gtu6cvkaa   web-test   global    3/3        sysopsid/web-test:latest   *:80->80/tcp
    =====================================================================================================
    docker service ps web-test
    ---<output>---
    ID             NAME                                 IMAGE                      NODE                DESIRED STATE   CURRENT STATE            ERROR     PORTS
    t9xcld15tl3k   web-test.8p4pq8w155ujpbqqoy72n6b27   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 41 seconds ago             
    swhcn9p1h4t7   web-test.qxhbqokwtpzawyyglua5gepk8   sysopsid/web-test:latest   node01.sys-ops.id   Running         Running 41 seconds ago             
    x71qbvm2xefv   web-test.v2qp2k4clgc6jdaa4qdey7zi1   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 41 seconds ago  
    • Remove docker service
    docker service rm web-test

    • Deploy service dengan nama web-test, jumlah replikasi container 4, dengan jumlah cpu 2 dan memory 256 MB per-container , hanya deploy pada node berdasarkan role worker saja dan menggunakan image sysopsid/web-test dengan publish port 80 ke port 80 yang ada pada container service
    docker service create --publish 80:80 --name web-test --replicas 4 --limit-cpu 2 --limit-memory 256M --constraint node.role==worker sysopsid/web-test
    ---<output>---
    lxby6nlgcaxqsiz51imb4drju
    overall progress: 4 out of 4 tasks 
    1/4: running   
    2/4: running   
    3/4: running   
    4/4: running   
    verify: Service lxby6nlgcaxqsiz51imb4drju converged 
    docker service ls
    ---<output>---
    ID             NAME       MODE         REPLICAS   IMAGE                      PORTS
    lxby6nlgcaxq   web-test   replicated   4/4        sysopsid/web-test:latest   *:80->80/tcp
    =====================================================================================================
    docker service ps web-test
    ---<output>---
    ID             NAME         IMAGE                      NODE                DESIRED STATE   CURRENT STATE            ERROR     PORTS
    qz3algp8t20m   web-test.1   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 41 seconds ago             
    mtlxpyegnn9m   web-test.2   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 41 seconds ago             
    m42z0b3z16w7   web-test.3   sysopsid/web-test:latest   node03.sys-ops.id   Running         Running 41 seconds ago             
    w3701m592sh1   web-test.4   sysopsid/web-test:latest   node02.sys-ops.id   Running         Running 41 seconds ago
    • Remove docker service
    docker service rm web-test

    herdiana3389

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