高可用负载均衡 haproxy+keepalived

服务器
20.0.0.206    10.0.0.206 bs-hk-hk01 高可用负载均衡节点 2c2g
20.0.0.207    10.0.0.207 bs-hk-hk02 高可用负载均衡节点 2c2g
软件版本
Keepalived 2.0.20
haproxy 2.1.2
Keepalived 安装配置
两个节点都安装   以bs-hk-hk01为例
#安装依赖包
[[email protected]-hk-hk01 tools]#yum -y install gcc openssl-devel libnl3-devel pcre-devel
[[email protected]-hk-hk01 tools]# ls
haproxy-2.1.2.tar.gz  keepalived-2.0.20.tar.gz
[[email protected]-hk-hk01 tools]# tar -zvxf keepalived-2.0.20.tar.gz
[[email protected]-hk-hk01 keepalived-2.0.20]# ./configure --prefix=/usr/local/keepalived-2.0.20
[[email protected]-hk-hk01 keepalived-2.0.20]# echo $?
0
[[email protected]-hk-hk01 keepalived-2.0.20]# make && make install
[[email protected]-hk-hk01 keepalived-2.0.20]# echo $?
0
#配置文件放在默认路径
[[email protected]-hk-hk01 keepalived-2.0.20]# mkdir /etc/keepalived/
[[email protected]-hk-hk01 keepalived-2.0.20]# cp /usr/local/keepalived-2.0.20/etc/keepalived/keepalived.conf /etc/keepalived/
#keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[[email protected]-hk-hk01 keepalived-2.0.20]# cp /usr/local/keepalived-2.0.20/etc/sysconfig/keepalived /etc/sysconfig/
[[email protected]-hk-hk01 keepalived-2.0.20]# cp /tools/keepalived-2.0.20/keepalived/keepalived.service /etc/systemd/system/
# 将keepalived主程序加入到环境变量(安装目录下)
[[email protected]-hk-hk01 keepalived-2.0.20]# ln -s /usr/local/keepalived-2.0.20/sbin/keepalived /usr/sbin/
#keepalived启动脚本,这个从keepalived源码目录复制,安装目录中没有
[[email protected]-hk-hk01 keepalived-2.0.20]# cp /tools/keepalived-2.0.20/keepalived/etc/init.d/keepalived /etc/init.d/
[[email protected]-hk-hk01 keepalived-2.0.20]# chmod 755 /etc/init.d/keepalived
[[email protected]-hk-hk01 keepalived-2.0.20]# systemctl enable keepalived.service
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /etc/systemd/system/keepalived.service.
[[email protected]-hk-hk01 keepalived-2.0.20]# mkdir /var/log/keepalived
[[email protected]-hk-hk01 keepalived-2.0.20]# vim /etc/sysconfig/keepalived
KEEPALIVED_OPTIONS="-D -d -S 0"
[[email protected]-hk-hk01 keepalived-2.0.20]# vim /etc/rsyslog.d/keepalived.conf
local0.* /var/log/keepalived/keepalived.log
#修改 /etc/rsyslog.conf 文件,添加如下:
[[email protected]-hk-hk01 log]# diff /etc/rsyslog.conf{,.bak}
74c74
< local0.*     /var/log/keepalived/keepalived.log
---
> 

[[email protected]-hk-hk01 keepalived-2.0.20]# systemctl restart rsyslog
[[email protected]-hk-hk01 keepalived-2.0.20]# systemctl start keepalived.service
[[email protected]-hk-hk01 keepalived]# cp /lib/systemd/system/keepalived.service{,.bak}
[[email protected]-hk-hk01 keepalived]# !vim
vim /lib/systemd/system/keepalived.service
[[email protected]-hk-hk01 keepalived]# diff /lib/systemd/system/keepalived.service{,.bak}
10,11c10,11
< EnvironmentFile=/etc/sysconfig/keepalived
< ExecStart=/sbin/keepalived $KEEPALIVED_OPTIONS
---
> EnvironmentFile=-/usr/local/keepalived-2.0.20/etc/sysconfig/keepalived
> ExecStart=/usr/local/keepalived-2.0.20/sbin/keepalived $KEEPALIVED_OPTIONS
[[email protected]-hk-hk01 keepalived]# systemctl daemon-reload
[[email protected]-hk-hk01 keepalived]# systemctl restart keepalived.service 

