1. 程式人生 > 其它 >建立 Docker Swarm 叢集

建立 Docker Swarm 叢集

初始化叢集(在第一個管理節點操作)

docker swarm init --advertise-addr <first_manager_node_ip>

部分輸出資訊:

Swarm initialized: current node (mo2p7vnjhzg68g6wzejozdomo) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-260gjixktxw5jnso7osrpuhsvqi33m2ksro78doxlqanug9kix-141p1sm3ugjsw4qh6o0ydp2ho <first_manager_node_ip>:2377    ## 此命令新增worker節點

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.    ## 此方法可獲取新增備用manager節點的命令,生產環境必須至少三個manager節點

後面可以使用以下命令獲取加入其他節點的命令:

docker swarm join-token manager
docker swarm join-token worker

叢集節點之間開啟協議和埠說明:

TCP port 2377 for cluster management communications
TCP and UDP port 7946 for communication among nodes
UDP port 4789 for overlay network traffic

檢視叢集狀態:

docker info
docker node ls

部署一個服務(必須在管理節點操作)

部署命令:

# docker service create --replicas 1 --name helloworld alpine ping docker.com

命令說明:

The docker service create command creates the service.
The --name flag names the service helloworld.
The --replicas flag specifies the desired state of 1 running instance.
The arguments alpine ping docker.com define the service as an Alpine Linux container that executes the command ping docker.com.

檢視服務:

# docker service ls

ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
9wquw4g6bt29        helloworld          replicated          1/1                 alpine:latest

# docker service inspect --pretty helloworld

ID:             9wquw4g6bt29yl213t4pg9spq
Name:           helloworld
Service Mode:   Replicated
 Replicas:      1
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Update order:      stop-first
RollbackConfig:
 Parallelism:   1
 On failure:    pause
 Monitoring Period: 5s
 Max failure ratio: 0
 Rollback order:    stop-first
ContainerSpec:
 Image:         alpine:latest@sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
 Args:          ping docker.com 
Resources:
Endpoint Mode:  vip

# docker service ps helloworld

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
f5tbcczzpy40        helloworld.1        alpine:latest       mysqlmaster         Running             Running 34 minutes ago

擴容服務:

# docker service scale helloworld=3

helloworld scaled to 3
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged

# docker service ps helloworld

ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE            ERROR               PORTS
f5tbcczzpy40        helloworld.1        alpine:latest       mysqlmaster         Running             Running 40 minutes ago                       
pbx26pn67shp        helloworld.2        alpine:latest       mysqlmaster         Running             Running 20 seconds ago                       
d7k1txanj0d2        helloworld.3        alpine:latest       mysqlslave          Running             Running 20 seconds ago

刪除服務:

# docker service rm helloworld
作者:Varden 出處:http://www.cnblogs.com/varden/ 本文內容如有雷同,請聯絡作者! 本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連線,否則保留追究法律責任的權利。