Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

network #4

Open
gobomb opened this issue Aug 27, 2019 · 30 comments
Open

network #4

gobomb opened this issue Aug 27, 2019 · 30 comments

Comments

@gobomb
Copy link
Owner

gobomb commented Aug 27, 2019

nc 创建一个 unix domain socket:

$ nc -Ul ./sock &	
[1] 892
$ ss -nlp | grep 892
u_str  LISTEN     0      5      ./sock 36998634              * 0                   users:(("nc",pid=892,fd=3))
$ netstat -nlp | grep 892
unix  2      [ ACC ]     STREAM     LISTENING     36998634 892/nc              ./sock
@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

https://www.ibm.com/developerworks/cn/linux/1310_xiawc_networkdevice/index.html

Linux 上的基础网络设备:

  1. Bridge 类似交换机,可以 attach 多个二层设备,可以绑定 IP;VLAN 做广播域(二层)隔离;TUN/TAP 提供给用户态向二层/三层网络注入数据的接口;VETH,一端写数据,一端读数据

  2. 对于 linux 虚拟设备,数据流的方向很重要。对于 attach 到 Bridge 上的设备而言,只有收到数据而非发送数据,数据才会被转发到 Bridge 上面。由此 VETH 可以反转通讯数据的方向;向 tap 发送数据,socket 和 file operation 操作的效果也不同。

  3. 查询系统里所有二层设备,包括 VETH/TAP 设备:$ ip link show

@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

#3 (comment)

VLAN 介绍

@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

FDB,Forwarding DataBase:交换机的 MAC 地址转发表

$ bridge fdb 可查看

@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

$ yum install bridge-utils

$ brictl show
bridge name	bridge id		STP enabled	interfaces
cni0		8000.be3ee6fb53b5	no		veth0f28831e
							veth8b845a88
							vethc3b08f13
docker0		8000.0242a88ed6ec	no

查看 bridge 下 attach 的网络设备

@gobomb
Copy link
Owner Author

gobomb commented Sep 4, 2019

$ bridge fdb

查看 MAC 地址转发表

@gobomb
Copy link
Owner Author

gobomb commented Sep 6, 2019

CNI:

CNI网络插件,需要用户将网络配置放到 /etc/cni/net.d 目录中,并将CNI插件的二进制文件放入 /opt/cni/bin

@gobomb
Copy link
Owner Author

gobomb commented Sep 9, 2019

https://cizixs.com/2016/07/16/flannel-source-code-insight/

flannel 源码解读,讲得比较细致,因为是年前的文章了,和现在的版本有出入,但总体架构是差不多的

@gobomb
Copy link
Owner Author

gobomb commented Sep 12, 2019

Linux 删除网路设备

$ ip vxlan100 down
$ ip link delete vxlan100

@gobomb
Copy link
Owner Author

gobomb commented Sep 17, 2019

访问容器的网络命名空间

ln -s /proc/1234/ns/net /var/run/netns/1234

$ ip netns show
1234
$ ip netns exec 1234 ip a

@gobomb
Copy link
Owner Author

gobomb commented Sep 17, 2019

在 10.10.13.61 与 10.10.13.63 之间建立 gre 隧道

# on 10.10.13.61
$ ip tunnel add gre1 mode gre remote 10.10.13.63 local 10.10.13.61 ttl 255
$ ip link set gre1 up
$ ip addr add 192.168.11.1 peer 192.168.11.2 dev gre1

# on 10.10.13.63
$ ip tunnel add gre1 mode gre remote 10.10.13.61 local 10.10.13.63 ttl 255
$ ip link set gre1 up
$ ip addr add 192.168.11.2 peer 192.168.11.1 dev gre1

撤销

$ ip link set gre1 down
$ ip tunnel del gre1

@gobomb
Copy link
Owner Author

gobomb commented Oct 9, 2019

$ip link add wg0 type wireguard出现错误(centos):RTNETLINK answers: Operation not supported

$ reboot

# 加载模块
$ modprobe wireguard

$ lsmod | grep wireguard

@gobomb
Copy link
Owner Author

gobomb commented Oct 11, 2019

k8s 的网络模型:

  1. 运行在一个节点当中的Pod能在不经过NAT的情况下跟集群中所有的Pod进行通信

  2. 节点当中的客户端(system daemon、kubelet)能跟该节点当中的所有Pod进行通信

  3. 以host network模式运行在一个节点上的Pod能跟集群中所有的Pod进行通信

@gobomb
Copy link
Owner Author

gobomb commented Oct 12, 2019

https://mp.weixin.qq.com/s/uJR4YmUuSCjgEi-VNkTLnA

flannel 三种 backend 介绍(udp、vxlan、route、host-gw)

@gobomb
Copy link
Owner Author

gobomb commented Nov 4, 2019

iptables 使用 raw 表追踪数据包

开启 iptalbes 的日志模块:

Load the (IPv4) netfilter log kernel module:

# modprobe nf_log_ipv4

Enable logging for the IPv4 (AF Family 2):

# sysctl net.netfilter.nf_log.2=nf_log_ipv4

reconfigure rsyslogd to log kernel messages (kern.*) to /var/log/messages:

# cat /etc/rsyslog.conf | grep -e "^kern"
kern.*;*.info;mail.none;authpriv.none;cron.none                /var/log/messages

restart rsyslogd:

# systemctl restart rsyslog

添加规则:

iptables -t raw -A PREROUTING -d 10.20.128.8 -j TRACE

