-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocker-swarm
175 lines (130 loc) · 8.54 KB
/
Docker-swarm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
What is a swarm
------------------
A swarm consists of multiple Docker hosts which run in swarm mode and act as managers (to manage membership and delegation) and workers (which run swarm services). A given Docker host can be a manager, a worker, or perform both roles. When you create a service, you define its optimal state (number of replicas, network and storage resources available to it, ports the service exposes to the outside world, and more). Docker works to maintain that desired state. For instance, if a worker node becomes unavailable, Docker schedules that node’s tasks on other nodes. A task is a running container which is part of a swarm service and managed by a swarm manager, as opposed to a standalone container.
Feature highlights
-------------------
* Cluster management integrated with Docker Engine.
* Decentralized design: Instead of handling differentiation between node roles at deployment time, the Docker Engine handles any specialization at runtime.
* Declarative service model: Docker Engine uses a declarative approach to let you define the desired state of the various services in your application stack.
* Scaling: When you scale up or down, the swarm manager automatically adapts by adding or removing tasks to maintain the desired state.
* Desired state reconciliation: The swarm manager node constantly monitors the cluster state and reconciles any differences between the actual state and your expressed desired state.
* Multi-host networking: The swarm manager automatically assigns addresses to the containers on the overlay network when it initializes or updates the application.
* Service discovery: Swarm manager nodes assign each service in the swarm a unique DNS name and load balances running containers.
* Load balancing: You can expose the ports for services to an external load balancer.
* Secure by default: Each node in the swarm enforces TLS mutual authentication and encryption to secure communications between itself and all other nodes.
* Rolling updates: At rollout time you can apply service updates to nodes incrementally.
Nodes
-------
A node is an instance of the Docker engine participating in the swarm. You can also think of this as a Docker node.
1. Manager nodes also perform the orchestration and cluster management functions required to maintain the desired state of the swarm. Manager nodes elect a single leader to conduct orchestration tasks.
2. Worker nodes receive and execute tasks dispatched from manager nodes. By default manager nodes also run services as worker nodes, but you can configure them to run manager tasks exclusively and be manager-only nodes. An agent runs on each worker node and reports on the tasks assigned to it.
Services and tasks
--------------------
A service is the definition of the tasks to execute on the manager or worker nodes. It is the central structure of the swarm system and the primary root of user interaction with the swarm.
* replicated: count of containers diffrent of count of nodes. In the replicated services model, the swarm manager distributes a specific number of replica tasks among the nodes based upon the scale you set in the desired state.
* global: count of containers must be equal count of nodes(masters and workers). For global services, the swarm runs one task for the service on every available node in the cluster.
Load balancing
---------------
The swarm manager uses ingress load balancing to expose the services you want to make available externally to the swarm. The swarm manager can automatically assign the service a PublishedPort or you can configure a PublishedPort for the service. You can specify any unused port. If you do not specify a port, the swarm manager assigns the service a port in the 30000-32767 range.
Getting started with swarm mode
---------------------------------
initializing a cluster of Docker Engines in swarm mode
adding nodes to the swarm
deploying application services to the swarm
managing the swarm once you have everything running
Create a swarm
----------------
Run the following command to create a new swarm in manager node:
docker swarm init --advertise-addr <MANAGER-IP>
The output includes the commands to join new nodes to the swarm. Nodes will join as managers or workers depending on the value for the --token flag.
docker info
#view the current state of the swarm
docker swarm join --tocken ... $manager_addr:2377
#create a worker node joined to the existing swarm
docker swarm join-token worker
#If you don’t have the command available, you can run the following command on a manager node to retrieve the join command for a worker
docker swarm join-token manager
#If you don’t have the command available, you can run the following command on a manager node to retrieve the join command for a manager
docker swarm join-token manager --rotate
#create new token for all manager nodes
docker swarm join-token worker --rotate
#create new token for all worker nodes
docker swarm leave
#this command should be run in worker nodes for leave the cluster.
** from master node you can delete worker nodes but this action isn't good solution for reconcilation loop.
docker node
=============
docker node ls
---------------
view information about nodes. This is a cluster management command, and must be executed on a swarm manager node.
options:
--filter , -f Filter output based on conditions provided
--format Pretty-print nodes using a Go template
--quiet , -q Only display IDs
Filtering:
The currently supported filters are:
* id: The id filter matches all or part of a node’s id.
* label:
* node.label:
* membership: matches nodes based on the presence of a membership and a value "accepted" or "pending".
* name
* role
Example:
docker node ls -f node.label=region -f "membership=accepted" -f "role=manager"
docker node promote
---------------------
Promote one or more nodes to manager in the swarm. This is a cluster management command, and must be executed on a swarm manager node.
docker node ps
---------------
List tasks running on one or more nodes, also show DESIRED STATE and CURRENT STATE of service. defaults to current node. This is a cluster management command, and must be executed on a swarm manager node.
The desired-state filter can take the values "running", "shutdown" or "accepted".
docker node rm
---------------
Remove one or more nodes from the swarm. This is a cluster management command, and must be executed on a swarm manager node.
A manager node must be demoted to a worker node (using docker node demote) before you can remove it from the swarm.
docker node update
--------------------
Update metadata about a node, such as its availability, labels, or roles. must be executed on a swarm manager node.
options:
--availability Availability of the node ("active"|"pause"|"drain")
--label-add Add or update a node label (key=value)
--label-rm Remove a node label if exists
--role Role of the node ("worker"|"manager")
Example:
docker node update --availability drain --label-add test $worker1
docker node demote
------------------
Demote one or more nodes from manager in the swarm.This is a cluster management command, and must be executed on a swarm manager node.
Example:
docker node demote $node_name
docker node inspect
---------------------
Display detailed information on one or more nodes. This is a cluster management command, and must be executed on a swarm manager node.
Example:
docker node inspect --format '{{ .ManagerStatus.Leader }}' $node_name
docker node inspect --format=pretty $node_name
Deploy a service to the swarm
===============================
After you create a swarm, you can deploy a service to the swarm. This is a cluster management command, and must be executed on a swarm manager node.
docker service create
----------------------
Creates a service as described by the specified parameters. This is a cluster management command, and must be executed on a swarm manager node.
docker service create --replicas 1 --name test alpine ping docker.com
docker service create --mode global --name redis2 redis:3.0.6
Create a service using an image on a private registry
------------------------------------------------------
1.
docker login registry.example.com
2.
docker service create \
--with-registry-auth \
--name my_service \
registry.example.com/acme/my_image:latest
docker service ls
------------------
#see the list of running services
docker service inspect --pretty <SERVICE-ID>
#display the details about a service. To return the service details in json format, run the same command without the --pretty flag.
docker service ps <SERVICE-ID>
#see which nodes are running the service
docker service scale apps=6