溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

haproxy基于centos和Ubuntu安裝

發(fā)布時(shí)間:2020-09-10 03:31:29 來源:網(wǎng)絡(luò) 閱讀:275 作者:wx5baa4aff60c00 欄目:系統(tǒng)運(yùn)維

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版本到本地倉(cāng)庫(kù)

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.
                             ##這里需要手動(dòng)ENTENR確認(rèn)后才會(huì)據(jù)需安裝包
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
#升級(jí)本地倉(cāng)庫(kù),所有有關(guān)更改包的操作,都需要執(zhí)行該操作

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
#看倉(cāng)庫(kù)中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
#驗(yàn)證haproxy版本

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

root@ubuntu:~# systemctl start haproxy
#啟動(dòng)haproxy


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

默認(rèn)情況centos7中的包都是較老的haproxy是1.5.18-9.el7的,不太滿足需求,此時(shí)我們可以基于第三方安裝包yum安裝
https://pkgs.org/download/haproxy 該網(wǎng)址el7最新rpm包更新至1.8.14

一般需要一個(gè)依賴倉(cāng)庫(kù)包和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

安裝對(duì)應(yīng)的包

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

#驗(yàn)證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版本,更多源碼包官網(wǎng)下載地址:http://www.haproxy.org/download/

HAProxy 支持基于lua實(shí)現(xiàn)功能擴(kuò)展,為應(yīng)用程序提供靈活的擴(kuò)展和定制功能,由于centos自帶的lua版本比較低并不符合HAProxy要求的lua最低版本(5.3)的要求,因此編譯時(shí)需要安裝該包,lua官方下載地址
https://www.lua.org/download.html

yun安裝編譯環(huán)境

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生成兩個(gè)可執(zhí)行文件
haproxy基于centos和Ubuntu安裝
系統(tǒng)版本

[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

準(zhǔn)備安裝haproxy的環(huán)境依賴

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版本編譯參數(shù):

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編譯參數(shù):

[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
#該配置默認(rèn)安裝在/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

#移動(dòng)到/usr/sbin/系統(tǒng)環(huán)境下
[root@localhost /usr/local/src/haproxy-2.0.12]#cp haproxy /usr/sbin/

#驗(yàn)證haproxy版本
[root@localhost /usr/local/src/haproxy-2.0.12]#haproxy -v

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

#準(zhǔn)備HAProxy啟動(dòng)腳本
[root@localhost /usr/local/src]#cat /usr/lib/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
#目錄需對(duì)應(yīng)安裝目錄
[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

#準(zhǔn)配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   #默認(rèn)單進(jìn)程啟動(dòng)
#nbthread 4  #可設(shè)置為單進(jìn)程多線程或者多進(jìn)程單線程,以及針對(duì)進(jìn)程進(jìn)程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    #啟動(dòng)web監(jiān)控
#  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

啟動(dòng)haproxy:
systemctl start harpoxy

驗(yàn)證haproxy狀態(tài):
[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等參數(shù),如果系統(tǒng)沒有相應(yīng)的資源會(huì)導(dǎo)致haproxy無法啟
動(dòng),具體參考日志文件/var/log/messages

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI