通过Dockerfile 基于alpine系统制作 HAProxy 镜像

2021/12/12 23:22:26

本文主要是介绍通过Dockerfile 基于alpine系统制作 HAProxy 镜像,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

通过Dockerfile 基于alpine系统制作 HAProxy 镜像

提前通过docker run运行两台Apache镜像

[root@localhost ~]# docker run -it -d --name web1 luojiatian1904/httpd
[root@localhost ~]# docker run -it -d --name web2 luojiatian1904/httpd
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS                     PORTS     NAMES
1854792e046f   luojiatian1904/httpd   "/usr/local/apache/b…"   22 minutes ago   Up 22 minutes              80/tcp    web2
55a78e08619e   luojiatian1904/httpd   "/usr/local/apache/b…"   22 minutes ago   Up 22 minutes              80/tcp    web1

更改web2的访问内容

[root@localhost ~]# docker exec -it 1854792e046f /bin/bash
[root@1854792e046f src]# cd /usr/local/apache/htdocs/
[root@1854792e046f htdocs]# vi index.html 
<html><body><h1>123</h1></body></html>
[root@1854792e046f ~]# /usr/local/apache/bin/apachectl restart
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.2.3. Set the 'ServerName' directive globally to suppress this message
[root@1854792e046f src]# exit
exit
[root@localhost ~]# curl 192.168.2.3
<html><body><h1>123</h1></body></html>
[root@localhost ~]# curl 192.168.2.4
<html><body><h1>It works!</h1></body></html>

在/opt/下创建一个haproxy目录负责存放haproxy的Dockerfile和文件目录

[root@localhost opt]# mkdir -p haproxy/files

编写Dockerfile文件

FROM alpine
  
LABEL MAINTAINER='jtluo luojiatian01518@163.com'
ENV version 2.4.0

COPY files /tmp/
RUN ["/bin/sh","-c","/tmp/install.sh"]
EXPOSE 80 8189
CMD ["/usr/local/haproxy/sbin/haproxy" ,"-Ws","-f","/etc/haproxy/haproxy.cfg"]

编写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
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/
tar xf haproxy-$version.tar.gz
cd haproxy-$version && \
make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1 && \
make install PREFIX=/usr/local/haproxy 
cp haproxy /usr/sbin/
echo 'net.ipv4.ip_nonlocal_bind = 1' >>  /etc/sysctl.conf && \
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
mkdir /etc/haproxy
cat > /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
EOF
count=1
for rs_ip in $(cat /tmp/RSs.txt);do
cat >> /etc/haproxy/haproxy.cfg <<EOF
  server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done

创建RSs.txt指定RSip

[root@localhost opt]# vim haproxy/files/RSs.txt 
192.168.2.3
192.168.2.4

通过Dockerfile构建镜像

[root@localhost opt]# docker run -d --name haproxy -p 80:80 -p 8189:8189 luojiatian1904/haproxy:alpine
/usr/lib/gcc/x86_64-alpine-linux-musl/10.3.1/../../../../x86_64-alpine-linux-musl/bin/ld: cannot find -lsystemd
collect2: error: ld returned 1 exit status
make: *** [Makefile:939: haproxy] Error 1
make: *** Waiting for unfinished jobs....


这篇关于通过Dockerfile 基于alpine系统制作 HAProxy 镜像的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程