Docker构建openresty镜像,并安装ngx_http_dyups_module模块,


一、ngx_http_dyups_module模块

ngx_http_dyups_module 是一个可以动态更新nginx的Upstream的模块,目前已加入了淘宝的Tengine。

ngx_http_dyups_module 模块的git地址: https://github.com/yzprofile/ngx_http_dyups_module

查看其readme,安装方式如下

$ git clone git://github.com/yzprofile/ngx_http_dyups_module.git

$ ./configure --add-module=./ngx_http_dyups_module   # 需要将其放在nginx的源码目录下,跟随nginx一起编译。

此处我们选择将该模块与OpenResty一起编译。

懒得看过程的可以直接去这里:https://github.com/greper/docker-openrestry-with-dyups ,构建与运行镜像。

二、OpenResty

OpenResty 是一个基于 Nginx 与Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。

我们需要找到OpenResty的docker构建的dockerfile,然后将ngx_http_dyups_module一起编译成docker镜像。

openresty在dockerhub上的地址:https://hub.docker.com/r/openresty/openresty

openresty的docker页面


翻译过来如上图,如果要自定义编译与安装模块,需要使用红框下面这四个dockerfile的其中之一。

此处我们选择xenial,也就是ubuntu为基础镜像的openresty源码编译的dockerfile。

点击进去 可以看到如下


openresty源码编译的dockerfile


下图是openresty编译命令代码

其中红框标识的是openresty的编译命令。

红框上面是准备工作,下载openresty源码等;

红框下面是清理源码包,及可以单独安装的其他模块。

&& 的作用是将多个命令聚合成一条命令执行,可以减少docker的构建层数。

要安装ngx_http_dyups_module就要从上图红框处着手修改。

三、修改dockerfile

我们将上述执行的一条命令,分成三部分

一、准备工作,下载openresty源码包

&& cd /tmp \

&& if [ -n "${RESTY_EVAL_PRE_CONFIGURE}" ]; then eval $(echo ${RESTY_EVAL_PRE_CONFIGURE}); fi \

&& curl -fSL https://www.openssl.org/source/openssl-${RESTY_OPENSSL_VERSION}.tar.gz -o openssl-${RESTY_OPENSSL_VERSION}.tar.gz \

&& tar xzf openssl-${RESTY_OPENSSL_VERSION}.tar.gz \

&& curl -fSL https://ftp.pcre.org/pub/pcre/pcre-${RESTY_PCRE_VERSION}.tar.gz -o pcre-${RESTY_PCRE_VERSION}.tar.gz \

&& tar xzf pcre-${RESTY_PCRE_VERSION}.tar.gz \

&& curl -fSL https://openresty.org/download/openresty-${RESTY_VERSION}.tar.gz -o openresty-${RESTY_VERSION}.tar.gz \

&& tar xzf openresty-${RESTY_VERSION}.tar.gz

二、将自定义模块复制进来,将模块加入编译配置

./modules/目录下放,ngx_http_dyups_module/ 模块源码

加入 --add-module=./ngx_http_dyups_module 编译配置,然后编译openresty

#-----------------这里复制自定义模块进来------------------------

ADD ./modules  /tmp/openresty-${RESTY_VERSION}/

#-----------------编译配置------------------------

RUN cd /tmp/openresty-${RESTY_VERSION}/ \

&&./configure -j${RESTY_J} ${_RESTY_CONFIG_DEPS} ${RESTY_CONFIG_OPTIONS} ${RESTY_CONFIG_OPTIONS_MORE} \

#-----------------这里追加自定义模块------------------------

--add-module=./ngx_http_dyups_module \

#-----------------开始编译------------------------

&& make -j${RESTY_J} \

&& make -j${RESTY_J} install

#-----------------编译结束--------------------------

三、继续执行剩余的指令

# 清理和安装剩余模块

RUN cd /tmp \

&& rm -rf \

openssl-${RESTY_OPENSSL_VERSION} \

openssl-${RESTY_OPENSSL_VERSION}.tar.gz \

openresty-${RESTY_VERSION}.tar.gz openresty-${RESTY_VERSION} \

pcre-${RESTY_PCRE_VERSION}.tar.gz pcre-${RESTY_PCRE_VERSION} \

&& curl -fSL https://github.com/luarocks/luarocks/archive/${RESTY_LUAROCKS_VERSION}.tar.gz -o luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \

&& tar xzf luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \

&& cd luarocks-${RESTY_LUAROCKS_VERSION} \

&& ./configure \

--prefix=/usr/local/openresty/luajit \

--with-lua=/usr/local/openresty/luajit \

--lua-suffix=jit-2.1.0-beta3 \

--with-lua-include=/usr/local/openresty/luajit/include/luajit-2.1 \

&& make build \

&& make install \

&& if [ -n "${RESTY_EVAL_POST_MAKE}" ]; then eval $(echo ${RESTY_EVAL_POST_MAKE}); fi \

&& rm -rf luarocks-${RESTY_LUAROCKS_VERSION} luarocks-${RESTY_LUAROCKS_VERSION}.tar.gz \

&& if [ -n "${RESTY_ADD_PACKAGE_BUILDDEPS}" ]; then DEBIAN_FRONTEND=noninteractive apt-get remove --purge "${RESTY_ADD_PACKAGE_BUILDDEPS}" ; fi \

&& DEBIAN_FRONTEND=noninteractive apt-get autoremove -y \

&& ln -sf /dev/stdout /usr/local/openresty/nginx/logs/access.log \

&& ln -sf /dev/stderr /usr/local/openresty/nginx/logs/error.log

以上就是将官方的dockerfile中的编译命令,拆分成三部分,在编译openresty中加入ngx_http_dyups_module模块的编译。


四、使用docker-compose构建镜像,并运行

我将以上dockerfile做成了docker-compose脚本,放在github上

地址:https://github.com/greper/docker-openrestry-with-dyups

step1 准备工作

安装docker、docker-compose

step2 构建镜像

git clone https://github.com/greper/ docker-openrestry-with-dyups.git

cd docker

docker-compose build

docker-compose up -d

step3 验证dyups效果

curl http://127.0.0.1:48081/detail

host1 server 127.0.0.1:8088 weight=1 max_conns=0 max_fails=1 fail_timeout=10 backup=0 down=0

host2 server 127.0.0.1:8089 weight=1 max_conns=0 max_fails=1 fail_timeout=10 backup=0 down=0

相关内容

    暂无相关文章