关于docker:docker支持ipv6

2次阅读

共计 2459 个字符,预计需要花费 7 分钟才能阅读完成。

办法

办法一、Pv6 地址

不为容器中的服务特地调配 IPv6 地址。
只有 Docker 把内部的 IPv6 地址端口映射到容器的 IPv4 端口上,随后拜访主机的 IPv6 相应端口即可。

办法二、为 Docker 网络调配 IPv6 地址

(1)Docker daemon 默认只反对 IPv4 地址,通过在运行 Docker 时减少 –ipv6 参数能够使其同时反对 ipv4 和 ipv6 地址。
(2)此时容器仅取得了本地 ipv6 地址,如果要取得全局 ipv6 地址,必须确保机器有至多 /80 的地址段,通过在运行 Docker 时减少 –fixed-cidr-v6 参数为其配置 ipv6 子网。
该参数会在路由表中增加一条路由,相当于执行了一下命令:
$ ip -6 route add 2001:db8:1::/64 dev docker0
$ sysctl net.ipv6.conf.default.forwarding=1
$ sysctl net.ipv6.conf.all.forwarding=1
随后,所有路由往 2001:db8:1::/64 的流量都会通过 docker0 接口路由
(3)除了在运行时增加参数,还能够间接编辑 /etc/docker/daemon.json 文件,退出以下内容:
{
“ipv6”: true,
“fixed-cidr-v6”: “2001:db8:1::/64”
}
其中 2001:db8:1::/64 是 IPv6 地址段。

systemctl reload docker

(4)应用 systemctl restart docker 命令重启 Docker。
(5)应用 ifconfig 命令查看调配到 Docker 网络的 IPv6 地址段
(6)实现了下面的配置之后,无需特意配置,只需失常建设容器,即可为容器调配 IPv6 地址。能够应用“docker inspect 容器名”查看容器的 IP 地址。

官网教程

启用 ipv6

Edit /etc/docker/daemon.json, set the ipv6 key to true and the fixed-cidr-v6 key to your IPv6 subnet. In this example we are setting it to 2001:db8:1::/64.


{
  "ipv6": true,
  "fixed-cidr-v6": "2001:db8:1::/64"
}

Save the file.
Reload the Docker configuration file.


$ systemctl reload docker

启用内部转发

https://docs.docker.com/netwo…

Enable forwarding from Docker containers to the outside world
By default, traffic from containers connected to the default bridge network is not forwarded to the outside world. To enable forwarding, you need to change two settings. These are not Docker commands and they affect the Docker host’s kernel.

Configure the Linux kernel to allow IP forwarding.


$ sysctl net.ipv4.conf.all.forwarding=1

Change the policy for the iptables FORWARD policy from DROP to ACCEPT.


$ sudo iptables -P FORWARD ACCEPT

These settings do not persist across a reboot, so you may need to add them to a start-up script.

创立本人的 ip6 网卡

开启 ip6 网卡后,只有 bridge 网卡,会主动启用 ip6

创立 user ip6 网卡

docker network create -d bridge --subnet 172.30.20.0/24 mynet2  --ipv6

docker network create -d bridge --ipv6 --subnet "fd00:daad:beee:1::/64" --gateway="fd00:daad:beee::1" --subnet=172.18.0.0/16 --gateway=172.18.0.1 myNet

# 须要仿照 bridge 的信息进行配置
docker inspect 37da
   {
      "Subnet": "2001:db8:1::/64",
      "Gateway": "2001:db8:1::1"
   }

docker network create -d bridge --ipv6 --subnet "2001:db8:2::/64" --gateway="2001:db8:2::1" --subnet=172.30.20.0/24 mynet2

绑定本人的容器到 自定义的网卡
docker run -d -p 30080:80 --net mynet2 nginx


$ sysctl net.ipv6.bindv6only = 1
$ sysctl net.ipv6.conf.default.forwarding=1
$ sysctl net.ipv6.conf.all.forwarding=1


docker network create -d bridge --ipv6 --subnet "2001:db8:3::/64" --gateway="2001:db8:3::1" --subnet=172.30.30.0/24 mynet3
docker run -d -p 30080:80 --net mynet3 nginx

如果须要 间接依据 ip 拜访容器 须要 减少 路由配置

ip -6 route 查看路由直达

# 定向 2001:db8:1::/64 ip6 到 docker0 网卡设施
ip -6 route add 2001:db8:1::/64 dev docker0

暂停 删除所有

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
正文完
 0