2018 Raspberry Pi and HaProxy

本文介绍如何在 Raspberry Pi 上安装并配置 HAProxy, 包括下载源码、编译安装 HAProxy,设置 MySQL (MariaDB) 以及 Docker 配置等过程。此外还展示了如何通过 HAProxy 实现两个静态网站和后端 MySQL 的负载均衡。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2018 Raspberry Pi and HaProxy

I will fetch the latest Haproxy
>wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.4.tar.gz

https://linuxacademy.com/howtoguides/posts/show/topic/13169-installation-of-haproxy
Unzip that file and use make compile to build
>make TARGET=linux2628
>sudo make install

Set up the Mysql(MariaDB)
http://sillycat.iteye.com/blog/2393787

Here is the set up for my current Haproxy Docker Configuration
Makefile
IMAGE=sillycat/public
TAG=raspberrypi-haproxy
NAME=raspberrypi-haproxy

docker-context:

build: docker-context
docker build -t $(IMAGE):$(TAG) .

run:
docker run -d -p 80:80 -p 3306:3306 --name $(NAME) $(IMAGE):$(TAG)

debug:
docker run -ti -p 80:80 -p 3306:3306 --name $(NAME) $(IMAGE):$(TAG) /bin/bash

clean:
docker stop ${NAME}
docker rm ${NAME}

logs:
docker logs ${NAME}

publish:
docker push ${IMAGE}:${TAG}

fetch:
docker pull ${IMAGE}:${TAG}

start.sh to start the Process
#!/bin/sh -ex

#start the haproxy
cd /tool/haproxy-1.8.4
haproxy -f conf/haproxy.conf

Here comes the most important file Dockerfile
#Set up haproxy in Docker

#Prepre the OS
FROM resin/rpi-raspbian:jessie
MAINTAINER Carl Luo <luohuazju@gmail.com>

ENV DEBIAN_FRONTEND noninteractive
RUN apt-get -y update
RUN apt-get install -y apt-utils
RUN apt-get -y dist-upgrade
RUN apt-get install -y build-essential gcc make
RUN apt-get install -y libpcre3 libpcre3-dev zlib1g-dev libgcrypt11-dev

#install haproxy
RUN mkdir -p /tool
RUN mkdir -p /install
ADDinstall/haproxy-1.8.4.tar.gz /install/
WORKDIR /install/haproxy-1.8.4
RUN make TARGET=linux2628
RUN make install

#config haproxy
RUN mkdir -p /tool/haproxy-1.8.4/conf
ADD conf/haproxy.conf /tool/haproxy-1.8.4/conf/
RUN mkdir -p /var/lib/haproxy

#start the application
EXPOSE 80 3306
RUN mkdir -p /app/
ADD start.sh /app/
WORKDIR /app/
CMD[ "./start.sh" ]

I plan to support 2 different websites which host static resources, and one proxy to my backend MySQL in conf/haproxy.conf
global
maxconn 400
#daemon

defaults
timeout connect 30000
timeout client 50000
timeout server 50000

stats enable
stats hide-version
stats uri /stats
stats auth useradmin:xxxxx

frontend http-in
bind *:80
mode http
acl url_water path_beg -i /water
acl url_price path_beg -i /price

use_backend web_water if url_water
default_backend web_price

backend web_water
mode http
balance roundrobin
server web_water1 192.168.1.108:8081 check

backend web_price
mode http
balance roundrobin
server web_price1 192.168.1.108:8081 check

listen mysql
bind *:3306
mode tcp
balance roundrobin
server mysql2 192.168.1.109:3306 check

Then we can visit the proxy status page as follow:
http://192.168.1.108/stats

Static Website 1
http://192.168.1.108/water/

Static Website 2
http://192.168.1.108/price/

References:
http://www.haproxy.org/
http://sillycat.iteye.com/blog/1055846
http://sillycat.iteye.com/blog/2066118
http://sillycat.iteye.com/blog/562645
http://sillycat.iteye.com/blog/1055846
http://sillycat.iteye.com/blog/2066119

http://liaoph.com/haproxy-tutorial/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值