Loading

docker compose ipv6的处理 容器互联的方法

简介:

docker compose 是我常用的容器编排工具,至今没有升级到K8S,汗

那么compose的yaml配置的容器如何互联呢?

常见的恐怕就是上次折腾nginx的那一次了。

一个nginx,反向代理apache,tomcat,iis……

当然也有一个数据库,同时为多个应用服务。

经多次实验,现整理完整无错的方法来处理docker compose ipv6的容器互联。

关键点:使用自定义网络,开启ip6tables

一:docker daemon.json

docker的配置

打开扩展选项,打开ip6tables,这样就不用自己去写那个nat转发了。

{
    "experimental": true,
    "ip6tables": true
}

二:创建自定义网络

我写成脚本了

#!/bin/sh
docker network create --ipv6 \
--subnet="fc00:0:0:1::/64" \
mynet

 IPv6 的私有 IP 定义在 RFC 4193,地址块 fc00 :: /7 已保留。

http://www.ab126.com/goju/7983.html 这里我计算了几个。

免得冲突。几个不同的服务器,使用不同的私有地址吧。

你真使用一样的也没关系。

 

 

三:yaml加入默认网络

version: "3" #版本3
services: #服务
  nginx:   #服务名
    image: nginx:stable-alpine   #镜像名称
    privileged: true  #高级权限
    tty: true         #开一个终端
    container_name: nginx  #自定义容器名
    restart: always
    ports:          #开放端口映射
        - 80:80     #冒号左边是宿主机开放端口,冒号右边是容器开放端口
networks:
  default:
    external: true
    name: mynet

红字是加入已存在的默认网络。

四:测试网络互联

 

 test.yaml

version: "3.9"

services:

  busybox:
    image: busybox 
    command: ping6 -c 4 nginx
networks:
  default:
    external: true
    name: mynet

 

docker compose -f test.yaml up 

[root@test build]# docker compose -f test.yaml up
[+] Running 2/2
 ⠿ busybox Pulled                                                                                                                                                                                                                       8.7s
   ⠿ 50783e0dfb64 Pull complete                                                                                                                                                                                                         2.7s
[+] Running 1/1
 ⠿ Container build-busybox-1  Created                                                                                                                                                                                                   0.4s
Attaching to build-busybox-1
build-busybox-1  | PING nginx (fc00:0:0:1::2): 56 data bytes
build-busybox-1  | 64 bytes from fc00:0:0:1::2: seq=0 ttl=64 time=0.570 ms
build-busybox-1  | 64 bytes from fc00:0:0:1::2: seq=1 ttl=64 time=0.159 ms
build-busybox-1  | 64 bytes from fc00:0:0:1::2: seq=2 ttl=64 time=0.103 ms
build-busybox-1  | 64 bytes from fc00:0:0:1::2: seq=3 ttl=64 time=0.156 ms
build-busybox-1  |
build-busybox-1  | --- nginx ping statistics ---
build-busybox-1  | 4 packets transmitted, 4 packets received, 0% packet loss
build-busybox-1  | round-trip min/avg/max = 0.103/0.247/0.570 ms
build-busybox-1 exited with code 0

容器互联成功

nginx解析为fc00:0:0:1::/64

五:测试访问者IP

[root@test build]# docker logs -f apache
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.3. Set the 'ServerName' directive globally to suppress this message
[Wed Aug 24 10:19:27.854991 2022] [mpm_event:notice] [pid 1:tid 139815773776712] AH00489: Apache/2.4.54 (Unix) configured -- resuming normal operations
[Wed Aug 24 10:19:27.855106 2022] [core:notice] [pid 1:tid 139815773776712] AH00094: Command line: 'httpd -D FOREGROUND'
2408:****:****:****:****:**** - - [24/Aug/2022:10:20:20 +0000] "GET / HTTP/1.1" 304 -
2408:****:****:****:****:**** - - [24/Aug/2022:10:20:21 +0000] "GET / HTTP/1.1" 304 -
2408:****:****:****:****:**** - - [24/Aug/2022:10:20:21 +0000] "GET / HTTP/1.1" 304 -
2408:****:****:****:****:**** - - [24/Aug/2022:10:20:22 +0000] "GET / HTTP/1.1" 304 -
2408:****:****:****:****:**** - - [24/Aug/2022:10:20:22 +0000] "GET / HTTP/1.1" 304 -
240e:****:****:****:****:**** - - [24/Aug/2022:10:20:30 +0000] "GET / HTTP/1.1" 304 -
240e:****:****:****:****:**** - - [24/Aug/2022:10:21:20 +0000] "-" 408 -

apache,已经可以正确获取访问者IP了。

 

posted @ 2022-08-23 18:22  上官飞鸿  阅读(1598)  评论(0编辑  收藏  举报