Docker Swarm Visualizer

Setelah kemaren bahas Docker Swarm lab, kali ini kita akan coba Swarm Visualizer, semacam web GUI untuk melihat Swarm.

Lab kali ini masih lanjutan dari Lab Docker Swarm Cluster.

Install unzip

ubuntu@docker-1:~$ sudo apt install unzip
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following package was automatically installed and is no longer required:
grub-pc-bin
Use 'sudo apt autoremove' to remove it.
Suggested packages:
zip
The following NEW packages will be installed:
unzip
0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.
Need to get 167 kB of archives.
After this operation, 558 kB of additional disk space will be used.
Get:1 http://nova.clouds.archive.ubuntu.com/ubuntu bionic/main amd64 unzip amd64 6.0-21ubuntu1 [167 kB]
Fetched 167 kB in 2s (78.5 kB/s)
Selecting previously unselected package unzip.
(Reading database … 60376 files and directories currently installed.)
Preparing to unpack …/unzip_6.0-21ubuntu1_amd64.deb …
Unpacking unzip (6.0-21ubuntu1) …
Processing triggers for mime-support (3.60ubuntu1) …
Setting up unzip (6.0-21ubuntu1) …
Processing triggers for man-db (2.8.3-2ubuntu0.1) …

Setelah itu download docker swarm visualizer

ubuntu@docker-1:~$ wget https://github.com/dockersamples/docker-swarm-visualizer/archive/master.zip

Kemudian extract

ubuntu@docker-1:~$ unzip master.zip

Ubah nama directory menjadi dockersample

mv docker-swarm-visualizer-master dockersamples

Jalankan perintah docker

ubuntu@docker-1:~$ docker run -it -d -p 5000:8080 -v /var/run/docker.sock:/var/run/docker.sock dockersamples/visualizer
Unable to find image 'dockersamples/visualizer:latest' locally
latest: Pulling from dockersamples/visualizer
cd784148e348: Pull complete
f6268ae5d1d7: Pull complete
97eb9028b14b: Pull complete
9975a7a2a3d1: Pull complete
ba903e5e6801: Pull complete
7f034edb1086: Pull complete
cd5dbf77b483: Pull complete
5e7311667ddb: Pull complete
687c1072bfcb: Pull complete
aa18e5d3472c: Pull complete
a3da1957bd6b: Pull complete
e42dbf1c67c4: Pull complete
5a18b01011d2: Pull complete
Digest: sha256:54d65cbcbff52ee7d789cd285fbe68f07a46e3419c8fcded437af4c616915c85
Status: Downloaded newer image for dockersamples/visualizer:latest
765b970ff0010e0956fb2bf002861a04a290f04ef3d6980afed31f2609b38b71

Sekarang kita akan coba membuat service nginx dengan 3 replika

ubuntu@docker-1:~$ docker service create --name my-web1 --publish 8080:80 --replicas 3 nginx
xsvgsuh3nsxg2ogg1wbqptj9f
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged

Cek service nginx jalan

Sekarang buka web browser, untuk melihat Swarm Visualizer (pada lab kali ini menggunakan port 5000)

Docker Swarm cluster

Docker swarm adalah container orchestration dan clustering tool untuk mengatur Docker hosts, yang merupakan bagian dari Docker engine. Yang merupakan original clustering yang dibuat dan disediakan oleh Docker sehingga menjamin High Availability dan High Performance aplikasi yang jalan.

Kali ini kita akan mencoba docker swarm dengan topologi seperti berikut

Docker1 (192.168.192.45) sebagai manager kemudian docker2 (192.168.192.47) dan docker3 (192.168.192.46) sebagai workernya. Semua server menggunakan OS Ubuntu 18.04.

Konfigurasi hosts di semua node docker

Edit file /etc/hosts disemua server, dan tambahkan syntax berikut

192.168.192.45 docker1
192.168.192.47 docker2
192.168.192.46 docker3

Coba ping dari node docker1 ke docker2 atau docker3, lakukan sebaliknya juga

