我是靠谱客的博主 不安乌冬面,最近开发中收集的这篇文章主要介绍haproxy+keepalived高可用搭建 实现vip漂移Haproxy高可用搭建,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Haproxy高可用搭建

准备工作

机器ip

VIP					   42.90.6.252
HA主  					42.90.6.84
HA从  					42.90.6.85
Keepalived主  	42.90.6.84
Keepalived从  	42.90.6.85

查看挂载情况

df -Th

如果内网环境,先挂载yum源

mkdir -p /mnt/cdrom
mount CentOS-7-x86_64-Everything-1708.iso /mnt/cdrom/

配置本地yum源

cd /etc/yum.repos.d/

vim /etc/yum.repos.d/cdrom.repo

[CDROM]
name=CentOS7.2 cdrom
baseurl=file:///mnt/cdrom/
enable=1
gpgcheck=0

安装haproxy+keepalived

yum clean all
yum makecache

yum -y install haproxy
yum -y install keepalived

配置haproxy

Ha主从配置相同即可(42.90.6.84,42.90.6.85)

vim /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     40000
    user        haproxy
    group       haproxy
    ulimit-n    65535
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  dontlognull
    option                  redispatch
    option                  forwardfor
    retries                 3
    timeout http-request    10s
    timeout queue           30s
    timeout connect         10s
    timeout client          30s
    timeout server          30s
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 40000
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------



frontend mycat_api
  bind *:8066
  mode tcp
  option tcplog
  default_backend mycat_api_server

backend mycat_api_server
  mode tcp
  balance roundrobin
  option tcplog
  server s1 42.90.6.79:8066 check
  server s2 42.90.6.30:8066 check
  server s3 42.90.6.78:8066 check

listen  stats
  mode  http
  bind *:8199
  stats  enable
  stats hide-version
  stats uri /haproxy-status
  stats realm HaproxyStatistics
  stats auth admin:admin
  stats admin if TRUE

配置keepalived

本文ha高可用是通过vip漂移实现(nginx同理)

主(42.90.6.84)

vim /etc/keepalived/keepalived.conf
! Copnfiguration File for keepalived
global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id inode2
}

vrrp_script chk_http_port {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}

vrrp_instance http1 {
    state MASTER
    interface bond0
    virtual_router_id 69
    priority 100
    advert_int 1
    nopreempt   
    authentication {
    auth_type PASS
    auth_pass 111111
    }
    virtual_ipaddress {
    		#配置vip
        42.90.6.252
    }
}

check_haproxy.sh

#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl start haproxy                                ###如果没有启动,则启动haproxy程序
fi
sleep 2                                                    ###睡眠两秒钟,等待haproxy完全启动
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl stop keepalived                              ###如果haproxy没有启动起来,则将keepalived停掉,则VIP自动漂移到另外一台haproxy机器,实现了对haproxy的高可用
fi

从(42.90.6.85)

vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
   notification_email {
    root@localhost
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id inode2
}

vrrp_script chk_http_port {
    script "/etc/keepalived/check_haproxy.sh"
    interval 2
    weight 2
}


vrrp_instance http1 {
    state BACKUP
    interface bond0
    virtual_router_id 69
    priority 70
    advert_int 1
    authentication {
    auth_type PASS
    auth_pass 111111
    }
    virtual_ipaddress {
        42.90.6.252
    }
}

check_haproxy.sh

#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl start haproxy                                ###如果没有启动,则启动haproxy程序
fi
sleep 2                                                    ###睡眠两秒钟,等待haproxy完全启动
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ]; then    ###判断haproxy是否已经启动
    systemctl stop keepalived                              ###如果haproxy没有启动起来,则将keepalived停掉,则VIP自动漂移到另外一台haproxy机器,实现了对haproxy的高可用
fi

最后

以上就是不安乌冬面为你收集整理的haproxy+keepalived高可用搭建 实现vip漂移Haproxy高可用搭建的全部内容,希望文章能够帮你解决haproxy+keepalived高可用搭建 实现vip漂移Haproxy高可用搭建所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部