这里将介绍如何使用lxc创建集群环境用于模拟服务器环境,方便测试项目。
系统环境ubuntu
创建router容器
lxc-create -n router -t debian
修改router的网卡配置
nano /etc/network/interface
auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 iface eth0 inet static address 192.168.122.186 netmask 255.255.255.0 gateway 192.168.122.1 auto eth0:1 allow-hotplug eth0:1 iface eth0:1 inet static address 192.168.0.1 netmask 255.255.0.0
iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth0 -j MASQUERADE
创建srv容器
lxc-create -n router -t debian
修改srv1网卡配置
nano /etc/network/interface
auto lo iface lo inet loopback auto eth0 allow-hotplug eth0 iface eth0 inet manual pre-up ifconfig $IFACE up pre-down ifconfig $IFACE down auto br0 allow-hotplug br0 iface br0 inet static bridge_ports eth0 address 192.168.1.1 netmask 255.255.255.0 gateway 192.168.0.1修改dns
echo "nameserver 8.8.8.8" > /etc/resolv.conf
添加route
route add -net 192.168.0.0 netmask 255.255.0.0 dev br0 route add default gw 192.168.0.1
srv2 , srv3 ........都用上面的方式去创建区别是将 192.168.1.1 换成 192.168.1.2
container内安装lxc
首先需要安装lxc
在srv里创建容器
lxc-create -n app -t debian
lxc.network.mtu = 1500 lxc.network.type = veth lxc.network.name = eth0 lxc.network.link = br0 lxc.network.ipv4 = 192.168.100.2/16 lxc.network.flags = up lxc.network.ipv4.gateway = 192.168.0.1