1 编写Dockerfile
[root@Docker ~]# tree alpine/
alpine/
├── Dockerfile
├── entrypoint.sh
└── files
├── haproxy-2.4.0.tar.gz
└── install.sh
//Dockerfile
[root@Docker ~]# cat alpine/Dockerfile
FROM alpine
LABEL MAINTAINER='19888429136@qq.com'
ENV haproxy_version 2.4.0
ENV PATH /usr/local/harpoxy/sbin:$PATH
ADD files/haproxy-${haproxy_version}.tar.gz /tmp/
ADD files/install.sh /tmp/
COPY entrypoint.sh /
RUN /tmp/install.sh
WORKDIR /usr/local/haproxy
EXPOSE 80 443 8189
ENTRYPOINT ["/entrypoint.sh"]
//安装脚本
[root@Docker ~]# cat alpine/files/install.sh
#! /bin/sh
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/' /etc/apk/repositories &&\
apk update && \
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 && \
addgroup haproxy
adduser -S -H -s /sbin/nologin haproxy && \
cd /tmp/haproxy-${haproxy_version} && make clean && \
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 && \
make install PREFIX=/usr/local/haproxy && \
echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
mkdir /usr/local/haproxy/conf
apk del gcc make
rm -rf /tmp/* /var/cache/*
//启动脚本
[root@Docker ~]# cat alpine/entrypoint.sh
#! /bin/sh
cat > /usr/local/haproxy/conf/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
EOF
count=1
for rs_ip in $(cat /tmp/RSs.txt);do
cat >> /usr/local/haproxy/conf/haproxy.cfg << EOF
server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done
/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/conf/haproxy.cfg -db
// 创建haproxy镜像
[root@Docker alpine]# docker build -t haproxy:v0.3 .
[root@Docker alpine]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
haproxy v0.3 ffc1a0f74bfb 5 seconds ago 177MB
haproxy v0.2 dda5e46ab6a6 5 hours ago 389MB
haproxy v0.1 56fd1c66327a 6 hours ago 560MB
busybox latest d23834f29b38 12 days ago 1.24MB
alpine latest c059bfaa849c 2 weeks ago 5.59MB
centos latest 5d0da3dc9764 2 months ago 231MB
//挂载的变量文件
[root@Docker haproxy]# cat /haproxy_RS/RSs.txt
192.168.1.2
192.168.1.3
192.168.1.4
//使用haproxy镜像创建容器
[root@Docker alpine]# docker run -dit --name haproxy -p 80:80 -p 8189:8189 -v /haproxy_RS:/tmp haproxy:v0.3
ec2c80707e8c0e2a02d4da8a582e6851137da86ebefb5594961acd6024fd1a75
[root@Docker alpine]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ec2c80707e8c haproxy:v0.3 "/entrypoint.sh" 2 seconds ago Up 1 second 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8189->8189/tcp, :::8189->8189/tcp, 443/tcp haproxy
//运行两个网站容器
[root@Docker alpine]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
75f7d9173f50 zhaojie10/httpd "/usr/local/apache/b…" 46 minutes ago Up 46 minutes 80/tcp, 443/tcp httpd
c0068a32e1d3 zhaojie10/nginx:v0.3 "/scripts/start.sh" 46 minutes ago Up 46 minutes 80/tcp nginx
在浏览器上查看
|