已经写过一个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

 
 
 
 

lua-resty-shell 多任务执行的更多相关文章

  1. shell script 执行常用的两种方式

    2016-11-17 直接输入脚本名执行 ./script #!/bin/bash# /root/shell/001 # 2016-11-17 test for script running name ...

  2. shell的执行顺序问题

    &&,||,(),{},& 五个符号的运用 shell脚本执行命令的时候,有时候会依赖于前一个命令是否执行成功.而&&和||就是用来判断前一个命令执行效果的. ...

  3. shell各种执行方式区别

    shell 脚本各种执行方式(source ./*.sh, . ./*.sh, ./*.sh)的区别 原文出处:http://blog.csdn.net/dance_rise/article/deta ...

  4. shell 后台执行命令

    shell 后台执行命令方法: 1. nohup cmd &          后台会生成 nohup.out 文件 2.cmd >/路径/xx.log &   后台生成 xx. ...

  5. windows下建立文件的换行符^M导致linux下的shell脚本执行错误的解决方式

    常常在windows下编辑的文件远程传送到linux下的时候每行末尾都会出现^M.这将导致shell脚本执行错误,主要是由于dos下的编辑器和linux下的编辑器对文件末行的回车符处理不一致导致. 主 ...

  6. 【Linux】 环境变量与shell配置&执行

    ■ 变量与环境变量 shell环境通常存在很多变量,变量可以通过echo $VAR或${VAR}的方式查看.set命令可以查看当前环境中的所有变量(包括一般的自定义变量和环境变量) 变量的设置通过简单 ...

  7. shell命令执行hive脚本(hive交互,hive的shell编程)

    Hive执行方式 Hive的hql命令执行方式有三种: 1.CLI 方式直接执行 2.作为字符串通过shell调用hive –e执行(-S开启静默,去掉"OK","Tim ...

  8. 远程shell脚本执行工具类

    /** * 远程shell脚本执行工具类 */public class RemoteShellExecutorUtils { private static final Logger logger = ...

  9. shell脚本执行错误 $'\r':command not found

    shell脚本执行错误 $'\r':command not found Linux下有命令dos2unix 可以用一下命令测试 vi -b filename 我们只要输入dos2unix *.sh就可 ...

随机推荐

  1. Android : 获取声卡信息的测试代码

    完整的编译包(android平台): 链接:http://pan.baidu.com/s/1qXMTT7I 密码:2bow /* * ALSA parameter test program * * C ...

  2. C++基础知识:构造与析构

    1.构造函数的定义: C++中的类可以定义与类名相同的特殊成员函数这种与类名相同的成员函数叫做构造函数构造函数在定义时可以有参数,但是没有任何返回类型的声明 2.构造函数的调用: 一般情况下C++编译 ...

  3. js重写trim()方法

    最近项目中出现去空格的需求,我本地是IE11,使用trim()函数的时候,是无法识别的.因此重写String的trim()方法. 1.要求 可以去除首位全角,半角空格. 2.对   能去除所有 Str ...

  4. day 67 django 之ORM 基础安装

    一 ORM的基础部分 1 ORM的概念 对象关系映射(Object Relational Mapping(映射),简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 2   ...

  5. 四、使用汇编编写LED裸机驱动

    1. 确定硬件连接 打开OK6410底板电路图,找到LED,可以发现NLEDx为0时LED点亮. 找到LED的控制引脚,发现LED控制引脚通过连接器连到了核心板: 打开核心板电路图,找到对应的连接器中 ...

  6. 【转】strmbasd.lib(dllentry.obj) : error LNK2001: 无法解析的外部符号"int g_cTemplates"

    加入了DirectShow的基类链接库后,如果此时编译就会出现以下编译错误: strmbasd.lib(wxutil.obj) : error LNK2019: 无法解析的外部符号 __imp__ti ...

  7. 初识Linux------文件管理

    初识Linux------文件管理 说明 由于本章的命令比较多,先对命令有一个整体的说明 命令的一般格式:命令名[选项][参数1][参数2]…… 命令名由小写的英文字母构成,往往是表示相应功能的英文单 ...

  8. vue项目功能

    vue-router         {            path: '/',            name: 'home',            // 路由的重定向            ...

  9. exe程序嵌入Winform窗体

    1.新建winform程序,添加一个Panel控件和一个button控件,winform窗体命名为:Mainform: 2.新建一个类文件,方便引用,命名为:exetowinform: 3.Mainf ...

  10. Ubuntu 18.04开启TCP网络协议BBR加速的方法(Google BBR 拥塞控制算法)

    TCP BBR 是Google给出的一个改良版的tcp网络协议,相当于在已有TCP协议的基础上打了个补丁的意思,这个改良版TCP协议对拥塞控制有很好的支持,对于网络较差的环境有不错的应用场景,当然这里 ...