环境:
web服务器:
web1:192.168.1.51
web2:192.168.1.52
负载均衡:
LB主:192.168.1.41
LB从;192.168.1.42
VIP:192.168.1.44
安装配置nginx负载均衡(主从都一样)
#tar zxf nginx-1.1.2.tar.gz
# ./configure --user=www --group=www --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_gzip_static_module--with-http_ssl_module
#make && make install
# vim /usr/local/nginx/conf/nginx.conf
#iptables -A INPUT -p vrrp -jACCEPT //开防火墙需要添加规则,否则备用机检测不到主机会认为主机 死掉而强制切换为主机
user www;
worker_processes 1;
#pid /data/www/logs/nginx.pid;
events {
useepoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
connection_pool_size 256;
client_max_body_size 20m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 60;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css aplication/xml;
gzip_vary on;
fastcgi_cache_path /usr/local/nginx/fastcgi_cachelevels=1:2 keys_zone=TEST:10m inactive=5m;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
upstreamlb.os.com {
# ip_hash;
server 192.168.1.51;
server 192.168.1.52;
# server 192.168.1.53 down; //暂不参与负载均衡
# server 192.168.1.54 max_fails=3 fail_timeout=20s; //连接失败次数3,超时20s.
}
server{
listen 80;
server_name lb.os.com;
location / {
proxy_next_upstream http_502 http_504 error timeoutinvalid_header;
proxy_pass http://lb.os.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format www '$remote_addr -$remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for""$upstream_addr"';
access_log /data/www/access.log www;
error_log /data/www/error.log;
}
}
安装keepalived
#tar -zxvf keepalived-1.2.7.tar.gz
#./configure --prefix=/usr/local/keepalived
#make && make install
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
# cp /usr/local/keepalived/etc/sysconfig/keepalived/etc/sysconfig/
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived/etc/init.d/
#mkdir /etc/keepalived
vim /etc/keepalived/keepalived.conf
主服务器配置:
vim /etc/keepalived/keepalived.conf ,键入以下内容
global_defs {
notification_email {
admin@company.com (这里可以定义多个报警邮箱)
}
notification_email_from alarm@company.com (报警人)
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/sh/check_nginx.sh"
interval2 (检测的间隔)
weight 2
}
vrrp_instance VI_1 {
stateMASTER (显示定义为主服务器)
interface eth0 (绑定的网口,该网口即上面提到的两个IP的接口)
virtual_router_id51 (定义的ID,官方的是 51,主从服务器必须一直)
mcast_src_ip 192.168.1.41 (主服务器的IP)
priority100 (优先级,任意定义,但是一定要比从服务器高)
advert_int 1
authentication {
auth_typePASS
auth_pass1111 (默认即可)
}
track_script {
chk_http_port (调用检测脚本)
}
virtual_ipaddress {
192.168.1.44 (绑定的虚IP)
}
}
从服务器配置
vim /etc/keepalived/keepalived.conf ,键入以下内容
global_defs {
notification_email {
admin@company.com (这里可以定义多个报警邮箱)
}
notification_email_from alarm@company.com (报警人)
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/opt/sh/check_nginx.sh"
interval2 (检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
stateBACKUP (显示定义为从服务器)
interface eth0 (绑定的网口,该网口即上面提到的两个IP的接口)
virtual_router_id51 (定义的ID,官方的是 51,主从服务器必须一直)
mcast_src_ip 192.168.1.42 (从服务器的IP)
priority50 (优先级,任意定义,但是一定要比主服务器低)
advert_int 1
authentication {
auth_typePASS
auth_pass1111 (默认即可)
}
track_script {
chk_http_port (调用检测脚本)
}
virtual_ipaddress {
192.168.1.44 (绑定的虚IP)
}
}
#vim /opt/sh/check_nginx.sh
#!/bin/bash
nginxpid=`ps -C nginx --no-header | wc -l`
if [ $nginxpid -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 3
nginxpid=`ps -C nginx --no-header | wc -l`
echo $nginxpid
if [ $nginxpid -eq 0 ];then
/etc/init.d/keepalived stop
fi
fi
开启keepalived服务
#/etc/init.d/keepalived start
查看网卡状态
#ip a
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether00:0c:29:3c:7e:a7 brd ff:ff:ff:ff:ff:ff
inet192.168.1.41/24 brd 192.168.1.255 scope global eth0
inet192.168.1.44/32 scope global eth0
版权声明:本文为博主原创文章,未经博主允许不得转载。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com