首页 > 代码库 > haproxy+keepalived搭建nginx+lamp集群
haproxy+keepalived搭建nginx+lamp集群
haproxy+keepalived搭建nginx+lamp集群
实验拓扑:
实验环境:
主机 | Ip地址 | 软件 |
haproxy主调度器 | 192.168.100.154 | keepalived-1.2.13.tar.gz haproxy-1.4.24.tar.gz |
haproxy从调度器 | 192.168.100.155 | keepalived-1.2.13.tar.gz haproxy-1.4.24.tar.gz |
Nginx1 | 192.168.100.152 | nginx-1.6.2.tar.gz |
Nginx2 | 192.168.100.153 | nginx-1.6.2.tar.gz |
Lamp1 | 192.168.100.150 | httpd-2.2.17.tar.gz cmake-2.8.6.tar.gz mysql-5.5.22.tar.gz libmcrypt-2.5.8.tar.gz mhash-0.9.9.9.tar.gz mcrypt-2.6.8.tar.gz php-5.3.28.tar.gz |
Lamp2 | 192.168.100.151 | httpd-2.2.17.tar.gz cmake-2.8.6.tar.gz mysql-5.5.22.tar.gz libmcrypt-2.5.8.tar.gz mhash-0.9.9.9.tar.gz mcrypt-2.6.8.tar.gz php-5.3.28.tar.gz |
实验原理:
常用开源软件负载均衡器有:Nginx、LVS、Haproxy。
三大主流软件负载均衡器对比(LVS VS Nginx VS Haproxy)
|
衡量负载均衡器好坏的几个重要因素:
1、会话率 :单位时间内的处理的请求数
2、会话并发能力:并发处理能力
3、数据率:处理数据能力
经过官方测试统计,haproxy 单位时间处理的最大请求数为20000个,可以同时维护40000-50000个并发连接,最大数据处理能力为10Gbps。综合上述,haproxy是性能优越的负载均衡、反向代理服务器。
总结HAProxy主要优点:
一、免费开源,稳定性也是非常好,这个可通过我做的一些小项目可以看出来,单Haproxy也跑得不错,稳定性可以与LVS相媲美;
二、根据官方文档,HAProxy可以跑满10Gbps-New benchmark of HAProxyat 10 Gbps using Myricom‘s 10GbE NICs (Myri-10G PCI-Express),这个作为软件级负载均衡,也是比较惊人的;
三、HAProxy可以作为MySQL、邮件或其它的非web的负载均衡,我们常用于它作为MySQL(读)负载均衡;
四、自带强大的监控服务器状态的页面,实际环境中我们结合Nagios进行邮件或短信报警,这个也是我非常喜欢它的原因之一;
五、HAProxy支持虚拟主机。
重点难点:
1.注意在haproxy主从调度器中的配置文件中,分别根据acl来指定不同页面分发到不同的web站点;
实验步骤:
1.部署lamp1和lamp2:192.168.100.150-151
wgetftp://ftp.linuxfan.cn/tools/lamp_install_publis-app-2015-07-16.tar.xz
tar Jxvflamp_install_publis-app-2015-07-16.tar.xz
cd bin/
./apache_install.sh&&mysql_install.sh &&php_install.sh 脚本展示在文档最后
./php_config.sh &&mysql_config.sh&&lamp_config.sh
/etc/init.d/mysqld start
mysql -uroot -p123123
/etc/init.d/mysqld stop
ln -s /usr/local/httpd/bin/*/usr/local/bin/ ##优化执行命令的路径
cp /usr/local/httpd/bin/apachectl/etc/init.d/httpd
vim /etc/init.d/httpd ##在开始位置修改bash和添加chkconfig和description;修改第82行实现执行命令时友好提示
1 #!/bin/bash ##声明shell为bash
2 # chkconfig: 35 85 15 ##在3和5运行级别开机启动,开机启动顺序为85,关机关闭顺序为15
3 # description: A Scripts for apache httpddeamon!
82 $HTTPD -k $ARGV &&echo "httpd is $ARGVcomplete." ##第82行
:wq
ls -l /etc/init.d/httpd ##确认文件有执行权限,如果没有使用命令“chmod+x /etc/init.d/httpd”授权
chkconfig --add httpd
chkconfig httpd on
2.部署nginx1和nginx2:192.168.100.152-153
lftp ftp.linuxfan.cn
cd tools/
get nginx-1.6.2.tar.gz
bye
[root@www ~]# yum install pcre-develzlib-devel
安装nginx:
[root@www ~]# useradd -M -s /sbin/nologinnginx
[root@www ~]# tar zxvf nginx-1.6.2.tar.gz-C /usr/src/
[root@www ~]# cd /usr/src/nginx-1.6.2/
[root@www nginx-1.6.2]# ./configure--prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module
[root@www nginx-1.6.2]# make &&makeinstall
[root@www nginx-1.6.2]# ls/usr/local/nginx/ ##验证安装
conf html logs sbin
[root@www ~]# ln -s/usr/local/nginx/sbin/nginx /usr/local/sbin/ ##优化执行路径
启动nginx:
[root@www ~]# nginx ##启动
[root@www ~]# netstat -utpln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8311/nginx
添加nginx为系统服务:
[root@www ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Server Control Script
NP="/usr/local/nginx/sbin/nginx"
NPF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$NP;
if [ $? -eq 0 ]
then
echo "nginx is starting!! "
fi
;;
stop)
kill -s QUIT $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx is stopping!! "
fi
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $NPF)
if [ $? -eq 0 ]
then
echo "nginx config file is reload! "
fi
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@www ~]# chmod +x /etc/init.d/nginx
[root@www ~]# chkconfig --add nginx
[root@www ~]# chkconfig nginx on
[root@www ~]# /etc/init.d/nginx start
nginx is starting!!
3.部署nginx2的html测试网页:192.168.100.153
[root@www ~]# cat/usr/local/nginx/html/index.html
192.168.100.153
部署nginx1的html测试网页:192.168.100.152
[root@www ~]# cat/usr/local/nginx/html/index.html
192.168.100.152
4.部署lamp2的php测试页面:192.168.100.151
cat /usr/local/httpd/htdocs/index.php
<?php
session_start();
$_SESSION[‘time‘]=date("Y:m:d:H:s",time());
echo "本次访问时间"."<fontcolor=red>".$_SESSION[‘time‘]."</font>"."<br>";
echo "访问的服务器地址是"."<fontcolor=red>".$_SERVER[‘SERVER_ADDR‘]."</font>"."<br>";
echo "访问的服务器域名是"."<fontcolor=red>".$_SERVER[‘SERVER_NAME‘]."</font>"."<br>";
echo "SESSIONNAME是"."<fontcolor=red>".session_name()."</font>"."<br>";
echo "SESSIONID是"."<fontcolor=red>".session_id()."</font>"."<br>";
?>
5.部署lamp1的php测试页面:192.168.100.150
cat /usr/local/httpd/htdocs/index.php
<?php
session_start();
$_SESSION[‘time‘]=date("Y:m:d:H:s",time());
echo "本次访问时间"."<fontcolor=red>".$_SESSION[‘time‘]."</font>"."<br>";
echo "访问的服务器地址是"."<fontcolor=red>".$_SERVER[‘SERVER_ADDR‘]."</font>"."<br>";
echo "访问的服务器域名是"."<fontcolor=red>".$_SERVER[‘SERVER_NAME‘]."</font>"."<br>";
echo "SESSIONNAME是"."<fontcolor=red>".session_name()."</font>"."<br>";
echo "SESSIONID是"."<fontcolor=red>".session_id()."</font>"."<br>";
?>
6.HAproxy调度器:192.168.100.154-155
yum -y install pcre-devel bzip2-devel
wget ftp://ftp.linuxfan.cn/tools/haproxy-1.4.24.tar.gz
tar zxvf haproxy-1.4.24.tar.gz -C /usr/src/
cd /usr/src/haproxy-1.4.24/
make TARGET=linux26
make install 编译安装haproxy
mkdir /etc/haproxy
cd examples/
cp haproxy.cfg /etc/haproxy/ 复制样例配置文件
vi /etc/haproxy/haproxy.cfg
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 4096
uid 99
gid 99
daemon
#debug
#quiet
defaults
log global
mode http
option httplog
option dontlognull
retries 3
maxconn 2000
contimeout 5000
clitimeout 50000
srvtimeout 50000
frontend http ##定义名称为http
bind *:80 ##指定监听地址
acl linuxfan hdr_end(host) -i www.linuxfan.cn ###定义acl名称为linuxfan:访问www.linuxfan.cn这个域名
acl static path_end -i .html .css .js .png .jpg .jpeg .gif .ico .swf.xml .txt .pdf ##定义acl名称为static:访问url为以上后缀的页面
use_backend jingtai if static or linuxfan ##定义使用banckend:符合linuxfan和static两条acl的请求使用backend jingtai
default_backend dongtai ##默认的请求使用backenddongtai
backend jingtai ##定义backend :jingtai
mode http ##定义模式
balance roundrobin ##定义调度算法为轮询
server jingtai01 192.168.100.152:80 check inter 2000 fall 3 ##定义节点
server jingtai01 192.168.100.153:80 check inter 2000 fall 3
backend dongtai
mode http
balance roundrobin
server dongtai01 192.168.100.150:80 check inter 2000 fall 3
server dongtai01 192.168.100.151:80 check inter 2000 fall 3
:wq
cp/usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
ln -s /usr/local/sbin/haproxy /usr/sbin/
/etc/init.d/haproxy restart
chkconfig --add haproxy
chkconfig haproxy on
7.配置主调度器的keepalived:192.168.100.154
yum -y install kernel-devel openssl-develpopt-devel ipvsadm
wgetftp.linuxfan.cn:/tools/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz -C/usr/src/
cd /usr/src/keepalived-1.2.13/
./configure --prefix=/--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
make &&make install 安装keepalived
chkconfig --add keepalived
chkconfig keepalived on
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak 备份配置文件
vi /etc/keepalived/keepalived.conf
global_defs {
router_id HA_TEST_R1 ##本服务器的名称,若环境中有多个 keepalived时,此名称不能一致
}
vrrp_instance VI_1 { ##定义VRRP热备实例,每一个keep组都不同
state MASTER ##MASTER表示主服务器
interface eth0 ##承载VIP地址的物理接口
virtual_router_id 1 ##虚拟路由器的ID号,每一个keep组都不同
priority 100 ##优先级,数值越大优先级越高
advert_int 1 ##通告间隔秒数(心跳频率)
authentication { ##认证信息
auth_type PASS ##认证类型
auth_pass 123456 ##密码字串
}
virtual_ipaddress {
192.168.100.95 ##指定漂移地址(VIP)
}
}
:wq
/etc/init.d/keepalived start
ip a
8.配置从服务器的keepalived:192.168.100.155
yum -y install kernel-devel openssl-develpopt-devel ipvsadm
wgetftp.linuxfan.cn:/tools/keepalived-1.2.13.tar.gz
tar -zxvf keepalived-1.2.13.tar.gz -C/usr/src/
cd /usr/src/keepalived-1.2.13/
./configure --prefix=/--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/
make &&make install
chkconfig --add keepalived
chkconfig keepalived on
cd /etc/keepalived/
mv keepalived.conf keepalived.conf.bak
vi /etc/keepalived/keepalived.conf
global_defs {
router_id HA_TEST_R2 ##本服务器的名称
}
vrrp_instance VI_1 {
state BACKUP ##SLAVE表示从服务器
interface eth0
virtual_router_id 1
priority 99 ##优先级,低于主服务器
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.100.95
}
}
:wq
/etc/init.d/keepalived start
9.客户端访问测试:
脚本展示:
[root@www bin]# cat apache_install.sh
#!/bin/bash
#by linuxfan
rpm -e httpd httpd-manual --nodeps
ls /root/httpd*
if [ $? -eq 0 ];then
tar zxvf /root/httpd-2.2.17.tar.gz -C/usr/src/
cd /usr/src/httpd-2.2.17/
./configure --prefix=/usr/local/httpd--enable-rewrite --enable-so --disable-access 1>/dev/null
make &&make install
fi
[root@www bin]# cat mysql_install.sh
#!/bin/bash
##第一配置yum,安装ncurses依赖包
yum -y install ncurses-*
#解压cmake,安装基础环境
tar zxvf /root/cmake-2.8.6.tar.gz -C/usr/src/
cd /usr/src/cmake-2.8.6
#配置,编译安装cmake
./configure &&gmake &&gmakeinstall
##解压mysql
tar zxvf /root/mysql-5.5.22.tar.gz -C/usr/src/
cd /usr/src/mysql-5.5.22/
#cmake进行配置mysql
cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql #指定安装目录\
-DDEFAULT_CHARSET=utf8 #指定字符集为utf8 \
-DDEFAULT_COLLATION=utf8_general_ci ##指定字符校验 \
-DWITH_EXTRA_CHARSETS=all ##支持额外字符集\
-DSYSCONFDIR=/etc/ ##指定配置文件位置
make &&make install #编译安装
if [ -e /usr/local/mysql ];then
echo "mysql installsuccessfully."
fi
[root@www bin]#cat php_install.sh
#!/bin/bash
##by linuxfan20150611
#1.卸载已经安装rpm包
rpm -qa |grep php
if [ $? -eq 0];then
rpm -e phpphp-mysql --nodeps
fi
#2.安装mcrypt支持,安装的顺序必须libmcrypt-->mhash-->mcrypt,每安装都必须ln链接到系统库中,echo"/usr/local/lib/" >>/etc/ld.conf&&ldconfig
##########installmcrypt###########
tar zxvf/root/libmcrypt-2.5.8.tar.gz -C /usr/src/
cd/usr/src/libmcrypt-2.5.8/
./configure&&make &&make install
ln -s/usr/local/lib/libmcrypt.* /usr/lib
tar zxvf/root/mhash-0.9.9.9.tar.gz -C /usr/src/
cd/usr/src/mhash-0.9.9.9/
./configure&&make &&make install
ln -s/usr/local/lib/libmhash* /usr/lib/
tar zxvf/root/mcrypt-2.6.8.tar.gz -C /usr/src/
cd/usr/src/mcrypt-2.6.8/
./configure&&make &&make install
#3.安装php
##############installphp #############
yum -y installlibxml2-* zlib-*
PHV=php-5.3.28
tar zxvf/root/$PHV.tar.gz -C /usr/src/
cd /usr/src/$PHV/
./configure--prefix=/usr/local/php5 --with-mcrypt --with-apxs2=/usr/local/httpd/bin/apxs--with-mysql=/usr/local/mysql/ \
--with-config-file-path=/usr/local/php5--enable-mbstring &&make &&make install
if [ -e/usr/local/php5 ]
then
echo "phpinstall success."
fi
[root@www bin]#cat mysql_config.sh
#!/bin/bash
#1.复制配置文件
cp/usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf
#2.添加系统服务
cp/usr/src/mysql-5.5.22/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --addmysqld
chkconfigmysqld on
#3.优化PATH路径,执行命令时方便,单引号双引号都行
grep mysql/etc/profile
if [ $? -eq 0];then
echo "PATHis set."
else
echo "exportPATH=$PATH:/usr/local/mysql/bin" >>/etc/profile
source/etc/profile ##执行文件
fi
#4.初始化mysql,创建用户,赋权
useradd -M -s/sbin/nologin mysql
chown -Rmysql:mysql /usr/local/mysql
/usr/local/mysql/scripts/mysql_install_db \
--basedir=/usr/local/mysql\
--datadir=/usr/local/mysql/data--user=mysql
#5.启动mysql,并设置为开机启动
if [ -e/tmp/mysql.sock ];then
/etc/init.d/mysqldrestart
else
/etc/init.d/mysqldstart
fi
chkconfig mysqldon
#6.修改密码,并提示密码
mysqladmin -uroot password ‘123123‘ &&echo"mysql root password is 123123"
[root@www bin]#cat php_config.sh
#!/bin/bash
##by linuxfan
##############configphp############
PHV=php-5.3.28
cp/usr/src/$PHV/php.ini-development /usr/local/php5/php.ini
#修改配置项支持php标记<?php?>
sed -i‘s/short_open_tag = Off/short_open_tag = On/g‘ /usr/local/php5/php.ini
##设置默认字符集utf8
echo"default_charset = "utf8" " >>/usr/local/php5/php.ini
###########addmodule zend############
ZDV=ZendGuardLoader-php-5.3-linux-glibc23-x86_64
tar zxvf/root/$ZDV.tar.gz -C /root/
cp -rf/root/$ZDV/php-5.3.x/ZendGuardLoader.so /usr/local/php5/lib/php/
cat <<END>>/usr/local/php5/php.ini
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_enable=1
END
[root@www bin]#cat lamp_config.sh
#!/bin/bash
##by scfa2015-06-30
#################Setting apache with php ################
#1.修改apache的配置文件
APACHE_C=/usr/local/httpd/conf/httpd.conf
##添加ServerName设置FQDN
sed -i‘/^#ServerName/a ServerName www.linuxfan.cn‘ $APACHE_C
##在第310行后一行添加php应用类型的支持
sed -i ‘310a\ AddType application/x-httpd-php.php‘ $APACHE_C
##修改默认首页,支持index.php
sed -i‘s/DirectoryIndex index.html/DirectoryIndex index.html index.php/g‘ $APACHE_C
netstat -uptln|grep 80 &>/dev/null
if [ $? -eq 0 ]
then
/usr/local/httpd/bin/apachectl stop &&/usr/local/httpd/bin/apachectl start
netstat -uptln |grep 80
echo "apache restart successful"
else
/usr/local/httpd/bin/apachectlstart
netstat -utpln |grep 80
fi
#2.mysql的配置
if [ -e/tmp/mysql.sock ];then
echo "mysqlis running."
else
/etc/init.d/mysqldstart
fi
拓展:
haproxy的配置文件详解:
http://blog.csdn.net/tantexian/article/details/50056199 负载均衡haproxy配置
本文出自 “11628832” 博客,请务必保留此出处http://11638832.blog.51cto.com/11628832/1879305
haproxy+keepalived搭建nginx+lamp集群