Add some missing questions.
The main content of this chapter: use constraints to specify service constraints. Service mounts use local volume. The Elk Log platform uses the Gelf log driver to collect logs. 1. Specify service constraint
In the previous chapters, we created the Registry service, which was executed on a node by the Swarm automatic dispatch definition. In that case, if we restart the service, registry services may be started with random nodes.
All the mirrors we uploaded were gone. How to solve this problem?
When creating a service, you can use the--constraints parameter, followed by an expression, to restrict the service container's scheduling at each node. For example you want to specify that the service run on a node.
For example, the specified service runs on node01:
Docker Service Create--name registry--publish 5000:5000 \--constraint ' node.hostname==node01 ' registry
In addition to hostname, you can use other node properties to create a constraint expression, see the following table:
Node Properties |
Match |
Sample |
Node.id |
Node ID |
Node.id = = 2IVKU8V2GVTG4 |
Node.hostname |
Node hostname |
Node.hostname!= NODE02 |
Node.role |
Node Role:manager |
Node.role = = Manager |
Node.labels |
User-defined node labels |
Node.labels.security = = High |
Engine.labels |
Docker Engine Labels |
Engine.labels.operatingsystem = = Ubuntu 14.04 |
User-defined labels can be added using the Docker Node Update command, for example:
# Docker node Update--label-add Security=high node01
View Custom Labels
# docker node Inspect node01
[
{
' ID ': ' 0nhjsflo3tbd0b7hv2cyrjpin ',
...
' Spec ': {'
Labels ': {' Security
': ' High '
},
' role ': ' manager ',
' availability ': ' Active '
},
"Description": {
"Hostname": "Node01",
"Platform": {
"architecture": "x86_64",
"OS": " Linux "
},
...
}"
For existing service, you can add constraint configuration via Docker service update, for example:
Docker Service update registry \
--constraint-add ' Node.labels.security==high '
2. Volume Creation Management
With service constraints, we can guarantee that our registry service has been on the NODE01 node. But there is another problem, if we delete the registry service. The image of the container we uploaded is also deleted.
How do I guarantee that a mirror can be preserved even if the registry service is deleted?
Here we can use Docker volume to specify that a data volume is mounted to hold a mirror, even if the registry service is deleted. Let's restart a service to mount this data volume. The images we upload can also be saved.
In the Swarm cluster we can create local volumes or global volumes to mount to the container to hold the data.
Global volumes can be mounted on any node in the Swarm cluster, so you can access the data regardless of which node your service container starts on. However, Docker currently does not have the default global volume driver support, you can install a number of plug-in drivers to achieve global volumes such as Flocker, Portworx, and so on.
Local volume, there is only one mount volume that is local to a node.
Storage is Hard Here we use a simple local roll, ^_^.
To mount a local volume for the registry service we just created, you can use the following command:
Docker Service update registry \
--mount-add Type=volume,source=registry-vol,target=/var/lib/registry
Source=registry-vol Registry-vol is a volume name, Docker automatically creates a registry-vol local volume for us after executing the above command.
You can use the Docker Volume LS command to view:
# Docker Volume ls
DRIVER volume NAME
local registry-vol
# docker
Volume inspect [
{
' Name ': ' Registry-vol ', '
Driver ': ' Local ',
' mountpoint ': '/var/lib/docker/volumes/ Registry-vol/_data ",
" Labels ": null,
" Scope ":" Local "
}
]
The above command allows you to see the directory where the native volume is mounted to the node.
So even if we delete the registry service now. You can also use the following command to recreate a registry service that mounts Registry-vol to retrieve our mirrors.
Docker Service Create--name registry--publish 5000:5000 \--mount
source=registry-vol,type=volume,target=/var/ Lib/registry \
e search_backend=sqlalchemy \
--constraint ' node.hostname==node01 ' registry
Test
Use Docker-compose to upload our Docker currency image
[Root@node01 ~]# git clone https://github.com/jpetazzo/orchestration-workshop.git
[Root@node01 ~]# Orchestration-workshop/dockercoins
[root@node01 dockercoins]# cat docker-compose.yml-images
version: "2"
Services:
rng:
build:rng
Image: ${registry_slash}rng${colon_tag}
ports:
-"8,001:80"
hasher:
build:hasher
Image: ${registry_slash}hasher${colon_tag}
ports:
-"8,002:80"
WebUI:
build:webui
Image: ${registry_slash}webui${colon_tag}
ports:
-"8000:80"
Volumes:
-"./webui/files/:/files/"
redis:
image:redis
worker:
build:worker
image : ${registry_slash}worker${colon_tag}
Upload our application mirrors to registry