当前位置: 首页 > news >正文

dockerfile基于alpine构建haproxy

1. 结构目录

[root@localhost ~]# tree haproxy/
haproxy/
├── dockerfile
└── files
    ├── env.txt
    ├── haproxy-2.5.0.tar.gz
    ├── haproxycfg.sh
    ├── install.sh
    └── sysctl.conf

1 directory, 6 files
[root@localhost ~]# 

2. 编写dockerfile

[root@localhost ~]# cd haproxy/
[root@localhost haproxy]# cat dockerfile 
FROM alpine

LABEL MAINTAINER "lry 1@2qq.com"

ENV version 2.5.0

ADD files/haproxy-${version}.tar.gz /tmp/
ADD files/install.sh /tmp/
ADD files/haproxycfg.sh /tmp/
ADD files/sysctl.conf /tmp/

RUN /tmp/install.sh
ENTRYPOINT /tmp/haproxycfg.sh

[root@localhost haproxy]#

3. 配置文件

[root@localhost haproxy]# cd files/
[root@localhost files]# ls
env.txt  haproxy-2.5.0.tar.gz  haproxycfg.sh  install.sh  sysctl.conf

//RS ip
[root@localhost files]# cat  env.txt 
RSs=172.17.0.2 172.17.0.3

//内核参数
[root@localhost files]# cat 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 files]# cat 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
addgroup 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/haproxy-${version}
make TARGET=linux-musl USE_OPENSSL=1 USE_ZLIB=1 USE_PCRE=1 
make install PREFIX=/usr/local/haproxy
cp haproxy  /usr/sbin/
mkdir /etc/haproxy
apk del gcc make
rm -rf /tmp/haproxy-${version}/ /tmp/install.sh

//haproxy配置文件
[root@localhost files]# cat haproxycfg.sh 
#!/bin/sh

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 $RSs;do
cat >> /etc/haproxy/haproxy.cfg <<EOF
    server web$count $rs_ip:80 check inter 2000 fall 5
EOF
let count++
done

haproxy -f  /etc/haproxy/haproxy.cfg -db

[root@localhost files]#

4. 构建镜像

[root@localhost haproxy]# docker build -t haproxy:v1.0 .
.........忽略N行
(1/10) Purging gcc (10.3.1_git20211027-r0)
(2/10) Purging binutils (2.37-r3)
(3/10) Purging libatomic (10.3.1_git20211027-r0)
(4/10) Purging libgomp (10.3.1_git20211027-r0)
(5/10) Purging libgphobos (10.3.1_git20211027-r0)
(6/10) Purging make (4.3-r0)
(7/10) Purging mpc1 (1.2.1-r0)
(8/10) Purging mpfr4 (4.1.0-r0)
(9/10) Purging isl22 (0.22-r0)
(10/10) Purging gmp (6.2.1-r0)
Executing busybox-1.34.1-r3.trigger
OK: 35 MiB in 45 packages
Removing intermediate container 7d2ec6aa8327
 ---> c9813eff9a55
Step 9/9 : ENTRYPOINT /tmp/haproxycfg.sh
 ---> Running in 54c3e78ce2d5
Removing intermediate container 54c3e78ce2d5
 ---> bc383740a37a
Successfully built bc383740a37a
Successfully tagged haproxy:v1.0

[root@localhost haproxy]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
haproxy      v1.0      bc383740a37a   2 minutes ago   85.1MB
alpine       latest    c059bfaa849c   2 weeks ago     5.59MB
[root@localhost haproxy]#

6. 创建容器

