您的位置:首页 > 其它

Iptables & tc在公有云的应用(示例)

2014-03-11 18:59 309 查看
系统拓扑:



如图所示,公有云平台分作管理网络和用户网络:

管理网络用于管理者对整个云平台的运维管理。

用户网络为终端用户提供虚拟机访问入口和可用的带宽保证。

根据上图结构和大家分享一下使用Iptables和TC实现虚拟机地址映射(NAT)和IP限速的实现过程:

功能说明:

Node节点:为虚拟机提供必要的计算、存储及虚拟网络资源。

用户网络:在此使用的Cisco三层交换,配置多VLAN满足不同虚拟网络的需求,VLAN接口地址即各子网的网关地址,在三层交换的某个接口上启三层,配置地址为10.1.0.2,上联一台服务器的内网接口,三层交换使用默认路由指向上联服务器的内网接口IP地址。

防火墙:在此使用一台Linux(CentOS 6.4 mini x64bit)主机实现IP地址mapping和限速。下面对该功能配置做重点介绍:

1.接口地址配置如上图所示,在此省略。

2.添加内网网段路由:

[root@iptables-tc ~]# route add -net 172.16.10.0 netmask 255.255.255.0 dev eth0

[root@iptables-tc ~]# route add -net 172.16.20.0 netmask 255.255.255.0 dev eth0

*删除命令:route del -net network netmask mask

3.启动配置防火墙:

[root@iptables-tc ~]# service iptables start

[root@iptables-tc ~]# chkconfig iptables on

[root@iptables-tc ~]# iptables -L -v -n --line-numbers

num pkts bytes target prot opt in out source destination
1 2516 195K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
5 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

Chain OUTPUT (policy ACCEPT 1974 packets, 212K bytes)
num pkts bytes target prot opt in out source destination

删除红色的规则:

[root@iptables-tc ~]# iptables -D INPUT 5

[root@iptables-tc ~]# iptables -D FORWARD 1

*这两个规则必需删除,否则NAT之后ping测试会出现Destination Host Prohibited。

4.设置转发:

[root@iptables-tc ~]# echo “1” > /proc/sys/net/ipv4/ip_forward /*临时修改重启后失效*/

[root@iptables-tc ~]# sed -i 's/net.ipv4.ip_forward = 0/net.ipv4.ip_forward = 1/g' /etc/sysctl.conf /*永久修改*/

[root@iptables-tc ~]# sysctl -p /*使配置生效*/

5.创建子接口:

[root@iptables-tc ~]# ifconfig eth1:0 211.45.10.2 netmask 255.255.255.0 up

*删除子接口: ifconfig eth1:0 down

6.IP地址映射:

Public IP -------> Private IP的地址mapping:

[root@iptables-tc ~]# iptables -t nat -A PREROUTING -d 211.45.10.2 -j DNAT --to-destination 172.16.10.10

Private IP ------->PublicIP的地址mapping:

[root@iptables-tc ~]# iptables -t nat -A POSTROUTING -s 172.16.10.10 -j SNAT --to-source 211.45.10.2

保存配置:

[root@iptables-tc ~]# iptables-save

*删除规则:

iptables -t nat -D PREROUTING -d 211.45.10.2 -j DNAT --to-destination 172.16.10.10

iptables -t nat -D POSTROUTING -s 172.16.10.10 -j SNAT --to-source 211.45.10.2

7.上传流量限速:

[root@iptables-tc ~]# tc qdisc add dev eth1 root handle 1:0 htb r2q 1

[root@iptables-tc ~]# tc class add dev eth1:0 parent 1:0 classid 1:1 htb rate 100kbps ceil 100kbps burst 100kb cburst 100kb

[root@iptables-tc ~]# tc filter add dev eth1:0 parent 1:0 protocol ip prio 2 u32 match ip src 211.45.10.2 flowid 1:1

*删除上传限速规则:

全部删除:

[root@iptables-tc ~]# tc qdisc del dev eth1 root

单条删除:

[root@iptables-tc ~]# tc filter list dev eth1:0 | grep "flowid 1:1" | awk '{print $10}'` /*查询对应filter的handle值*/

800::800

[root@iptables-tc ~]# tc filter del dev eth1:0 parent 1:0 prio 2 handle800::800u32

[root@iptables-tc ~]# tc class del dev eth1:0 parent 1:0 classid 1:1 htb rate 100kbps ceil 100kbps burst 100kb cburst 100kb

8.下载流量限速:

[root@iptables-tc ~]# tc qdisc add dev eth1 handle ffff: ingress

[root@iptables-tc ~]# tc filter add dev eth1:0 parent ffff: protocol ip prio 2 u32 match ip dst 211.45.10.2 police rate 100kbps burst 100kb mtu 1500 drop flowid :1

*删除下载限速规则:

全部删除:

[root@iptables-tc ~]# tc qdisc add dev eth1 handle ffff: ingress

单条删除:

[root@iptables-tc ~]# tc filter del dev eth1:0 parent ffff: protocol ip prio 2 u32 match ip dst 211.45.10.2 police rate 100kbps burst 100kb mtu 1500 drop flowid :1

9.下载流量限速:

限速队列查看命令:

tc -s -d qdisc show dev eth1

tc -s -d class show dev eth1

tc -s -d filter show dev eth1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: