Swarm与Docker的整合Swarm-mode

37次阅读

共计 5525 个字符,预计需要花费 14 分钟才能阅读完成。

github repository: https://github.com/azzinoths0…

和集群相关的对象

SWARM

$ docker swarm --help

Usage:    docker swarm COMMAND

Manage Swarm

Commands:
  ca          Display and rotate the root CA
  init        Initialize a swarm
  join        Join a swarm as a node and/or manager
  join-token  Manage join tokens
  leave       Leave the swarm
  unlock      Unlock swarm
  unlock-key  Manage the unlock key
  update      Update the swarm

Run 'docker swarm COMMAND --help' for more information on a command.

创建 swarm 集群并启动 manager:

docker swarm init --listen-addr 192.168.99.200:2377 --advertise-addr 192.168.99.200

此时会输出加入 swarm 集群的方式:

docker swarm join --token SWMTKN-1-0nbwp5l3wsrtvf1hhrzf7dxbabpvitzo2ssyjqhnymg7d2ypzr-b1v1t5lnabvr4ojw9xe7eugkk 192.168.99.200:2377

如果没有记住的话:

docker swarm join-token worker

依旧会输出:

docker swarm join --token SWMTKN-1-0nbwp5l3wsrtvf1hhrzf7dxbabpvitzo2ssyjqhnymg7d2ypzr-b1v1t5lnabvr4ojw9xe7eugkk 192.168.99.200:2377

在另一台节点上执行

docker swarm join --token SWMTKN-1-0nbwp5l3wsrtvf1hhrzf7dxbabpvitzo2ssyjqhnymg7d2ypzr-b1v1t5lnabvr4ojw9xe7eugkk 192.168.99.200:2377

NODE

运行 Docker 的主机可以主动初始化一个 Swarm 集群或者加入一个已存在的 Swarm 集群,这样这个运行 Docker 的主机就成为一个 Swarm 集群的节点 (node)。

$ docker node --help

Usage:    docker node COMMAND

Manage Swarm nodes

Commands:
  demote      Demote one or more nodes from manager in the swarm
  inspect     Display detailed information on one or more nodes
  ls          List nodes in the swarm
  promote     Promote one or more nodes to manager in the swarm
  ps          List tasks running on one or more nodes, defaults to current node
  rm          Remove one or more nodes from the swarm
  update      Update a node

Run 'docker node COMMAND --help' for more information on a command.

使用 ls 命令查看节点信息:

$ docker node ls

ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
v1oo73vt40hnjivnhnw78po4u *   dameng-00           Ready               Active              Leader              18.09.8-ce
yrz7drszt4hq5j8c8rbhoexqc     dameng-01           Ready               Active                                  18.09.8-ce

STACK

Stack 可以说是升级版的 Compose:

$ docker stack --help

Usage:    docker stack [OPTIONS] COMMAND

Manage Docker stacks

Options:
      --kubeconfig string     Kubernetes config file
      --orchestrator string   Orchestrator to use (swarm|kubernetes|all)

Commands:
  deploy      Deploy a new stack or update an existing stack
  ls          List stacks
  ps          List the tasks in the stack
  rm          Remove one or more stacks
  services    List the services in the stack

部署:

# compose:

$ docker-compose -f CONFIG-YAML up

# stack:

$ docker stack deploy -c CONFIG-YAML STACK-NAME

查看:

# compose:

$ docker-compose ps
$ docker ps

# stack:

$ docker node ls
$ docker stack ls
$ docker service ls

终止:

# compose:

$ docker-compose -f CONFIG-YAML down

# stack:

$ docker stack rm STACK-NAME

网络:

单机 vs 跨节点

注意: docker stack 默认使用的是 swarm,但也是可以对接 k8s 的

SERVICE

$ docker service --help

Usage:    docker service COMMAND

Manage services

Commands:
  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's configuration
  scale       Scale one or multiple replicated services
  update      Update a service

Run 'docker service COMMAND --help' for more information on a command.

查看:

$ docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
oyiesfc3biiq        myapp_myapp         replicated          1/1                 friendlyhello:v3    *:5000->5000/tcp
xrk7kska1z76        myapp_redis         replicated          1/1                 redis:latest

扩容:

$ docker service scale myapp_myapp=2

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
oyiesfc3biiq        myapp_myapp         replicated          2/2                 friendlyhello:v3    *:5000->5000/tcp
xrk7kska1z76        myapp_redis         replicated          1/1                 redis:latest

