Part 4: Docker Machine, Swarm Introduction and implementation
1. Introduction
Docker Swarm is an Orchestration tool(like kubernetes, or Apache Mesos.) to automate the administration of docker machines which in turn consists of containers & images.
Docker Machine:- Sets up a VM using Oracle VirtualBox driver with lightweight system and binaries, and can be managed through docker-machine command, and each docker machine can contain several containers or images to build up a redundancy stack.
2. Pre-requisites:
Docker 1.13 or higher
Docker Machine (which is already installed with Docker for Windows/Mac)
$ docker --version Docker version 18.09.0, build 4d60db4 $ docker-machine --version docker-machine version 0.16.0, build 702c267f If docker machine is not installed, please go ahead and install it from: https://docs.docker.com/machine/install-machine/
3. Create Docker Machine: Manager First
$ docker-machine create --driver virtualbox firstmanager Creating CA: /Users/saket1447583/.docker/machine/certs/ca.pem Creating client certificate: /Users/saket1447583/.docker/machine/certs/cert.pem Running pre-create checks... (firstmanager) Image cache directory does not exist, creating it at /Users/saket1447583/.docker/machine/cache... (firstmanager) No default Boot2Docker ISO found locally, downloading the latest release... (firstmanager) Latest release for github.com/boot2docker/boot2docker is v18.09.0 (firstmanager) Downloading /Users/saket1447583/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso... (firstmanager) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100% Creating machine... (firstmanager) Unable to get the local Boot2Docker ISO version: Did not find prefix "-v" in version string (firstmanager) Default Boot2Docker ISO is out-of-date, downloading the latest release... (firstmanager) Latest release for github.com/boot2docker/boot2docker is v18.09.0 (firstmanager) Downloading /Users/saket1447583/.docker/machine/cache/boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.09.0/boot2docker.iso... (firstmanager) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100% (firstmanager) Copying /Users/saket1447583/.docker/machine/cache/boot2docker.iso to /Users/saket1447583/.docker/machine/machines/firstmanager/boot2docker.iso... (firstmanager) Creating VirtualBox VM... (firstmanager) Creating SSH key... (firstmanager) Starting the VM... (firstmanager) Check network to re-create if needed... (firstmanager) Found a new host-only adapter: "vboxnet0" (firstmanager) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env firstmanager $$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS firstmanager - virtualbox Running tcp://192.168.99.100:2376 v18.09.0 $ docker-machine env firstmanager export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/Users/saket/.docker/machine/machines/firstmanager" export DOCKER_MACHINE_NAME="firstmanager" # Run this command to configure your shell: # eval $(docker-machine env firstmanager) $ eval $(docker-machine env firstmanager) $ $ docker-machine ip firstmanager 192.168.99.100 $ docker-machine ssh firstmanager ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www.tinycorelinux.net docker@firstmanager:~$ sudo -i root@firstmanager:~#
4. Create Workers: Worker1 and Worker2 which work as replica of manager
$ docker-machine create --driver virtualbox worker1 Running pre-create checks... Creating machine... (worker1) Copying /Users/saket1447583/.docker/machine/cache/boot2docker.iso to /Users/saket1447583/.docker/machine/machines/worker1/boot2docker.iso... (worker1) Creating VirtualBox VM... (worker1) Creating SSH key... (worker1) Starting the VM... (worker1) Check network to re-create if needed... (worker1) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env worker1 $ docker-machine create --driver virtualbox worker2 Running pre-create checks... Creating machine... (worker2) Copying /Users/saket1447583/.docker/machine/cache/boot2docker.iso to /Users/saket1447583/.docker/machine/machines/worker2/boot2docker.iso... (worker2) Creating VirtualBox VM... (worker2) Creating SSH key... (worker2) Starting the VM... (worker2) Check network to re-create if needed... (worker2) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env worker2 $ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS manager1 - virtualbox Running tcp://192.168.99.100:2376 v18.09.1 worker1 - virtualbox Running tcp://192.168.99.101:2376 v18.09.1 worker2 - virtualbox Running tcp://192.168.99.102:2376 v18.09.1
5. Initialize Docker Swarm Manager
$ docker-machine ssh firstmanager ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www.tinycorelinux.net docker@firstmanager:~$ docker swarm ca init join join-token leave unlock unlock-key update docker@firstmanager:~$ docker swarm init --advertise-addr 192.168.99.100 Swarm initialized: current node (hovtav35t9iy126sr3i4cph0d) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-66lucqcy4fc33edg4tgwqym2e4ba9kob25qn841wuwvq5exgij-egs6ptn42bmywm2y3flwr9fju 192.168.99.100:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions. docker@firstmanager:~$ docker@firstmanager:~$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION hovtav35t9iy126sr3i4cph0d * firstmanager Ready Active Leader 18.09.1 docker@firstmanager:~$
6. Add workers in the swarm manager
$ docker-machine ssh worker1 ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www.tinycorelinux.net docker@worker1:~$ docker swarm join --token SWMTKN-1-66lucqcy4fc33edg4tgwqym2e4ba9kob25qn841wuwvq5exgij-egs6ptn42bmywm2y3flwr9fju 192.168.99.100:2377 This node joined a swarm as a worker. docker@worker1:~$ $ docker-machine ssh worker2 ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www.tinycorelinux.net docker@worker2:~$ docker swarm join --token SWMTKN-1-66lucqcy4fc33edg4tgwqym2e4ba9kob25qn841wuwvq5exgij-egs6ptn42bmywm2y3flwr9fju 192.168.99.100:2377 This node joined a swarm as a worker. docker@firstmanager:~$ docker node ls ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION hovtav35t9iy126sr3i4cph0d * firstmanager Ready Active Leader 18.09.1 zl35nrpwy8r7bhrk1lmoqt0u0 worker1 Ready Active 18.09.1 ldquoyox5mk2o0f6mq3hv0as5 worker2 Ready Active 18.09.1 docker@firstmanager:~$
7. Viewing docker machine information
$ docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 18.09.1 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: active NodeID: hovtav35t9iy126sr3i4cph0d Is Manager: true ClusterID: k01wstqtkitfd2qtm38rcmnrj Managers: 1 Nodes: 3 Default Address Pool: 10.0.0.0/8 SubnetSize: 24 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.99.100 Manager Addresses: 192.168.99.100:2377 Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: 9754871865f7fe2f4e74d43e2fc7ccd237edcbce runc version: 96ec2177ae841256168fcf76954f7177af9446eb init version: fec3683 Security Options: seccomp Profile: default Kernel Version: 4.14.92-boot2docker Operating System: Boot2Docker 18.09.1 (TCL 8.2.1) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 989.4MiB Name: firstmanager ID: DLT5:TQEO:JODC:OZHS:MRJJ:PVA6:UDOR:FCKV:2C7R:MSHU:PPQ3:OE2X Docker Root Dir: /mnt/sda1/var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: provider=virtualbox Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false Product License: Community Engine docker@firstmanager:~$
8. Check which service to deploy in docker swarm from hub.docker.com.
For example:- I am deploying nginx server.
9. Create nginx service 3 replicas.
docker@firstmanager:~$ docker service create --replicas 3 -p 80:80 --name webserver nginx waipleiz63xxkokaoflm7o5l7 overall progress: 3 out of 3 tasks 1/3: running 2/3: running 3/3: running verify: Service converged docker@firstmanager:~$ docker@firstmanager:~$ docker service ls ID NAME MODE REPLICAS IMAGE PORTS waipleiz63xx webserver replicated 3/3 nginx:latest *:80->80/tcp docker@firstmanager:~$ docker service ps webserver ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS qk9dxcpe856w webserver.1 nginx:latest firstmanager Running Running 13 seconds ago uji9lri8t2u1 webserver.2 nginx:latest worker1 Running Running 14 seconds ago 1itqowxe6pko webserver.3 nginx:latest worker2 Running Running 12 seconds ago docker@firstmanager:~$
10. Verify the server status.
We can see below that nginx service is running on all the deployed hosts. 1. Scaling Service To scale the service and reduce/increase its replicas we can use below commands: $ docker service scale webserver=2 (create 2 replicas) or, $ docker service scale webserver=5 (create 5 replicas) 2. Updating Service And to update the deployed service to a specific version, we can use below: $ docker service update --image nginx:1.15.0 webserver1 3. Removal of Service/Node Run below commands on node, say worker1: ### To remove the service $ docker service rm webserver1 ### To leave from swarm $ docker swarm leave
References:
https://docs.docker.com/get-started/
https://www.youtube.com/watch?v=bU2NNFJ-UXA