IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> 高可用篇之Keepalived (HAProxy+keepalived 搭建高可用负载均衡集群) -> 正文阅读

[系统运维]高可用篇之Keepalived (HAProxy+keepalived 搭建高可用负载均衡集群)

环境:

主机名IP网卡名称服务
haproxy-master192.168.58.140网卡接口:ens160haproxy、keepalived
haproxy-slave:192.168.58.141网卡接口:ens160haproxy、keepalived
web-1:192.168.58.138网卡接口:ens160httpd
web-2:192.168.58.139网卡接口:ens160httpd

部署httpd

//web-1和web-2部署httpd
[root@web-1 ~]# yum -y install httpd
[root@web-1 ~]# echo 'master' > /var/www/html/index.html
[root@web-1 ~]# systemctl restart httpd
[root@web-1 ~]# systemctl enable httpd

[root@web-2 ~]# yum -y install httpd
[root@web-2 ~]# echo 'slave' > /var/www/html/index.html
[root@web-2 ~]# systemctl restart httpd

安装haproxy

//haproxy-master和haproxy-slave主机安装haproxy 
//安装haproxy
#源码安装
[root@haproxy-master ~]# wget https://src.fedoraproject.org/repo/pkgs/haproxy/haproxy-2.1.3.tar.gz/sha512/4728c1177b2bba69465cbc56b1ed73a1b2d36891ba2d94d29bb49714ad98ccfac4b52947735aded211f0cd8070002f5406ddd77cabd2f8230b00438189dd7a60/haproxy-2.1.3.tar.gz
//安装编译环境
[root@haproxy-master ~]# yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel
//创建haproxy用户
[root@haproxy-master ~]# useradd -r -M -s /sbin/nologin haproxy
//解压和安装
[root@haproxy-master ~]# tar -zxvf haproxy-2.1.3.tar.gz 
[root@haproxy-master ~]# cd haproxy-2.1.3
[root@haproxy-master haproxy-2.1.3]# make clean
[root@haproxy-master haproxy-2.1.3]# make -j $(grep 'processor' /proc/cpuinfo |wc -l)   TARGET=linux-glibc   USE_OPENSSL=1   USE_ZLIB=1   USE_PCRE=1   USE_SYSTEMD=1
[root@haproxy-master haproxy-2.1.3]# make install PREFIX=/usr/local/haproxy
[root@haproxy-master haproxy-2.1.3]# cp haproxy  /usr/sbin/

//修改LB的内核参数
[root@haproxy-master ~]# mkdir /etc/haproxy
[root@haproxy-master ~]# vim /etc/haproxy/haproxy.cfg
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
//修改haproxy配置文件
[root@haproxy-master ~]# vim /etc/haproxy/haproxy.cfg
global
    daemon
    maxconn 256

defaults
    mode http
    timeout connect 5000ms
    timeout client 50000ms
    timeout server 50000ms

frontend http-in
    bind *:80
    default_backend servers

backend servers
    server web01 192.168.58.138:80
    server web02 192.168.58.139:80
//启动haproxy,配置haproxy.service服务单元文件
[root@haproxy-master ~]# vim /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target

[Service]
ExecStartPre=/usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg   -c -q
ExecStart=/usr/local/haproxy/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg  -p /var/run/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target
//启动haproxy服务
[root@haproxy-master ~]# systemctl  restart haproxy
[root@haproxy-master ~]# systemctl restart rsyslog
[root@haproxy-master ~]# systemctl  enable haproxy
[root@haproxy-master ~]# systemctl enable rsyslog
//配置日志信息
[root@haproxy-master ~]# vim /etc/rsyslog.conf 
local0.*        /var/log/haproxy.log
//使用WEB网页访问测试
[root@haproxy-master ~]#  vim /etc/haproxy/haproxy.cfg 
//清空,修改为如下:
global
    log 127.0.0.1 local0  info
    #log loghost local0 info
    maxconn 20480
#chroot /usr/local/haproxy
    pidfile /var/run/haproxy.pid
    #maxconn 4000
    user haproxy
    group haproxy
    daemon
#---------------------------------------------------------------------
#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 httpclose
    option httplog
    #option forwardfor
    option redispatch
    balance roundrobin
    timeout connect 10s
    timeout client 10s
    timeout server 10s
    timeout check 10s
    maxconn 60000
    retries 3
#--------------统计页面配置------------------
listen admin_stats
    bind 0.0.0.0:8189
    stats enable
    mode http
    log global
    stats uri /haproxy_stats            #访问网页后缀URL
    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
    cookie SESSION_COOKIE insert indirect nocache
    server web01 192.168.58.138:80 check inter 2000 fall 5
    server web02 192.168.58.139:80 check inter 2000 fall 5
[root@haproxy-master ~]# systemctl restart haproxy
[root@haproxy-master ~]# ss -anlt
State   Recv-Q  Send-Q    Local Address:Port     Peer Address:Port  Process  
LISTEN  0       128             0.0.0.0:8189          0.0.0.0:*              
LISTEN  0       128             0.0.0.0:80            0.0.0.0:*              
LISTEN  0       128             0.0.0.0:22            0.0.0.0:*              
LISTEN  0       128                [::]:22               [::]:*    


 
haproxy-slave安装haproxy和上方一样
略

keepalived安装

配置主keepalived
[root@haproxy-master ~]# yum -y install keepalived
 配置主keepalived
[root@haproxy-master ~]# vim /etc/keepalived/keepalived.conf 

! Configuration File for keepalived
  
global_defs {
   router_id lb01
}

vrrp_instance VI_1 {
    state MASTER
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yexiaotian
    }
    virtual_ipaddress {
        192.168.58.250
    }
}

virtual_server 192.168.58.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.58.140 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@haproxy-master ~]# systemctl restart keepalived

配置备keepalived
[root@haproxy-slave ~]# yum -y install keepalived
[root@haproxy-slave ~]# vim /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   router_id lb02
}

vrrp_instance VI_1 {
    state BACKUP
    interface ens160
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass yexiaotian
    }
    virtual_ipaddress {
        192.168.58.250
    }
}

virtual_server 192.168.58.250 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    persistence_timeout 50
    protocol TCP

    real_server 192.168.58.141 80 {
        weight 1
        TCP_CHECK {
            connect_port 80
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}
[root@haproxy-slave ~]# systemctl  restart keepalived

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-10-17 13:11:56  更:2022-10-17 13:15:39 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/19 9:24:57-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码