Swarm與Docker的整合——Swarm mode

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

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

此時會輸出加入swarm集羣的方式:node

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

若是沒有記住的話:python

docker swarm join-token worker

依舊會輸出:git

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

在另外一臺節點上執行github

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

NODE

運行Docker的主機能夠主動初始化一個Swarm集羣或者加入一個已存在的Swarm集羣,這樣這個運行Docker的主機就成爲一個Swarm集羣的節點 (node) 。web

$ 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命令查看節點信息:redis

$ 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:sql

$ 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

部署:docker

# compose:

$ docker-compose -f CONFIG-YAML up

# stack:

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

查看:shell

# 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

相關文章
相關標籤/搜索