Swarm管理分散式docker

二進位制-程式猿發表於2017-11-06

作者:【吳業亮】雲端計算開發工程師
部落格:https://wuyeliang.blog.csdn.net/

這裡寫圖片描述

在各個節點上

# yum -y install docker
# systemctl start docker 
# systemctl enable docker

docker版本必須在v1.12以上,如果不是用 yum upgrade docker

# docker -v 
Docker version 1.12.6, build 96d83a5/1.12.6

禁用live-restore(各個節點)

 vi /etc/docker/daemon.json
{
    "live-restore": false
}

重啟服務(各個節點)

# systemctl restart docker

初始化管理節點(管理節點)

# docker swarm init
Swarm initialized: current node (4ejgdnkv3ldew0ol6rpxrw1it) is now a manager.

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

    docker swarm join \
    --token SWMTKN-1-1fhqkce0ryjzthluf5lro4mesmfuuk2apbr8339e6ybu7sj4bx-27n8grtwz9aa0cpqr7crjz2ru \
    192.168.10.226:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

將work節點加入到Swarm Cluste中(work節點)

# docker swarm join \
    --token SWMTKN-1-1fhqkce0ryjzthluf5lro4mesmfuuk2apbr8339e6ybu7sj4bx-27n8grtwz9aa0cpqr7crjz2ru \
    192.168.10.226:2377

以下操作均在manager節點
驗證各個節點已經加入叢集中

# docker node ls
ID                           HOSTNAME            STATUS  AVAILABILITY  MANAGER STATUS
4ejgdnkv3ldew0ol6rpxrw1it *  host-192-168-10-226  Ready   Active        Leader
7xykqelamornqz4ovzzzw51yl    host-192-168-10-227  Ready   Active
ebb3mnzguc94qycs6g3q8gy2y    host-192-168-10-228  Ready   Active

在各個節點上構建image
節點1上Dockerfile

FROM centos
MAINTAINER serverworld <admin@cloud>
RUN yum -y install httpd
RUN echo "Hello DockerFile 1" > /var/www/html/index.html
EXPOSE 80
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
# docker build -t web_server:latest .

節點2上Dockerfile

FROM centos
MAINTAINER serverworld <admin@cloud>
RUN yum -y install httpd
RUN echo "Hello DockerFile 2 " > /var/www/html/index.html
EXPOSE 80
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
# docker build -t web_server:latest .

節點3上Dockerfile

FROM centos
MAINTAINER serverworld <admin@cloud>
RUN yum -y install httpd
RUN echo "Hello DockerFile 3 " > /var/www/html/index.html
EXPOSE 80
CMD ["-D", "FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
# docker build -t web_server:latest .

在管理節點上建立負載均衡叢集

# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
web_server          latest              18494cba819c        About a minute ago   318.4 MB

建立叢集

# docker service create --name swarm_cluster --replicas=2 -p 80:80 web_server:latest
20o5l3yrtssfi7au86s7t4m1f

檢視叢集

# docker service ls
ID            NAME           REPLICAS  IMAGE              COMMAND
20o5l3yrtssf  swarm_cluster  0/2       web_server:latest

檢視詳細資訊

# docker service inspect swarm_cluster --pretty
ID:             20o5l3yrtssfi7au86s7t4m1f
Name:           swarm_cluster
Mode:           Replicated
 Replicas:      2
Placement:
UpdateConfig:
 Parallelism:   1
 On failure:    pause
ContainerSpec:
 Image:         web_server:latest
Resources:
Ports:
 Protocol = tcp
 TargetPort = 80
 PublishedPort = 80

檢視容器狀態

#  docker service ps swarm_cluster
ID                         NAME                 IMAGE              NODE                DESIRED STATE  CURRENT STATE            ERROR
abcz5de7owcw681mdmxwrce74  swarm_cluster.1      web_server:latest  host-192-168-10-227  Running        Running 26 seconds ago
euuvlsvo5fe1p79g49yo5yjrd  swarm_cluster.2      web_server:latest  host-192-168-10-228  Running        Running 12 seconds ago
18tf6wzuc9zl5e3kkf822z17e   \_ swarm_cluster.2  web_server:latest  host-192-168-10-226  Shutdown       Rejected 32 seconds ago  "No such image: web_server:lat…"

測試

# curl http://192.168.10.226/
Hello DockerFile 3
[root@host-192-168-10-226 ~]# curl http://192.168.10.226/
Hello DockerFile 2

更改叢集的數量

#  docker service scale swarm_cluster=3

swarm_cluster scaled to 3

檢視數量

# docker service ps swarm_cluster
ID                         NAME                 IMAGE              NODE                DESIRED STATE  CURRENT STATE             ERROR
abcz5de7owcw681mdmxwrce74  swarm_cluster.1      web_server:latest  host-192-168-10-227  Running        Running 2 minutes ago
euuvlsvo5fe1p79g49yo5yjrd  swarm_cluster.2      web_server:latest  host-192-168-10-228  Running        Running 2 minutes ago
18tf6wzuc9zl5e3kkf822z17e   \_ swarm_cluster.2  web_server:latest  host-192-168-10-226  Shutdown       Rejected 2 minutes ago    "No such image: web_server:lat…"
5fb0cew9ih12wm4wioanm13up  swarm_cluster.3      web_server:latest  host-192-168-10-226  Running        Preparing 11 seconds ago

相關文章