keepalived 配置
#bs-hk-hk01
[[email protected]-hk-hk01 keepalived]# cat keepalived.conf
! Configuration File for keepalived
! By zisefeizhu

vrrp_script chk_haproxy_port {
    script "/service/scripts/chk_hapro.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}

vrrp_instance kubernetes_master {
    state MASTER
    interface eth0
    virtual_router_id 1
    priority 150
    advert_int 1
    unicast_src_ip 20.0.0.206
    unicast_peer {
        20.0.0.207
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        20.0.0.250 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy_port
    }
}

#bs-hk-hk02
[[email protected]-hk-hk02 keepalived]# cp keepalived.conf{,.bak}
[[email protected]-hk-hk01 keepalived]# scp keepalived.conf 20.0.0.207:/etc/keepalived/keepalived.conf
[[email protected]-hk-hk02 keepalived]# cat keepalived.conf
! Configuration File for keepalived
! By zisefeizhu

vrrp_script chk_haproxy_port {
    script "/service/scripts/chk_hapro.sh"
    interval 2
    weight -5
    fall 2
    rise 1
}

vrrp_instance kubernetes_master {
    state BACKUP
    interface eth0
    virtual_router_id 1
    priority 70
    advert_int 1
    unicast_src_ip 20.0.0.207
    unicast_peer {
        20.0.0.206
    }
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        20.0.0.250 dev eth0 label eth0:1
    }
    track_script {
        chk_haproxy_port
    }
}
[[email protected]-hk-hk02 keepalived]# systemctl restart keepalived.service 

测试
[[email protected]-hk-hk01 keepalived]# systemctl stop keepalived.service
[[email protected]-hk-hk02 keepalived]# hostname -I
20.0.0.207 20.0.0.250 10.0.0.207
[[email protected]-hk-hk01 keepalived]# systemctl restart keepalived.service
[[email protected]-hk-hk01 keepalived]# hostname -I
20.0.0.206 20.0.0.250 10.0.0.206
[[email protected]-hk-hk02 keepalived]# hostname -I
20.0.0.207 10.0.0.207
# systemctl enable keepalived.service

Haproxy 安装配置
两个节点都安装   以bs-hk-hk01为例
[[email protected]-hk-hk01 ~]# yum install  vim iotop bc gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl  openssl-devel zip unzip zlib-devel  net-tools lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel bc  systemd-devel bash-completion traceroute libtermcap-devel ncurses-devel libevent-devel readline-devel  -y
#LUA编译安装及Systemd开发包
[[email protected]-hk-hk01 ~]# cd /tools/
[[email protected]-hk-hk01 tools]# curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
[[email protected] tools]# ls
haproxy-2.1.2.tar.gz  keepalived-2.0.20  keepalived-2.0.20.tar.gz  lua-5.3.5.tar.gz
[[email protected]-hk-hk01 tools]# tar -zxvf lua-5.3.5.tar.gz
[roo[email protected]-hk-hk01 lua-5.3.5]# make linux
[[email protected]-hk-hk01 lua-5.3.5]# echo $?
0
[[email protected]-hk-hk01 lua-5.3.5]# make INSTALL_TOP=/usr/local/lua install
[[email protected]-hk-hk01 lua-5.3.5]# echo $?
0
[[email protected]-hk-hk01 lua-5.3.5]# yum install systemd-devel

#安装haproxy
[[email protected]-hk-hk01 lua-5.3.5]# cd ..
[[email protected]-hk-hk01 tools]# ls
haproxy-2.1.2.tar.gz  keepalived-2.0.20  keepalived-2.0.20.tar.gz  lua-5.3.5  lua-5.3.5.tar.gz
[[email protected]-hk-hk01 tools]# tar xf haproxy-2.1.2.tar.gz
[[email protected]-hk-hk01 tools]# cd haproxy-2.1.2/
[[email protected]-hk-hk01 haproxy-2.1.2]# make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_ZLIB=1 USE_LUA=1 LUA_LIB=/usr/local/lua/lib/ LUA_INC=/usr/local/lua/include/ USE_PCRE=1 USE_SYSTEMD=1 PREFIX=/usr/local/haproxy
[[email protected]-hk-hk01 haproxy-2.1.2]# make install PREFIX=/usr/local/haproxy
[[email protected]-hk-hk01 haproxy-2.1.2]# cp haproxy /usr/sbin/
#准备启动脚本
[[email protected]-hk-hk01 haproxy-2.1.2]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /var/lib/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
#配置文件
[[email protected]-hk-hk01 haproxy-2.1.2]# cd /etc/haproxy/
[[email protected]-hk-hk01 log]# cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Global settings
# By   zisefeizhu
#---------------------------------------------------------------------
global
maxconn 10000
uid 99
gid 99
daemon
nbproc 2
cpu-map 1 0
cpu-map 2 1
log 127.0.0.1 local3 info
#log 127.0.0.1 local1 notice

