zoukankan      html  css  js  c++  java
  • Docker集群管理(Docker Swarm)

    1.准备工作。

    • 准备好主机,我测试准备了3台主机。

      IP:192.168.43.28    担任角色:swarm manager

      IP:192.168.43.182  担任角色:swarm node

      IP:192.168.43.110  担任角色:swarm node

    • 安装好Docker Engine 

      CentOS Docker安装:

    yum -y install docker
    

      树莓派安装Docker可以参考:https://yeasy.gitbook.io/docker_practice/install/raspberry-pi

      Windows安装Docker:直接官网下载安装即可。https://desktop.docker.com/win/stable/Docker%20Desktop%20Installer.exe

    • 设置防火墙

    关闭三台机器上的防火墙。如果开启防火墙,则需要在所有节点的防火墙上依次放行2377/tcp(管理端口)、7946/udp(节点间通信端口)、4789/udp(overlay 网络端口)端口。

    2.创建Swarm并加入节点。

    1)创建Swarm,在192.168.43.28 中执行

    docker swarm init --advertise-addr 192.168.43.28
    执行结果:
    Swarm initialized: current node (1lptx3e7syne0b6pnrm987xun) is now a manager.
     
    To add a worker to this swarm, run the following command:
     
        # 这就是添加节点的方式(要保存初始化后token,因为在节点加入时要使用token作为通讯的密钥)
        docker swarm join --token SWMTKN-1-4xb3dz6kg3bexpxz5eeifss2dtsmucf74nsixstyin3q169ona-9cwtdkjtdjwlwnsfeloxja6ug 192.168.43.28:2377 
     
    To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
    

     2)其他节点加入集群,

    •   获取加入工作节点的token
    docker swarm join-token worker
    
    执行结果:
    To add a worker to this swarm, run the following command:
    
    docker swarm join --token SWMTKN-1-4xb3dz6kg3bexpxz5eeifss2dtsmucf74nsixstyin3q169ona-9cwtdkjtdjwlwnsfeloxja6ug 192.168.43.28:2377
    

      

      如果想加入管理节点,获取管理节点token命令

    docker swarm join-token manager
    
    •   加入集群,执行:
    docker swarm join --token SWMTKN-1-4xb3dz6kg3bexpxz5eeifss2dtsmucf74nsixstyin3q169ona-9cwtdkjtdjwlwnsfeloxja6ug 192.168.43.28:2377
    
    •   查看集群中机器及状态
    docker node ls
    执行结果:
    ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
    v02i9sn3yet34o4twq7ir7ek6     docker-desktop      Ready               Active                                  19.03.8
    vqevr5pamsiv155x2ccuxg6eg     docker-desktop      Ready               Active                                  19.03.8
    1lptx3e7syne0b6pnrm987xun *   raspberrypi         Ready               Active              Leader              19.03.12
    
    •   修改节点availablity状态
    设置节点不可用:
    docker node update --availability drain v02i9sn3yet34o4twq7ir7ek6
    设置节点可用:
    docker node update --availability active v02i9sn3yet34o4twq7ir7ek6
    
    •   删除节点
    docker node rm --force v02i9sn3yet34o4twq7ir7ek6
    

     

     3.在swarm中部署服务(nginx为例)

    1)创建网络在部署服务,在管理节点(192.168.43.28 )运行命令,

    • 在管理节点(192.168.43.28 )运行命令,创建网络
    docker network create -d overlay nginx_net
    查看网络
    docker network ls | grep nginx_net
    
    • 在管理节点(192.168.43.28 )运行命令,部署服务
      docker service create --replicas 1 --network nginx_net --name my_nginx -p 80:80 nginx    # 只创建了一个具有一个副本(--replicas 1 )的nginx服务,使用镜像nginx
      运行结果:
      olexfmtdf94sxyeetkchwhehg
      overall progress: 1 out of 1 tasks
      1/1: running   [==================================================>]
      verify: Service converged
      

      注意:不需要提前在节点上下载nginx镜像,这个命令执行后会自动下载这个容器镜像

    • 使用 docker service ls 查看正在运行服务的列表
      docker service ls
      执行结果:
      ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
      olexfmtdf94s        my_nginx            replicated          1/1                 nginx:latest        *:80->80/tcp
      

        

    2)查询Swarm中服务的信息(在管理节点上运行)

    • 查看swarm
    docker service inspect --pretty my_nginx
    运行结果:
    ID:             8nx16idy6eystnelu2214tnjj
    Name:           my_nginx
    Service Mode:   Replicated
     Replicas:      5
    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:         nginx:latest@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
     Init:          false
    Resources:
    Networks: nginx_net
    Endpoint Mode:  vip
    Ports:
     PublishedPort = 80
      Protocol = tcp
      TargetPort = 80
      PublishMode = ingress
    
    • 查询到哪个节点正在运行该服务
    docker service ps my_nginx
    运行结果:
    ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR                              PORTS
    2xjblebabprw        my_nginx.1          nginx:latest        raspberrypi         Running             Running 7 hours ago
    92qra1x9nmsn        my_nginx.2          nginx:latest        docker-desktop      Running             Running 6 hours ago
    6my8umxa19nb         \_ my_nginx.2      nginx:latest        docker-desktop      Shutdown            Rejected 7 hours ago         "No such image: nginx:latest@s…"
    9r9yp00mku5w        my_nginx.3          nginx:latest        docker-desktop      Running             Running 4 hours ago
    kxkw4zofnzu9         \_ my_nginx.3      nginx:latest        docker-desktop      Shutdown            Shutdown 3 hours ago
    q39bye7fexwo         \_ my_nginx.3      nginx:latest        docker-desktop      Shutdown            Rejected 6 hours ago         "No such image: nginx:latest@s…"
    r5p344468c2h        my_nginx.4          nginx:latest        raspberrypi         Running             Running about an hour ago
    v7un0150x9o7         \_ my_nginx.4      nginx:latest        docker-desktop      Shutdown            Shutdown about an hour ago
    1h8l0yrokdcz         \_ my_nginx.4      nginx:latest        docker-desktop      Shutdown            Shutdown 3 hours ago
    mkx7uh745rfl        my_nginx.5          nginx:latest        raspberrypi         Running             Running about an hour ago
    murmvfdqn3kk         \_ my_nginx.5      nginx:latest        docker-desktop      Shutdown            Shutdown about an hour ago
    

    4.在Swarm中动态扩展服务(scale) 

    docker service scale my_nginx=4
    运行结果:
    my_nginx scaled to 4
    overall progress: 4 out of 4 tasks
    1/4: running   [==================================================>]
    2/4: running   [==================================================>]
    3/4: running   [==================================================>]
    4/4:
    verify: Service converged
    

    5.其他操作

    1)将要离开的节点停止工作(管理节点上执行)

    docker node update --availability drain v02i9sn3yet34o4twq7ir7ek6

    2)工作节点离开集群(在工作节点执行)

    docker swarm leave -f
    运行结果:
    Node left the swarm.
    

    3)移除节点(管理节点上执行)

    docker node rm v02i9sn3yet34o4twq7ir7ek6

    docker node rm vqevr5pamsiv155x2ccuxg6eg

    4)删除服务,my_nginx

    docker service rm my_nginx
    

    这样就会把所有节点上的所有容器(task任务实例)全部删除了  

    5)管理节点离开(管理节点上执行)

    docker swarm leave -f
    

      


    内容参考:https://www.cnblogs.com/zhujingzhi/p/9792432.html

    上图转自:https://www.cnblogs.com/csnd/p/12061828.html

  • 相关阅读:
    Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; 前言中不允许有内容。
    [Pulgin] jQuery插件之ajaxFileUpload
    [Plugin] JQuery.uploadify上传文件插件的使用详解For ASP.NET
    [Plugin] 文件上传利器SWFUpload使用指南
    [HTML] 微信开发之移动手机WEB页面(HTML5)Javascript实现一键拨号及短信发送功能
    [JavaEE] IBM
    [TB-Technology] 淘宝在数据处理领域的项目及开源产品介绍
    [Hacker] 端口大全
    [.Net] DataTable添加列和行的三种方法
    [.Net] 导出Excel中身份证等数字串的解决方式
  • 原文地址:https://www.cnblogs.com/LittleJin/p/14396068.html
Copyright © 2011-2022 走看看