Hai semuanya, di materi kali ini kita akan membahas salah satu contoh penggunaannya untuk Docker Machine. Diantaranya

  1. Multiple Docker Host by Projects
  2. DRC (Disaster Revovery Center) system.

Ok langsung aja kita bahas materi yang pertama:

Multiple Docker Host by projects

Salah satu implementasi docker-machine adalah menggunakan multiple Docker Host by project. Magsudnya jika kita punya 3 project setiap project punya specifikasi containernya yang berbeda seperti berikut:

prejects:
    projectA: ['mysql', 'laravel']
    projectB: ['postgresql', 'springboot', 'angular']
    projectC: ['oracle', 'java-web'] 

Nah klo kita deploy dalam 1 docker host, maka semua container tersebut akan tercampur atau istilahnya (shared host). Ada kalanya kita ingin jalankan setiap project tersebut dalam environtment terpisah atau isolate (dedicated host) nah disini kita bisa manfaatkan docker-machine untuk membuat Docker Host masing-masing project dengan perintah seperti berikut:

docker-machine create -d virtualbox project-a;
eval $(docker-machine env project-a);
## deploy your container for projectA

docker-machine create -d virtualbox project-b;
eval $(docker-machine env project-b);
## deploy your container for projectB

docker-machine create -d virtualbox project-c;
eval $(docker-machine env project-c);
## deploy your container for projectC

Jika kita jalankan maka hasilnya seperti berikut:

 ~  docker-machine create -d virtualbox --virtualbox-no-vtx-check project-a
Running pre-create checks...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe env project-a

 ~  docker-machine env project-a
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.103:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\dimasm93\.docker\machine\machines\project-a"
$Env:DOCKER_MACHINE_NAME = "project-a"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

 ~  & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env project-a | Invoke-Expression

 ~  docker run -p 80:80 -d nginx
 ~  docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
903498c8d16c   nginx     "/docker-entrypoint.…"   25 seconds ago   Up 24 seconds   0.0.0.0:80->80/tcp   exciting_mirzakhani

 ~  curl $(docker-machine ip project-a)
StatusCode        : 200
StatusDescription : OK
Content           : <!DOCTYPE html>
                    <html>
                    <head>
                    <title>Welcome to nginx!</title>
                    <style>
                    html { color-scheme: light dark; }
                    body { width: 35em; margin: 0 auto;
                    font-family: Tahoma, Verdana, Arial, sans-serif; }
                    </style...
RawContent        : HTTP/1.1 200 OK

 ~  docker-machine create -d virtualbox --virtualbox-no-vtx-check project-b
Running pre-create checks...
Creating machine...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe env project-b

 ~  docker-machine env project-b
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.104:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\dimasm93\.docker\machine\machines\project-b"
$Env:DOCKER_MACHINE_NAME = "project-b"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

 ~  & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env project-b | Invoke-Expression

 ~  docker run -p 80:80 -d httpd
 ~  docker container ls
CONTAINER ID   IMAGE     COMMAND              CREATED          STATUS          PORTS
NAMES
cf215cc32b4a   httpd     "httpd-foreground"   11 seconds ago   Up 10 seconds   0.0.0.0:80->80/tcp   focused_dewdney

 ~  curl $(docker-machine ip project-b)                                                            
StatusCode        : 200
StatusDescription : OK
Content           : <html><body><h1>It works!</h1></body></html>
RawContent        : HTTP/1.1 200 OK

 ~  docker-machine ls
NAME        ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
default     -        virtualbox   Stopped                                       Unknown
project-a   -        virtualbox   Running   tcp://192.168.99.103:2376           v19.03.12
project-b   *        virtualbox   Running   tcp://192.168.99.104:2376           v19.03.12

DRC (Disaster Recovery Center) system simulation.

Dan selain untuk multiple project docker-machine juga bisa kita gunakan untuk meng-simulasi DRC (Disaster Recovery Center) system. Disaster Recovery System ini biasanya digunakan pada perusahan berscala menengah hinga besar, pemerintahan, dan beberapa startup karena untuk melindungi aplikasi, service, data agar tetap running jika terjadi bencana alam, hardware failure, network down dan lain-lain.

Dalam best practice suatu Disaster Recovery Center biasanya di tentukan berdasarkan jarak lokasi penentuan Data Center dan replicasinya memiliki radius 60KM (Kilometer). secara Topology kalo kita gambarkan maka seperti berikut:

