iptable forward转发实现docker -p 功能共局域网内其他主机访问

 测试环境需要临摹开发k8s调度环境,搭建本地minikube时,局域网中别的机器无法访问:
 1.minikube version: v0.28.1 无法设置type=loadbalancer;
 2.type=nodeport只能通过`minikube ip`:${port}实现本机访问。
 故需要配置iptable forward

1./proc/sys/net/ipv4/ip_forward=1 开启转发功能
2.docker pull nginx:1.15
3.docker run -d –name ngx nginx:1.15
4.查询ngx服务ip


xurun@star:~$ docker inspect ngx | grep IPAddress
“SecondaryIPAddresses”: null,
“IPAddress”: “172.17.0.2”,
“IPAddress”: “172.17.0.2”,


5.查询本机网卡


docker0 Link encap:Ethernet HWaddr 02:42:18:39:0d:15
inet addr:172.17.0.1 Bcast:172.17.255.255 Mask:255.255.0.0
inet6 addr: fe80::42:18ff:fe39:d15/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:33 errors:0 dropped:0 overruns:0 frame:0
TX packets:82 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:5814 (5.8 KB) TX bytes:7978 (7.9 KB)

eno1 Link encap:Ethernet HWaddr 80:18:44:e8:23:34
inet addr:192.168.129.5 Bcast:192.168.143.255 Mask:255.255.240.0
inet6 addr: fe80::8218:44ff:fee8:2334/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:559589 errors:0 dropped:0 overruns:0 frame:0
TX packets:54195 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:164342926 (164.3 MB) TX bytes:4364974 (4.3 MB)
Interrupt:58

eno2 Link encap:Ethernet HWaddr 80:18:44:e8:23:35
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:59

eno3 Link encap:Ethernet HWaddr 80:18:44:e8:23:36
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:61

eno4 Link encap:Ethernet HWaddr 80:18:44:e8:23:37
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:62

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:22130 errors:0 dropped:0 overruns:0 frame:0
TX packets:22130 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1
RX bytes:311318902 (311.3 MB) TX bytes:311318902 (311.3 MB)

vboxnet0 Link encap:Ethernet HWaddr 0a:00:27:00:00:00
inet addr:192.168.99.1 Bcast:192.168.99.255 Mask:255.255.255.0
inet6 addr: fe80::800:27ff:fe00:0/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:110 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:22207 (22.2 KB)

veth6330473 Link encap:Ethernet HWaddr 42:fc:f0:a9:df:1d
inet6 addr: fe80::40fc:f0ff:fea9:df1d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:33 errors:0 dropped:0 overruns:0 frame:0
TX packets:111 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:6276 (6.2 KB) TX bytes:11441 (11.4 KB)

veth4f363a1 Link encap:Ethernet HWaddr aa:d4:7d:7e:2d:40
inet6 addr: fe80::a8d4:7dff:fe7e:2d40/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:40 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:0 (0.0 B) TX bytes:4538 (4.5 KB)


6.配置路由,使ngx服务暴露给局域网中其他用户
iptable forward转发实现docker -p 功能共局域网内其他主机访问_第1张图片

7.访问成功
iptable forward转发实现docker -p 功能共局域网内其他主机访问_第2张图片

你可能感兴趣的:(docker)