ubuntu@docker-1:~$ ping docker2
PING docker2 (192.168.192.47) 56(84) bytes of data.
64 bytes from docker2 (192.168.192.47): icmp_seq=1 ttl=64 time=0.308 ms
64 bytes from docker2 (192.168.192.47): icmp_seq=2 ttl=64 time=0.318 ms
64 bytes from docker2 (192.168.192.47): icmp_seq=3 ttl=64 time=0.350 ms
^C
--- docker2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2031ms
rtt min/avg/max/mdev = 0.308/0.325/0.350/0.023 ms
ubuntu@docker-1:~$ ping docker3
PING docker3 (192.168.192.46) 56(84) bytes of data.
64 bytes from docker3 (192.168.192.46): icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from docker3 (192.168.192.46): icmp_seq=2 ttl=64 time=0.555 ms
64 bytes from docker3 (192.168.192.46): icmp_seq=3 ttl=64 time=0.574 ms
^C
--- docker3 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2052ms
rtt min/avg/max/mdev = 0.521/0.550/0.574/0.021 ms

Install dan menjalankan Docker Service di semua node docker

Update dan upgrade apt

# apt update && apt upgrade -y

Install paket untuk meng”allow” apt dapat digunakan melalui https

# apt-get install apt-transport-https ca-certificates curl software-properties-common -y

Tambahkan Docker GPG key dan set repositorinya

# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
# add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Install Docker CE versi terbaru

# apt-get install docker-ce

Enable docker service agar dapat jalan otomatis setiap server booting/direstart, kemudian start docker service

# systemctl enable docker
# systemctl start docker

Agar docker bisa dijalan pada user biasa (bukan root), gunakan command

# usermod -aG docker <username>

Pada lab kali ini saya menggunakan user ubuntu

# usermod -aG docker ubuntu

Sekarang kita coba menjalankan docker hello-world menggunakan user biasa

root@docker-1:/home/ubuntu# su - ubuntu
ubuntu@docker-1:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
The Docker client contacted the Docker daemon.
The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/

Konfigurasi Docker1 Node untuk Inisialisasi Swarm Cluster

Sekarang kita akan membuat swram cluster untuk ketiga node. Untuk membuatnya, memerlukan inialisasi pada dockermanager node (pada lab ini docker1 sebagai manager) kemudian dockerworker (docker2 dan docker3) untuk join cluster.

docker swarm init –advertise-addr <manager node IP address>

ubuntu@docker-1:~$ docker swarm init --advertise-addr 192.168.192.45
Swarm initialized: current node (ifoxr4e0uvx75wjoa5x38ebn5) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-0oxl56zp3d6uafxq21tk6pyblh4a1d2l7v6nfod0cl1f6d6wa6-40ptwffkqc8ynp07urtgkdl35 192.168.192.45:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
ubuntu@docker-1:~$

Pada hasil diatas, join token sudah digenerate oleh dockermanager, ini nanti digunakan untuk join dockerworkers

Konfigurasi Docker2 dan Docker3 Node untuk join Swarm Cluster

Dari hasil join token diatas, ketikkan command diatas tadi ke docker2 dan docker3

ubuntu@docker-2:~$ docker swarm join --token SWMTKN-1-0oxl56zp3d6uafxq21tk6pyblh4a1d2l7v6nfod0cl1f6d6wa6-40ptwffkqc8ynp07urtgkdl35 192.168.192.45:2377
This node joined a swarm as a worker.
ubuntu@docker-3:~$ docker swarm join --token SWMTKN-1-0oxl56zp3d6uafxq21tk6pyblh4a1d2l7v6nfod0cl1f6d6wa6-40ptwffkqc8ynp07urtgkdl35 192.168.192.45:2377
This node joined a swarm as a worker.

Verifikasi Swarm Cluster

Untuk melihat status node menggunakan command berikut di dockermanager

ubuntu@docker-1:~$ docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
ifoxr4e0uvx75wjoa5x38ebn5 * docker-1 Ready Active Leader 18.09.3
2ar0x7l132sxmvdpxyijd7r4p docker-2 Ready Active 18.09.3
moxtattg9gmsigwhgxk2rzh9s docker-3 Ready Active 18.09.3

Apabila kehilangan join token, bisa menggunakan command

docker swarm join-token manager -q

docker swarm join-token worker -q