All about docker swarm
There is always a requirement to run every individual service without a fail over and load balancing. When this comes to container services docker swarm comes into picture.
Docker swarm is a cluster of docker containers which provides a container orchestration framework. it basically composed of few key elements:
- managers and workers
- managers are also know as workers
- there will be only one manager as leader, other managers will act as a backup
- as a pre-requisite, you docker version should be on 1.12+
how to initiate docker swarm:
docker swarm init — advertise-addr :2377 — listen-addr managerIP:swarmListenPort
2377: is the default port for swarm
-addvertise-addr: will let swarm manager to use specific IP:PORT.
here I am running this on ec2 instance as manager1(in case if your host contains multiple IPs its best practice to use a specific one for all swarm related stuff)
[root@ip-172–31–22–15 ec2-user]# docker swarm init — advertise-addr 172.31.22.15:2377 — listen-addr 172.31.22.15:2377
Swarm initialized: current node (icuih1r0n8juo8xigkceniu3j) is now a manager.
#To add a worker to this swarm, run the following command:
$ docker swarm join --token SWMTKN-1-15z6ejowow53p7cn1ncam9v4vz049jzgnoghpi7tr63dn550as-7998mw9s4djvozgde40xnh3ig 172.31.22.15:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
[root@ip-172-31-22-15 ec2-user]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
icuih1r0n8juo8xigkceniu3j * docker Ready Active Leader
#the highlighted command is the exact command that we need to run on a worker/manager that you wanna join to this swarm, it includes a token
[root@ip-172-31-22-15 ec2-user]# docker swarm join-token manager
To add a manager to this swarm, run the following command:
$ docker swarm join --token SWMTKN-1-15z6ejowow53p7cn1ncam9v4vz049jzgnoghpi7tr63dn550as-9wiyb3pyiviqikgid7wettiqs 172.31.22.15:2377
[root@ip-172-31-22-15 ec2-user]# docker swarm join-token worker
To add a worker to this swarm, run the following command:
$ docker swarm join --token SWMTKN-1-15z6ejowow53p7cn1ncam9v4vz049jzgnoghpi7tr63dn550as-7998mw9s4djvozgde40xnh3ig 172.31.22.15:2377#following above command to join leader as worker/manager launch another ec2 instance or any with docker 1.12+ in it and
#docker swarm join --token SWMTKN-1-15z6ejowow53p7cn1ncam9v4vz049jzgnoghpi7tr63dn550as-9wiyb3pyiviqikgid7wettiqs 172.31.22.15:2377
#you will see all the workers/managers you have joined with your swarm from Leader node as:
[root@ip-172-31-22-15 ec2-user]# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
1ndqsslh7fpquc7fi35leig54 worker4 Ready Active
1qh4aat24nts5izo3cgsboy77 worker5 Ready Active
25nwmw5eg7a5ms4ch93aw0k03 worker3 Ready Active
icuih1r0n8juo8xigkceniu3j * manager1 Ready Active Leader
5pm9f2pzr8ndijqkkblkgqbsf worker2 Ready Active
9yq4lcmfg0382p39euk8lj9p4 worker1 Ready Active# docker info will give you a detailed info on your swarm
[root@ip-172-31-22-15 ec2-user]# docker info
Containers: 12
Running: 0
Paused: 0
Stopped: 12
Images: 1
Server Version: 1.13.1
Storage Driver: aufs
Root Dir: /var/lib/docker/aufs
Backing Filesystem: extfs
Dirs: 54
Dirperm1 Supported: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Swarm: active
NodeID: icuih1r0n8juo8xigkceniu3j
Is Manager: true
ClusterID: hpvfpcevwt8144bj65yk744q8
Managers: 1
Nodes: 6
Orchestration:
.
..
Node Address: 10.91.20.119
Manager Addresses:
10.91.20.119:2377........
now creating a SERVICE and running it on docker swarm
(the whole idea of setting this orchestration layer is, we don’t need to worry on our app as where it is running but it will be up for the whole time)
different option with docker service:
docker service create | scale | ls | ps | inspect | rm
ex:
docker network create -d overlay pp-net
docker service scale >> docker service update — replicas
docker service scale Name=7
docker service ps Name
red@docker:/software/docker-images$ docker service create --name myswarmapp -p 9090:80 punitporwal07/apache
rvzrpe4szt0vdyqte7g7tfshs
by doing this, any time when you gonna hit your exposed port for service to any host/IP in swarm it will give you your application , without having its container running on it. (service will be running only on leader/manager1)
accessing the service now:
NOTE: after advertising listen address to docker swarm, you may get error next time when you try to initialize docker daemon. (if you are using dynamic IP)
/var/lib/docker/swarm/docker-state.json
/var/lib/docker/swarm/state.json
above two files hold the IP and failed to initialize docker daemon
ERRO[0001] cluster exited with error: failed to listen on remote API address: listen tcp 10.91.20.119:2377: bind: cannot assign requested address
FATA[0001] Error creating cluster component: swarm component could not be started: failed to listen on remote API address: listen tcp 10.91.20.119:2377: bind: cannot assign requested address
change the IP and initialize it again
$ service docker restart
Punit