以CentOS 6为例:
安装openvz
首先下载openvz 的repo文件:
wget -P /etc/yum.repos.d/
rpm --import
yum install vzkernel
yum install vzctl.x86_64 vzquota.x86_64
修改sysctl.conf文件
# packet forwarding enabled and proxy arp disabled
net.ipv4.ip_forward = 1
net.ipv6.conf.default.forwarding = 1
net.ipv6.conf.all.forwarding = 1
net.ipv4.conf.default.proxy_arp = 0
# Enables source route verification
net.ipv4.conf.all.rp_filter = 1
# Enables the magic-sysrq key
kernel.sysrq = 1
# We do not want all our interfaces to send redirects
net.ipv4.conf.default.send_redirects = 1
net.ipv4.conf.all.send_redirects = 0
禁用selinux
重启系统进入到打了vzkernel补丁的内核
启动 vz
/etc/init.d/vz start
配置vz虚拟机使用桥接网络:
以eth0为例:
DEVICE="eth0"
BRIDGE=vmbr0
HWADDR="00:24:E8:B9:00:DA"
NM_CONTROLLED="yes"
ONBOOT="yes"
TYPE="Ethernet"
DELAY=0
vmbr0:
DEVICE="vmbr0"
TYPE=Bridge
BOOTPROTO="dhcp"
ONBOOT="yes"
重启网络服务
/etc/init.d/network restart
brctl show 查看桥接信息:
bridge name bridge id STP enabled interfaces
vmbr0 8000.0024e8b900da no eth0
关掉 eth0和vmbr0 的forwarding and proxy_arp功能.
# echo 0 > /proc/sys/net/ipv4/conf/eth0/forwarding
# echo 0 > /proc/sys/net/ipv4/conf/eth0/proxy_arp
# echo 0 > /proc/sys/net/ipv4/conf/vmbr0/forwarding
# echo 0 > /proc/sys/net/ipv4/conf/vmbr0/proxy_arp
并加入到开机脚本中 rc.local
使用独立的通过桥接的虚拟以太网络通信:
veth的mac地址需配置为:FE:FF:FF:FF:FF:FF
在配置容器时需这用做:
vzctl set 1001 --netif_add eth0,00:0C:29:97:80:DD,veth1001.0,FE:FF:FF:FF:FF:FF,vmbr0 --save
这样 虚拟机的eth0对应的就是 主机的 veth1001.0
需要将将veth1001.0和vmbr0桥接起来
brctl addif vmbr0 veth1001.0
brctl show
bridge name bridge id STP enabled interfaces
vmbr0 8000.0024e8b900da no eth0
veth1001.0
但是这样,当虚拟机重启后,桥接关系就没了,需要在手动的创建桥接
openvz自带一个脚本专门用于自动桥接:
创建一个配置文件
vim /etc/vz/vznet.conf
#!/bin/bash
EXTERNAL_SCRIPT="/usr/sbin/vznetaddbr"
这样每次重启,脚本就会自动为veth创建桥接关系
在创建vz虚拟机的时候需要一条一条输入命令,我们可以写脚本来处理:我写的为例:
createct.sh
#!/bin/bash
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: two parameters, first is ctid, second is hostname"
exit 250
fi
ifcfg_eth0="/data/vz/$1/etc/sysconfig/network-scripts/ifcfg-eth0"
vzmac=`easymac.sh -R |awk '{print $5}' |grep -v ^$`
. /root/bin/config/$1
vzctl create $1 --ostemplate centos-6-x86_64
vzctl set $1 --ipdel all --save
vzctl set $1 --netif_add eth0,$vzmac,veth$1.0,FE:FF:FF:FF:FF:FF,vmbr0 --save
vzctl set $1 --hostname $2 --save
vzctl set $1 --diskspace $ct_quota:$ct_quota --save
vzctl set $1 --ram $ct_memory --swap $ct_swap --save
vzctl set $1 --cpus $ct_cpu --save
echo "DEVICE=eth0" > $ifcfg_eth0
echo "BOOTPROTO=dhcp" >> $ifcfg_eth0
echo "HWADDR=$vzmac" >>$ifcfg_eth0
echo "ONBOOT=yes" >>$ifcfg_eth0
echo "NETWORKING=yes" >/data/vz/$1/etc/sysconfig/network
echo "HOSTNAME=$2" >>/data/vz/$1/etc/sysconfig/network
vzctl start $1
config/1001:
ct_quota=40G
ct_memory=512M
ct_cpu=1
ct_swap=1024M
easymac.sh是一个随机产生 mac地址的脚本文件
h
这样创建虚拟机的时候只需修改一下ctid相对应的config文件
createct.sh ctid hostname
阅读(2957) | 评论(0) | 转发(0) |