Dockerfile文件写haproxy

Dockerfile文件写haproxy

文章目录

结构目录

[root@localhost ~]# tree haproxy
haproxy
├── 1
├── Dockerfile
└── files
    ├── haproxy2.4.0.tar.gz
    ├── haproxy.cfg
    ├── haproxy.sh
    ├── run_haproxy.sh
    └── sysctl.conf

1 directory, 7 files 

dockerfile配置文件

[root@localhost ~]# cd haproxy
[root@localhost haproxy]# cat Dockerfile 
FROM centos

ENV version 2.4.0
WORKDIR /usr/local
ADD files/haproxy${version}.tar.gz /usr/local/
ADD files/haproxy.sh /usr/local
ADD files/haproxy.cfg /etc/haproxy/
ADD files/run_haproxy.sh /usr/local
ADD files/sysctl.conf /etc/
RUN ["/bin/bash","-c","/usr/local/haproxy.sh"]

EXPOSE 80
CMD ["/usr/local/run_haproxy.sh"]

haproxy配置文件

[root@localhost ~]# cd haproxy
[root@localhost haproxy]# ls files/
haproxy2.4.0.tar.gz  haproxy.cfg  haproxy.sh  run_haproxy.sh  sysctl.conf
[root@localhost haproxy]# cat files/haproxy.sh 
#!/bin/bash
yum -y install make gcc pcre-devel bzip2-devel openssl-devel systemd-devel

useradd -r -M -s /sbin/nologin haproxy

cd /usr/local
tar xf haproxy2.4.0.tar.gz
cd haproxy-2.4.0

make clean
make -j $(grep 'processor' /proc/cpuinfo |wc -l)  \
        TARGET=linux-glibc  \
        USE_OPENSSL=1  \
        USE_ZLIB=1  \
        USE_PCRE=1  \
        USE_SYSTEMD=1
make install PREFIX=/usr/local/haproxy
cp haproxy /usr/sbin/ 

配置各个负载的内核参数配置

[root@localhost files]# ls
haproxy-2.4.0.tar.gz  haproxy.sh      sysctl.conf
haproxy.cfg           run_haproxy.sh
[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

提供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 web1 172.17.0.3:80 check inter 3000 fall 5
    server web2 172.17.0.4:80 check inter 3000 fall 5   

写容器启动脚本

[root@localhost haproxy]# cat files/run_haproxy.sh 
#!/bin/bash
haproxy -f /etc/haproxy/haproxy.cfg
tail -f /etc/hosts
 

构建haproxy镜像

[root@localhost haproxy]# docker build -t haproxy:v1.0 .
[root@localhost haproxy]# docker images
REPOSITORY       TAG       IMAGE ID       CREATED             SIZE
haproxy          v1.0      78b11c5cc028   40 minutes ago      578MB

映射端口后进入容器查看端口

[root@localhost haproxy]# docker run -itd --name haproxy -p 1111:80 haproxy:v1.0
df35f4c0a3d2c8cbfedc00d2a6addd8690d5a60c897348f1bfaea676fced19b3
[root@localhost haproxy]# docker ps -a
CONTAINER ID   IMAGE          COMMAND                  CREATED              STATUS         PORTS     NAMES
6488e4fab8eb   haproxy:v1.0   "/usr/local/run_hapr…"   5 seconds ago        Up 3 seconds 
0.0.0.0:1111->80/tcp, :::1111->80/tcp   haproxy
[root@localhost haproxy]# docker exec -it haproxy /bin/bash
[root@6488e4fab8eb local]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port     Peer Address:Port   Process   
LISTEN   0        128              0.0.0.0:80            0.0.0.0:*                
LISTEN   0        128              0.0.0.0:8189          0.0.0.0:*

在已有的镜像中运行一个nginx和httpd

[root@localhost haproxy]# docker run -d --name httpd yunjisuanlp/httpd:v1.0
f839b45baf93e82e71393438049a5949ebb7fc1e2fce3a7e0678604b95ec1dee
[root@localhost haproxy]# docker run -d --name nginx  yunjisuanlp/nginx:v1.0
5052fb8620d88b201aee40512ce737569dd5d0b1f835e756c7ac9c3ab5830aa0
[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED          STATUS          PORTS     NAMES
5052fb8620d8   yunjisuanlp/nginx:v1.0   "/usr/local/nginx/sb…"   6 seconds ago    Up 6 seconds              nginx
f839b45baf93   yunjisuanlp/httpd:v1.0   "/usr/local/apache/b…"   25 seconds ago   Up 25 seconds   80/tcp    httpd
6488e4fab8eb   haproxy:v1.0          "/usr/local/run_hapr…"   3 minutes ago    Up 3 seconds 0.0.0.0:1111->80/tcp, :::1111->80/tcp   haproxy

访问测试

Dockerfile文件写haproxy
Dockerfile文件写haproxy

上一篇:将keystone安全策略从http优化至https


下一篇:【长期】常用命令记录