已经写过一个openresty 使用lua-resty-shell 执行shell 脚本的demo,但是实际上我们可能是多节点运行,
同时需要负载均衡的机制。
lua-resty-shell 支持unix socket 以及tcp soket 的管理,但是在测试的时候发现tcp 有问题,所以只好
使用unix socket了,通过nginx 的stream 将unix 转为tcp,因为是测试,使用docker-compose 进行缩放
以及负载均衡的处理
说明: 可以同时参考 https://www.cnblogs.com/rongfengliang/p/10079432.html

环境准备

  • docker-compose 文件
version: "3"
services:
app:
build: ./
ports:
- "8080:80"
volumes:
- "./app/:/opt/app/"
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
backend:
build: ./
volumes:
- "./nginx-back.conf:/usr/local/openresty/nginx/conf/nginx.conf"
  • 代码说明
    dockerfile 主要是进行环境的准备,包括镜像的准备&&启动需要的服务
FROM openresty/openresty:alpine-fat
LABEL author="1141591465@qq.com"
WORKDIR /sockproc
COPY ./sockproc/ /sockproc/
RUN make sockproc
COPY entrypoint.sh /entrypoint.sh
COPY sockproc.sh /sockproc.sh
COPY shell.lua /usr/local/openresty/lualib/resty/shell.lua
ENTRYPOINT [ "/entrypoint.sh" ]

nginx 配置要两个,一个是调用端的,一个是后端真正执行服务的
调用端,很简单,就是使用shell.lua 的封装进行调用

worker_processes 1;
user root;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
# resolver 127.0.0.11;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
default_type text/html;
index index.html; }
location /test {
resolver 127.0.0.11;
content_by_lua_block {
require("app").call();
}
}
location /loop {
content_by_lua_block {
require("app").loop();
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

后端服务nginx配置,就是unix 转tcp 服务

worker_processes 1;
user root;
events {
worker_connections 1024;
}
stream {
server {
listen 13000;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass unix:/tmp/shell.sock;
}
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_code_cache off;
gzip on;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
lua_package_path '/opt/app/?.lua;;';
server {
listen 80;
server_name localhost;
charset utf-8;
root html;
default_type text/html;
location / {
default_type text/html;
index index.html; }
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
  • lua 调用代码
    app/app.lua
local shell = require("resty.shell")
local log = ngx.log
local ERR = ngx.ERR
local delay = 5
local handler
handler = function (premature,param)
-- do some routine job in Lua just like a cron job
if premature then
return
end
log(ERR, "param is : ", param)
ngx.timer.at(delay, handler,"again run... dalongrong")
end local args = {
socket = {
host="backend",
port=13000
}
} function call()
local status, out, err = shell.execute("cat /proc/sys/kernel/random/uuid", args)
ngx.say(out)
end function loop()
local ok, err = ngx.timer.at(delay, handler,"dalong demo timer init")
end return {
call=call,
loop=loop
}

启动&&测试

  • 启动
docker-compose up -d
docker-compose scale backend=3

参考资料

https://www.cnblogs.com/rongfengliang/p/10079432.html
https://github.com/rongfengliang/lua-resty-shell-docker--multi-running
https://github.com/juce/lua-resty-shell

 
 
 
 

最新文章

  1. dos命令行 指令
  2. Android 学习第17课,使用文件的数据存储(4种存储模式)
  3. 龙威零式_团队项目例会记录_18 (Beta架构讨论)
  4. mysql数据库设计
  5. RabbitMQ的安装使用
  6. java时间库Joda-Time
  7. thinkphpcmf框架中的短信验证!
  8. 第五篇、C_二叉搜索树
  9. 2D游戏编程6—windows程序模板
  10. Visual Studio 2017 RC 初探安装
  11. 【C#】组件分享:FormDragger-窗体拖拽器
  12. 前端布局之Flex语法
  13. asp.net core后台系统登录的快速构建
  14. IDEA启动后页面没有tomcat server选项,显示灰色问号和红叉不能使用
  15. C# 生成二维码 QRCoder
  16. 第56章 Client - Identity Server 4 中文文档(v1.0.0)
  17. 详解Linux双网卡绑定之bond0
  18. SQL优化工具SQLAdvisor使用
  19. 关于python中的is和==的区别
  20. Flask:redirect()函数

热门文章

  1. SQL-35 对于表actor批量插入如下数据,如果数据已经存在,请忽略,不使用replace操作
  2. Centos7安装配置MySQL5.7
  3. 四、使用汇编编写LED裸机驱动
  4. mongodb启动出现Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds,giving up
  5. Day5作业及默写
  6. tomcat 配置成服务
  7. PHP实现二叉树的深度优先遍历(前序、中序、后序)和广度优先遍历(层次)
  8. python学习笔记第一周
  9. webgl opengl教程样例
  10. HBase使用压缩存储(snappy)