qidao123.com ToB IT社区-企服评测·应用市场

标题: dockerfile构建haproxy [打印本页]

作者: 汕尾海湾    时间: 2025-10-26 02:58
标题: dockerfile构建haproxy
1. 结构目次
  1. [root@localhost ~]# tree haproxy/
  2. haproxy/
  3. ├── dockerfile
  4. └── files
  5.     ├── haproxy-2.5.0.tar.gz
  6.     ├── haproxy.cfg
  7.     ├── install.sh
  8.     └── start.sh
  9. 1 directory, 5 files
  10. [root@localhost ~]#
复制代码
  1. [root@localhost ~]# cd haproxy/
  2. [root@localhost haproxy]# ls
  3. dockerfile  files
  4. [root@localhost haproxy]# cat dockerfile
  5. FROM centos
  6.   
  7. LABEL MAINTAINER='pengyudong 1@2.com@qq.com'
  8. ENV version 2.5.0
  9. ADD files/haproxy-${version}.tar.gz /usr/src
  10. ADD files/haproxy.cfg /etc/haproxy/
  11. ADD files/install.sh /tmp/
  12. ADD files/start.sh /usr/local/
  13. RUN ["/bin/bash","-c","/tmp/install.sh"]
  14. EXPOSE 80 8189
  15. WORKDIR /usr/local/haproxy
  16. CMD ["/usr/local/start.sh"]
  17. [root@localhost haproxy]#
  18. [root@localhost haproxy]# cd files/
  19. [root@localhost files]# ls
  20. haproxy-2.5.0.tar.gz  haproxy.cfg  install.sh  start.sh
  21. [root@localhost files]# vim start.sh
  22. [root@localhost files]# cat start.sh
  23. #!/bin/sh
  24. /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
  25. /bin/bash
  26. [root@localhost files]# cat install.sh
  27. #!/bin/bash
  28. rm -rf /etc/yum.repos.d/*
  29. curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-$(awk -F'"' 'NR==2{print $2}' /etc/os-release).repo
  30. yum -y install make gcc gcc-c++ pcre-devel bzip2-devel openssl-devel systemd-devel
  31. useradd -r -M -s /sbin/nologin haproxy
  32. cd /usr/src/haproxy-${version}
  33. cd /usr/src/haproxy-${version} && make clean && \
  34. make -j $(nproc) TARGET=linux-glibc USE_OPENSSL=1 USE_PCRE=1 USE_SYSTEMD=1 && \
  35. make install PREFIX=/usr/local/haproxy && \
  36. yum -y remove make gcc gcc-c++ && \
  37. rm -rf /usr/src/haproxy-*
  38. [root@localhost files]# cat haproxy.cfg
  39. #--------------全局配置----------------
  40. global
  41.     log 127.0.0.1 local0  info
  42.     #log loghost local0 info
  43.     maxconn 20480
  44. #chroot /usr/local/haproxy
  45.     pidfile /var/run/haproxy.pid
  46.     #maxconn 4000
  47.     user haproxy
  48.     group haproxy
  49.     daemon
  50. #---------------------------------------------------------------------
  51. #common defaults that all the 'listen' and 'backend' sections will
  52. #use if not designated in their block
  53. #---------------------------------------------------------------------
  54. defaults
  55.     mode http
  56.     log global
  57.     option dontlognull
  58.     option httpclose
  59.     option httplog
  60.     #option forwardfor
  61.     option redispatch
  62.     balance roundrobin
  63.     timeout connect 10s
  64.     timeout client 10s
  65.     timeout server 10s
  66.     timeout check 10s
  67.     maxconn 60000
  68.     retries 3
  69. #--------------统计页面配置------------------
  70. listen admin_stats
  71.     bind 0.0.0.0:8189
  72.     stats enable
  73.     mode http
  74.     log global
  75.     stats uri /haproxy_stats
  76.     stats realm Haproxy\ Statistics
  77.     stats auth admin:admin
  78.     #stats hide-version
  79.     stats admin if TRUE
  80.     stats refresh 30s
  81. #---------------web设置-----------------------
  82. listen webcluster
  83.     bind 0.0.0.0:80
  84.     mode http
  85.     #option httpchk GET /index.html
  86.     log global
  87.     maxconn 3000
  88.     balance roundrobin
  89.     cookie SESSION_COOKIE insert indirect nocache
  90.     server web01 172.17.0.3:80 check inter 2000 fall 5
  91.     server web02 172.17.0.4:80 check inter 2000 fall 5
  92. [root@localhost files]#
复制代码
2. 构建镜像
  1. [root@localhost ~]# docker build -t pengyudong/haproxy:v1.0 haproxy/
  2. root@localhost ~]# docker images
  3. pengyudong/haproxy   v1.0      8819e319cf32   19 minutes ago   418MB
  4. [root@localhost ~]#
复制代码
3. 创建容器
  1. [root@localhost ~]# docker run -itd --name haproxy -p 8080:80 haproxy:v1.0
  2. [root@localhost ~]# docker exec -it haproxy /bin/bash
  3. [root@ce02ae7f027e haproxy]# ss -antl
  4. State       Recv-Q      Send-Q           Local Address:Port            Peer Address:Port      Process      
  5. LISTEN      0           128                    0.0.0.0:80                   0.0.0.0:*                     
  6. LISTEN      0           128                    0.0.0.0:8189                 0.0.0.0:*                     
  7. [root@ce02ae7f027e haproxy]#
  8. [root@localhost ~]# docker run -itd --name httpd pengyudong/httpd
  9. 462bd9babdcdac7f2abbb6da551858466659602bb3031129bb5b5de6e71853eb
  10. [root@localhost ~]#  docker run -itd --name nginx nginx
  11. 0dffef28f17692db84e93c7caece9fbfe303d66b042bc1f40f13e0304c09227f
  12. [root@localhost ~]# docker ps
  13. CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS                                             NAMES
  14. 0dffef28f176   nginx                     "/docker-entrypoint.…"   8 minutes ago    Up 8 minutes    80/tcp                                            nginx
  15. 462bd9babdcd   pengyudong/httpd          "/usr/local/apache/b…"   8 minutes ago    Up 8 minutes    80/tcp                                            httpd
  16. ce02ae7f027e   pengyudong/haproxy:v1.0   "/usr/bin/start.sh"      22 minutes ago   Up 22 minutes   8189/tcp, 0.0.0.0:8080->80/tcp, :::8080->80/tcp   haproxy
  17. [root@localhost ~]#
复制代码



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com ToB IT社区-企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.5