1
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

我们可以看到:

Screen Shot 2019-08-19 at 3.17.14 PM.png

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

$ docker swarm init --advertise-addr 192.168.99.107

我们可以看到:

Screen Shot 2019-08-19 at 3.27.10 PM.png

现在我们的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 .

Screen Shot 2019-08-19 at 3.21.41 PM.png

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

我们可以看到:

Screen Shot 2019-08-19 at 3.25.26 PM.png

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

Screen Shot 2019-08-19 at 3.27.10 PM.png

部署完毕。

STEP 5: 访问friendlyhello

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

Screen Shot 2019-08-19 at 4.41.31 PM.png

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了:

Screen Shot 2019-08-19 at 5.17.16 PM.png


Ron_You
196 声望3 粉丝

一个普普通通的大学生。