Skip to content

docker swarm

docker swarm内置于docker中,用来创建docker服务集群, 通过docker stack命令可以部署/更新和查看服务状态.

docker swarm

  • docker swarm init 初始化集群,并将当前机器作为Leader节点

  • docker swarm join-token 生成token凭证,通过这个凭证可以加入集群
    docker swarm join-token manager 生成管理节点凭证

  • docker swarm join 使用凭证加入集群

bash
docker swarm join --token SWMTKN-1-3x2gr9c55j90hkzh1ah14djtkb89beqxqmyq8fu54bfknjgicp-5c0sowobxyebwd6bydz985xgq 192.168.65.3:2377

docker node

  • docker node ls 查看当前swarm有哪些节点
sh
ID                            HOSTNAME         STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
uy9kagy60l1v3qmlmqzloe6jj *   VM-0-4-centos    Ready     Active         Leader           20.10.17
9ctdfnctlib7b3slqdl8nd5z1     VM-0-16-centos   Ready     Active         Reachable        20.10.17
nohdt0129u8cmt1vez0hdeubw     VM_0_12_centos   Ready     Active         Reachable        20.10.17
ly31hrazpl6hubutmi7py0kh1     VM_0_15_centos   Ready     Active                          19.03.13

其中 Reachable 表示管理节点,空内容是worker是节点,Leader也是管理节点,一个swarm集群中只有一个leader节点

  • docker node rm 删除节点
  • docker node demote 从manager节点降为worker节点
  • docker node promote 从worker节点提升为manager节点
  • docker node ps 列出节点上有哪些任务(container)
  • docker node update 更新node信息
    使用 --role=worker|manager可以替代deomote和promote功能
    使用 --label-add--lable-rm用来增减node标签,可以配合部署使用

stack

  • docker stack deploy 部署或更新已存在的stack, 是stack的核心功能
    其中,-c 参数指定一个 docker-compose.yaml 文件, --with-registry-auth docker凭证在集群中可用, --resolve-image 是否尝拉取镜像,默认always, 如果镜像没有发生变化也会尝试拉取并且更新
  • docker stack ls stack的列表
  • docker stack ps 列出指定stack的task(相当于container)
  • docker stack services 列出指定stack的services

service

bash
> docker service -h
create      Create a new service
inspect     Display detailed information on one or more services
logs        Fetch the logs of a service or task
ls          List services
ps          List the tasks of one or more services
rm          Remove one or more services
rollback    Revert changes to a service configuration
scale       Scale one or multiple replicated services
update      Update a service

查看服务 dc 的log日志

bash
docker service logs dc --tail=999 -f

查看 task 日志

bash
docker service logs taskId --tail=999 -f

container

bash
> docker container -h
attach      Attach local standard input, output, and error streams to a running container
commit      Create a new image from a container changes
cp          Copy files/folders between a container and the local filesystem
create      Create a new container
diff        Inspect changes to files or directories on a container filesystem
exec        Run a command in a running container
export      Export a container filesystem as a tar archive
inspect     Display detailed information on one or more containers
kill        Kill one or more running containers
logs        Fetch the logs of a container
ls          List containers
pause       Pause all processes within one or more containers
port        List port mappings or a specific mapping for the container
prune       Remove all stopped containers
rename      Rename a container
restart     Restart one or more containers
rm          Remove one or more containers
run         Run a command in a new container
start       Start one or more stopped containers
stats       Display a live stream of container(s) resource usage statistics
stop        Stop one or more running containers
top         Display the running processes of a container
unpause     Unpause all processes within one or more containers
update      Update configuration of one or more containers
wait        Block until one or more containers stop, then print their exit codes

进入容器bash环境

bash
docker exec -it continerId bash

部署nginx服务,把本地90端口映射到容器内部的80端口上,并且让容器在后台运行,当容器停止后自动删除容器

bash
docker run -d --rm -p 90:80 --name=nginx nginx

搭建本地仓库,本地5000端口映射到容器内部的5000端口,并且把用户家目录的docker-repository挂载到容器内部的 /var/lib/registry

bash
docker run -d -p 5000:5000 --restart=always --name registry -v $(pwd)/docker-repository:/var/lib/registry registry:2

docker compose

如果安装的是较新的 Docker 版本(例如 Docker Desktop 或 Docker Engine),通常已经包含了 Docker Compose 插件,你可以直接使用 docker compose 命令,无需单独安装 docker-compose。
docker compose用来将一些应用部署在 单台服务器, 便于开发和测试,如果你想对你的服务做扩容,那么最好部署在docker swarm集群上, docker-compose.yaml文件格式参考 https://docs.docker.com/compose/compose-file/

Reference