dockerfile构建haproxy

dockerfile构建haproxy,第1张

dockerfile构建haproxy

dockerfile构建haproxy
    • 准备工作
    • 构建haproxy镜像
    • 访问测试

准备工作
//运行两个web容器
[root@localhost ~]# docker run -it -d --name web1 wyus/httpd:v0.2 
a0375f00ae8354dd86d8f6457f9326f732c064b110569b3d9e37afdf66045458
[root@localhost ~]# docker run -it -d --name web2 wyus/httpd:v0.2 
e9c62ae0d51de83212962e6de85a8f38de516012d03ed7fbd26db7d604055fb3
[root@localhost ~]# docker ps
ConTAINER ID   IMAGE             COMMAND                  CREATED          STATUS         PORTS     NAMES
e9c62ae0d51d   wyus/httpd:v0.2   "/bin/sh -c '/usr/lo…"   3 seconds ago    Up 3 seconds             web2
a0375f00ae83   wyus/httpd:v0.2   "/bin/sh -c '/usr/lo…"   10 seconds ago   Up 9 seconds             web1

//修改两个web容器页面内容
[root@localhost ~]# curl 172.17.0.2
web1
[root@localhost ~]# curl 172.17.0.3
web2
构建haproxy镜像
//目录树
[root@localhost ~]# tree haproxy/
haproxy/
├── Dockerfile
└── files
    ├── haproxy-2.5.0.tar.gz
    ├── haproxy.cfg
    └── sysctl.conf

1 directory, 4 files


//Dockerfile文件
[root@localhost haproxy]# cat Dockerfile 
FROM centos

LABEL MAINTAINER 'abc 1@2.com'

ENV version 2.5.0
ENV PATH /usr/local/haproxy/sbin:$PATH

ADD files/haproxy-${version}.tar.gz /usr/src/
ADD files/haproxy.cfg /etc/haproxy/
ADD files/sysctl.conf /etc/

RUN rm -rf /etc/yum.repos.d/* && 
    curl -o /etc/yum.repos.d/CentOS-base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'[ .]+' '{print }' /etc/redhat-release).repo && 
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-base.repo && 
    yum clean all && yum makecache && 
    yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel && 
    useradd -r -M -s /sbin/nologin haproxy && 
    cd /usr/src/haproxy-$version && 
    make -j $(nproc)  
    TARGET=linux-glibc  
    USE_OPENSSL=1  
    USE_ZLIB=1  
    USE_PCRE=1  
    USE_SYSTEMD=1 && 
    make install PREFIX=/usr/local/haproxy && 
    yum -y remove gcc gcc-c++ make && 
    rm -rf /usr/src/haproxy-$version && 
    rm -rf /var/cache/*

EXPOSE 80 8189

WORKDIR /usr/local/haproxy

CMD ["-Ws","-f","/etc/haproxy/haproxy.cfg"]

ENTRYPOINT ["haproxy"]

//配置文件
[root@localhost haproxy]# cat files/haproxy.cfg 
#--------------全局配置----------------
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
    server web01 172.17.0.2:80 check inter 2000 fall 5
    server web02 172.17.0.3:80 check inter 2000 fall 5

//配置内核参数
[root@localhost haproxy]# cat files/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 wyus/haproxy:v0.2 haproxy/
......
Successfully built dfb55f06ae7f
Successfully tagged wyus/haproxy:v0.2

//查看镜像
[root@localhost ~]# docker images
REPOSITORY     TAG       IMAGE ID       CREATED          SIZE
wyus/haproxy   v0.2      dfb55f06ae7f   5 seconds ago    391MB
centos         latest    5d0da3dc9764   2 months ago     231MB

//运行容器
[root@localhost ~]# docker run -it -d --name haproxy -p 8080:80 -p 8189:8189 wyus/haproxy:v0.2 
[root@localhost ~]# docker exec -it haproxy /bin/bash
[root@acd5e0746cff /]# ss -anlt
State        Recv-Q       Send-Q             Local Address:Port             Peer Address:Port      Process       
LISTEN       0            128                      0.0.0.0:8189                  0.0.0.0:*                       
LISTEN       0            128                      0.0.0.0:80                    0.0.0.0:*                       
访问测试



登录后台

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5669840.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存