框架
keepavlive master 192.168.178.134
keepavlive backup 192.168.178.135
vip 192.168.178.132
nginx 192.168.178.134
nginx 192.168.178.135
tomcat 192.168.178.135 8080 8081
tomcat 192.168.178.135 8080 8081
keepavlive master安装
wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
tar -xf keepalived-1.1.20.tar.gz
ln -s /usr/src/kernels/2.6.32-431.el6.i686/ /usr/src/linux
yum install popt-devel
./configure && make && make install
cp /usr/local/etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
vim /etc/sysctl.conf
修改和添加下面参数
net.ipv4.ip_forward = 1(原先是0)
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
sysctl -p 加载文件
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected] #email 通知
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1 # 设置lvs的id,在一个网络内应该是唯一的
}
vrrp_sync_group test { #设置vrrp组
group {
loadbalance
}
}
vrrp_instance loadbalance {
state MASTER #设置lvs的状态,报错MASTER和BACKUP两种,必须大写
interface eth0 #设置对外服务的接口
lvs_sync_daemon_inteface eth0 #设置lvs监听的接口
virtual_router_id 195 #设置虚拟路由表示
priority 180 #设置优先级,数值越大,优先级越高
advert_int 5 #设置同步时间间隔
authentication { #设置验证类型和密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #设置lvs vip
192.168.178.132
}
}
virtual_server 192.168.178.132 {
delay_loop 6 #健康检查时间间隔
lb_algo rr #负载均衡调度算法
lb_kind DR #负载均衡转发规则
#persistence_timeout 20 #设置会话保持时间,对bbs等很有用
protocol TCP #协议
real_server 192.168.178.133 {
weight 3 #设置权重
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.178.134 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
keepavlive backup安装
keepalive的安装和master一样
配置文件
global_defs {
notification_email {
[email protected] #email 通知
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS1 # 设置lvs的id,在一个网络内应该是唯一的
}
vrrp_sync_group test { #设置vrrp组
group {
loadbalance
}
}
vrrp_instance loadbalance {
state MASTER #设置lvs的状态,报错MASTER和BACKUP两种,必须大写
interface eth0 #设置对外服务的接口
lvs_sync_daemon_inteface eth0 #设置lvs监听的接口
virtual_router_id 195 #设置虚拟路由表示
priority 180 #设置优先级,数值越大,优先级越高
advert_int 5 #设置同步时间间隔
authentication { #设置验证类型和密码
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #设置lvs vip
192.168.178.132
}
}
virtual_server 192.168.178.132 {
delay_loop 6 #健康检查时间间隔
lb_algo rr #负载均衡调度算法
lb_kind DR #负载均衡转发规则
#persistence_timeout 20 #设置会话保持时间,对bbs等很有用
protocol TCP #协议
real_server 192.168.178.133 {
weight 3 #设置权重
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.178.134 {
weight 3
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
nginx 配置
192.168.178.134/135 负载均衡
192.168.178.134 web01
192.168.178.123 web02
3台服务器上都安装相同的nginx版本
192.168.178.131 的配置加入
upstream test.miaohr.com { ——————####### test.miaohr.com这个必须和下面的 proxy_pass 一致 下面是weight模式负载
server 192.168.178.134:80 weight=80;
server 192.168.178.133:80 weight=10;
}
server {
listen 80;
server_name test.miaohr.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://test.miaohr.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
192.168.178.133 web01和web02 只需要配置域名主机就可以了
server {
listen 80;
server_name test.miaohr.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
2轮询模式
——————####### test.miaohr.com这个必须和下面的 proxy_pass 一致 下面是轮询 会1:1的去访问后端服务器
upstream test.miaohr.com {
server 192.168.178.129:80;
server 192.168.178.130:80;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream test.miaohr.com { ——————####### test.miaohr.com这个必须和下面的 proxy_pass 一致 下面是轮询 会1:1的去访问后端服务器
upstream test.miaohr.com {
ip_hash;
server 192.168.178.134 80;
server 192.168.178.130:80;
}
测试 绑定vhost 负载均衡ip 192.168.178.131 test.miaohr.com
在web01和web02下 test.miaohr.com 目录同时建一个test.html的文件不同内容
访问test.miaohr.com/test.html 页面页面信息不一样