已经写过一个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. Linux如何从零开始搭建nfs服务器(centOS6)

    Server端 1.打印系统版本 cat /etc/redhat-release uname -r uname -m 2.检查是否安装NFS服务 rpm -aq nfs-utils rpcbind L ...

  2. 《深入分析Java Web技术内幕》读书笔记 - 第1章 深入Web请求过程

    第1章 深入Web请求过程 1 1.1 B/S网络架构概述 2 基于统一的应用层协议HTTP来交互数据. 1.2 如何发起一个请求 4 HTTP连接本质是建立Socket连接.请求实现方式:工具包如H ...

  3. L327 找灵魂伴侣

    Looking for the Perfect Partner I'm sure we all remember a time when we fell in love. For some it wa ...

  4. js 数组去重的几种方式及原理

    let arr = [1,1,'true','true',true,true,15,15,false,false, undefined,undefined, null,null, NaN, NaN,' ...

  5. jquery原理集合

    人生就是一场梦 完全理解jquery算的是...水平  (jquery常用方法) jQuery分析源码正确方法 详细分析jQuery2.0.3 new的原理 玩转jquery

  6. DRF-Rest_Framework 学习文档

    序列化器(serializer) 定义Serializer 1. 定义方法 Django REST framework中的Serializer使用类来定义,须继承自rest_framework.ser ...

  7. day 03 变量与基本数据类型

    变量的命名规范 一:变量命名的大前提,应该能够反映出变量值所记录内容 1:变量名只能由数字,字母,下划线组成 2:变量名不能以数字开头 3:变量名不能使用系统的关键字,不然可能会报错 二:变量名的命名 ...

  8. 图片宽度为2000px,使图片在电脑不同分辨率下都水平居中,不压缩。

    <div style="postion:relative;width:100%;overflow:hidden;"> <img src="路径" ...

  9. 整理有关浏览器兼容性的css样式

    去掉IE自带的删除功能的×号 input::-ms-clear{display:none;} 去掉IE自带密码框的眼睛样式 input::-ms-reveal{display:none;}

  10. async/await学习笔记

    async/await 的目的是简化使用 promises 的写法.     让我们来看看下面的例子: // 一个标准的 JavaScript 函数 function getNumber1() { r ...