CentOS/RedHat下用自有或第三方源yum安装HAPROXY

Posted njsummer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOS/RedHat下用自有或第三方源yum安装HAPROXY相关的知识,希望对你有一定的参考价值。

简要说明:在Linux 下的很多应用,比如:nginxmysql、httpd、LVS等等,强烈建议大家先yum安装,了解其目录结构、文件构成、服务启动文件及配置文件格式后,再在官网查找编译安装的教程一步步去实践编译自定义式的安装,这样非常利于理解透这类应用。

1. 用CentOS8.4自有源yum安装HAPROXY1.8.27

当然,我们也出于同样的目的(理解HAPROXY软件),简要地记录下CentOS8.4下yum安装HAPROXY1.8.27的基本过程。

# CentOS8 下的版本信息 Version:1.8.27 官网目前最新的 2.5;CentOS7上的HAPROXY版本信息1.5.18,版本很老的了,很多特性不被支持,因此在7.9一定要编译安装最新版本的HAPROXY。
[root@CentOS84-IP172-08 ]#dnf info haproxy
...............
Installed Packages
Name : haproxy
Version : 1.8.27
Release : 2.el8
Architecture : x86_64
Size : 4.2 M
Source : haproxy-1.8.27-2.el8.src.rpm
Repository : @System
From repo : AppStream
...............
[root@CentOS84-IP172-08 ]#
# yum 安装 haproxy
[root@CentOS84-IP172-08 ]#dnf -y install haproxy

