haproxy+keepalived搭建nginx+lamp集羣php
主機css |
Ip地址html |
軟件node |
haproxy主調度器mysql |
192.168.100.154linux |
keepalived-1.2.13.tar.gzios haproxy-1.4.24.tar.gznginx |
haproxy從調度器web |
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主要優勢:
1、免費開源,穩定性也是很是好,這個可經過我作的一些小項目能夠看出來,單Haproxy也跑得不錯,穩定性能夠與LVS相媲美;
2、根據官方文檔,HAProxy能夠跑滿10Gbps-New benchmark of HAProxyat 10 Gbps using Myricom's 10GbE NICs (Myri-10G PCI-Express),這個做爲軟件級負載均衡,也是比較驚人的;
3、HAProxy能夠做爲MySQL、郵件或其它的非web的負載均衡,咱們經常使用於它做爲MySQL(讀)負載均衡;
4、自帶強大的監控服務器狀態的頁面,實際環境中咱們結合Nagios進行郵件或短信報警,這個也是我很是喜歡它的緣由之一;
5、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配置