【问题标题】:How to deploy an IPv6 container with Docker Swarm Mode or Docker Compose如何使用 Docker Swarm Mode 或 Docker Compose 部署 IPv6 容器
【发布时间】:2017-06-16 18:26:12
【问题描述】:

最后,我希望通过 compose 或 swarm 模式部署一个纯 IPv6 网络。现在,我只想部署一个使用 IPv6(仅)的容器。我目前对路由不感兴趣(只是容器到容器的连接)。

我的设置:

  • 操作系统:Centos 7
  • dockerd --ipv6 --fixed-cidr-v6=2001:db8:1::/64 --iptables=true --ip-masq=true --mtu=1600 --experimental=true
  • docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
  • 主机具有 IPv4 和 IPv6 地址。两者都启用了转发(这对我来说并不重要)。

我已经尝试了每个组合(我只列出了几个)

带有容器和网络的独立 Docker 堆栈:

version: '3'

networks:
  app_net:
    driver: overlay
    driver_opts:
      com.docker.network.enable_ipv6: "true"
    ipam:
      driver: default
      config:
      -
        subnet: 172.16.238.0/24
      -
        subnet: 2001:3984:3989::/64        

services:
  app:
    image: alpine
    command: sleep 600
    networks:
      app_net:
        ipv4_address: 0.0.0.0
        ipv6_address: 2001:3984:3989::10

结果:容器中只有 IPv4 地址,0.0.0.0 被忽略。


外部预先创建的网络 (根据https://stackoverflow.com/a/39818953/1735931

docker network create --driver overlay --ipv6 --subnet=2001:3984:3989::/64 --attachable ext_net

version: '3'

networks:
  ext_net:
    external:
      name: ext_net

services:
  app:
    image: alpine
    command: ifconfig eth0 0.0.0.0 ; sleep 600
    cap_add:
     - NET_ADMIN
    networks:
      ext_net:
        ipv4_address: 0.0.0.0
        ipv6_address: 2001:3984:3989::10

结果:容器中同时包含 IPv4 和 IPv6 地址,但 cap_add 被忽略(在 Swarm 模式下不支持),因此上面的 ifconfig disable ipv4 尝试不起作用。

我目前没有安装 docker-compose,接下来可能会尝试,但是有没有办法在 Docker Swarm 模式下运行纯 IPv6 容器?

注意:我可以在没有 swarm/compose 的情况下手动运行和配置一些仅 IPv6 的容器: (如上创建网络,甚至只使用默认网桥)

$ docker run --cap-add=NET_ADMIN --rm -it alpine
$$ ifconfig eth0 0.0.0.0
$$ ping6 other-container-ipv6-address # WORKS!

或简写:

$ docker run --cap-add=NET_ADMIN --rm -it alpine sh -c "/sbin/ifconfig eth0 0.0.0.0 ; sh"

【问题讨论】:

    标签: docker docker-compose ipv6 docker-swarm-mode


    【解决方案1】:

    我可以用 docker-compose 来破解它,因为它非常丑陋。如果你绝望了,就在这里。 (由于特权升级,此方法永远无法用于 Swarm 模式)。

    计划

    1. 授予容器管理 IP 的权限
    2. 在启动时从每个容器中删除 IPv4 IP 地址。
    3. 使用卷即兴生成主机文件来代替 DNS(DNS 在 docker 中仅支持 IPv4)。

    步骤

    1. Enable IPv6 in Docker daemon
    2. 创建一个 docker-compose.yml 文件,该文件创建一个 ipv6 网络、一个用于共享文件的卷和两个容器
    3. 在每个执行上述步骤的容器中运行入口点脚本。

    文件

    docker-compose.yml

    # Note: enable_ipv6 does not work in version 3!
    version: '2.1'
    
    networks:
      app_net:
        enable_ipv6: true
        driver: overlay
        ipam:
          driver: default
          config:
          -
            subnet: 172.16.238.0/24
          -
            subnet: 2001:3984:3989::/64
    
    services:
      app1:
        build: ./server 
        hostname: server1
        command: blablabla # example of arg passing to ipv6.sh
        cap_add:
         - NET_ADMIN
        volumes:
         - ipv6stuff:/ipv6stuff
        networks:
          - app_net
    
      app2:
        build: ./server 
        hostname: server2
        command: SOMETHING # example of arg passing to ipv6.sh
        cap_add:
         - NET_ADMIN
        volumes:
         - ipv6stuff:/ipv6stuff
        networks:
          - app_net
    
    volumes:
      ipv6stuff:
    

    服务器/Dockerfile

    FROM alpine:latest
    ADD files /
    RUN apk --update add bash  #simpler scripts
    # Has to be an array for parameters to work via command: x in compose file, if needed
    ENTRYPOINT ["/ipv6.sh"]
    

    服务器/文件/ipv6.sh

    #!/bin/bash
    # Optionally conditional logic based on parameters here...
    # (for example, conditionally leave ipv4 address alone in some containers)
    #
    # Remove ipv4
    ifconfig eth0 0.0.0.0
    
    IP6=$(ip addr show eth0 | grep inet6 | grep global | awk '{print $2}' | cut -d / -f 1)
    
    echo "Host $HOSTNAME has ipv6 ip $IP6" 
    
    # Store our entry in the shared volume
    echo "$IP6   $HOSTNAME" > /ipv6stuff/hosts.$HOSTNAME
    
    # Remove existing ipv4 line from /etc/hosts just to be thorough
    # Docker does not allow removal of this file and thus simple sed -i isn't going to work.
    cp /etc/hosts /tmp/1 ; sed -i "s/^.*\s$HOSTNAME//" /tmp/1 ; cat /tmp/1 > /etc/hosts
    
    # Wait for all containers to start
    sleep 2
    
    # Put everyone's entries in our hosts file.
    cat /ipv6stuff/hosts.* >> /etc/hosts
    
    echo "My hosts file:"
    cat /etc/hosts
    
    # test connectivity (hardcoded)
    ping6 -c 3 server1
    ping6 -c 3 server2
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多