随着互联网IT行业的发展,越来越多的企业开始使用开源软件搭建自己的web架构,主流的LVS也得到了广泛的应用,在保证高可用的同时,用户对网站的体验速度也有了很高的要求,这时候需要我们在我们的架构既要满足高并发同时还要求高速度。 今天我们来搭建主流架构Lvs+Keepalived+Squid+Nginx
相关软件包:
- 系统:CentOS6.0 x64
- Ipvsadm:ipvsadm-1.24.tar.gz
- Keepalived:keepalived-1.1.15.tar.gz
- Squid:squid-3.1.10
- Nginx:nginx-1.2.0.tar.gz
- Ip信息:
- Lvs-master 192.9.117.139
- Lvs-backup 192.9.117.140
- Lvs-vip 192.9.117.145
- Squid_1 192.9.117.141
- Squid_2 192.9.117.142
- Nginx_1 192.9.117.143
- Nginx_1 192.9.117.144
网络架构图:
一、LVS+Keepalived安装
1、下载源码包
cd /usr/src/
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
2、建立软链接
ln -s /usr/src/kernels/2.6.* /usr/src/linux
3、安装ipvsadm、keepalived
cd /usr/src/ && tar xzvf ipvsadm-1.24.tar.gz &&cd ipvsadm-1.24
&& make && make install
cd /usr/src/ && tar -xzvf keepalived-1.1.15.tar.gz &&
cd keepalived-1.1.15 && ./configure && make && make install
4、配置keepalived
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir -p /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
5、修改keepalived配置文件
vi /etc/keepalived/keepalived.conf,根据实际情况修改:
- ! Configuration File for keepalived
- global_defs {
- notification_email {
- wgkgood@163.com
- }
- notification_email_from wgkgood@163.com
- smtp_server 127.0.0.1
- smtp_connect_timeout 30
- router_id LVS_DEVEL
- }
- # VIP1
- vrrp_instance VI_1 {
- state MASTER //备份服务器改成BACKUP
- interface eth0
- lvs_sync_daemon_inteface eth0
- virtual_router_id 51
- priority 100 //备份服务器改成90
- advert_int 5
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.9.117.145 //VIP地址
- }
- }
- virtual_server 192.9.117.145 80 {
- delay_loop 6
- lb_algo rr //lvs算法
- lb_kind DR
- persistence_timeout 60
- protocol TCP
- real_server 192.9.117.141 80 {
- weight 100
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- real_server 192.9.117.142 80 {
- weight 100
- TCP_CHECK {
- connect_timeout 10
- nb_get_retry 3
- delay_before_retry 3
- connect_port 80
- }
- }
- }
同理Lvs_backup也如上安装,并修改参数,Lvs+keepalived配置完毕后,启动keepalived, /etc/init.d/keepalived start 即可!然后在squid_1、squid_2分别执行realserver.sh脚本
二、Squid安装
1、给squid指定单独的缓存目录:
mkdir -p /data/squid &&chown -R squid:squid /data/squid/
2、设置squid日志路径:
mkdir -p /data/logs/squid/ && chown -R squid:squid /data/logs/squid/
3、安装squid
yum install squid -y 即可。
4、修改squid.conf配置文件,贴一个简单的squid部分配置文件
####http port 2012 05 20
http_port 0.0.0.0:80 vhost vport
cache_peer 192.9.117.143 parent 80 0 no-digest no-query originserver
cache_peer 192.9.117.144 parent 80 0 no-digest no-query originserver
cache_mem 2048 MB
maximum_object_size_in_memory 4096 KB
cache_dir aufs /data/squid 4000 16 512
coredump_dir /data/squid
启动squid /etc/init.d/squid start 即可!
配置完两台squid并启动。并且都可以看到lo:0接口ip地址为VIP地址:
三、Nginx安装
分别在192.9.117.143、192.9.117.144安装nginx,并配置好相应的测试页面,最后访问LVS-vip结果如下 http://192.9.117.145/
查看lvs转发状态