TRY IT OUT: 部署friendlyhello

我们可以用 Docker Machine 很快地创建一个虚拟的 Docker 主机,接下来我们来创建 2 个新的 Docker 主机,并加入到集群中。

STEP 1: 创建 Swarm 集群——管理节点

首先是一个管理节点,创建并通过 ssh 连接:

$ docker-machine create -d virtualbox manager
$ docker-machine ssh manager

我们可以看到:

然后,我们用 docker swarm init 从这个节点初始化一个 Swarm 集群,如果这个 Docker 主机有多个 IP(多个网卡),就要用 --advertise-addr 指定一个:

$ docker swarm init --advertise-addr 192.168.99.107

我们可以看到:

现在我们的 Manager 节点就是刚刚创建的集群的管理节点了,

记得复制一下它输出的添加工作节点的那句命令。

STEP 2: 添加项目文件

接下来我们 Manager 节点的 ~/try-it-out-4 里添加几个文件:

  • app.py
  • Dockerfile
  • docker-stack.yaml
$ mkdir try-it-out-4
$ cd try-it-out-4
$ vi app.py
$ vi Dockerfile
$ vi docker-stack.yaml

$ docker build -t friendlyhello .

app.py

from flask import Flask
from redis import Redis, RedisError
import os
import socket

# Connect to Redis
redis = Redis(host="redis", db=0, socket_connect_timeout=2, socket_timeout=2)

app = Flask(__name__)


@app.route("/")
def hello():
    try:
        visits = redis.incr("counter")
    except RedisError:
        visits = "<i>cannot connect to Redis, counter disabled</i>"

    html = "<h3>Hello {name}!</h3>" \
           "<b>Hostname:</b> {hostname}<br/>" \
           "<b>Visits:</b> {visits}"
    return html.format(name=os.getenv("NAME", "world"), hostname=socket.gethostname(), visits=visits)


if __name__ == "__main__":
    app.run(host='0.0.0.0', port=5000)

Dockerfile

FROM python:3.7-slim

WORKDIR /app

COPY . /app

RUN pip install flask redis -i https://mirrors.aliyun.com/pypi/simple  --trusted-host mirrors.aliyun.com 
EXPOSE 5000
ENV NAME World

CMD ["python", "app.py"]

docker-stack.yaml

version: "3"

services:
  myapp:
    image: friendlyhello
    container_name: myapp
    ports:
      - 5000:5000
    environment:
      NAME: World

  redis:
    image: redis
    container_name: web

STEP 3: 创建 Swarm 集群——工作节点

继续,我们来创建一个工作节点:

首先回到主机:

$ exit 

接着创建一个新的虚拟机worker,并通过上面复制的那句命令加入到集群里:

$ docker-machine create -d virtualbox worker
$ docker-machine ssh worker

$ docker swarm join --token SWMTKN-1-3wd0vdozskitmpw5vofkjc9ie6251wuno21dmbugqk56pd97iv-eu9w5gkkmy7chvgcwt7j71iu4 192.168.99.107:2377

我们可以看到:

STEP 4: 使用 Stack 部署服务

我们先回到 manager 节点:

$ exit

$ docker-machine ssh manager

然后使用 docker stack deploy 部署服务,其中 -c 参数指定 docker-stack.yaml 文件:

$ docker stack deploy -c ~/try-it-out/docker-stack.yaml friendlyhello

部署完毕。

STEP 5: 访问 friendlyhello

现在就可以通过集群中的任意一个节点的 IP 访问到这个 flask 项目了:

TRY IT OUT: 部署postgresql

我们继续用前一个 Case 的集群。

进入 manager 节点,在 try-it-out-5 里新建一个docker-stack.yaml

$ docker-machine ssh manager

$ mkdir try-it-out-5
$ vi docker-stack.yaml

docker-stack.yaml

version: '3.1'

services:

  db:
    image: postgres
    command: postgres -c 'shared_buffers=512MB' -c 'max_connections=2000'
    restart: always
    environment:
      POSTGRES_USER: dameng
      POSTGRES_PASSWORD: pythonic
    ports:
      - 5432:5432
    volumes:
      - pgdata:/var/lib/postgresql/data


  adminer:
    image: adminer
    restart: always
    ports:
      - 8998:8080

volumes:
  pgdata:

然后部署:

$ docker stack deploy -c docker-stack.yaml postgresql

接着就可以从 8998 端口 访问 GUI 了:

正文完
 0