keepalived+haproxy高可用

发布时间:2020-07-04 15:44:23 作者:无双战绩
来源:网络 阅读:697
环境:
主Haproxy服务器    192.168.80.100   keepalived+Haproxy
备Haproxy服务器    192.168.80.101   keepalived+Haproxy
web服务器1             192.168.80.102  httpd(nginx、tomcat)
web服务器2             192.168.80.103  httpd
80.100和80.101需要联网

———————————————————————————————————————
在80.100虚拟机上:

systemctl stop firewalld        //关闭防火墙
setenforce 0                 //关闭监控
cd /etc/yum.repos.d/    //进入目录
cp back/* ./         //把back里内容移动到上一个目录
ls               //查看目录内容

keepalived+haproxy高可用

yum install -y epel-release
yum install ntp -y           //安装ntp
ntpdate time1.aliyun.com        //同步网络时间
date

keepalived+haproxy高可用

yum install keepalived -y         //安装keepalived
cd       //回到根目录
vi /etc/keepalived/keepalived.conf`
删除所有内容并添加以下内容:
! Configuration File for keepalived
global_defs {
        route_id haproxy-01
   }
vrrp_script haproxy {    定义一个热备脚本取名为haproxy
        script "/opt/haproxy.sh"    # 检测 haproxy 状态的脚本路径
        interval 2    # 检测时间间隔
        weight 2    # 如果条件成立,权重+2
}
vrrp_instance VI_1 {
    state MASTER
    interface ens32
    virtual_router_id 51
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {    # 将 track_script 块加入 instance 配置块
        haproxy    # 检查 HAProxy 服务是否存活
        }
    virtual_ipaddress {
        192.168.80.188
    }
}

:wq

keepalived+haproxy高可用

scp /etc/keepalived/keepalived.conf root@192.168.80.101:/etc/keepalived/keepalived.conf      //把keepalived.conf传送到80.101中
yes
root密码

keepalived+haproxy高可用

yum install lrz* -y        //安装上传软件
把haproxy软件包拉入

keepalived+haproxy高可用

scp haproxy-1.5.15.tar.gz root@192.168.80.101:/root      //把haproxy软件包传送到80.101中
密码

keepalived+haproxy高可用

tar xf haproxy-1.5.15.tar.gz -C /opt/          //解压haproxy软件包
cd /opt/haproxy-1.5.15/           
源码编译安装haproxy
yum install -y \
pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
make TARGET=linux26  PREFIX=/usr/local/haproxy    //标识64为系统
make install PREFIX=/usr/local/haproxy
mkdir /etc/haproxy          //创建目录
cd
useradd -M -s /sbin/nologin haproxy
id  haproxy

keepalived+haproxy高可用

cd /opt/haproxy-1.5.15/
cp examples/haproxy.cfg /etc/haproxy
vi /etc/haproxy/haproxy.cfg
修改以下内容:
#       chroot /usr/share/haproxy        注释这一行
uid 1000
gid 1000
在default模块下
统计页面模块内容
listen admin_stats
    bind 0.0.0.0:8089
    stats enable
    mode http
    log global
    stats uri  /stats
    stats realm Haproxy\ Statistics
    stats auth  admin:admin
    #stats hide-version
    stats admin if TRUE
    stats refresh 30s
web服务器/终端
listen webcluster
        bind 0.0.0.0:80
        mode http
        option httpchk GET /index.html
        log global
        maxconn 3000
        balance roundrobin
server web01 192.168.80.102:80 check inter 2000 fall 5
server web02 192.168.80.103:80 check inter 2000 fall 5

:wq

keepalived+haproxy高可用
keepalived+haproxy高可用

cd /opt/
ls

keepalived+haproxy高可用

vi haproxy.sh
#!/bin/bash
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
        /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg      启动haproxy服务
fi
sleep 2     等待2秒
if [ $(ps -C haproxy --no-header | wc -l) -eq 0 ];then
        service keepalived stop
fi

   :wq        //保存退出

keepalived+haproxy高可用

chmod +x /opt/haproxy.sh       //给这个文件设置权限
cd /opt/haproxy-1.5.15/
cp examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy
ll /etc/init.d/haproxy
chkconfig --add haproxy
ln -s /usr/local/haproxy/sbin/haproxy  /usr/sbin/haproxy     //软链接
cat /var/log/messages

keepalived+haproxy高可用

service haproxy start        //启动服务

keepalived+haproxy高可用

netstat -anpt | grep haproxy

keepalived+haproxy高可用

在网页上输入192.168.80.100:8089/stats

keepalived+haproxy高可用

scp haproxy.cfg root@192.168.80.101:/etc/haproxy/             //把haproxy.cfg 传送到80.101虚拟机上
密码

keepalived+haproxy高可用

service haproxy stop
netstat -anpt | grep haproxy
systemctl start keepalived
ip addr show ens32
netstat -anpt | grep haproxy

keepalived+haproxy高可用
——————————————————————————————————————
在80.101虚拟机上:

systemctl stop firewalld        //关闭防火墙
setenforce 0                 //关闭监控
cd /etc/yum.repos.d/    //进入目录
cp back/* ./         //把back里内容移动到上一个目录
ls               //查看目录内容

keepalived+haproxy高可用

yum install -y epel-release
yum install ntp -y           //安装ntp
ntpdate time1.aliyun.com        //同步网络时间
date
yum install keepalived -y         //安装keepalived
cd       //回到根目录

vi /etc/keepalived/keepalived.conf //进入成以下内容:
keepalived+haproxy高可用
修改成以下内容:
keepalived+haproxy高可用

ls
tar xf haproxy-1.5.15.tar.gz -C /opt/
cd /opt/haproxy-1.5.15/

keepalived+haproxy高可用

源码编译安装haproxy
yum install -y \
pcre-devel \
bzip2-devel \
gcc \
gcc-c++ \
make
make TARGET=linux26  PREFIX=/usr/local/haproxy    //标识64为系统
make install PREFIX=/usr/local/haproxy
mkdir /etc/haproxy
useradd -s /sbin/nologin -M  haproxy
id  haproxy

keepalived+haproxy高可用

ls /etc/haproxy/
cp examples/haproxy.init /etc/init.d/haproxy
chmod +x /etc/init.d/haproxy             //给这个文件设置权限
chkconfig --add haproxy
ln -s /usr/local/haproxy/sbin/haproxy  /usr/sbin/haproxy     //软链接

keepalived+haproxy高可用

service haproxy start        //启动服务
netstat -anpt | grep haproxy
service haproxy stop       //关闭服务
netstat -anpt | grep haproxy
cd /opt/
vi haproxy.sh
#!/bin/bash
#Filename:
A=$(ip addr | grep 192.168.80.188/32 | grep -v grep | wc -l)
if [ $A -gt 0 ]; then
        service haproxy start
else
        service haproxy stop
fi

   :wq
chmod +x haproxy.sh         //设置权限
ll                          
systemctl start keepalived     
ip addr show ens32
netstat -anpt | grep haproxy

————————————————————————————————————
在80.102虚拟机上:

systemctl stop firewalld      //关闭防火墙
setenforce 0          //关闭监控
yum install httpd -y        //安装httpd
vi /etc/httpd/conf/httpd.conf
把ServerName www.example.com:80 前面的#删除

keepalived+haproxy高可用

cd /var/www/html/       //进入这个目录
echo "<h2>server aa</h2>" > index.html
systemctl start httpd      //启动服务
在网页上输入192.168.80.102

keepalived+haproxy高可用
—————————————————————————————————————
在80.103虚拟机上:

systemctl stop firewalld      //关闭防火墙
setenforce 0          //关闭监控
yum install httpd -y        //安装httpd
vi /etc/httpd/conf/httpd.conf
把ServerName www.example.com:80 前面的#删除

keepalived+haproxy高可用

cd /var/www/html/       //进入这个目录
echo "<h2>server bb</h2>" > index.html
systemctl start httpd      //启动服务
在网页上输入192.168.80.103

keepalived+haproxy高可用

推荐阅读:
  1. keepalived+Haproxy搭建高可用Web群集
  2. keepalived高可用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

keepalived haproxy 高可

上一篇:storm写redis问题小结

下一篇:makefile(03)_条件判断

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》