topology-drc

Jadi pada gambar tersebut, kita lihat ada 4 region dan 1 routing yaitu

  1. dc1 (Data Center utama)
  2. drc1 (Replication)
  3. drc2 (Replication)
  4. drc3 (Replication)

Setiap dc dan drc biasanya kita akan kita installkan aplikasi, service, database dan lain-lain. Nah sekarang kita coba implement dengan docker-machine ya berikut perintahnya:

docker-machine create -d virtualbox dc1;
docker-machine create -d virtualbox drc1;
docker-machine create -d virtualbox drc2;

Nah Untuk routing sendiri biasanya adalah hardware tersediri ada banyak sekali implementasinya bisa pake f5, cisco, microtic dan lain-lain. Tetapi karena disini kita menggunakan di local semua kita bisa manfaat loadbalancer/proxy seperti

  1. Traefik proxy
  2. nginx load balancer
  3. etcd
  4. dan lain-lain.

Ok sekarang kita coba implement untuk routingnya juga, dengan menggunakan docker-machine seperti berikut:

docker-machine create -d virtualbox routing;

Sebagai contoh kita akan menggunakan nginx loadbalancer menggunakan method least-connected. Berikut configurasinya:

Jika di jalankan maka hasilnya seperti berikut:

 docker-machine ls
NAME              ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
machine-dc1       -        virtualbox   Running   tcp://192.168.99.105:2376           v19.03.12
machine-drc1      -        virtualbox   Running   tcp://192.168.99.106:2376           v19.03.12
machine-routing   -        virtualbox   Running   tcp://192.168.99.107:2376           v19.03.12

## connect to machine dc1
 docker-machine env machine-dc1
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.105:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\dimasm93\.docker\machine\machines\machine-dc1"
$Env:DOCKER_MACHINE_NAME = "machine-dc1"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

 & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env machine-dc1 | Invoke-Expression

 docker run -p 80:80 -d --restart always httpd

## connect to machine drc1
 docker-machine env machine-drc1
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.106:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\dimasm93\.docker\machine\machines\machine-drc1"
$Env:DOCKER_MACHINE_NAME = "machine-drc1"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

 & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env machine-drc1 | Invoke-Expression

 docker run -p 80:80 -d --restart always httpd

## connect to machine routing
 docker-machine env machine-routing
$Env:DOCKER_TLS_VERIFY = "1"
$Env:DOCKER_HOST = "tcp://192.168.99.107:2376"
$Env:DOCKER_CERT_PATH = "C:\Users\dimasm93\.docker\machine\machines\machine-routing"
$Env:DOCKER_MACHINE_NAME = "machine-routing"
$Env:COMPOSE_CONVERT_WINDOWS_PATHS = "true"

 & "C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env machine-routing | Invoke-Expression

 docker run -p 80:80 -v nginx.least-connect.template.conf:/etc/nginx/templates/default.conf.template -d nginx

 docker container ls
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                NAMES
d8d1aa0410a7   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 10 seconds   0.0.0.0:80->80/tcp   mystifying_dubinsky

## Test all up
 curl $(docker-machine ip machine-routing)
StatusCode        : 200
StatusDescription : OK
Content           : <html><body><h1>It works!</h1></body></html>

## Test down dc1
 docker-machine stop machine-dc1
Stopping "machine-dc1"...
Machine "machine-dc1" was stopped.

 curl $(docker-machine ip machine-routing)
StatusCode        : 200
StatusDescription : OK
Content           : <html><body><h1>It works!</h1></body></html>

## Test down dc1 & drc1
 curl $(docker-machine ip machine-routing)
curl : 502 Bad Gateway
nginx/1.21.6

## Test drc1 up again
 docker-machine start machine-drc1
Starting "machine-drc1"...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

 docker-machine ls
NAME              ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER      ERRORS
machine-dc1       -        virtualbox   Stopped                                       Unknown
machine-drc1      *        virtualbox   Running   tcp://192.168.99.106:2376           v19.03.12
machine-routing   -        virtualbox   Running   tcp://192.168.99.107:2376           v19.03.12

 curl $(docker-machine ip machine-routing)
StatusCode        : 200
StatusDescription : OK
Content           : <html><body><h1>It works!</h1></body></html>