前期准备:
1、实验拓扑图
2、地址规划
Master | 10.10.0.224(VIP:10.10.0.220) | keepalived、nginx |
Backup | 10.10.0.226(VIP:10.10.0.220) | keepalived、nginx |
Real Server 1 | 10.10.0.225 | httpd |
Real Server 2 | 10.10.0.221 | httpd |
一、安装keepalived和nginx
Master和Backup分别安装:
1、安装keepalived
[[email protected] ~]# yum install keepalived -y
2、配置repo源
[[email protected] ~]# vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1
3、安装ngxin
[[email protected] ~]# yum install nginx -y
4、Master修改
(1).修改keepaliaved配置文件
[[email protected] ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1
}
vrrp_script chk_nginx{
script "killall -0 nginx"
interval 1
weight -2
fall 2
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.0.220 dev eth0 label eth0:0
}
track_script {
chk_nginx
}
}
(3).拷贝一份配置文件到Backup服
[[email protected] ~]# scp -r /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
[[email protected] ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/
(4).开启服务并开机自动启动
[[email protected] ~]# service keepalived restart
[[email protected] ~]# service nginx restart
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig nginx on
5、Backup修改
(1).修改keepalived配置文件
[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS2 #修改唯一标识
}
vrrp_script chk_nginx{
script "killall -0 nginx"
interval 1
weight -2
fall 2
rise 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.10.0.220 dev eth0 label eth0:0
}
track_script {
chk_nginx
}
}
(2).开启服务并开机自动启动
[[email protected] ~]# service keepalived restart
[[email protected] ~]# service nginx restart
[[email protected] ~]# chkconfig keepalived on
[[email protected] ~]# chkconfig nginx on
6、Real Server 1
(1).安装httpd
[[email protected]]# yum install httpd -y
(2).建立测试页
[[email protected]]# vim /var/www/html/index.html
<h1>real server 1</h1>
(3).开启服务
[[email protected]]# service httpd restart
[[email protected]]# chkconfig httpd on
(4).测试
7、Real Server 2
(1).安装httpd
[[email protected]]# yum install httpd -y
(2).建立测试页
[[email protected]]# vim /var/www/html/index.html
<h1>real server 2</h1>
(3).开启服务
[[email protected]]# service httpd restart
[[email protected]]# chkconfig httpd on
(4).测试
8、nginx实现后端real server的负载均衡,在Master上配置
(1).修改nginx配置文件
[[email protected] ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[[email protected] ~]# vim /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream apacheweb {
server 10.10.0.225:80 max_fails=3 fail_timeout=2s;
server 10.10.0.221:80 max_fails=3 fail_timeout=2s;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
root /var/www/html; #此处定义后端服务器网页存放路径
proxy_pass http://apacheweb;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
(2).拷贝一份配置文件到Backup
[[email protected] ~]# scp -r /etc/nginx/nginx.conf [email protected]:/etc/nginx/
(3).重启nginx服务
[[email protected] ~]# service nginx restart
[[email protected] ~]# service nginx restart
9、测试
(1).在浏览器中输入vip地址
刷新浏览器之后
(2).模拟故障:
[[email protected]]# service httpd stop
停掉real server 1的apache服务,刷新浏览器发现访问的内容一直是real server2的内容
启用之后real server 1会被加入会话。反之停掉real server 2,访问的内容就一直是real server 1啦