[root@localhost ~]# cd haproxy/[root@localhost haproxy]# cat dockerfile
FROM alpine
LABEL MAINTAINER "pyd 1@2qq.com"
ENV version 2.5.0
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 haproxy]#
3. 配置文件
[root@localhost haproxy]# cd files/[root@localhost files]# ls
env.txt haproxy-2.5.0.tar.gz haproxycfg.sh install.sh sysctl.conf
//RS ip[root@localhost files]# cat env.txt RSs=172.17.0.2 172.17.0.3
//内核参数
[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 files]# cat install.sh #!/bin/shsed -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-${version}makeTARGET=linux-musl USE_OPENSSL=1USE_ZLIB=1USE_PCRE=1makeinstallPREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/
mkdir /etc/haproxy
apk del gcc makerm -rf /tmp/haproxy-${version}/ /tmp/install.sh
//haproxy配置文件
[root@localhost files]# cat haproxycfg.sh #!/bin/shcat> /etc/haproxy/haproxy.cfg <<EOF
#--------------????----------------
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
EOFcount=1forrs_ipin$RSs;docat>> /etc/haproxy/haproxy.cfg <<EOF
server web$count$rs_ip:80 check inter 2000 fall 5
EOFlet count++
done
haproxy -f /etc/haproxy/haproxy.cfg -db
[root@localhost files]#
//利用haproxyv:1.0镜像创建容器
[root@localhost haproxy]# docker run -d --name haproxy -p 6666:80 --env-file files/env.txt haproxy:v1.0
dc201555c9810abebb6037319db127d99db72cf64161e1fbefc6c5f3c7eefb78
[root@localhost haproxy]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dc201555c981 haproxy:v1.0 "/bin/sh -c /tmp/hap…"8 seconds ago Up 7 seconds 0.0.0.0:6666->80/tcp, :::6666->80/tcp haproxy
1f4fed0191c8 nginx "/docker-entrypoint.…"10 minutes ago Up 10 minutes 80/tcp nginx
dc5872009f97 httpd "httpd-foreground"10 minutes ago Up 10 minutes 80/tcp apache
[root@localhost haproxy]# docker port haproxy 80/tcp ->0.0.0.0:6666
80/tcp -> :::6666
[root@localhost haproxy]#
7. 访问
[root@localhost ~]# curl 192.168.8.130:6666<html><body><h1>It works!</h1></body></html>[root@localhost ~]# curl 192.168.8.130:6666<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style>
html { color-scheme: light dark;}
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>[root@localhost ~]#