#默认配置
defaults
    log global
    mode http
    #option http-keep-alive
    option httpclose
    option  forwardfor
    option abortonclose
    maxconn 10000
    mode http
    timeout connect 300000ms
    timeout client  300000ms
    timeout server  300000ms
  balance leastconn  

#统计页面配置
listen stats
  bind 0.0.0.0:9999
    mode http
  option httplog
  maxconn 10
  stats refresh 30s
  stats enable
  log global
  stats uri     /haproxy-status
  stats realm zisefeizhu\ Haproxy
  stats auth    admin:zisefeizhu
  stats hide-version

#K8S-API-Server
frontend K8S_API
    bind *:8443
    mode tcp
    default_backend k8s_api_nodes_6443

backend k8s_api_nodes_6443
    mode tcp
    balance leastconn
    server 20.0.0.200  20.0.0.200:6443  check inter 2000 fall 3 rise 5
    server 20.0.0.201  20.0.0.201:6443  check inter 2000 fall 3 rise 5
    server 20.0.0.202  20.0.0.202:6443  check inter 2000 fall 3 rise 5

[[email protected]-hk-hk01 haproxy]# mkdir /var/lib/haproxy
[[email protected]-hk-hk01 haproxy]# chown 99.99 /var/lib/haproxy/ -R
[[email protected]-hk-hk01 haproxy]# vim /etc/rsyslog.conf
# 对如下两行取消注释
$ModLoad imudp
$UDPServerRun 514
# 在末尾添加如下行
local3.* /var/log/haproxy/haproxy.log
*.info;mail.none;authpriv.none;cron.none;local0.none;local3.none
[[email protected]-hk-hk01 log]# systemctl restart rsyslog
[[email protected]-hk-hk01 log]# systemctl enable haproxy.service
Created symlink from /etc/systemd/system/multi-user.target.wants/haproxy.service to /usr/lib/systemd/system/haproxy.service.
[[email protected]-hk-hk01 haproxy]# systemctl start haproxy.service 

设置服务启动顺序及依赖关系
[[email protected]-hk-hk01 keepalived]# diff /usr/lib/systemd/system/keepalived.service{,.bak02}
3c3
< After=network-online.target syslog.target haproxy.service
---
> After=network-online.target syslog.target
5d4
< Requires=haproxy.service
[[email protected]-hk-hk01 keepalived]# scp /usr/lib/systemd/system/keepalived.service 20.0.0.207:/usr/lib/systemd/system/keepalived.service

检查脚本
[[email protected]-hk-hk01 keepalived]# mkdir /service/scripts -p
[[email protected]-hk-hk01 keepalived]# vim /service/scripts/chk_hapro.sh
##########################################################################
#Author:                     zisefeizhu
#QQ:                         2********0
#Date:                       2020-02-02
#FileName:                   /service/scripts/chk_hapro.sh
#URL:                        https://www.cnblogs.com/zisefeizhu/
#Description:                The test script
#Copyright (C):              2020 All rights reserved
##########################################################################
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
export $PATH
counts=$(ps -ef|grep -w "haproxy"|grep -v grep|wc -l)
if [ "${counts}" = "0" ]; then
    systemctl restart keepalived.service
    sleep 2
    counts=$(ps -ef|grep -w "haproxy"|grep -v grep|wc -l)
    if [ "${counts}" = "0" ]; then
        systemctl stop keepalived.service
    fi
fi

查看日志
[[email protected]-hk-hk01 log]# tree keepalived/  haproxy/
keepalived/
└── keepalived.log
haproxy/
└── haproxy.log

0 directories, 2 files

完成!

原文地址:https://www.cnblogs.com/zisefeizhu/p/12318361.html

时间: 2024-10-22 07:57:03

高可用负载均衡 haproxy+keepalived的相关文章

Linux集群之高可用负载均衡lvs+keepalived

