首页 > 代码库 > 腾讯云安装openvz,快速搭建测试环境

腾讯云安装openvz,快速搭建测试环境

CSDN送了腾讯云的测试资格,准备拿来作为cici的软件发布首页,想在上面做个demo,无奈没有设备环境,于是想要用openvz来虚拟一些vps:

第一步:选择腾讯云的os模板,centos6.3

第二步:安装openvz           安装方法参见:http://openvz.org/Quick_installation

第三步:下载os模板,创建4个vps,分别是ubutnu centos6.5 suse centos7.0

第四步:开启vps

这里发现vps上不了网,估计是腾讯对nat的源地址做了限制,没有网络的vps可不好玩。于是在云主机上做个nat,步骤如下:

1、新建一个网卡,配上自己的地址

 eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 52:54:00:3b:54:96 brd ff:ff:ff:ff:ff:ff
    inet x.x.x.x/22 brd 10.221.31.255 scope global eth0
    inet 192.168.100.100/24 brd 192.168.100.255 scope global eth0:0
    inet6 fe80::5054:ff:fe3b:5496/64 scope link 
       valid_lft forever preferred_lft forever

这里是192.168.100.100/24,新建一个配置文件

[root@HN network-scripts]# cat ifcfg-eth0:0
DEVICE='eth0:0'
NM_CONTROLLED='yes'
ONBOOT='yes'
IPADDR='192.168.100.100'
NETMASK='255.255.255.0'

2、配置iptables

# Generated by iptables-save v1.4.7 on Fri Aug  8 11:51:29 2014
*nat
:PREROUTING ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
<strong>-A POSTROUTING -o eth0 -j MASQUERADE</strong>
COMMIT
# Completed on Fri Aug  8 11:51:29 2014
# Generated by iptables-save v1.4.7 on Fri Aug  8 11:51:29 2014
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [40:5190]
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
#-A INPUT -j REJECT --reject-with icmp-host-prohibited
#-A FORWARD -j REJECT --reject-with icmp-host-prohibited
<strong>-A FORWARD -i eth0:0 -j ACCEPT</strong>
COMMIT

vi /etc/sysctl.conf    将  net.ipv4.ip_forward = 1
建立nat伪装
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE    (eth0为外网)
建立转发:
iptables -A  FORWARD -i eth0:0  -j ACCEPT   (eth0为内网网关)
保存iptables配置:
service iptables save


如果openvz是最新版本,可能nat列表无法初始化,解决办法http://yzs.me/2222.html

修改/etc/modprobe.d/openvz.conf,

把options nf_conntrack ip_conntrack_disable_ve0=1

修改为:

options nf_conntrack ip_conntrack_disable_ve0=0


3、进入vps查看效果

创建vps: vzctl create 101 --ostemplate ubuntu-14.04-x86_64 --hostname ubu-101 --ipadd 192.168.100.101/24

vzctl start 101

vzctl enter 101

root@ubu-101:/# ping www.sina.com
PING ara.sina.com.cn (58.63.236.33) 56(84) bytes of data.
64 bytes from 58.63.236.33: icmp_seq=1 ttl=51 time=8.21 ms
64 bytes from 58.63.236.33: icmp_seq=2 ttl=51 time=8.27 ms
64 bytes from 58.63.236.33: icmp_seq=3 ttl=51 time=8.33 ms
64 bytes from 58.63.236.33: icmp_seq=4 ttl=51 time=8.25 ms

OK  可以上网了