如果你对haproxy啥都不懂,这篇文章将对你有很大的帮助。大神请绕行!
haproxy是一个性能不输于nginx的工具。和nginx功能类似,可以实现负载均衡。他与nginx不同的是,haproxy既可以实现基于http的七层架构,又可以实现基于tcp/udp的四层架构。
[root@localhost 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仓库如下:
[root@localhost 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仓库
[root@localhost 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
[root@localhost 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
服务端:
一、关闭防火墙
[root@localhost ~]# iptables -F
二、安装docker-ce 、haproxy
[root@localhost ~]# yum install haproxy -y;yum install docker-ce -y
三、修改docker配置文件
[root@localhost ~]# 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
.......
[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start docker
添加第二行,因为我们启动docker后,会修改我们的防火墙规则。为了不使我们的其他服务受影响,添加第二行的信息。
修改配置文件,启动docker。
四、上传docker镜像文件
[root@localhost ~]# docker pull httpd:2.4.37-alpine
五、创建两个docker容器
[root@localhost ~]# docker run --name web1 -d --network bridge httpd:2.4.37-alpine
[root@localhost ~]# docker run --name web2 -d --network bridge httpd:2.4.37-alpine
run 运行一个容器,--name web1 容器的名字web1 ,-d 后台运行 ,--network 网络类型是bredge ,httpd:2.4.37-alpine 镜像文件
六、修改容器内的响应页面
[root@localhost ~]# 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>
[root@localhost ~]# 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配置文件
[root@localhost ~]# 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
......
[root@localhost ~]# systemctl restart haproxy
客户端测试;
[root@localhost /]# 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>
我们实现了简单的负载均衡!