linux系统keepalived介绍以及和nginx联合使用

keepalived

      • keepalived介绍
      • nginx+keepalived

keepalived介绍

keepalived是集群管理中保证集群高可用的一个服务软件,用来防止单点故障。

keepalived是以VRRP协议为实现基础的,VRRP全称Virtual Router Redundancy Protocol,即虚拟路由冗余协议。

虚拟路由冗余协议,可以认为是实现路由器高可用的协议,即将N台提供相同功能的路由器组成一个路由器组,这个组里面有一个master和多个backup,master上面有一个对外提供服务的vip(该路由器所在局域网内其他机器的默认路由为该vip),master会发组播,当backup收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master。这样的话就可以保证路由器的高可用了。

keepalived主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。

nginx+keepalived

两台服务器做nginx负载均衡,均衡后台的两台服务器
nginx负载均衡的服务器设置nginx的配置文件
vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;
    upstream backend {
    server 后端提供web服务的ip:80 weight=1 max_fails=3 fail_timeout=20s;
    server 后端提供web服务的ip:80 weight=1 max_fails=3 fail_timeout=20s;
    }
    server {
        listen       80;
        server_name  localhost;
        location / {
        proxy_pass http://backend;
        proxy_set_header Host $host:$proxy_port;
        proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}


做nginx负载均衡的服务器配置keepalived高可用
yum install -y keepalived

master端
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   router_id directory1   #辅助改为directory2
}
vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx_status.sh"       #脚本位置
   interval 5                                           #间隔时间
}

vrrp_instance VI_1 {
    state MASTER        #定义主还是备
    interface ens33     #VIP绑定接口
    virtual_router_id 80  #整个集群的调度器一致
    priority 100         #back改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.147.100/24   # vip
    }
    track_script {
        check_nginx
    }
}



backup端
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   router_id directory2
}
vrrp_script check_nginx {
   script "/etc/keepalived/check_nginx_status.sh"       //脚本位置
   interval 5                                           //间隔时间
}

vrrp_instance VI_1 {
    state BACKUP    #设置为backup
    interface ens33
    nopreempt        #设置到back上面,不抢占资源
    virtual_router_id 80
    priority 50   #辅助改为50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.147.100/24
    }
    track_script {
        check_nginx
    }
}

启动负载均衡的keepalived
systemctl start keepalived

编写脚本对负载均衡器Nginx健康检查
vim /etc/keepalived/check_nginx_status.sh
if [ $? -ne 0 ];then										    
#	/etc/init.d/keepalived stop
	systemctl stop keepalived
fi					

chmod a+x /etc/keepalived/check_nginx_status.sh


注:必须先启动nginx,再启动keepalived

你可能感兴趣的:(linux,linux,nginx,运维)