我知道,我可以手动分配静态 IP,使用/etc/network/interfaces
.
我也知道,我可以通过寻找阅读LXC容器(例如MAC地址lxc.network.hwaddr
条目/var/lib/lxc/<container-name>/config
,并指定使用项基于IPdhcp-host=<mac-addr>,10.0.3.3
的/etc/dnsmasq.d/<some file>
。
在/etc/default/lxc-net
我阅读的文件中
# Uncomment the next line if you'd like to use a conf-file for the lxcbr0
# dnsmasq. For instance, you can use 'dhcp-host=mail1,10.0.3.100' to have
# container 'mail1' always get ip address 10.0.3.100.
#LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
Run Code Online (Sandbox Code Playgroud)
那将适合我的需要;不幸的是,这样做没有任何效果。
小智 18
我最近遇到了这个问题,我想我找到了一个简单的解决方案。我(仅)在 Ubuntu 14.04 上对其进行了测试。
首先,取消注释 /etc/default/lxc-net 这一行:
LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
Run Code Online (Sandbox Code Playgroud)
在 /etc/lxc/dnsmasq.conf 中,定义一个 dhcp-hosts 文件:
dhcp-hostsfile=/etc/lxc/dnsmasq-hosts.conf
Run Code Online (Sandbox Code Playgroud)
然后在 /etc/lxc/dnsmasq-hosts.conf 中添加条目,如下所示:
mail,10.0.3.16
web,10.0.3.17
Run Code Online (Sandbox Code Playgroud)
当心:更改将在您重新启动 lxc-net(重新启动 dnsmasq)后生效:
service lxc-net restart
Run Code Online (Sandbox Code Playgroud)
之后你可以修改 /etc/lxc/dnsmasq-hosts.conf 并将 SIGHUP 信号发送到 dnsmasq:
killall -s SIGHUP dnsmasq
Run Code Online (Sandbox Code Playgroud)
所以是的,您需要重新启动 lxc-net,但只需一次。希望这可以帮助。
该解决方案通过修补 lxc upstart 脚本来工作。它确实将启动 lxcbr0 桥接器和启动 a 的复杂任务dnsmasq
分成了两个单独的工作。现在您不需要重新启动整个lxc-net
桥来重新加载dnsmasq
- 重新加载sudo service restart lxc-dnsmasq
就足够了,不需要关闭桥。
sudo service lxc-net stop
并确保没有 lxcbr0(或同等)桥接。/etc/init/lxc-net.conf
为以下内容:。
description "lxc network"
author "Serge Hallyn <serge.hallyn@canonical.com>"
start on starting lxc
stop on stopped lxc
env USE_LXC_BRIDGE="true"
env LXC_BRIDGE="lxcbr0"
env LXC_ADDR="10.0.3.1"
env LXC_NETMASK="255.255.255.0"
env LXC_NETWORK="10.0.3.0/24"
env varrun="/run/lxc"
env LXC_DOMAIN=""
pre-start script
[ -f /etc/default/lxc ] && . /etc/default/lxc
[ "x$USE_LXC_BRIDGE" = "xtrue" ] || { stop; exit 0; }
use_iptables_lock="-w"
iptables -w -L -n > /dev/null 2>&1 || use_iptables_lock=""
cleanup() {
# dnsmasq failed to start, clean up the bridge
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p udp --dport 67 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p tcp --dport 67 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p udp --dport 53 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p tcp --dport 53 -j ACCEPT
iptables $use_iptables_lock -D FORWARD -i ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -D FORWARD -o ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -t nat -D POSTROUTING -s ${LXC_NETWORK} ! -d ${LXC_NETWORK} -j MASQUERADE || true
iptables $use_iptables_lock -t mangle -D POSTROUTING -o ${LXC_BRIDGE} -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
ifconfig ${LXC_BRIDGE} down || true
brctl delbr ${LXC_BRIDGE} || true
}
if [ -d /sys/class/net/${LXC_BRIDGE} ]; then
if [ ! -f ${varrun}/network_up ]; then
# bridge exists, but we didn't start it
stop;
fi
exit 0;
fi
# set up the lxc network
brctl addbr ${LXC_BRIDGE} || { echo "Missing bridge support in kernel"; stop; exit 0; }
echo 1 > /proc/sys/net/ipv4/ip_forward
mkdir -p ${varrun}
ifconfig ${LXC_BRIDGE} ${LXC_ADDR} netmask ${LXC_NETMASK} up
iptables $use_iptables_lock -I INPUT -i ${LXC_BRIDGE} -p udp --dport 67 -j ACCEPT
iptables $use_iptables_lock -I INPUT -i ${LXC_BRIDGE} -p tcp --dport 67 -j ACCEPT
iptables $use_iptables_lock -I INPUT -i ${LXC_BRIDGE} -p udp --dport 53 -j ACCEPT
iptables $use_iptables_lock -I INPUT -i ${LXC_BRIDGE} -p tcp --dport 53 -j ACCEPT
iptables $use_iptables_lock -I FORWARD -i ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -I FORWARD -o ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -t nat -A POSTROUTING -s ${LXC_NETWORK} ! -d ${LXC_NETWORK} -j MASQUERADE
iptables $use_iptables_lock -t mangle -A POSTROUTING -o ${LXC_BRIDGE} -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
touch ${varrun}/network_up
end script
post-stop script
[ -f /etc/default/lxc ] && . /etc/default/lxc
[ -f "${varrun}/network_up" ] || exit 0;
# if $LXC_BRIDGE has attached interfaces, don't shut it down
ls /sys/class/net/${LXC_BRIDGE}/brif/* > /dev/null 2>&1 && exit 0;
if [ -d /sys/class/net/${LXC_BRIDGE} ]; then
use_iptables_lock="-w"
iptables -w -L -n > /dev/null 2>&1 || use_iptables_lock=""
ifconfig ${LXC_BRIDGE} down
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p udp --dport 67 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p tcp --dport 67 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p udp --dport 53 -j ACCEPT
iptables $use_iptables_lock -D INPUT -i ${LXC_BRIDGE} -p tcp --dport 53 -j ACCEPT
iptables $use_iptables_lock -D FORWARD -i ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -D FORWARD -o ${LXC_BRIDGE} -j ACCEPT
iptables $use_iptables_lock -t nat -D POSTROUTING -s ${LXC_NETWORK} ! -d ${LXC_NETWORK} -j MASQUERADE || true
iptables $use_iptables_lock -t mangle -D POSTROUTING -o ${LXC_BRIDGE} -p udp -m udp --dport 68 -j CHECKSUM --checksum-fill
pid=`cat ${varrun}/dnsmasq.pid 2>/dev/null` && kill -9 $pid || true
rm -f ${varrun}/dnsmasq.pid
brctl delbr ${LXC_BRIDGE}
fi
rm -f ${varrun}/network_up
end script
Run Code Online (Sandbox Code Playgroud)
/etc/init/lxc-dnsmasq
其中包含以下内容:。
description "lxc dnsmasq service"
author "Adam Ryczkowski, ispired by Serge Hallyn <serge.hallyn@canonical.com>"
expect fork
start on started lxc-net
stop on stopped lxc-net
env USE_LXC_BRIDGE="true"
env LXC_BRIDGE="lxcbr0"
env LXC_ADDR="10.0.3.1"
env LXC_NETMASK="255.255.255.0"
env LXC_NETWORK="10.0.3.0/24"
env LXC_DHCP_RANGE="10.0.3.2,10.0.3.254"
env LXC_DHCP_MAX="253"
env LXC_DHCP_CONFILE=""
env varrun="/run/lxc-dnsmasq"
env LXC_DOMAIN=""
pre-start script
[ -f /etc/default/lxc ] && . /etc/default/lxc
[ "x$USE_LXC_BRIDGE" = "xtrue" ] || { stop; exit 0; }
if [ ! -d ${varrun} ]; then
mkdir -p ${varrun}
fi
opts="$LXC_DOMAIN_ARG -u lxc-dnsmasq --strict-order --bind-interfaces --pid-file=${varrun}/dnsmasq.pid --conf-file=${LXC_DHCP_CONFILE} --listen-address ${LXC_ADDR} --dhcp-range ${LXC_DHCP_RANGE} --dhcp-lease-max=${LXC_DHCP_MAX} --dhcp-no-override --except-interface=lo --interface=${LXC_BRIDGE} --dhcp-leasefile=/var/lib/misc/dnsmasq2.${LXC_BRIDGE}.leases --dhcp-authoritative --keep-in-foreground"
/usr/sbin/dnsmasq $opts &
end script
post-stop script
if [ -f ${varrun}/dnsmasq.pid ]; then
PID=`cat ${varrun}/dnsmasq.pid`
kill $PID
fi
end script
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27628 次 |
最近记录: |