一個簡單的例子,帶你理解haproxy!
阿新 • • 發佈:2018-12-09
如果你對haproxy啥都不懂,這篇文章將對你有很大的幫助。大神請繞行!
haproxy是一個性能不輸於nginx的工具。和nginx功能類似,可以實現負載均衡。他與nginx不同的是,haproxy既可以實現基於http的七層架構,又可以實現基於tcp/udp的四層架構。
[[email protected] haproxy]# yum info haproxy Loaded plugins: fastestmirror, langpacks Repository base is listed more than once in the configuration Repository epel is listed more than once in the configuration Loading mirror speeds from cached hostfile * epel: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Installed Packages Name : haproxy Arch : x86_64 Version : 1.5.18 Release : 7.el7 Size : 2.6 M Repo : installed From repo : base Summary : TCP/HTTP proxy and load balancer for high availability environments URL : http://www.haproxy.org/ License : GPLv2+ Description : HAProxy is a TCP/HTTP reverse proxy which is particularly suited for : high availability environments. Indeed, it can: : - route HTTP requests depending on statically assigned cookies : - spread load among several servers while assuring server : persistence through the use of HTTP cookies : - switch to backup servers in the event a main server fails : - accept connections to special ports dedicated to service : monitoring : - stop accepting connections without breaking existing ones : - add, modify, and delete HTTP headers in both directions : - block requests matching particular patterns : - report detailed status to authenticated users from a URI : intercepted by the application
下面就先介紹一個簡單的例子吧:
實驗環境:兩臺虛擬機器。一臺作為服務端,一塊作為客戶端。
服務端yum倉庫如下:
[[email protected] yum.repos.d]# cat centos.repo [base] name=Centos $releasever baseurl=http://172.18.0.1/centos/$releasever/ gpgcheck=0 [epel] name=Fedora EPEL baseurl=https://mirrors.aliyun.com/epel/$releasever/x86_64/ https://mirrors.tuna.tsinghua.edu.cn/epel/$releasever/x86_64/ gpgcheck=0 [extras] name=CentOS-$releasever - Extras baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ gpgcheck=0 [updates] name=CentOS-$releasever - Updates - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 [extras] name=CentOS-$releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 [centosplus] name=CentOS-$releasever - Plus - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 [contrib] name=CentOS-$releasever - Contrib - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
阿里雲的docker-ce倉庫
[[email protected] yum.repos.d]# cat docker-ce.repo [docker-ce-stable] name=Docker CE Stable - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/stable enabled=1 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-stable-debuginfo] name=Docker CE Stable - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/stable enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-stable-source] name=Docker CE Stable - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/stable enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge] name=Docker CE Edge - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge-debuginfo] name=Docker CE Edge - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-edge-source] name=Docker CE Edge - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/edge enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test] name=Docker CE Test - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test-debuginfo] name=Docker CE Test - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-test-source] name=Docker CE Test - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/test enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-nightly] name=Docker CE Nightly - $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/$basearch/nightly enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-nightly-debuginfo] name=Docker CE Nightly - Debuginfo $basearch baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/debug-$basearch/nightly enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg [docker-ce-nightly-source] name=Docker CE Nightly - Sources baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/source/nightly enabled=0 gpgcheck=1 gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
[[email protected] yum.repos.d]# cat magedu.repo
[base]
name=Centos $releasever
baseurl=http://172.18.0.1/centos/$releasever/
gpgcheck=0
[epel]
name=Fedora EPEL
baseurl=http://172.18.0.1/fedora-epel/$releasever/$basearch/
gpgcheck=0
服務端:
一、關閉防火牆
[[email protected] ~]# iptables -F
二、安裝docker-ce 、haproxy
[[email protected] ~]# yum install haproxy -y;yum install docker-ce -y
三、修改docker配置檔案
[[email protected] ~]# vim /usr/lib/systemd/system/docker.service
......
ExecStart=/usr/bin/dockerd -H unix://
ExecStartPost=/usr/sbin/iptables -PFORWARD ACCEPT
ExecReload=/bin/kill -s HUP $MAINPID
.......
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl start docker
新增第二行,因為我們啟動docker後,會修改我們的防火牆規則。為了不使我們的其他服務受影響,新增第二行的資訊。
修改配置檔案,啟動docker。
四、上傳docker映象檔案
[[email protected] ~]# docker pull httpd:2.4.37-alpine
五、建立兩個docker容器
[[email protected] ~]# docker run --name web1 -d --network bridge httpd:2.4.37-alpine
[[email protected] ~]# docker run --name web2 -d --network bridge httpd:2.4.37-alpine
run 執行一個容器,--name web1 容器的名字web1 ,-d 後臺執行 ,--network 網路型別是bredge ,httpd:2.4.37-alpine 映象檔案
六、修改容器內的響應頁面
[[email protected] ~]# docker exec -it web1 /bin/bash
bash-4.4# cd /htdocs
bash-4.4# vi index.html
<html><body><h1>It works web1!</h1></body></html>
[[email protected] ~]# docker exec -it web2 /bin/bash
bash-4.4# cd /htdocs
bash-4.4# vi index.html
<html><body><h1>It works web2!</h1></body></html>
七、修改haproxy配置檔案
[[email protected] ~]# vim /etc/haproxy/haproxy.cfg
......
frontend web *:80
default_backend websrvs
backend websrvs
server web1 172.17.0.2:80 check
server web2 172.17.0.3:80 check
......
[[email protected] ~]# systemctl restart haproxy
客戶端測試;
[[email protected] /]# while true;do curl http://172.18.252.121/;sleep .5;done
<html><body><h1>It works web1!</h1></body></html>
<html><body><h1>It works web2!</h1></body></html>
<html><body><h1>It works web1!</h1></body></html>
<html><body><h1>It works web2!</h1></body></html>
我們實現了簡單的負載均衡!