keepalived+ngnix

来源:互联网 发布:几种排序算法的比较 编辑:程序博客网 时间:2024/05/22 02:19

keepalived+nginx实验来实现负载均衡

keepalived实现高可用
ngnix实现负载均衡

实验环境 rhel6.5 selinux and iptables disabled
高可用虚拟机:172.25.12.1 server1
172.25.12.1 server1
负载均衡虚拟机: 172.25.12.1 server3
172.25.12.1 server3
在server1上
所需软件包
nginx-1.8.1.tar.gz
keepalived-1.2.20.tar.gz

nginx的安装与配置
tar zxf nginx-1.8.1.tar.gz
cd nginx-1.8.1
注释掉dubuge
vim auto/cc/gcc
178 # debug
179 #CFLAGS=”$CFLAGS -g”
./configure –prefix=/usr/local/nginx
解决依赖
yum install -y gcc
yum install -y pcre-devel

make && make install
修改配置文件,配置文件如下:

[root@server1 log]# cat  /usr/local/nginx/conf/nginx.confworker_processes  1;events {    worker_connections  1024;}http { upstream westos {server 172.25.12.3:80;server 172.25.12.4:80;    }    include       mime.types;    default_type  application/octet-stream;    sendfile        on;    keepalive_timeout  65;    server {        listen       80;        server_name  localhost;        location / {            root   html;            index  index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }    }    server {        listen 80;    server_name www.westos.org;    location / {        proxy_pass http://westos;    }    }     }

keepalived的安装与配置
tar zxf keepalived-1.2.20.tar.gz
cd keepalived-1.2.20
./configure –prefix=/usr/local/keepalived –with-init=SYSV
make && make install

ln -s /usr/local/keepalived/sbin/keepalived /sbin/ln -s /usr/local/keepalived/etc/keepalived/ /etc/ln -s /usr/local/keepalived/etc/sysconfig/keepalived   /etc/sysconfig/ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived   /etc/init.d/ chmod +x /usr/local/keepalived/etc/rc.d/init.d/keepalived

vim /etc/keepalived/keepalived.conf

[root@server1 opt]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedvrrp_script nginx_check {    script /opt/nginx_check.sh    interval 2    }global_defs {   notification_email {      root@localhost   }   notification_email_from keepalived@server4   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_instance VI_1 {    state MASTER    interface eth0    virtual_router_id 12    priority 100    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        172.25.12.200    }   track_script {nginx_check    }}

keepalived调用的脚本

[root@server1 log]# cat /opt/nginx_check.sh#!/bin/bashcurl http://127.0.0.1/index.html -o /dev/null -s || /usr/local/nginx/sbin/nginxif [ $? -ne 0  ];then/etc/init.d/keepalived stop &> /dev/nullfi

注意脚本要有执行权限

将安装好的ngnix和keepalived复制到另一个节点
cd /usr/local
scp -r nginx root@172.25.12.2:/usr/local
scp -r keepalived root@172.25.12.2:/usr/local
cd /opt/
scp nginx_check.sh root@172.25.12.2:/opt/

在server2 上查看 并修改keepalived的配置文件

[root@server2 opt]# ll nginx_check.sh-rwxr-xr-x 1 root root 160 Jul 30 15:35 nginx_check.sh[root@server2 opt]# cat /etc/keepalived/keepalived.conf! Configuration File for keepalivedvrrp_script nginx_check {    script /opt/nginx_check.sh    interval 2    }global_defs {   notification_email {      root@localhost   }   notification_email_from keepalived@server2   smtp_server 127.0.0.1   smtp_connect_timeout 30   router_id LVS_DEVEL}vrrp_instance VI_1 {    state BUCKUP    interface eth0    virtual_router_id 12    priority 50    advert_int 1    authentication {        auth_type PASS        auth_pass 1111    }    virtual_ipaddress {        172.25.12.200    }   track_script {nginx_check    }}

资源服务器:
server3:
[root@server3 ~]# yum install -y httpd
[root@server3 ~]# cd /var/www/html/
[root@server3 html]# ls
[root@server3 html]# echo server3 > index.html
[root@server3 html]# /etc/init.d/httpd start

server4:
[root@server4 ~]# yum install -y httpd
[root@server4 ~]# cd /var/www/html/
[root@server4 html]# ls
[root@server4 html]# echo server4-redhat > index.html
[root@server4 html]# /etc/init.d/httpd start

测试:
添加解析:
vim /etc/hosts
172.25.12.1 www.westos.org
测试:
[root@localhost day07]# curl www.westos.org
server3
[root@localhost day07]# curl www.westos.org
server4-redhat
在server1查看虚拟IP

[root@server1 nginx-1.8.1]# ip addr show eth01: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 52:54:00:48:ea:87 brd ff:ff:ff:ff:ff:ff    inet 172.25.12.1/24 brd 172.25.12.255 scope global eth0    inet 172.25.12.200/32 scope global eth0    inet6 fe80::5054:ff:fe48:ea87/64 scope link       valid_lft forever preferred_lft forever

在server2上查看虚拟IP

[root@server2 opt]# ip addr show eth01: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000    link/ether 52:54:00:6c:37:0b brd ff:ff:ff:ff:ff:ff    inet 172.25.12.2/24 brd 172.25.12.255 scope global eth0    inet6 fe80::5054:ff:fe6c:370b/64 scope link       valid_lft forever preferred_lft forever

然后将servrer1的keepalived关掉,虚拟IP转到了server2,即server2接管了服务,然后在将server1的keepalived打开,服务又回切了。