//在docker环境中拉取alpine,用Dockerfile基于alpine来制作haproxy镜像
[root@localhost ~]# docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
59bf1c3509f3: Pull complete
Digest: sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300
Status: Downloaded newer image for alpine:latest
docker.io/library/alpine:latest
//Dockerfile文件内容
[root@localhost haproxy]# vim Dockerfile
[root@localhost haproxy]# cat Dockerfile
FROM alpine
LABEL MAINTAINER="yaya 1870648704@qq.com"
ENV version 2.4.8
ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
ADD files/sysctl.conf /tmp/
RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh
[root@localhost files]# vim install.sh
[root@localhost files]# cat install.sh
#!/bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories
apk update
adduser -S -H -s /sbin/nologin haproxy
addgroup haproxy
apk add --no-cache -U make gcc pcre-dev bzip2-dev openssl-dev elogind-dev libc-dev dahdi-tools dahdi-tools-dev libexecinfo libexecinfo-dev ncurses-dev zlib-dev zlib
cd /tmp/haproxy-2.4.8
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-2.4.8/* /tmp/install.sh
[root@localhost files]# cat haproxycfg.sh
#--------------全局配置----------------
global
log 127.0.0.1 local0 info
#log loghost local0 info
maxconn 20480
#chroot /usr/local/haproxy
pidfile /var/run/haproxy.pid
#maxconn 4000
user haproxy
group haproxy
daemon
#---------------------------------------------------------------------
#common defaults that all the 'listen' and 'backend' sections will
#use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option dontlognull
option httpclose
option httplog
#option forwardfor
option redispatch
balance roundrobin
timeout connect 10s
timeout client 10s
timeout server 10s
timeout check 10s
maxconn 60000
retries 3
#--------------统计页面配置------------------
listen admin_stats
bind 0.0.0.0:8189
stats enable
mode http
log global
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
stats auth admin:admin
#stats hide-version
stats admin if TRUE
stats refresh 30s
#---------------web设置-----------------------
listen webcluster
bind 0.0.0.0:80
mode http
#option httpchk GET /index.html
log global
maxconn 3000
balance roundrobin
cookie SESSION_COOKIE insert indirect nocache
EOF
count=1
for rs_ip in $RSs;do
cat >> /etc/haproxy/haproxy.cfg <<EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
haproxy -f /etc/haproxy/haproxy.cfg -db
[root@localhost files]# cat sysctl.conf
# sysctl settings are defined through files in
# /usr/lib/sysctl.d/, /run/sysctl.d/, and /etc/sysctl.d/.
#
# Vendors settings live in /usr/lib/sysctl.d/.
# To override a whole file, create a new file with the same in
# /etc/sysctl.d/ and put new settings there. To override
# only specific settings, add a file with a lexically later
# name in /etc/sysctl.d/ and put new settings there.
#
# For more information, see sysctl.conf(5) and sysctl.d(5).
net.ipv4.ip_nonlocal_bind = 1
net.ipv4.ip_forward = 1
[root@localhost ~]# docker build -t haproxy:v2.0 haproxy/
Sending build context to Docker daemon 3.608MB
Step 1/9 : FROM alpine
---> c059bfaa849c
Step 2/9 : LABEL MAINTAINER="yaya 1870648704@qq.com"
---> Using cache
---> f85188c470b2
Step 3/9 : ENV version 2.4.8
---> Using cache
---> a6cfc47fcb87
Step 4/9 : ADD files/haproxy-${version}.tar.gz /tmp/
---> Using cache
---> 8a3780f9322c
Step 5/9 : ADD files/install.sh /tmp/
---> Using cache
---> 72e4e31eb609
Step 6/9 : ADD files/haproxycfg.sh /tmp/
---> Using cache
---> dd8a492e8c02
Step 7/9 : ADD files/sysctl.conf /tmp/
---> Using cache
---> 1c71bd585baa
Step 8/9 : RUN /tmp/install.sh
---> Using cache
---> 225f540cd0f7
Step 9/9 : ENTRYPOINT /tmp/haproxycfg.sh
---> Using cache
---> f3c00e79bc2c
Successfully built f3c00e79bc2c
Successfully tagged haproxy:v2.0
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v2.0 f3c00e79bc2c 6 minutes ago 19.8MB
[root@localhost ~]# docker run -d --name apache httpd
c9d1342a0d1e7e2c533436ade332ceee20e9555e0f06e1cf40740e1fed52cb07
[root@localhost ~]# docker run -d --name nginx nginx
6cde538bdfdb38aa7fabfef1328b4f1e0c48b4c8dd13ff794bb020de032b1062
[root@localhost files]# docker run -itd --name haproxy -p 80:80 -e RSs="172.17.0.3 172.17.0.2" haproxy:v2.0
5ea56ba961d920f150064922ddc8f1432959c6b009a65bfdefe5db4054662f90
[root@localhost ~]# docker ps -a
[root@localhost files]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5ea56ba961d9 haproxy:v2.0 "/bin/sh -c /tmp/hap…" 2 minutes ago Created haproxy
b7747293a893 nginx "/docker-entrypoint.…" 15 minutes ago Up 15 minutes 80/tcp nginx
78efde73c7cb httpd "httpd-foreground" 15 minutes ago Up 15 minutes 80/tcp httpd