haproxy+keepalived搭建web群集

一、实验环境

wKiom1mb-TLSCCT1AAFMpasu4gE254.png二、实验步骤

haproxy+keepalived 1配置

IP192.168.1.10

#yum   -y  install pcre-devel  zlib-devel bzip2-devel kernel-devel  openssl-devel popt-devel  zlib-devel

#tar zxf  keepalived-1.2.13.tar.gz         解压源码包

#cd keepalived-1.2.13

#./configure --prefix=/    --with-kernel-dir=/usr/src/kernels/2.6.32-431.e16.x86_64

#make  && make install

#chkconfig  --add keepalived             添加为系统服务

#chkconfig  keepalived  on           设置为开机自启动

#cd /etc/keeplived/

#cp  keepalived.conf     keepalived.conf.bak 备份一份以防万一

#vim keepalived.conf                编辑主配置文件

wKiom1mb-Vyj5wd3AAB7DoP9fK8945.png

#service keepalived   start      启动keepalved服务  

#ip addr  show  eth0            查看漂移地址

wKioL1mb-WOAguTfAABzxB0KP0M153.png

配置haproxy  1

#tar  zxf  haproxy-1.4.24.tar.gz

#cd  haproxy-1.4.24

#make TARGET=linux26                    64位系统

#make install

#mkdir  /etc/haproxy                   创建配置文件目录

#cp  examples/haproxy.cfg   /etc/haproxy/   haproxy.cfg文件复制到配置文件目录

#vim  /etc/haproxy/haproxy.cfg

wKiom1mb-Z6wi-cqAAChLoCdufI095.png

wKiom1mb-azjZmSyAABHCZnnTjg002.png#cp /usr/src/haproxy-1.4.24/examples/haproxy.init  /etc/init.d/haproxy

#ln   -s  /usr/local/sbin/haproxy  /usr/sbin/haproxy

#chmod   +x  /etc/init.d/haproxy

#service haproxy   start

#service  iptables  stop

haproxy+keepalived  2配置

IP192.168.1.20

#yum   -y  install pcre-devel  zlib-devel bzip2-devel kernel-devel  openssl-devel popt-devel  zlib-devel

#tar zxf  keepalived-1.2.13.tar.gz

#cd keepalived-1.2.13

#./configure  --prefix=/    --with-kernel-dir=/usr/src/kernels/2.6.32-431.e16.x86_64

#make  && make install

#chkconfig  --add keepalived

#chkconfig  keepalived  on

#scp  root@192.168.1.10:/etc/keepalived/keepalived.conf  /etc/keepalived/

#scp root@192.168.1.10:/etc/keepalived/keepalived.conf.bak      /etc/keepalived/          远程复制一份主的配置

#vim /etc/keepalived/keepalived.conf    做简单修改即可

wKioL1mb-c3Qgny7AABcjSHKlRw000.png

#service keepalived   restart

#ip  addr  show   dev  eth0

wKiom1mb-eGhKQuuAABiu162kts372.png配置haproxy  2

#tar  zxf  haproxy-1.4.24.tar.gz        使用源码编译安装

#cd  haproxy-1.4.24

#make TARGET=linux26                     64位系统

#make install

#mkdir  /etc/haproxy                   创建配置文件目录

#cp  examples/haproxy.cfg   /etc/haproxy/   haproxy.cfg文件复制到配置文件目录

#scp root@192.168.1.10:/etc/haproxy/haproxy.cfg  /etc/haproxy/haproxy/

从主复制到从一份此处不用修改

#cp /usr/src/haproxy-1.4.24/examples/haproxy.init  /etc/init.d/haproxy

#ln   -s  /usr/local/sbin/haproxy  /usr/sbin/haproxy

#chmod   +x  /etc/init.d/haproxy      给一个执行权限

#service  haproxy  start    

#service  iptables  stop

WEB 1配置

IPvmnet 1:192.168.1.30

vmnet 2:192.168.2.20

#yum  -y install  pcre-devel zlib-devel  rpcbind nfs-utils

#chkconfig  rpcbind   on

# chkconfig  nfs   on

#service  rpcbind start

#service nfs  start

#useradd  -M  -s  /sbin/nologin nginx

#tar  zxf   nginx-1.6.2.tar.gz

#cd nginx-1.6.2

#./configure   --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

#echo 11111111 > /usr/local/nginx/html/index.html

#/usr/local/nginx/sbin/nginx

#netstat  -anpt | grep nginx

wKioL1mb-jPyffF6AAASNpujBVc491.pngWEB 2配置

IPvmnet 1:192.168.1.40

vmnet 2:192.168.2.10

#yum  -y install  pcre-devel zlib-devel  rpcbind nfs-utils

#chkconfig  rpcbind   on

# chkconfig  nfs   on

#service  rpcbind start

#service nfs  start

#useradd  -M  -s  /sbin/nologin nginx

#tar  zxf   nginx-1.6.2.tar.gz

#cd nginx-1.6.2

#./configure   --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install

#echo 11111111 > /usr/local/nginx/html/index.html

#/usr/local/nginx/sbin/nginx

#netstat  -anpt | grep nginx

wKiom1mb-kjjbPkCAAASNpujBVc795.png客户机windows 7 访问http//192.168.1.254

wKioL1mb-l-Bbz6FAABOPFZTpbA957.png

wKiom1mb-oHDMwleAABgEHbjitE752.png

haproxy+keepalived 1宕掉

客户机测试访问http://192.168.1.254

wKiom1mb-qjh7yPBAABQC8S4VWM423.png

wKiom1mb-rODWB1NAABjP8rDgL0619.png

NFS共享存储配置

IPvmnet 2192.168.2.30

#yum  -y  install  rpcbind  nfs-utils

#chkconfig  rpcbind   on

# chkconfig  nfs   on

#mkdir  -p  /opt/wwwroot

#vim /etc/exports

/opt/wwwroot   192.168.2.0/24(rw,sync,no_root_squash)

#service  rpcbind  start

#service  nfs  start

#echo   www.itdby.com  > /opt/wwwroot/index.html

各节点服务器

mount  192.168.2.30:/opt/wwwroot  /usr/local/nginx/html/

客户机测试访问http://192.168.1.254

wKiom1mb-sHTRcUgAAAeUUnlxUk750.png