负载均衡与keepalived
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web01 www.oldboy.com
[root@lb01 ~]# curl 10.0.0.5
web02 www.oldboy.com
--->web01 PC 存放在web01
LB01 判断用户客户端类型 ----->
|
--->web02 移动端 web02
#web01
echo this is PC website >/app/www/lidao.html
#web02
echo this is Mobile website >/app/www/lidao.html
[root@lb01 ~]# curl 10.0.0.7/lidao.html
this is PC website
[root@lb01 ~]# curl 10.0.0.8/lidao.html
this is Mobile website
[root@lb01 ~]# curl 10.0.0.[7-8]/lidao.html
[1/2]: 10.0.0.7/lidao.html --> <stdout>
--_curl_--10.0.0.7/lidao.html
this is PC website
[2/2]: 10.0.0.8/lidao.html --> <stdout>
--_curl_--10.0.0.8/lidao.html
this is Mobile website
upstream default {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream mobile {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
if ($http_user_agent ~* "Android|IOS") {
proxy_pass http://mobile;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
[root@lb01 /etc/nginx]# curl 10.0.0.5/lidao.html
this is PC website
[root@lb01 /etc/nginx]# curl -A ios 10.0.0.5/lidao.html
this is Mobile website
根据用户的uri进行转发 动静分离
web01
mkdir -p /app/www/upload/
echo this is upload >/app/www/upload/guoav.html
#web02
mkdir -p /app/www/static/
echo this is static >/app/www/static/guoav.html
#web03
mkdir -p /app/www/
echo this is default >/app/www/guoav.html
#测试
[root@lb01 /etc/nginx]# curl 10.0.0.7/upload/guoav.html
this is upload
[root@lb01 /etc/nginx]# curl 10.0.0.8/static/guoav.html
this is static
[root@lb01 /etc/nginx]# curl 10.0.0.9/guoav.html
this is default
#修改lb负载均衡配置
nginx.conf核心配置
upstream upload {
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream static {
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
upstream default {
server 10.0.0.9:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location /static {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
lb01 lb02 web01 web02
lb01 lb02
yum install keepalived -y
[root@lb01 /etc/nginx]# cat 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 web_pools {
ip_hash;
server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=10s;
}
# include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name www.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://web_pools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
cookie session(会话)
共同点:
存放用户信息
key value类型 变量和变量内容
区别
cookie
存放在浏览器
存放钥匙
开发设置
响应服务器给你设置
session
存放在服务器 redis中
存放敏感
锁头
1)VRRP协议,全称Virtual Router Redundancy Protocol,中文名为虚拟路由冗余协议,VRRP的出现是为了解决静态路由的单点故障。
3)VRRP是用过IP多播的方式(默认多播地址(224.0.0.18))实现高可用对之间通信的。
4)工作时主节点发包,备节点接包,当备节点接收不到主节点发的数据包的时候,就启动接管程序接管主节点的资源。备节点可以有多个,通过优先级竞选,但一般Keepalived系统运维工作中都是一对。
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.3/24 dev eth0 label eth0:1
}
}