# 查看haproxy文件
[root@CentOS84-IP172-08 ]#rpm -ql haproxy
/etc/haproxy
/etc/haproxy/haproxy.cfg
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/lib/.build-id
/usr/lib/.build-id/05
/usr/lib/.build-id/05/3e8866280b6206e36255a46f0ea1bb79705784
/usr/lib/.build-id/a1
/usr/lib/.build-id/a1/0bb61846c6f940fb88d7d3c250bb10f525a102
/usr/lib/.build-id/ab
/usr/lib/.build-id/ab/964c67b286c6f72430c820b0a7d30f88cb6b49
/usr/lib/systemd/system/haproxy.service
/usr/sbin/haproxy
/usr/share/doc/haproxy
/usr/share/doc/haproxy/51Degrees-device-detection.txt
/usr/share/doc/haproxy/CHANGELOG
/usr/share/doc/haproxy/DeviceAtlas-device-detection.txt
/usr/share/doc/haproxy/README
/usr/share/doc/haproxy/ROADMAP
/usr/share/doc/haproxy/SPOE.txt
/usr/share/doc/haproxy/VERSION
/usr/share/doc/haproxy/WURFL-device-detection.txt
/usr/share/doc/haproxy/acl-content-sw.cfg
/usr/share/doc/haproxy/acl.fig
/usr/share/doc/haproxy/architecture.txt
/usr/share/doc/haproxy/auth.cfg
/usr/share/doc/haproxy/close-options.txt
/usr/share/doc/haproxy/coding-style.txt
/usr/share/doc/haproxy/configuration.txt
/usr/share/doc/haproxy/content-sw-sample.cfg
/usr/share/doc/haproxy/cookie-options.txt
/usr/share/doc/haproxy/design-thoughts
/usr/share/doc/haproxy/design-thoughts/backends-v0.txt
/usr/share/doc/haproxy/design-thoughts/backends.txt
/usr/share/doc/haproxy/design-thoughts/be-fe-changes.txt
/usr/share/doc/haproxy/design-thoughts/binding-possibilities.txt
/usr/share/doc/haproxy/design-thoughts/buffer-redesign.txt
/usr/share/doc/haproxy/design-thoughts/buffers.fig
/usr/share/doc/haproxy/design-thoughts/config-language.txt
/usr/share/doc/haproxy/design-thoughts/connection-reuse.txt
/usr/share/doc/haproxy/design-thoughts/connection-sharing.txt
/usr/share/doc/haproxy/design-thoughts/dynamic-buffers.txt
/usr/share/doc/haproxy/design-thoughts/entities-v2.txt
/usr/share/doc/haproxy/design-thoughts/how-it-works.txt
/usr/share/doc/haproxy/design-thoughts/http2.txt
/usr/share/doc/haproxy/design-thoughts/http_load_time.url
/usr/share/doc/haproxy/design-thoughts/rate-shaping.txt
/usr/share/doc/haproxy/design-thoughts/sess_par_sec.txt
/usr/share/doc/haproxy/gpl.txt
/usr/share/doc/haproxy/haproxy.1
/usr/share/doc/haproxy/internals
/usr/share/doc/haproxy/internals/acl.txt
/usr/share/doc/haproxy/internals/body-parsing.txt
/usr/share/doc/haproxy/internals/buffer-operations.txt
/usr/share/doc/haproxy/internals/buffer-ops.fig
/usr/share/doc/haproxy/internals/connect-status.txt
/usr/share/doc/haproxy/internals/connection-header.txt
/usr/share/doc/haproxy/internals/connection-scale.txt
/usr/share/doc/haproxy/internals/entities-v2.txt
/usr/share/doc/haproxy/internals/entities.fig
/usr/share/doc/haproxy/internals/entities.pdf
/usr/share/doc/haproxy/internals/entities.svg
/usr/share/doc/haproxy/internals/entities.txt
/usr/share/doc/haproxy/internals/filters.txt
/usr/share/doc/haproxy/internals/hashing.txt
/usr/share/doc/haproxy/internals/header-parser-speed.txt
/usr/share/doc/haproxy/internals/header-tree.txt
/usr/share/doc/haproxy/internals/http-cookies.txt
/usr/share/doc/haproxy/internals/http-docs.txt
/usr/share/doc/haproxy/internals/http-parsing.txt
/usr/share/doc/haproxy/internals/list.fig
/usr/share/doc/haproxy/internals/listener-states.fig
/usr/share/doc/haproxy/internals/listener-states.png
/usr/share/doc/haproxy/internals/lua_socket.fig
/usr/share/doc/haproxy/internals/lua_socket.pdf
/usr/share/doc/haproxy/internals/naming.txt
/usr/share/doc/haproxy/internals/pattern.dia
/usr/share/doc/haproxy/internals/pattern.pdf
/usr/share/doc/haproxy/internals/polling-states.fig
/usr/share/doc/haproxy/internals/repartition-be-fe-fi.txt
/usr/share/doc/haproxy/internals/sequence.fig
/usr/share/doc/haproxy/internals/stats-v2.txt
/usr/share/doc/haproxy/internals/stream-sock-states.fig
/usr/share/doc/haproxy/intro.txt
/usr/share/doc/haproxy/lgpl.txt
/usr/share/doc/haproxy/linux-syn-cookies.txt
/usr/share/doc/haproxy/lua-api
/usr/share/doc/haproxy/lua-api/Makefile
/usr/share/doc/haproxy/lua-api/_static
/usr/share/doc/haproxy/lua-api/_static/channel.fig
/usr/share/doc/haproxy/lua-api/_static/channel.png
/usr/share/doc/haproxy/lua-api/conf.py
/usr/share/doc/haproxy/lua-api/index.rst
/usr/share/doc/haproxy/lua.txt
/usr/share/doc/haproxy/management.txt
/usr/share/doc/haproxy/netscaler-client-ip-insertion-protocol.txt
/usr/share/doc/haproxy/network-namespaces.txt
/usr/share/doc/haproxy/option-http_proxy.cfg
/usr/share/doc/haproxy/peers-v2.0.txt
/usr/share/doc/haproxy/peers.txt
/usr/share/doc/haproxy/proxy-protocol.txt
/usr/share/doc/haproxy/queuing.fig
/usr/share/doc/haproxy/regression-testing.txt
/usr/share/doc/haproxy/ssl.cfg
/usr/share/doc/haproxy/transparent_proxy.cfg
/usr/share/doc/haproxy/wurfl-example.cfg
/usr/share/haproxy
/usr/share/haproxy/400.http
/usr/share/haproxy/403.http
/usr/share/haproxy/408.http
/usr/share/haproxy/500.http
/usr/share/haproxy/502.http
/usr/share/haproxy/503.http
/usr/share/haproxy/504.http
/usr/share/haproxy/README
/usr/share/licenses/haproxy
/usr/share/licenses/haproxy/LICENSE
/usr/share/man/man1/halog.1.gz
/usr/share/man/man1/haproxy.1.gz
/var/lib/haproxy
[root@CentOS84-IP172-08 ]#

