已经写过一个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. 第六节 静态的(static)和单例模式

    main函数 主函数是一个特殊的函数,作为程序的入口,可以被jvm(虚拟器)调用 主函数的定义 public 表示该函数的访问权限是最大的. static 代表主函数随着类的加载就已经存在了. voi ...

  2. AOP 实现自定义注解

    1.自定义注解2.编写 AOP3.测试 1.自定义注解 package com.base.yun.spring.aop; import java.lang.annotation.Documented; ...

  3. L256 阅读理解

    1主旨题 2细节题 题干关键词 人名,地名,专有名词,时间,和主题相关的核心词汇,带特殊标点的词汇 干扰项 词意猜测

  4. c# 中foreach 循环

    使用foreach循环可以迭代数组或一个集合对象, 1.通过foreach 循环输出整型数组中的数组: 2.通过for循环输出整型数组中的元素: 3.foreach 循环设置数组元素的计算器: cla ...

  5. day 33 线程池有关的

    # cpu 的核心数# import os# print(os.cpu_count()) ## 爬虫的进程和线程的应用# 第一步 虚拟一个浏览器下载 在cmd 里输入 pip install requ ...

  6. 更换Homebrew为中科大源

    官网:https://brew.sh/index_zh-cn /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha's staff(几何找规律)

    Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in ...

  8. Proxy --支持的拦截操作篇

    下面是 Proxy 支持的拦截操作一览. 对于可以设置.但没有设置拦截的操作,则直接落在目标对象上,按照原先的方式产生结果. (1)get(target, propKey, receiver) 拦截对 ...

  9. Python之路,第四篇:Python入门与基础4

    Python3 字符串 字符串是一个有序的字符序列 如何表示一个字符串: 在非注释中凡是用引号括起来的部分都是字符串: ‘  单引号     ”  双引号      ‘’‘  三单引号    “”“ ...

  10. 检测IP地址冲突的shell脚本-check_server_ip_conflict.sh

    check_server_ip_conflict.sh 使用arping获取对应IP地址的MAC地址,如果和预料的不一致则报警: #!/bin/bash epg_addr_01="00:50 ...