k8s 网络_1

网络

直连

添加两个命名空间ns1 ns2

ip netns add ns1
ip netns add ns2

每个网络命名空间有自己的网路和端口

ip netns exec ns1 ip addr

1: lo: mtu 65536 qdisc noop state DOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

需要网卡veth,成对出现

ip link add veth1 type veth peer name veth2

[root@node3 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:5f:aa:08 brd ff:ff:ff:ff:ff:ff
inet 192.168.164.14/24 brd 192.168.164.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fd15:4ba5:5a2b:1008:1ef3:cc2:a7c:fbdf/64 scope global noprefixroute dynamic
valid_lft 2591954sec preferred_lft 604754sec
inet6 fe80::9d93:f6c8:28fd:503f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: veth2@veth1: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether 82:96:6d:9f:aa:9f brd ff:ff:ff:ff:ff:ff
4: veth1@veth2: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether ea:01:38:0a:a1:7a brd ff:ff:ff:ff:ff:ff

网卡移动到ns1中

ip link set veth1 netns ns1
ip netns exec ns1 ip addr

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
4: veth1@if3: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
link/ether ea:01:38:0a:a1:7a brd ff:ff:ff:ff:ff:ff link-netnsid 0

ip link set veth2 netns ns2

启动网卡

ip netns exec ns1 ip link set dev veth1 up
ip netns exec ns2 ip link set dev veth2 up

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
3: veth2@if4: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 82:96:6d:9f:aa:9f brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::8096:6dff:fe9f:aa9f/64 scope link
valid_lft forever preferred_lft forever

设置网卡的ip地址

ip netns exec ns1 ip a a 192.168.0.2/24 dev veth1
ip netns exec ns2 ip a a 192.168.0.3/24 dev veth2
ip netns exec ns1 ping 192.168.0.3

PING 192.168.0.3 (192.168.0.3) 56(84) bytes of data.
64 bytes from 192.168.0.3: icmp_seq=1 ttl=64 time=0.082 ms
64 bytes from 192.168.0.3: icmp_seq=2 ttl=64 time=0.075 ms
^C

ip netns exec ns2 ping 192.168.0.2

PING 192.168.0.2 (192.168.0.2) 56(84) bytes of data.
64 bytes from 192.168.0.2: icmp_seq=1 ttl=64 time=0.037 ms
64 bytes from 192.168.0.2: icmp_seq=2 ttl=64 time=0.055 ms
^C

centos安装docker

会自动安装一个docker0的bridge

docker pull springcloud/eureka:latest
docker run -dit --name eureka -p 8761:8761 springcloud/eureka:latest

Bridge

添加bridge br0起到了交换机的作用

ip link add br0 type bridge
ip link set br0 up

添加两个网卡对

ip link a veth11 type veth peer name br-veth11
ip link a veth22 type veth peer name br-veth22

将一对分别移动到ns1 ns2

ip link set veth11 netns ns1
ip link set veth22 netns ns2

剩下的移动到bro

ip link set br-veth11 master br0
ip link set br-veth22 master br0

启动所有网卡设备

ip link set br-veth11 up
ip link set br-veth22 up
ip netns exec ns1 ip link set dev veth11 up
ip netns exec ns2 ip link set dev veth22 up

设置Ip地址

ip netns exec ns1 ip address add 192.168.0.11/24 dev veth11
ip netns exec ns2 ip address add 192.168.0.12/24 dev veth22

ping对方ip

ip netns exec ns1 ping 192.168.0.12

[root@node3 ~]# ip netns exec ns1 ping 192.168.0.12
PING 192.168.0.12 (192.168.0.12) 56(84) bytes of data.
64 bytes from 192.168.0.12: icmp_seq=1 ttl=64 time=0.249 ms
64 bytes from 192.168.0.12: icmp_seq=2 ttl=64 time=0.057 ms
64 bytes from 192.168.0.12: icmp_seq=3 ttl=64 time=0.061 ms
^C

从外部主机无法访问 192.168.0.11 跨网络找路由

为br0设置一个ip

ifconfig br0 192.168.0.1/24 up

[root@node3 ~]# ip route
default via 192.168.164.1 dev ens33 proto static metric 100
192.168.0.0/24 dev br0 proto kernel scope link src 192.168.0.1
192.168.164.0/24 dev ens33 proto kernel scope link src 192.168.164.14 metric 100

[root@node3 ~]# ping 192.168.0.11
PING 192.168.0.11 (192.168.0.11) 56(84) bytes of data.
64 bytes from 192.168.0.11: icmp_seq=1 ttl=64 time=0.062 ms
64 bytes from 192.168.0.11: icmp_seq=2 ttl=64 time=0.121 ms
64 bytes from 192.168.0.11: icmp_seq=3 ttl=64 time=0.118 ms
^C
--- 192.168.0.11 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.062/0.100/0.121/0.028 ms

posted @ 2022-10-28 00:23  Herenwei_Wayne  阅读(51)  评论(0编辑  收藏  举报