过去习惯每台服务器都有外网IP,等服务器数量增多之后有所收敛。迎面而来的需要就是:服务好一个大内网。
上一个文章用的iptables是很痛苦的,所以现在决定使用新的方案。
NAT网关,通常用iptables实现,但性能很差不适合用于生产环境。可通过硬件设备或小米开源的dsnat完成。
端口映射的需求也常有,通过也是用iptables实现。可是iptables太重,性能是个大问题,且每次更新都需要重启。
此时,我们需要一个工具来实现端口映射。如果纯粹是HTTP协议,那么直接用Nginx即可。像MySQL、MongoDB之类则不适用。Google一翻,答案知晓:redir 、socat 。
其中 redir 只支持TCP协议,但使用非常简单。比如将外网的2669端口映射对内网1台MySQL服务器,一键搞定:
|
还有一点高级功能就去自行查阅帮助吧。
socat则更强大,支持ipv4、ipv6、tcp、udp、unix-socks、socket等等了。出于“The simpler the better”的观念,恭喜redir入围!
如果映射量比较大,需要易维护、搞攻击,就必须想到xinted了。配置 /etc/xinetd.d/mysql-portmap 如下:
01
service mysql-portmap {
02
id = 1
03
disable = no
04
type = UNLISTED
05
socket_type = stream
06
protocol = tcp
07
wait = no
08
redirect = 10.33.66.88 3306
09
bind = 183.68.36.138
10
port = 2669
11
user = nobody
12
group = nobody
13
flags = NODELAY KEEPALIVE NOLIBWRAP IPv4
14
log_type = FILE /data/log/xinetd/tcp-portmap.log
15
cps = 100 30
16
}
转发11022 到另外一台的22端口,2种任意
service ssh-11022
{
disable = no
type = UNLISTED
socket_type = stream
protocol = tcp
wait = no
redirect = 192.168.122.59 22
#bind = 183.68.36.138
port = 11022
user = nobody
group = nobody
flags = NODELAY KEEPALIVE NOLIBWRAP IPv4
log_type = FILE /home/root/log/xinetd/ssh-11022.log
cps = 100 30
}
service test
{
type = UNLISTED
disable = no
socket_type = stream
protocol = tcp
wait = no
user = root
port = 11022
#only_from = 192.168.0.0
redirect = 192.168.122.59 22
}
每个映射可以有独立配置文件来管理,修改只需要reload即可!当受到攻击时,在系统日志 /var/log/messages 中会看到:
xinetd[26269]: Starting reconfiguration
xinetd[26269]: Swapping defaults
xinetd[26269]: readjusting service rsync
xinetd[26269]: service mysql-portmap deactivated
xinetd[26269]: mysql-portmap: svc_release with 0 count
xinetd[26269]: Reconfigured: new=1 old=1 dropped=1 (services)
xinetd[26269]: Deactivating service mysql-portmap due to excessive incoming connections. Restarting in 10 seconds.
xinetd[26269]: Activating service mysql-portmap
用xinetd来管理端口映射简单太完美了!
KVM.virbr0的IP更改
virbr0的默认IP是192.168.122.0/24网段的IP,如果你的虚拟机需要使用自己的IP规划,就得修改咯。看看怎么改:
1. vim /etc/libvirt/qemu/networks/default.xml
2. <network>
3. <name>default</name>
4. <uuid>8d004490-ee73-4e36-b9ef-821d7e73f9f6</uuid>
5. <forward dev='br0' mode='nat'>
6. <interface dev='br0'/>
7. </forward>
8. <bridge name='virbr0' stp='on' delay='0' />
9. <mac address='88:58:00:51:98:C8'/>
10. <ip address='192.168.10.1' netmask='255.255.255.0'>
11. <dhcp>
12. <range start='192.168.10.11' end='192.168.10.50' />
13. </dhcp>
14. </ip>
15. </network>
配置文件如上咯,还有一个位置是/var/lib/libvirt/network/default.xml,这两个是一样的内容,不过这个应该是执行virsh net命令后根据/etc下的文件生成的。另外,注意上面配置文件中的forward字段里的内容,是br0,因为作了桥接了,否则使用virbr0的虚拟机能获取到IP,但是就是上不了网,原因就很可能出现在这咯,注意。
重新生成配置文件(var目录下的)
1. virsh # net-define /etc/libvirt/qemu/network/default.xml
2. virsh # net-destroy default
3. virsh # net-start default
然后重启下服务:
- service libvirtd restart
再ifconfig查看就OK 了。