# 查看 服务器启动文件,我们在后面编译安装过程中要借鉴这个文件的内容,拟写编译安装的启动文件
[root@CentOS84-IP172-08 ]#cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.target

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
EnvironmentFile=/etc/sysconfig/haproxy
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $OPTIONS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
SuccessExitStatus=143
KillMode=mixed
Type=notify

[Install]
WantedBy=multi-user.target
[root@CentOS84-IP172-08 ]#cat /usr/lib/systemd/system/haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=network-online.target
Wants=network-online.target

[Service]
Environment="CONFIG=/etc/haproxy/haproxy.cfg" "PIDFILE=/run/haproxy.pid"
EnvironmentFile=/etc/sysconfig/haproxy
ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS
ExecStart=/usr/sbin/haproxy -Ws -f $CONFIG -p $PIDFILE $OPTIONS
ExecReload=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS
ExecReload=/bin/kill -USR2 $MAINPID
SuccessExitStatus=143
KillMode=mixed
Type=notify

[Install]
WantedBy=multi-user.target
[root@CentOS84-IP172-08 ]#

# 查看HAPROXY 的配置文件,我们在后面编译安装过程中要借鉴这个文件的内容,拟写编译的配置文件
[root@CentOS84-IP172-08 ]#cat /etc/haproxy/haproxy.cfg
#---------------------------------------------------------------------
# Example configuration for a possible web application. See the
# full configuration options online.
#
# https://www.haproxy.org/download/1.8/doc/configuration.txt
#
#---------------------------------------------------------------------

#---------------------------------------------------------------------
# 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 4000
user haproxy
group haproxy
daemon

# turn on stats unix socket
stats socket /var/lib/haproxy/stats

# utilize system-wide crypto-policies
ssl-default-bind-ciphers PROFILE=SYSTEM
ssl-default-server-ciphers PROFILE=SYSTEM

#---------------------------------------------------------------------
# common defaults that all the listen and backend sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 10s
timeout queue 1m
timeout connect 10s
timeout client 1m
timeout server 1m
timeout http-keep-alive 10s
timeout check 10s
maxconn 3000

#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend main
bind *:5000
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js

use_backend static if url_static
default_backend app

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend static
balance roundrobin
server static 127.0.0.1:4331 check

#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend app
balance roundrobin
server app1 127.0.0.1:5001 check
server app2 127.0.0.1:5002 check
server app3 127.0.0.1:5003 check
server app4 127.0.0.1:5004 check

# 启动haproxy并开机自启
[root@CentOS84-IP172-08 ]#systemctl enable --now haproxy
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.

# 查看端口监听
[root@CentOS84-IP172-08 ]#ss -tln
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 3000 0.0.0.0:5000 0.0.0.0:*

[root@CentOS84-IP172-08 ]#systemctl status haproxy

[root@CentOS84-IP172-08 ]#pstree | grep haproxy
|-haproxy---haproxy
[root@CentOS84-IP172-08 ]#

2. CentOS7.9下用第三方源yum安装HAPROXY2.2.22

HAPROXY 官方没有提供rpm相关的包,可以通过第三方仓库的rpm包

从第三方网站下载rpm包:https://pkgs.org/download/haproxy

CentOS/RedHat下用自有或第三方源yum安装HAPROXY_第三方源yum安装HAPROXY

选择相应的版本 ,我们本次演示在CentOS7.9 上安装比较新的版本 haproxy

https://centos.pkgs.org/7/ius-x86_64/haproxy22-2.2.22-1.el7.ius.x86_64.rpm.html

CentOS/RedHat下用自有或第三方源yum安装HAPROXY_第三方源yum安装HAPROXY_02

#  官网提供的安装步骤及相应的命令,会报错,处置方法见实践过程中
Download latest ius-release rpm:
https://repo.ius.io/ius-release-el7.rpm
Install ius-release rpm:
# rpm -Uvh ius-release*rpm
Install haproxy22 rpm package:
# yum install haproxy22
# CentOS7 上的版本信息 1.5.18  版本还是很老的了,我们在这个CentOS7.9 上演示编译安装最新版本的HAPROXY
[root@haproxy-ip07 <sub>]# yum info haproxy
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.cloud.tencent.com
* centosplus: mirrors.cloud.tencent.com
* epel: mirrors.cloud.tencent.com
* extras: mirrors.cloud.tencent.com
* updates: mirrors.cloud.tencent.com
Available Packages
Name : haproxy
Arch : x86_64
Version : 1.5.18
..........................

[root@haproxy-ip07 </sub>]#

[root@centos79 <sub>]# hostnamectl set-hostname Happrox-IP47
[root@centos79 </sub>]# exit

# 下载 repo 信息文件
[root@happrox-ip47 <sub>]# wget https://repo.ius.io/ius-release-el7.rpm

[root@happrox-ip47 </sub>]# ll
-rw-r--r-- 1 root root 8412 Aug 8 2021 ius-release-el7.rpm

# 安装 ius-release-el7.rpm ,报错了
[root@happrox-ip47 <sub>]# rpm -Uvh ius-release*rpm
warning: ius-release-el7.rpm: Header V4 RSA/SHA256 Signature, key ID 4b274df2: NOKEY
error: Failed dependencies:
epel-release = 7 is needed by ius-release-2-1.el7.ius.noarch

# 出现上面的报错,需要先安装 epel-release
[root@happrox-ip47 </sub>]# yum -y install epel-release

# 再去安装 ius-release*rpm 就没有问题了,
[root@happrox-ip47 <sub>]# rpm -Uvh ius-release*rpm
warning: ius-release-el7.rpm: Header V4 RSA/SHA256 Signature, key ID 4b274df2: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:ius-release-2-1.el7.ius ################################# [100%]

# 安装 haproxy 2.2.22版
[root@happrox-ip47 </sub>]# yum install haproxy22

# 查看端口监听 看不到有 haproxy 相关的
[root@happrox-ip47 <sub>]# ss -tlnp | grep haproxy [root@happrox-ip47 </sub>]#

# 启动服务,再查看端口监听
[root@happrox-ip47 <sub>]# systemctl start haproxy
[root@happrox-ip47 </sub>]# ss -tlnp | grep haproxy
LISTEN 0 3000 *:5000 *:* users:(("haproxy",pid=2296,fd=7))

# 查看版本
[root@happrox-ip47 <sub>]# haproxy -v
HA-Proxy version 2.2.22-16420af 2022/03/14 - https://haproxy.org/
Status: long-term supported branch - will stop receiving fixes around Q2 2025.
Known bugs: http://www.haproxy.org/bugs/bugs-2.2.22.html
Running on: Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 x86_64
[root@happrox-ip47 </sub>]#

# 查看rpm 安装的软件haproxy22版本
[root@happrox-ip47 <sub>]#
[root@happrox-ip47 </sub>]# rpm -qi haproxy22
Name : haproxy22
Version : 2.2.22
..................

# 查看生成的文件
[root@happrox-ip47 <sub>]# rpm -ql haproxy22
/etc/haproxy #haproxy 主要目录之一
/etc/haproxy/haproxy.cfg #此文件是配置文件,很重要
/etc/logrotate.d/haproxy
/etc/sysconfig/haproxy
/usr/bin/halog
/usr/bin/iprange
/usr/lib/systemd/system/haproxy.service #haproxy 启动文件
/usr/sbin/haproxy
/usr/share/doc/haproxy22-2.2.22
/usr/share/doc/haproxy22-2.2.22/51Degrees-device-detection.txt
/usr/share/doc/haproxy22-2.2.22/CHANGELOG
/usr/share/doc/haproxy22-2.2.22/DeviceAtlas-device-detection.txt
/usr/share/doc/haproxy22-2.2.22/README
/usr/share/doc/haproxy22-2.2.22/ROADMAP
/usr/share/doc/haproxy22-2.2.22/SOCKS4.protocol.txt
/usr/share/doc/haproxy22-2.2.22/SPOE.txt
/usr/share/doc/haproxy22-2.2.22/VERSION
/usr/share/doc/haproxy22-2.2.22/WURFL-device-detection.txt
/usr/share/doc/haproxy22-2.2.22/acl-content-sw.cfg
/usr/share/doc/haproxy22-2.2.22/acl.fig
/usr/share/doc/haproxy22-2.2.22/architecture.txt
/usr/share/doc/haproxy22-2.2.22/close-options.txt
/usr/share/doc/haproxy22-2.2.22/coding-style.txt
/usr/share/doc/haproxy22-2.2.22/configuration.txt
/usr/share/doc/haproxy22-2.2.22/content-sw-sample.cfg
/usr/share/doc/haproxy22-2.2.22/cookie-options.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts
/usr/share/doc/haproxy22-2.2.22/design-thoughts/backends-v0.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/backends.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/be-fe-changes.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/binding-possibilities.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/config-language.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/connection-reuse.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/connection-sharing.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/dynamic-buffers.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/entities-v2.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/how-it-works.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/http2.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/http_load_time.url
/usr/share/doc/haproxy22-2.2.22/design-thoughts/rate-shaping.txt
/usr/share/doc/haproxy22-2.2.22/design-thoughts/sess_par_sec.txt
/usr/share/doc/haproxy22-2.2.22/gpl.txt
/usr/share/doc/haproxy22-2.2.22/haproxy.1
/usr/share/doc/haproxy22-2.2.22/internals
/usr/share/doc/haproxy22-2.2.22/internals/acl.txt
/usr/share/doc/haproxy22-2.2.22/internals/body-parsing.txt
/usr/share/doc/haproxy22-2.2.22/internals/buffer-api.txt
/usr/share/doc/haproxy22-2.2.22/internals/connect-status.txt
/usr/share/doc/haproxy22-2.2.22/internals/connection-header.txt
/usr/share/doc/haproxy22-2.2.22/internals/connection-scale.txt
/usr/share/doc/haproxy22-2.2.22/internals/entities-v2.txt
/usr/share/doc/haproxy22-2.2.22/internals/entities.fig
/usr/share/doc/haproxy22-2.2.22/internals/entities.pdf
/usr/share/doc/haproxy22-2.2.22/internals/entities.svg
/usr/share/doc/haproxy22-2.2.22/internals/entities.txt
/usr/share/doc/haproxy22-2.2.22/internals/filters.txt
/usr/share/doc/haproxy22-2.2.22/internals/hashing.txt
/usr/share/doc/haproxy22-2.2.22/internals/header-parser-speed.txt
/usr/share/doc/haproxy22-2.2.22/internals/header-tree.txt
/usr/share/doc/haproxy22-2.2.22/internals/http-cookies.txt
/usr/share/doc/haproxy22-2.2.22/internals/http-docs.txt
/usr/share/doc/haproxy22-2.2.22/internals/http-parsing.txt
/usr/share/doc/haproxy22-2.2.22/internals/htx-api.txt
/usr/share/doc/haproxy22-2.2.22/internals/initcalls.txt
/usr/share/doc/haproxy22-2.2.22/internals/list.fig
/usr/share/doc/haproxy22-2.2.22/internals/listener-states.fig
/usr/share/doc/haproxy22-2.2.22/internals/listener

以上是关于CentOS/RedHat下用自有或第三方源yum安装HAPROXY的主要内容,如果未能解决你的问题,请参考以下文章

Centos7/redhat7最小化安装后的简单配置

YUM源详解以及EPEL源的创建

centos redhat linux下如何怎么安装google chrome 设置谷歌浏览器桌面快捷方式

centos redhat linux下如何怎么安装google chrome 设置谷歌浏览器桌面快捷方式

MySQL 5.7MySQL 8.0 yum 源

ELK Centos Redhat yum 安装