LVS简介 LVS介绍 LVS是Linux Virtual Server的缩写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统,属于4层负载均衡 ipvs和ipvsadm的关系 我们使用配置LVS的时候,不能直接配置内核中的ipvs,需要使用ipvs的管理工具ipvsadm进行管理 LVS术语 LVS转发原理 LVS负载均衡器接受所有入站请求,并根据调度算法决定哪个realserver处理该请求 LVS调度算法 轮询(rr):按照请求顺序轮流分发到后端RS 加权轮询(wrr):权值高的获得

HaProxy + Keepalived 实现高可用负载均衡

软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HAProxy相比LVS的使用要简单很多,功能方面也很丰富.当前,HAProxy支持两种主要的代理模式:"tcp"也即4层(大多用于邮件服务器.内部协议通信服务器等),和7层(HTTP).在4层模式下,HAProxy仅在客户端和服务器之间转发双向流量.7层模式下,HAProxy会分析协议,并且

haproxy+keepalived实现高可用负载均衡web集群

haproxy+keepalived实现高可用负载均衡web集群 负载均衡集群的概念 负载均衡是设计分布式系统架构必须要考虑的因素之一,它指的是通过调度分发的方式尽可能将"请求"."访问"的压力负载平均分摊到集群中的各个节点,避免有些节点负载太高导致访问延迟,而有些节点负载很小导致资源浪费.这样,每个节点都可以承担一定的访问请求负载压力,并且可以实现访问请求在各节点之间的动态分配,以实现负载均衡,从而为企业提供更高性能,更加稳定的系统架构解决方案. 高可用集群的概念

基于HAProxy+Keepalived高可用负载均衡web服务的搭建

一 原理简介 1.HAProxyHAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理.HAProxy运行在时下的硬件上,完全可以支持数以万计的并发连接.并且它的运行模式使得它可以很简单安全的整合进当前的架构中, 同时可以保护web服务器不被暴露到网络上.2.KeepalivedKeepalived 是一个基于VRRP协议来实现的LVS服务高

Keepalived+Haproxy双主高可用负载均衡web和mysql综合实验

日期及版本:2014.5.4v1.0 架构图 实验目的: 1.Haproxy+Keepalived双主双机高可用模型,keepalived为Haproxy主从提供高可用保证haproxy-master若挂掉haproxy-backup能无缝接管,haproxy为后端Web提供负载均衡,缓解并发压力,实现WEB站点负载均衡+高可用性: 2. Haproxy反代web做动静分离: 3. Haproxy反代mysql 算法leastconn和roundrobin的不同效果: 系统环境: OS:cent

keepalived + haproxy 实现web 双主模型的高可用负载均衡

1.本文的架构图: 阐述各服务器用途: 1.haproxy在本构架中实现的是:负载均衡 2.keepalived实现对haproxy的高可用 3.apache static 实现静态页面的访问 4.aoache dynamic实现动态页面的访问,图中有两个是实现负载均衡的 配置各功能模块: 一.配置haproxy和keepalived 验证: 1.当一台keepalived宕机后,VIP会不会转移到另外一台服务器 2.当一台haproxy服务出故障,VIP会不会转移到另外一台服务器 注意: 那如

haproxy+keepalived实现高可用负载均衡

软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HAProxy相比LVS的使用要简单很多,功能方面也很丰富.当 前,HAProxy支持两种主要的代理模式:"tcp"也即4层(大多用于邮件服务器.内部协议通信服务器等),和7层(HTTP).在4层模式 下,HAProxy仅在客户端和服务器之间转发双向流量.7层模式下,HAProxy会分析协议,

haproxy+keepalived实现高可用负载均衡(转)

haproxy+keepalived实现高可用负载均衡 软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. HAProxy相比LVS的使用要简单很多,功能方面也很丰富.当 前,HAProxy支持两种主要的代理模式:"tcp"也即4层(大多用于邮件服务器.内部协议通信服务器等),和7层(HTTP).在4层模式 下,HAProxy仅在客户端和服务器

Haproxy+Keepalived搭建Weblogic高可用负载均衡集群

配置环境说明:   KVM虚拟机配置 用途 数量 IP地址 机器名 虚拟IP地址 硬件 内存3G  系统盘20G cpu 4核 Haproxykeepalived 2台 192.168.1.10192.168.1.20 Haserver01Haserver02 192.168.30192.168.40 内存6G  系统盘20G cpu 4核 Weblogic 4台 192.168.1.100192.168.1.150 192.168.1.200 192.168.1.250 Clusters01C