喜旎果 发表于 2018-12-29 14:08:35

keepalived高可用双主配置

  ###########################lb01
  cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived_lb01
  global_defs {
  #notification_email {
  #918391635@qq.com
  #}
  router_id LB01
  }
  vrrp_script check_lb {
  script "/server/scripts/check_lb.sh"
  interval 2
  weight 2
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.3/24 dev eth0 label eth0:1
  }
  track_script {
  check_lb
  }
  }
  vrrp_instance VI_2 {
  state MASTER
  interface eth0
  virtual_router_id 52
  priority 150
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.4/24 dev eth0 label eth0:2
  }
  }
  #######################lb02
  cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived_lb01
  global_defs {
  #notification_email {
  #918391635@qq.com
  #}
  router_id LB01
  }
  vrrp_script check_lb {
  script "/server/scripts/check_lb.sh"
  interval 2
  weight 2
  }
  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
  }
  track_script {
  check_lb
  }
  }
  vrrp_instance VI_2 {
  state BACKUP
  interface eth0
  virtual_router_id 52
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.4/24 dev eth0 label eth0:2
  }
  }
1.1 安装
  yuminsytall -y keepalived
1.2 启动
  /etc/init.d/keepalived start
1.3 查看运行状态
  /etc/init.d/keepalived status
1.4 修改keepalived配置文件【lb双主】
1.4.1 lb01
  vim /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived_lb01
  global_defs {
  #notification_email {
  #918391635@qq.com
  #}
  router_id LB01
  }
  vrrp_script check_lb {
  script "/server/scripts/check_lb.sh"
  interval 2
  weight 2
  }
  vrrp_instance VI_1 {
  state BACKUP
  interface eth0
  virtual_router_id 51
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.3/24 dev eth0 label eth0:1
  }
  track_script {
  check_lb
  }
  }
  vrrp_instance VI_2 {
  state MASTER
  interface eth0
  virtual_router_id 52
  priority 150
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.4/24 dev eth0 label eth0:2
  }
  }
1.4.2 lb02
  cat /etc/keepalived/keepalived.conf
  ! Configuration File for keepalived_lb01
  global_defs {
  #notification_email {
  #918391635@qq.com
  #}
  router_id LB02
  }
  vrrp_script check_lb {
  #/server/scripts/下的脚本 【当nginx停止时keeplived停止】
  #脚本内容
  cat check_lb.sh
  #!/bin/bash
  count=`ps -ef |grep ginx |wc -l`
  
  if [ $count -lt 2 ];then
         /etc/init.d/keepalived stop
     fi
  script "/server/scripts/check_lb.sh"
  interval 2
  weight 2
  }
  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
  }
  track_script {
  check_lb
  }
  }
  vrrp_instance VI_2 {
  state BACKUP
  interface eth0
  virtual_router_id 52
  priority 100
  advert_int 1
  authentication {
  auth_type PASS
  auth_pass 1111
  }
  virtual_ipaddress {
  10.0.0.4/24 dev eth0 label eth0:2
  }
  }
1.5 修改nginx配置文件【lb】
  vim /application/nginx/conf/nginx.conf
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  upstream web_pools {
  server 10.0.0.7:80 weight=1 max_fails=3 fail_timeout=30s;
  server 10.0.0.8:80 weight=1 max_fails=3 fail_timeout=30s;
  }
  server {
  listen       10.0.0.3:80;
  server_namewww.etiantian.org;
  location / {
  proxy_pass http://web_pools;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  server {
  listen       10.0.0.4:80;
  server_nameblog.etiantian.org;
  location / {
  proxy_pass http://web_pools;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $remote_addr;
  }
  }
  }
1.6 i查看vip
  ip a s eth0
1.7 修改内核参数能监听不属于本地的网卡
  echo 'net.ipv4.ip_nonlocal_bind = 1' >>/etc/sysctl.conf
  sysctl -p【配置生效】



页: [1]
查看完整版本: keepalived高可用双主配置