//创建apache和nginx容器
[root@localhost haproxy]# docker run -d --name apache httpd
Unable to find image 'httpd:latest' locally
latest: Pulling from library/httpd
e5ae68f74026: Pull complete 
bc36ee1127ec: Pull complete 
d3576f2b6317: Pull complete 
f1aa5f54b226: Pull complete 
aa379c0cedc2: Pull complete 
Digest: sha256:fba8a9f4290180ceee5c74638bb85ff21fd15961e6fdfa4def48e18820512bb1
Status: Downloaded newer image for httpd:latest
dc5872009f97f546fbd54856d7a40c65ff13c1c60af0122470a18a357cb36cea
[root@localhost haproxy]# docker run -d --name nginx nginx
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
e5ae68f74026: Already exists 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
1f4fed0191c810938072da3c6bfc70d2a73ea7ff3b4bcbf7144ce6a51896405b
[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
1f4fed0191c8   nginx     "/docker-entrypoint.…"   23 seconds ago       Up 22 seconds       80/tcp    nginx
dc5872009f97   httpd     "httpd-foreground"       About a minute ago   Up About a minute   80/tcp    apache
[root@localhost haproxy]#
//查看apache ip
[root@localhost haproxy]# docker inspect apache 
......省略N行
                "NetworkID": "18039c5e5ccec90bd886bf585700422392db62dbd2258ca378cf4eee1abc3f9f",
                    "EndpointID": "00bf38a60b64a858bbd35e445cc76e51ad70c58e6c11504d6c2a991c81b829af",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.2",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
......省略N行
//查看nginx ip
[root@localhost haproxy]# docker inspect nginx
......省略N行
                "NetworkID": "18039c5e5ccec90bd886bf585700422392db62dbd2258ca378cf4eee1abc3f9f",
                    "EndpointID": "0c34b972284c0dca798fa15173aee2c2da333b90f484bf161f3f68ab8cff2fbd",
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
......省略N行
//填写RS ip
[root@localhost haproxy]# cat files/env.txt 
RSs=172.17.0.2 172.17.0.3
[root@localhost haproxy]# 

//利用haproxyv:1.0镜像创建容器
[root@localhost haproxy]# docker run -d --name haproxy -p 8080:80 --env-file files/env.txt haproxy:v1.0 
dc201555c9810abebb6037319db127d99db72cf64161e1fbefc6c5f3c7eefb78
[root@localhost haproxy]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
dc201555c981   haproxy:v1.0   "/bin/sh -c /tmp/hap…"   8 seconds ago    Up 7 seconds    0.0.0.0:8080->80/tcp, :::8080->80/tcp   haproxy
1f4fed0191c8   nginx          "/docker-entrypoint.…"   10 minutes ago   Up 10 minutes   80/tcp                                  nginx
dc5872009f97   httpd          "httpd-foreground"       10 minutes ago   Up 10 minutes   80/tcp                                  apache
[root@localhost haproxy]# docker port haproxy 
80/tcp -> 0.0.0.0:8080
80/tcp -> :::8080
[root@localhost haproxy]#

7. 访问

[root@localhost ~]# curl 192.168.35.135:8080
<html><body><h1>It works!</h1></body></html>
[root@localhost ~]# curl 192.168.35.135:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@localhost ~]#

相关文章:

  • 常见的服务CPU过高Arthas快速排查问题详细笔记
  • DeepSeek + Ollama 部署自己本地的 AI 大模型(Windows,AMD 显卡也能用)
  • Linux虚拟机的三种网络连接方式
  • Spring Cloud工程搭建
  • python 进程池的基本使用
  • 【落羽的落羽 数据结构篇】顺序结构的二叉树——堆
  • 重学SpringBoot3-Spring WebFlux之SSE服务器发送事件
  • 图神经网络
  • 宝塔Linux面板配置环境 + 创建站点
  • 时间转换(acwing)c/c++/java/python
  • 【代码软件 | vs2019】vs2019+Qt5.12.12开发环境 的下载、安装详细介绍
  • 《解锁AI密码,机器人精准感知环境不再是梦!》
  • Linux 命令大全完整版(04)
  • 嵌入式八股文(五)硬件电路篇
  • 从零实现机器人自主避障
  • [内网基础] 内网基础知识 —— Windows 工作组
  • 在生产环境中部署和管理 PostgreSQL:实战经验与最佳实践
  • java中ArrayList用法
  • 引入elementUI时报错undefined is not an object (evaluating ‘h.a.prototype‘)
  • SQLMesh 系列教程9- 宏变量及内置宏变量
  • 素材设计做的好的网站有哪些/免费b站软件推广网站2023
  • wordpress网页设计价格设计/宁波关键词排名优化
  • 网站后台使用说明/成都百度推广
  • 贵阳专用网站建设/京津冀协同发展
  • 重庆做模块网站/百度app下载官方免费下载最新版
  • 开发公司产品部课件/seo查询友情链接