查看日志:

tailf /var/log/messages( CentOS )

ref: http://www.opensourcerers.org/how-to-trace-iptables-in-rhel7-centos7/

ubuntu

modprobe ipt_LOG
modprobe nf_log_ipv4
sysctl net.netfilter.nf_log.2=nf_log_ipv4

iptables -t raw -A OUTPUT-p icmp -j TRACE

tailf /var/log/syslog

@gobomb
Copy link
Owner Author

gobomb commented Dec 6, 2019

dnsmasq 在参数里添加 --log-queries 可在日志里输出dns查询记录

@gobomb
Copy link
Owner Author

gobomb commented Dec 9, 2019

在namespace中,tracerpath出现如下错误:

$ ip netns exec node2 tracepath 172.16.255.22
 1:  send failed
     Resume: pmtu 65535

ip netns exec node2 ip link set lo up

@gobomb
Copy link
Owner Author

gobomb commented Dec 13, 2019

添加静态路由规则的时候,需要保证gateway(gw)的IP和eth0(本机IP)在同一个网段内。

eth0 IP 为 10.10.12.27

ip route add 10.127.0.130 dev eth0 via 10.10.13.61

会报错:

RTNETLINK answers: Network is unreachable

@gobomb
Copy link
Owner Author

gobomb commented Jan 8, 2020

http://sites.inka.de/bigred/devel/tcp-tcp.html 为什么tunnel一般不用tcp而通常用udp封装

@gobomb
Copy link
Owner Author

gobomb commented Jan 8, 2020

TAP (Terminal Access Point)

@gobomb
Copy link
Owner Author

gobomb commented Jan 19, 2020

centos安装ipvs

yum install ipvsadm -y
ipvsadm #装载ip_vs模块
modprobe -a ip_vs ip_vs_rr ip_vs_wrr ip_vs_sh nf_conntrack_ipv4
lsmod| grep ip_vs #查看ipvs模块是否加载成功

kube-proxy(1.9)参数

- --feature-gates=SupportIPVSProxyMode=true

修改cm:mode=ipvs

@gobomb
Copy link
Owner Author

gobomb commented Jan 20, 2020

mac下特定域名使用特定dns server:

http://hints.macworld.com/article.php?story=2004062902195410

@gobomb
Copy link
Owner Author

gobomb commented Feb 24, 2020

CentOS Linux release 7.5.1804 (Core)

  1. network 脚本(/etc/rc.d/init.d/network)会去调用 /etc/sysconfig/network-scripts/ 下面的网卡配置来启动网络,属于低层级操作
  2. NetworkManager 服务,兼容 network 脚本,由 redhat 开发。nmcli 是其cli工具

没有NetworkManager服务时,network脚本是默认的;当NetworkManager服务启动时,会接管网络管理。

修改dns配置需修改 /etc/sysconfig/network-scripts/ifcfg-ens192 里的DNS才能保证重启之后/etc/resolv.conf生效。

https://forum.huawei.com/enterprise/zh/thread-174631-1-1.html

https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/sec-networkmanager_and_the_network_scripts

Ubuntu 16.04 LTS

/etc/resolv.conf/run/resolvconf/resolv.conf的软连接,配置由resolvconf.service服务生成

需要修改/etc/resolvconf/resolv.conf.d/base才能保证DNS配置生效

Ubuntu 18.04.3 LTS

/etc/resolv.conf的配置由systemd-resolved服务生成,systemd-resolved是运行在本地的DNS server,真正的解析配置在/run/systemd/resolve/resolv.conf

需要通过 /etc/systemd/resolved.conf进行更改

@gobomb
Copy link
Owner Author

gobomb commented Jul 6, 2020

@gobomb
Copy link
Owner Author

gobomb commented Nov 23, 2020

https://tools.ietf.org/html/rfc6902

json patch

curl -XPATCH -H 'Content-Type:application/json-patch+json' http://0.0.0.0:16020/apis/server/v1/datacenters/DC1 -d '[{ "op": "remove", "path": "/spec/metric" }]'

@gobomb
Copy link
Owner Author

gobomb commented Mar 4, 2021

https://miek.nl/2009/july/31/dns-classes/

dns 小众类型

@gobomb
Copy link
Owner Author

gobomb commented Apr 9, 2022

ip route get from 192.168.235.13 to 10.0.1.12 iif enp5s0 tos 0x00

检查路由

ref: https://www.yisu.com/zixun/46018.html

@gobomb
Copy link
Owner Author

gobomb commented Apr 9, 2022

ip route add 10.0.1.10/31 via 10.0.1.10 dev enp0s8

RTNETLINK Network is unreachable dev 指定的网卡,无法到达10.0.1.10

可以添加 onlink ,强制设置

ip route add 10.0.1.10/31 via 10.0.1.10 dev enp0s8 onlink

@gobomb
Copy link
Owner Author

gobomb commented Apr 9, 2022

iptables 数据包流向和路由决策

https://upload.wikimedia.org/wikipedia/commons/3/37/Netfilter-packet-flow.svg

@gobomb
Copy link
Owner Author

gobomb commented Apr 10, 2022

统计每个ip的连接数

conntrack -L -o extended | awk '{print $7}' | cut -d "=" -f 2 | sort | uniq -c | sort -nr | head -n 10

统计tcp各个状态的连接数

conntrack -L -o extended | awk '/^.*tcp.*$/ {sum[$6]++} END {for(i in sum) print i, sum[i]}'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant