如何在centos或Ubuntu中安装haproxy

发布时间:2020-06-04 17:37:29 作者:Leah
来源:亿速云 阅读:695

这篇文章给大家分享的是在centos或Ubuntu中安装haproxy的教程,相信大部分人都还不知道怎么安装,为了让大家学会,给大家总结了以下内容,话不多说,一起往下看吧。

Ubuntu18.04  apt 安装2.0.4
Centos7.6  Yum安装1.8.14
Centos7.6编译安装haproxy2.0.12

 

<h7 id=1>Ubuntu18.04  apt 安装2.0.4</h7>

root@ubuntu:~# apt-get install software-properties-common
#安装常用软件包

Reading package lists... Done
Building dependency tree       
Reading state information... Done
software-properties-common is already the newest version (0.96.24.32.12).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

root@ubuntu:~# add-apt-repository ppa:vbernat/haproxy-2.0
#添加最新的haproxy2.0版本到本地仓库

root@ubuntu:~# add-apt-repository ppa:vbernat/haproxy-2.0
 HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the Net.

This PPA contains packages for HAProxy 2.0.
 More info: https://launchpad.net/~vbernat/+archive/ubuntu/haproxy-2.0
Press [ENTER] to continue or Ctrl-c to cancel adding it.
                             ##这里需要手动ENTENR确认后才会据需安装包
Get:1 http://ppa.launchpad.net/vbernat/haproxy-2.0/ubuntu bionic InRelease [20.7 kB]
Hit:2 http://mirrors.aliyun.com/ubuntu bionic InRelease           
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                                                 
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease  
...

root@ubuntu:~#  apt update
#升级本地仓库,所有有关更改包的操作,都需要执行该操作

Hit:1 http://mirrors.aliyun.com/ubuntu bionic InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu bionic-security InRelease                                                 
Hit:3 http://mirrors.aliyun.com/ubuntu bionic-updates InRelease                                                  
Hit:4 http://mirrors.aliyun.com/ubuntu bionic-proposed InRelease                                                 
Hit:5 http://mirrors.aliyun.com/ubuntu bionic-backports InRelease                                   
Get:6 http://ppa.launchpad.net/vbernat/haproxy-2.0/ubuntu bionic InRelease [20.7 kB]
Get:7 http://ppa.launchpad.net/vbernat/haproxy-2.0/ubuntu bionic/main i386 Packages [9
...

root@ubuntu:~# apt-cache madison haproxy
#看仓库中haproxy·有哪些版本可以安装

root@ubuntu:~# apt-cache madison haproxy
   haproxy | 2.0.12-1ppa1~bionic | http://ppa.launchpad.net/vbernat/haproxy-2.0/ubuntu bionic/main amd64 Packages
   haproxy | 1.8.8-1ubuntu0.9 | http://mirrors.aliyun.com/ubuntu bionic-security/main amd64 Packages
   haproxy | 1.8.8-1ubuntu0.9 | http://mirrors.aliyun.com/ubuntu bionic-updates/main amd64 Packages
   haproxy |    1.8.8-1 | http://mirrors.aliyun.com/ubuntu bionic/main amd64 Packages
   haproxy |    1.8.8-1 | http://mirrors.aliyun.com/ubuntu bionic/main Sources
   haproxy | 1.8.8-1ubuntu0.9 | http://mirrors.aliyun.com/ubuntu bionic-security/main Sources
   haproxy | 1.8.8-1ubuntu0.9 | http://mirrors.aliyun.com/ubuntu bionic-updates/main Sources

root@ubuntu:~#  apt install haproxy=2.0.4-1ppa1\~bionic
#安装选择的版本

root@ubuntu:~#haproxy -v
#验证haproxy版本

HA-Proxy version 1.8.14-52e4d43 2018/09/20
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>

root@ubuntu:~#  systemctl start haproxy
#启动haproxy


<h7 id=2>Centos7.6  Yum安装1.8.14</h7>

默认情况centos7中的包都是较老的haproxy是1.5.18-9.el7的,不太满足需求,此时我们可以基于第三方安装包yum安装
https://pkgs.org/download/haproxy 该网址el7最新rpm包更新至1.8.14

一般需要一个依赖仓库包和haproxy.rpm的安装包

下载安装包

 wget https://centos.pkgs.org/7/cheese-x86_64/haproxy-1.8.14-1.el7.x86_64.rpm.html

下载依赖包

 weget https://centos.pkgs.org/7/cheese-x86_64/cheese-release-7-1.noarch.rpm.html

安装对应的包

#rpm -ivh cheese-release-7-1.noarch.rpm
#yum install haproxy-1.8.14-1.el7.x86_64.rpm -y

#验证haproxy版本

# haproxy -v
HA-Proxy version 1.8.14-52e4d43 2018/09/20
Copyright 2000-2018 Willy Tarreau <willy@haproxy.org>

<h7 id=3>Centos7.6编译安装haproxy2.0.12</h7>

编译安装HAProxy 2.0 LTS版本,更多源码包官网下载地址:http://www.haproxy.org/download/

HAProxy 支持基于lua实现功能扩展,为应用程序提供灵活的扩展和定制功能,由于centos自带的lua版本比较低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此编译时需要安装该包,lua官方下载地址
https://www.lua.org/download.html

yun安装编译环境

yum install libtermcap-devel ncurses-devel libevent-devel readline-devel wget make gcc-y

下载lua5.3.5

wget http://www.lua.org/ftp/lua-5.3.5.tar.gz
61% [===================================================>                                 ] 187,197     8.29KB/s  eta 25s   

解压并编译lua
[root@localhost /usr/local/src]#tar xf lua-5.3.5.tar.gz -C .
[root@localhost /usr/local/src]#cd lua-5.3.5
[root@localhost /usr/local/src/lua-5.3.5]#cd src && make linux

...
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX    -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o lctype.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o lbitlib.o lcorolib.o ldblib.o liolib.o lmathlib.o loslib.o lstrlib.o ltablib.o lutf8lib.o loadlib.o linit.o 
ranlib liblua.a
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX    -c -o lua.o lua.c
gcc -std=gnu99 -o lua   lua.o liblua.a -lm -Wl,-E -ldl -lreadline 
gcc -std=gnu99 -O2 -Wall -Wextra -DLUA_COMPAT_5_2 -DLUA_USE_LINUX    -c -o luac.o luac.c
gcc -std=gnu99 -o luac   luac.o liblua.a -lm -Wl,-E -ldl -lreadline 
make[1]: Leaving directory `/usr/local/src/lua-5.3.5/src'

最终在src生成两个可执行文件
如何在centos或Ubuntu中安装haproxy
系统版本

[root@localhost /usr/local/src/lua-5.3.5/src]#lua -v
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

编译的版本

[root@localhost /usr/local/src/lua-5.3.5/src]#./lua -v
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio

编译安装HAProxy

准备安装haproxy的环境依赖

yum install gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools vim iotop bc zip unzip zlib-devel lrzsz tree screen lsof tcpdump wget ntpdate -y

#HAProxy 1.8及1.9版本编译参数:

make ARCH=x86_64 TARGET=linux2628 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 PREFIX=/usr/local/haproxy

#HAProxy 2.0编译参数:

[root@localhost /usr/local/src/haproxy-2.0.12]#make ARCH=x86_64 TARGET=linux-glibc USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 USE_SYSTEMD=1 USE_CPU_AFFINITY=1 USE_LUA=1 LUA_INC=/usr/local/src/lua-5.3.5/src/ LUA_LIB=/usr/local/src/lua-5.3.5/src/ PREFIX=/usr/local/haproxy
#该配置默认安装在/usr/local/haproxy目录下
...
  CC      src/dict.o
  CC      src/xprt_handshake.o
  CC      ebtree/ebtree.o
  CC      ebtree/eb32sctree.o
  CC      ebtree/eb32tree.o
  CC      ebtree/eb64tree.o
  CC      ebtree/ebmbtree.o
  CC      ebtree/ebsttree.o
  CC      ebtree/ebimtree.o
  CC      ebtree/ebistree.o
  LD      haproxy

...

#安装该配置到/usr/local/haproxy目录下

[root@localhost /usr/local/src/haproxy-2.0.12]#make install PREFIX=/usr/local/haproxy

#移动到/usr/sbin/系统环境下
[root@localhost /usr/local/src/haproxy-2.0.12]#cp haproxy /usr/sbin/

#验证haproxy版本
[root@localhost /usr/local/src/haproxy-2.0.12]#haproxy -v

HA-Proxy version 2.0.12 2019/12/21 - https://haproxy.org/

#准备HAProxy启动脚本
[root@localhost /usr/local/src]#cat /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

#准配HA配置文件
#mkdir /etc/haproxy
#mkdir /var/lib/haproxy
#cat /etc/haproxy/haproxy.cfg

global
maxconn 100000
chroot /usr/local/haproxy
stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
#stats socket /var/lib/haproxy/haproxy.sock1 mode 600 level admin process 1
#stats socket /var/lib/haproxy/haproxy.sock2 mode 600 level admin process 2
#stats socket /var/lib/haproxy/haproxy.sock3 mode 600 level admin process 3
#stats socket /var/lib/haproxy/haproxy.sock4 mode 600 level admin process 4
uid 99
gid 99
daemon
#nbproc 4   #默认单进程启动
#nbthread 4  #可设置为单进程多线程或者多进程单线程,以及针对进程进程cpu绑定
#cpu-map 1 0
#cpu-map 2 1
#cpu-map 3 2
#cpu-map 4 3
pidfile /var/lib/haproxy/haproxy.pid
log 127.0.0.1 local3 info

defaults
option http-keep-alive
option forwardfor
maxconn 100000
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms

# listen stats    #启动web监控
#  bind :9009
#  stats enable
#  stats hide-version
#  stats uri /haproxy-status
#  stats realm HAPorxy\Stats\Page
#  stats auth admin:123456
#  #stats refresh 3s
#  stats admin if TRUE

启动haproxy:
systemctl start harpoxy

验证haproxy状态:
[root@localhost /usr/local/src]#haproxy -v

HA-Proxy version 2.0.12 2019/12/21 - https://haproxy.org/

注: haproxy.cfg文件中定义了chroot、pidfile、user、group等参数,如果系统没有相应的资源会导致haproxy无法启
动,具体参考日志文件/var/log/messages

看完上述内容,你们掌握在centos或Ubuntu中安装haproxy的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 如何在Ubuntu系统中编译安装HAProxy
  2. 如何在CentOS中安装Redis

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

ubuntu centos haproxy

上一篇:Spring怎么管理控制mybatis事务

下一篇:高防服务器中防火墙的重要性

相关阅读

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

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