在添加新的veth界面时运行脚本 [英] run a script when a new veth interface is added
问题描述
Docker为每个创建的容器创建连接到桥接器( docker0
)的 veth
接口。 / p>
我想限制带宽这些新的 veth
接口。我找到了一个方法来做到这一点与奇观。但是我想自动化这个。
有没有办法让每次新的$ code $ v $ $ c $运行脚本的钩子c>界面是否附加?
我已经研究了在 /etc/network/if-up.d /
中添加脚本,但是他们
以下是我要尝试获取的一些系统日志文件,而不是运行 veth
通知。我知道我可以拖尾这些日志,但是这种方法似乎有点麻木,而且必须有办法通过操作系统收到有关此事件的通知。
code> 5月2日23:28:41 ip-10-171-7-2内核:[22170163.565812] netlink:解析属性后剩下1个字节。
May 2 23:28:42 ip-10-171-7-2 kernel:[22170163.720571] IPv6:ADDRCONF(NETDEV_UP):veth5964:link还没有准备好
5月2日23:28:42 ip -10-171-7-2内核:[22170163.720587]设备veth5964进入混杂模式
May 2 23:28:42 ip-10-171-7-2 avahi-daemon [1006]:为vethdc8c提取工作站服务。
May 2 23:28:42 ip-10-171-7-2 kernel:[22170163.743283] IPv6:ADDRCONF(NETDEV_CHANGE):veth5964:link ready ready
May 2 23:28:42 ip- 10-171-7-2内核:[22170163.743344] docker0:port 27(veth5964)进入转发状态
May 2 23:28:42 ip-10-171-7-2 kernel:[22170163.743358] docker0:port 27(veth5964)进入转发状态
5月2日23:28:48 ip-10-171-7-2内核:[22170170.518670] docker0:端口26(vethb06a)进入转发状态
5月2日23: 28:57 ip-10-171-7-2 kernel:[22170178.774676] docker0:port 27(veth5964)进入转发状态
您应该在每次添加新界面时编写一个自定义的 udev
规则来运行您的脚本。这是Debian在处理界面hotplug时所做的。
/etc/udev/rules.d/90-my-networking.rules
:
SUBSYSTEM ==net,RUN + =/ usr / local / bin / my- networking-agent.sh
/ usr / local / bin / my- networking-agent.sh
:
#!/ bin / sh
logger嘿我刚接触到界面$ {INTERFACE},动作$ {ACTION}
strong>
以下是如何测试:
modprobe dummy0
#ifconfig dummy0 up
#tail -n1 / var / log / syslog
May 3 01:48:06 ernst logger:嘿,我刚刚接收到了带有动作的接口dummy0加
Docker creates a veth
interface connected to a bridge (docker0
) for each of the containers it create.
http://docs.docker.io/use/networking/
I want to limit the bandwidth these new veth
interfaces have. I found a way to do this with wondershaper. However I want to automate this.
Is there a way to have a hook that runs a script every time a new veth
interface is attached?
I have looked into adding scripts in /etc/network/if-up.d/
, but they do not run when a veth
is added only during boot.
Here are some syslogs of what I am trying to get notified about. I know I can tail these logs but that method seems sort of hacky and there has to be a way to get notified about this event via the OS.
May 2 23:28:41 ip-10-171-7-2 kernel: [22170163.565812] netlink: 1 bytes leftover after parsing attributes.
May 2 23:28:42 ip-10-171-7-2 kernel: [22170163.720571] IPv6: ADDRCONF(NETDEV_UP): veth5964: link is not ready
May 2 23:28:42 ip-10-171-7-2 kernel: [22170163.720587] device veth5964 entered promiscuous mode
May 2 23:28:42 ip-10-171-7-2 avahi-daemon[1006]: Withdrawing workstation service for vethdc8c.
May 2 23:28:42 ip-10-171-7-2 kernel: [22170163.743283] IPv6: ADDRCONF(NETDEV_CHANGE): veth5964: link becomes ready
May 2 23:28:42 ip-10-171-7-2 kernel: [22170163.743344] docker0: port 27(veth5964) entered forwarding state
May 2 23:28:42 ip-10-171-7-2 kernel: [22170163.743358] docker0: port 27(veth5964) entered forwarding state
May 2 23:28:48 ip-10-171-7-2 kernel: [22170170.518670] docker0: port 26(vethb06a) entered forwarding state
May 2 23:28:57 ip-10-171-7-2 kernel: [22170178.774676] docker0: port 27(veth5964) entered forwarding state
You should write a custom udev
rule that runs a script of yours each time a new interface is added. This is what Debian does for handling interface "hotplug".
/etc/udev/rules.d/90-my-networking.rules
:
SUBSYSTEM=="net", RUN+="/usr/local/bin/my-networking-agent.sh"
/usr/local/bin/my-networking-agent.sh
:
#!/bin/sh
logger "hey I just got interface ${INTERFACE} with action ${ACTION}"
EDIT
Here is how you can test it:
# modprobe dummy0
# ifconfig dummy0 up
# tail -n1 /var/log/syslog
May 3 01:48:06 ernst logger: hey I just got interface dummy0 with action add
这篇关于在添加新的veth界面时运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!