docker swarm

Posted 银灯玉箫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker swarm相关的知识,希望对你有一定的参考价值。

集群管理

Initialize a swarm. The docker engine targeted by this command becomes a manager in the newly created single-node swarm.

  • docker swarm init
  • docker swarm join --token SWMTKN-1-55g0lk1oofqdmf6adpn4idy7wxvql65t6rlhmq0khwlmwq5t6j-bkijx6trp01n8fn3hwhf5h7ap 10.10.1.195:2377

To remove worker2, issue the following command from worker2 itself:

  • docker swarm leave
    Remove one or more nodes from the swarm
  • docker node rm
  • docker node ls
  • docker swarm leave --force

service

  • docker service create --name busybox busybox:latest sh -c “while true; do sleep 3600; done”
  • docker service ls
  • docker service ps busybox
    当前busybox这个service的task只有1个,扩展为5个。
  • docker service scale busybox=5
    当某个task对应的容器挂掉时,会自动在任一节点启动该task对应的容器。
  • docker service ps busybox
  • docker rm -f 7d013a7eb685
  • docker service create --name nginx-service --host foo:10.0.1.30 --network swarm-net --constraint node.hostname==p09 --replicas 1 nginx:latest
  • docker service update --host-rm foo:127.0.0.1 my-service
  • docker service update --host-add foo:127.0.0.2 my-service
    -docker service inspect --format ‘.Spec.TaskTemplate.ContainerSpec.Hosts’ my-service
  • 查看端口映射
    docker port a7dafeccf24e
    [127.0.0.2 foo]
  • docker service ls
    删除service
  • docker service rm busybox
    inspect
  • docker service inspect hadoop-master

删除所有服务:

  • docker service rm $(docker service ls)

volume

https://docker-docs.netlify.app/engine/reference/commandline/service_create/#options

  • docker volume create my-vol
- docker service create \\
  --name my-service \\
  --replicas 3 \\
  --mount type=volume,source=my-volume,destination=/path/in/container,volume-label="color=red",volume-label="shape=round" \\
  nginx:alpine
docker service create \\
--name nginx -p 80:80 \\
-m type=bind,source=/Users/beenanner/Documents,target=/documents,writable=false \\
-m type=bind,source=/Users/beenanner/Pictures,target=/images,writable=true \\
nginx

For each replica of the service, the engine requests a volume named “my-volume” from the default (“local”) volume driver where the task is deployed. If the volume does not exist, the engine creates a new volume and applies the “color” and “shape” labels.

When the task is started, the volume is mounted on /path/in/container/ inside the container.

Be aware that the default (“local”) volume is a locally scoped volume driver. This means that depending on where a task is deployed, either that task gets a new volume named “my-volume”, or shares the same “my-volume” with other tasks of the same service. Multiple containers writing to a single shared volume can cause data corruption if the software running inside the container is not designed to handle concurrent processes writing to the same location. Also take into account that containers can be re-scheduled by the Swarm orchestrator and be deployed on a different node.

The following example bind-mounts a host directory at /path/in/container in the containers backing the service:

$ docker service create \\
  --name my-service \\
  --mount type=bind,source=/path/on/host,destination=/path/in/container \\
  nginx:alpine

network

  • docker network ls
  • docker network create -d overlay test
  • docker network create -d overlay swarm-net
  • docker service create --name mysql --network test -e
  • docker service inspect --format=“json .Endpoint.Spec.Ports” master
    MYSQL_ROOT_PASSWORD=123456789 -e MYSQL_DATABASE=wordpress --mount type=volume,source=mysql_data,destination=/var/lib/mysql mysql:5.7
  • docker network create -d overlay test
  • docker service ps mysql
    创建wordpress service:
  • docker service create --name wordpress --network test -p 80:80 -e WORDPRESS_DB_PASSWORD=123456789 -e WORDPRESS_DB_HOST=mysql wordpress
  • docker service ps wordpress

hadoop swarm 集群

hadoop-master

docker service create \\
	--name slave4 \\
	--hostname slave4 \\
	--network swarm-net \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	hadoop2.7-v1
docker service create \\
	--name slave4 \\
	--hostname slave4 \\
	--network swarm-net \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	registry.cn-hangzhou.aliyuncs.com/ruiclear/clean-hadoop:1.0.0

hadoop-slave1

docker service create \\
	--name p09 \\
	--hostname p09 \\
	--network swarm-net \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	hadoop2.7-v1
docker service create \\
	--name hadoop-slave1 \\
	--hostname hadoop-slave1 \\
	--network swarm-net \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	registry.cn-hangzhou.aliyuncs.com/ruiclear/clean-hadoop:1.0.0

docker service create \\
	--name hadoop-master \\
	--hostname hadoop-master \\
	--network swarm-net \\
	--constraint node.hostname==slave4 \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	lilelr/lele_hadoop2.7:1.0
	
docker service create \\
	--name hadoop-slave1 \\
	--hostname hadoop-slave1 \\
	--network swarm-net \\
	--constraint node.hostname==p09 \\
	--replicas 1 \\
	--endpoint-mode dnsrr \\
	lilelr/lele_hadoop2.7:1.0

以上是关于docker swarm的主要内容,如果未能解决你的问题,请参考以下文章

Docker Swarm集群部署实战

Docker Manager for Docker Swarm deploy

docker-swarm集群部署

docker swarm网络模式

Docker Swarm介绍

docker swarm网络问题