本文共 6106 字,大约阅读时间需要 20 分钟。
-----client---------haproxy-------nginx1---------nginx2------
192.168.1.250 192.168.1.1 192.168.1.10 192.168.1.20一、安装Nginx[root@localhost ~]# yum -y install pcre-devel zlib-devel[root@localhost ~]# useradd -M -s /sbin/nologin nginx[root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/[root@localhost ~]# cd /usr/src/nginx-1.6.0/[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module –with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module[root@localhost nginx-1.6.0]# make && make install[root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/[root@localhost ~]# nginx -t[root@localhost ~]# nginx[root@localhost ~]# netstat -anpt | grep 80[root@localhost ~]# killall -s HUP nginx //重新加载[root@localhost ~]# killall -s QUIT nginx //关闭服务[root@localhost ~]# nginx验证:web-1:[root@localhost ~]#echo “welcome to 192.168.1.20 web server” > /usr/local/nginx/html/index.htmlweb-2:[root@localhost ~]#echo “welcome to 192.168.1.30 web server” > /usr/local/nginx/html/index.html[root@localhost ~]# firefox &二、安装haproxy1、安装[root@localhost ~]# yum -y install pcre-devel zlib-devel[root@localhost ~]# tar -zxvf haproxy-1.4.24.tar.gz -C /usr/src/[root@localhost ~]# cd /usr/src/haproxy-1.4.24/[root@localhost ~]# make TARGET=linux26 PREFIX=/usr/local/haproxy注意:linux26是指linux的内核版本号。[root@localhost ~]# make install PREFIX=/usr/local/haproxy2、配置haproxy[root@localhost ~]# mkdir /etc/haproxy[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.cfg /etc/haproxy/[root@localhost ~]# vim /etc/haproxy/haproxy.cfg修改:globallog 127.0.0.1 local0 //配置日志记录,local0为日志设备,默认存放到系统日志log 127.0.0.1 local1 notice //notice 为日志级别,通常有7个级别#log loghost local0 infomaxconn 4096 //默认最大连接数,需考虑ulimit-n限制:可增加ulimit-n 819200 #ulimit 的数量限制chroot /usr/share/haproxy //运行路径uid 99gid 99#debug#quietdefaultslog global //定义日志为global中的日志mode http //模式为httpoption httplog //采用http的日志格式option dontlognull //不记录健康检查日志信息retries 3 //三次连接失败就认为是服务器不可用,也可以通过后面设置#redispatchmaxconn 2000 //最大连接数contimeout 5000 //连接超时时间clitimeout 50000 //客户端超时时间srvtimeout 50000 //服务端超时时间listen statsmode httpbind :6677stats enablestats hide-versionstats uri /haproxyadmin?statsstats realm Haproxy\ Statisticsstats auth admin:adminstats admin if TRUElisten webcluster 0.0.0.0:80 //定义集群名、监听地址及端口option httpchk GET /index.html 注意:可以删除 //检查服务器的index.html文件balance roundrobin //负载均衡轮询算法server inst1 192.168.1.20:80 check inter 2000 fall 3 //在线节点server inst2 192.168.1.30:80 check inter 2000 fall 3 //最后加backup表示备份借点注意:如果启动时出现报错:/haproxy.main()] Cannot chroot(/usr/share/haproxy)则手动创建:[root@localhost ~]# mkdir /usr/share/haproxy如果启动时出现报错:Starting proxy cacti: cannot bind socket则执行:[root@localhost ~]# sysctl -e net.ipv4.ip_nonlocal_bind=13、启动haproxy[root@localhost ~]# ln -s /usr/local/haproxy/sbin/ /usr/sbin/ //注意软链接的目录[root@localhost ~]# cp /usr/src/haproxy-1.4.24/examples/haproxy.init /etc/init.d/haproxy[root@localhost ~]# chmod +x /etc/init.d/haproxy[root@localhost ~]# /etc/init.d/haproxy start[root@localhost ~]# /etc/init.d/haproxy status[root@localhost ~]# netstat -anp | grep haproxy //占用的也是TCP的80端口[root@localhost ~]# chkconfig --add haproxy[root@localhost ~]# chkconfig haproxy on 查看集群的状态4、验证:客户端输入:断开其中一个节点,再访问:5、设置haproxy日志[root@localhost ~]# vim /etc/haproxy/haproxy.cfg修改:log 127.0.0.1 local3 //设置haproxy日志级别为3[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf添加:$ModLoad imudp //加载模块$UDPServerRun 514 //接收udp的514端口发送过来的日志local3. /var/log/haproxy.log //定义haproxy日志文件[root@localhost ~]# vim /etc/sysconfig/rsyslog修改:SYSLOGD_OPTIONS="-c 2 -r -m 0" //允许远程写入[root@localhost ~]# /etc/init.d/rsyslog restart[root@localhost ~]# /etc/init.d/haproxy restart验证:[root@localhost ~]# tail -f /var/log/haproxy.log //查看日志不好使:6、设置haproxy日志[root@localhost ~]# vim /etc/haproxy/haproxy.cfg修改:注释(两行):#log 127.0.0.1 local0#log 127.0.0.1 local1 notice添加(两行):log /dev/log local0 info //访问信息log /dev/log local0 notice //启动信息[root@localhost ~]# /etc/init.d/haproxy stop[root@localhost ~]# /etc/init.d/haproxy start[root@localhost ~]# vim /etc/rsyslog.d/haproxy.conf添加:if ($programname == ‘haproxy’ and KaTeX parse error: Expected 'EOF', got '&' at position 72: …proxy-info.log&̲ ~if (programname == ‘haproxy’ and $syslogseverity-text == ‘notice’) then -/var/log/haproxy/haproxy-notice.log& ~三、验证:[root@localhost ~]# /etc/init.d/rsyslog restart客户端输入:查看:[root@localhost ~]# tail -f /var/log/haproxy/haproxy-info.log日志会记录客户端访问信息[root@localhost ~]# tail -f /var/log/haproxy/haproxy-notice.log日志会记录haproxy启动/停止信息haproxy+Keepalived编译安装keepalived[root@localhost keepalived-1.2.13]#./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/[root@localhost keepalived-1.2.13]# make && make install[root@localhost ~]#chkconfig --add keepalived[root@localhost ~]#chkconfig keepalived on[root@localhost ~]#cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak[root@localhost ~]#vim /etc/keepalived/keepalived.conf[root@localhost conf]# vim /etc/keepalived/keepalived.conf! Configuration File for keepalivedglobal_defs { notification_email { //通知电子邮件}vrrp_instance VI_1 { //VRRP热备state MASTER state BACKUP //热备状态master为主,backup为辅nopreempt //不抢占,master恢复后不会转移interface eth0 //承载VIP的物理接口virtual_router_id 51 //虚拟路由编号,每组一个priority 100 priority 55 //优先级,越大越优先advert_int 1 //心跳频率,单位秒authentication { //认证信息,每组内一致auth_type PASS //认证类型auth_pass 1111 //认证字符串}virtual_ipaddress { //漂移地址VIP。可以有多个192.168.56.10}notify_master “/etc/init.d/haproxy start” //成为MASTER之后执行的动作notify_backup “/etc/init.d/haproxy stop” //成为BACKUP之后执行的动作notify_fault “/etc/init.d/haproxy stop” //FAULT之后执行的动作}[root@localhost ~]#/etc/init.d/keepalived start[root@localhost ~]#ip addr showinet 192.168.56.201/24 brd 192.168.56.255 scope global eth0inet 192.168.56.10/32 scope global eth0[root@localhost ~]#netstat -anput | grep 80本文来自 sen918 的CSDN 博客 ,全文地址请点击:
转载于:https://blog.51cto.com/12910101/2286621