首页 > 代码库 > 自建docker swarm体验简单之美

自建docker swarm体验简单之美

之前用的阿里云容器服务,但由于acsrouting的路由错乱问题,被逼上自建docker swarm的梁山。今天尝试自己搭建docker swarm,竟然轻松搞定,简单的超乎想象。

以下是实际搭建操作步骤:

1. 创建集群

# docker swarm init --advertise-addr 10.251.242.231Swarm initialized: current node (m9dfl7r9wo1e9jxsp3oe5du3x) is now a manager.To add a worker to this swarm, run the following command:    docker swarm join --token xxx 10.251.242.231:2377To add a manager to this swarm, run ‘docker swarm join-token manager‘ and follow the instructions.

主:10.251.242.231 是 docker 主机的内网IP地址

2. docker info 查看刚刚创建的集群信息

Swarm: active NodeID: m9dfl7r9wo1e9jxsp3oe5du3x Is Manager: true ClusterID: j01wzizw7gy0ck95p1d7a4pmv Managers: 1 Nodes: 1 Orchestration:  Task History Retention Limit: 5

3. docker node ls 查看节点信息

ID                            HOSTNAME               STATUS              AVAILABILITY        MANAGER STATUSm9dfl7r9wo1e9jxsp3oe5du3t *   swarm-websites-node1   Ready               Active              Leader

4. 创建用于部署应用容器的网络

# docker network create --driver=overlay --attachable cnblogs

5. 创建用于部署Docker Flow Proxy容器的网络(Docker Flow Proxy支持根据主机名将请求转发至对应的应用容器)

docker network create --driver overlay proxy

6. 查看刚刚创建的网络

# docker network lsNETWORK ID          NAME                DRIVER              SCOPErl96kcw8ytpa        cnblogs             overlay             swarm61kbxtvnivkx        proxy               overlay             swarm

7. 部署 Docker Flow Proxy stack

7.1 下载 docker-compose-stack.yml 文件

# curl -o docker-compose-stack.yml     https://raw.githubusercontent.com/vfarcic/docker-flow-proxy/master/docker-compose-stack.yml

7.2 部署 stack

# docker stack deploy -c docker-compose-stack.yml proxy

8. 编写示例应用 openapi 的 docker-compose-stack.yml 

version: ‘3.2‘services:  api:    image: open-api:latest    deploy:      replicas: 2      update_config:        delay: 5s      labels:        - com.df.notify=true        - com.df.distribute=true        - com.df.serviceDomain=api.cnblogs.com        - com.df.port=80    networks:      - cnblogs      - proxynetworks:  cnblogs:    external: true  proxy:    external: true

9. 部署示例应用的 stack

docker stack deploy -c docker-compose-stack.yml openapi

10. 部署完成后可以通过 http://api.cnblogs.com 访问运行在 openapi_api service 中的站点

【参考资料】

  • Getting started with swarm mode
  • Using Docker Stack To Run Docker Flow Proxy In Swarm Mode

自建docker swarm体验简单之美