- create a new swarm
$ docker swarm init --advertise-addr <MANAGER-IP>
- To retrieve the join command including the join token for worker/manager nodes, run the following command on a manager node:
$ docker swarm join-token worker/manager
- Run the command from the output on the worker/manager to join the swarm:
docker swarm join \ --token SWMTKN-1-49nj1cmql0jkz5s954yi3oex3nedyz0fb0xx14ie39trti4wxv-8vxv8rssmk743ojnwacrr2e7c \ 192.168.99.100:2377
The docker run command creates and starts a container on the local docker host. A docker "service" is one or more containers with the same configuration running under docker's swarm mode. It's similar to docker run in that you spin up a container. The difference is that you now have orchestration. That orchestration restarts your container if it stops, finds the appropriate node to run the container on based on your constraints, scale your service up or down, allows you to use the mesh networking and a VIP to discover your service, and perform rolling updates to minimize the risk of an outage during a change to your running application.
- Initialize a swarm with autolocking enabled
$ docker swarm init --autolock
- Enable or disable autolock on an existing swarm
docker swarm update --autolock=true
- Create a service
$ docker service create --name="myservice" --replicas 3 ubuntu:latest
- Change a service:
docker service update --replicas 5 myservice
Return low-level information on Docker objects. Docker inspect provides detailed information on constructs controlled by Docker. By default, docker inspect will render results in a JSON array.
6. Convert an application deployment into a stack file using a YAML compose file with "docker stack deploy"
The docker stack deploy command supports any Compose file of version “3.0” or above.
Create and update a stack from a compose or file on the swarm. This command has to be run targeting a manager node.
docker stack deploy --compose-file docker-compose.yml stackdemo
docker service update --replicas 2 SERVICE
docker service scale SERVICE=REPLICAS
docker service update \ --publish-add published=8080,target=80 \ myservice
docker service update \ --network-rm my-network \ --network-add name=my-network,alias=web1 \ myservice
docker service update \ --mount-add \ type=volume,source=other-volume,target=/somewhere-else \ myservice
There are two types of service deployments, replicated and global.
For a replicated service, you specify the number of identical tasks you want to run. For example, you decide to deploy an HTTP service with three replicas, each serving the same content.
A global service is a service that runs one task on every node. There is no pre-specified number of tasks. Each time you add a node to the swarm, the orchestrator creates a task and the scheduler assigns the task to the new node. Good candidates for global services are monitoring agents, an anti-virus scanners or other types of containers that you want to run on every node in the swarm.
The diagram below shows a three-service replica in yellow and a global service in gray.
???
We can use node constraints to control which nodes a service’s tasks will run on based upon node labels. Here is an example:
docker service create --constraint node.labels.availability_zone==east nginx
To use various expressions for constraints, such as inequality, we can run, for instance:
docker service create --constraint node.labels.availability_zone!=east
Use --placement-pref
to spread tasks evenly based on the value of a specific label:
docker service create --placement-pref spread=node.labels.availability_zone --replicas 3 nginx
To make our swarm cluster highly available, we must use multiple manager nodes. Swarm managers use a consensus algorithm to maintain consistent data about the swarm state across all nodes. This algorithm requires that a quorum be maintained for changes to be made to the cluster.
Quorum: The majority (more than half) of the manager nodes in our swarm. To achieve quorum, more than half of the total amount of nodes in our swarm must be available and communicate with the other available nodes. Remember if exactly half of our nodes are available, this does not count as a quorum. We must have more than half.
You can use templates for some flags of service create, using the syntax provided by the Go’s text/template package.
The supported flags are the following :
--hostname
--mount
--env
docker service create --name hosttempl \ --hostname="{{.Node.Hostname}}-{{.Node.ID}}-{{.Service.Name}}"\ busybox top