2017-06-16 493 views
0

最後,我希望通過組合或羣集模式部署純IPv6網絡。目前,我只想使用IPv6(僅)部署一個容器。我目前不感興趣路由(只是容器到容器的連接)。如何使用Docker Swarm模式或Docker Compose部署IPv6容器

我的設置:

  • OS:Centos的7
  • dockerd --ipv6 --fixed-CIDR-V6 = 2001:DB8:1 ::/64 --iptables =真--ip- masq = true --mtu = 1600 --experimental = true
  • docker-engine-17.05.0.ce-1.el7.centos.x86_64.rpm
  • 主機有IPv4和IPv6地址。轉發是爲兩個(不是對我來說很重要)。

我試了一下,似乎是每一個組合(我只列出一對夫婦)

自包含泊塢堆集裝箱和網絡:

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

搬運工網絡創建--driver覆蓋--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容器?

注:我能跑手動配置一些專用的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" 

回答

0

我能夠通過嚴厲的醜陋與泊塢窗,撰寫破解它。如果你絕望,就在這裏。 (由於權限提升,這種方法永遠不能用於Swarm模式)。

該計劃

  1. 格蘭特集裝箱權從啓動每個容器內管理IP的
  2. 刪除IPv4的IP地址。
  3. 使用卷即興主機文件來代替DNS(DNS在docker中僅用於IPv4)。

步驟

  1. Enable IPv6 in Docker daemon
  2. 創建一個docker-compose.yml文件,該文件創建一個ipv6網絡,共享文件的卷和兩個容器
  3. 在每個執行上述步驟的容器中運行入口點腳本。

文件

泊塢窗,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"] 

server/files/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