用openresty搭一个类似GAE/SAE的PAAS的想法

90 views
Skip to first unread message

Simon

unread,
May 22, 2012, 9:55:20 AM5/22/12
to openresty
在openresty基础上构建沙箱是否可行?

我想最大的问题在于nginx的多进程和IO复用模型,虽然性能很好,但是由于不同用户只是通过协程进行隔离,协程是非抢占式的,一旦有某个用户脚本恶
意占用CPU,那么平台怎么能识别并限制他的资源呢?

另外_G变量是进程内全局共享的,也容易成为一个薄弱点。

请各位老师指教,:)


--
Best Regard!

Zoom.Quiet

unread,
May 22, 2012, 10:03:30 AM5/22/12
to open...@googlegroups.com
在 2012年5月22日 下午9:55,Simon <big...@gmail.com> 写道:
> 在openresty基础上构建沙箱是否可行?
>
> 我想最大的问题在于nginx的多进程和IO复用模型,虽然性能很好,但是由于不同用户只是通过协程进行隔离,协程是非抢占式的,一旦有某个用户脚本恶
> 意占用CPU,那么平台怎么能识别并限制他的资源呢?
>
> 另外_G变量是进程内全局共享的,也容易成为一个薄弱点。
>

- 俺听过几个 PaaS 的架构讲座,有以下几个方向:
- lua 脚本预判,对恶意代码,部署前识别并拒絶
- 虚拟机,不同应用是不同的 nginx 实例空间,限定资源,无全局数据可以跨应用访问
- nginx 自管,进行内存申用的次数限定,阻止恶意代码占用整体资源


> 请各位老师指教,:)
>


--
人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
俺: http://about.me/zoom.quiet
文字协议: http://creativecommons.org/licenses/by-sa/2.5/cn/

Weiqiang

unread,
May 22, 2012, 11:01:55 PM5/22/12
to open...@googlegroups.com
呵呵,我已经把openresty用在前端的应用路由和负载均衡上了。



2012/5/22 Zoom.Quiet <zoom....@gmail.com>

agentzh

unread,
May 22, 2012, 11:09:38 PM5/22/12
to open...@googlegroups.com
2012/5/22 Simon <big...@gmail.com>:
> 在openresty基础上构建沙箱是否可行?
>

目前还有不少工作需要做 :)

> 我想最大的问题在于nginx的多进程和IO复用模型,虽然性能很好,但是由于不同用户只是通过协程进行隔离,协程是非抢占式的,一旦有某个用户脚本恶
> 意占用CPU,那么平台怎么能识别并限制他的资源呢?
>

ngx_lua 的抢占式协程支持一直是我们的一个 TODO :) 我们可以基于 Lua 语言的 Debug hook
来做。类似地,基于相同的技术可以实现计算资源的管配,强制中止 CPU 资源占用过多的协程。

具体到用户界面上,即会通过 lua_abort_after 和 lua_yield_after 这两条配置指令来控制。

> 另外_G变量是进程内全局共享的,也容易成为一个薄弱点。
>

_G 和 Lua module 都是整个 VM 共享的,因为涉及 lua registry,这一块的隔离确实需要做更多的工作 :)

Regards,
-agentzh

Weiqiang

unread,
May 22, 2012, 11:17:05 PM5/22/12
to open...@googlegroups.com
我倒是有个想法,就是采用类似Java版GAE的方法来做
应用独占一个openresty服务器。然后用lxc+cgroup这样的技术来实现应用服务器的隔离和QoS保证。

前端呢也用openresty来做应用路由(路由信息保存在shdict里,通过rest接口实现动态配置)


李伟强

2012/5/23 agentzh <age...@gmail.com>

Weiqiang

unread,
May 22, 2012, 11:18:27 PM5/22/12
to open...@googlegroups.com
呵呵,刚才没说清楚,其实我也不了解真正的GAE是怎么做的。只是我的猜测:-)


2012/5/23 Weiqiang <lwei...@gmail.com>

Simon

unread,
May 23, 2012, 4:23:32 AM5/23/12
to openresty
如果是给每个请求一个VM,倒是能简单做到应用隔离,只要做好资源管理就可以了。

On May 23, 11:09 am, agentzh <agen...@gmail.com> wrote:
> 2012/5/22 Simon <bigp...@gmail.com>:

agentzh

unread,
May 23, 2012, 6:39:31 AM5/23/12
to open...@googlegroups.com
2012/5/23 Simon <big...@gmail.com>:
> 如果是给每个请求一个VM,倒是能简单做到应用隔离,只要做好资源管理就可以了。
>

那样的话运行时开销会明显增大 :)

Regards,
-agentzh

kindy

unread,
May 24, 2012, 5:38:51 AM5/24/12
to open...@googlegroups.com
也许,
可以考虑每 server 一个 VM,还是有可能的。

不过,
也许有强烈的需求才有意义。

消耗还是较大的。


Regards,
-agentzh




--

林青(Kindy Lin)

Anfernee Gui

unread,
May 24, 2012, 7:42:32 AM5/24/12
to open...@googlegroups.com
PaaS还是比较复杂的。请大家关注Cloud Foundry,CF是完全open source的,可以自己搭一个玩玩。
做个广告:如果大家对CF在上海的职位有兴趣,可以发简历给我。



--
Best Regards,
Yongkun

Zoom.Quiet

unread,
May 24, 2012, 8:02:48 AM5/24/12
to open...@googlegroups.com
2012/5/24 Anfernee Gui <anfern...@gmail.com>:

> PaaS还是比较复杂的。请大家关注Cloud Foundry,CF是完全open source的,可以自己搭一个玩玩。

- 楼开始歪了吼,,,俺推荐 OpenStack
- 其实,只是想作个純粹 OpenResty 的 PaaS 不应该复杂到哪儿的
- 因为全部内置了
- 只要解决对多个 Nginx 事例的統一监控,管理

--

Anfernee Gui

unread,
May 24, 2012, 8:15:48 AM5/24/12
to open...@googlegroups.com
No. Openstack是一个通用的IaaS,他屏蔽了底层虚拟化的实现,提供了统一的接口。
Cloud Foundry是PaaS,可以建在OpenStack之上,当然CF是VMware的,所以目前用
了VMware的接口。不过开放的CF,将来可能会提供对更多平台的支持。
我不太了解OpenResty,不过我觉得PaaS的话一定要具备扩展能力,不然就不是云了嘛。。

Anfernee Gui

unread,
May 24, 2012, 8:26:11 AM5/24/12
to open...@googlegroups.com
Weiqiang的想法和cloud foundry很相似。

2012/5/23 Weiqiang <lwei...@gmail.com>



--
Best Regards,
Yongkun

XinQi Yang

unread,
May 24, 2012, 9:35:16 AM5/24/12
to open...@googlegroups.com
对访问控制进行一个细粒度的控制可以先暂时解决问题
不过对云不咋个了解,个人感觉如果就资源控制方面还是有很多地方可以做的,不需要到执行到lua里,就可以限制掉吧。

不过这个还是使用虚拟化技术会好多了,类似amazon的云,系统级别的隔离而不是应用级别的隔离。

xinqiyang

Simon

unread,
May 25, 2012, 2:26:53 AM5/25/12
to openresty
OpenResty天生就是做应用路由的料,扩展能力无与伦比,呵呵。

On 5月24日, 下午8时15分, Anfernee Gui <anfernee....@gmail.com> wrote:
> No. Openstack是一个通用的IaaS,他屏蔽了底层虚拟化的实现,提供了统一的接口。
> Cloud Foundry是PaaS,可以建在OpenStack之上,当然CF是VMware的,所以目前用
> 了VMware的接口。不过开放的CF,将来可能会提供对更多平台的支持。
> 我不太了解OpenResty,不过我觉得PaaS的话一定要具备扩展能力,不然就不是云了嘛。。
>
>
>
>
>
>
>
>
>

> On Thu, May 24, 2012 at 8:02 PM, Zoom.Quiet <zoom.qu...@gmail.com> wrote:
> > 2012/5/24 Anfernee Gui <anfernee....@gmail.com>:


> > > PaaS还是比较复杂的。请大家关注Cloud Foundry,CF是完全open source的,可以自己搭一个玩玩。
>
> > - 楼开始歪了吼,,,俺推荐 OpenStack
> > - 其实,只是想作个純粹 OpenResty 的 PaaS 不应该复杂到哪儿的
> > - 因为全部内置了
> > - 只要解决对多个 Nginx 事例的統一监控,管理
>
> > > 做个广告:如果大家对CF在上海的职位有兴趣,可以发简历给我。
>
> > >http://cloudfoundry.com
> > >http://cloudfoundry.org
> > >http://github.com/cloudfoundry
>

> > > 2012/5/24 kindy <kind...@gmail.com>


>
> > >> 也许,
> > >> 可以考虑每 server 一个 VM,还是有可能的。
>
> > >> 不过,
> > >> 也许有强烈的需求才有意义。
>
> > >> 消耗还是较大的。
>

> > >> On Wed, May 23, 2012 at 6:39 PM, agentzh <agen...@gmail.com> wrote:
>
> > >>> 2012/5/23 Simon <bigp...@gmail.com>:


> > >>> > 如果是给每个请求一个VM,倒是能简单做到应用隔离,只要做好资源管理就可以了。
>
> > >>> 那样的话运行时开销会明显增大 :)
>
> > >>> Regards,
> > >>> -agentzh
>
> > >>> --
> > >>> 邮件自: 列表"openresty",专用于技术讨论!
> > >>> 发言: 请发邮件到 open...@googlegroups.com
> > >>> 退订: 请发邮件至 openresty+...@googlegroups.com
> > >>> 详情:http://groups.google.com/group/openresty
> > >>> 官网:http://openresty.org/
> > >>> 仓库:https://github.com/agentzh/ngx_openresty

> > >>> 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp


> > >>> 教程:http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
>
> > >> --
>
> > >> 林青(Kindy Lin)
>
> > >> --
> > >> 邮件自: 列表"openresty",专用于技术讨论!
> > >> 发言: 请发邮件到 open...@googlegroups.com
> > >> 退订: 请发邮件至 openresty+...@googlegroups.com
> > >> 详情:http://groups.google.com/group/openresty
> > >> 官网:http://openresty.org/
> > >> 仓库:https://github.com/agentzh/ngx_openresty

> > >> 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp


> > >> 教程:http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
>
> > > --
> > > Best Regards,
> > > Yongkun
>
> > > --
> > > 邮件自: 列表"openresty",专用于技术讨论!
> > > 发言: 请发邮件到 open...@googlegroups.com
> > > 退订: 请发邮件至 openresty+...@googlegroups.com
> > > 详情:http://groups.google.com/group/openresty
> > > 官网:http://openresty.org/
> > > 仓库:https://github.com/agentzh/ngx_openresty

> > > 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp


> > > 教程:http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
>
> > --
> > 人生苦短, Pythonic! 冗余不做,日子甭过!备份不做,十恶不赦!
> > 俺:http://about.me/zoom.quiet
> > 文字协议:http://creativecommons.org/licenses/by-sa/2.5/cn/
>
> > --
> > 邮件自: 列表"openresty",专用于技术讨论!
> > 发言: 请发邮件到 open...@googlegroups.com
> > 退订: 请发邮件至 openresty+...@googlegroups.com
> > 详情:http://groups.google.com/group/openresty
> > 官网:http://openresty.org/
> > 仓库:https://github.com/agentzh/ngx_openresty

> > 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp

Simon

unread,
May 25, 2012, 2:47:30 AM5/25/12
to openresty
虚拟化就是IAAS了,这个反而会简单很多。

资源控制确实可以在lua之前做,但如果进入lua之后,现在是没法再限制了。

On 5月24日, 下午9时35分, XinQi Yang <xinqiy...@gmail.com> wrote:
> 对访问控制进行一个细粒度的控制可以先暂时解决问题
> 不过对云不咋个了解,个人感觉如果就资源控制方面还是有很多地方可以做的,不需要到执行到lua里,就可以限制掉吧。
>
> 不过这个还是使用虚拟化技术会好多了,类似amazon的云,系统级别的隔离而不是应用级别的隔离。
>

> 在 2012年5月24日 下午8:26,Anfernee Gui <anfernee....@gmail.com>写道:
>
>
>
>
>
>
>
>
>
> > Weiqiang的想法和cloud foundry很相似。
>

> > 2012/5/23 Weiqiang <lweiqi...@gmail.com>


>
> >> 我倒是有个想法,就是采用类似Java版GAE的方法来做
> >> 应用独占一个openresty服务器。然后用lxc+cgroup这样的技术来实现应用服务器的隔离和QoS保证。
>
> >> 前端呢也用openresty来做应用路由(路由信息保存在shdict里,通过rest接口实现动态配置)
>
> >> 李伟强
>

> >> 2012/5/23 agentzh <agen...@gmail.com>
>
> >>> 2012/5/22 Simon <bigp...@gmail.com>:


> >>> > 在openresty基础上构建沙箱是否可行?
>
> >>> 目前还有不少工作需要做 :)
>
> >>> > 我想最大的问题在于nginx的多进程和IO复用模型,虽然性能很好,但是由于不同用户只是通过协程进行隔离,协程是非抢占式的,一旦有某个用户脚本恶
> >>> > 意占用CPU,那么平台怎么能识别并限制他的资源呢?
>
> >>> ngx_lua 的抢占式协程支持一直是我们的一个 TODO :) 我们可以基于 Lua 语言的 Debug hook
> >>> 来做。类似地,基于相同的技术可以实现计算资源的管配,强制中止 CPU 资源占用过多的协程。
>
> >>> 具体到用户界面上,即会通过 lua_abort_after 和 lua_yield_after 这两条配置指令来控制。
>
> >>> > 另外_G变量是进程内全局共享的,也容易成为一个薄弱点。
>
> >>> _G 和 Lua module 都是整个 VM 共享的,因为涉及 lua registry,这一块的隔离确实需要做更多的工作 :)
>
> >>> Regards,
> >>> -agentzh
>
> >>> --
> >>> 邮件自: 列表"openresty",专用于技术讨论!
> >>> 发言: 请发邮件到 open...@googlegroups.com
> >>> 退订: 请发邮件至 openresty+...@googlegroups.com
> >>> 详情:http://groups.google.com/group/openresty
> >>> 官网:http://openresty.org/
> >>> 仓库:https://github.com/agentzh/ngx_openresty

> >>> 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp


> >>> 教程:http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
>
> >> --
> >> 邮件自: 列表"openresty",专用于技术讨论!
> >> 发言: 请发邮件到 open...@googlegroups.com
> >> 退订: 请发邮件至 openresty+...@googlegroups.com
> >> 详情:http://groups.google.com/group/openresty
> >> 官网:http://openresty.org/
> >> 仓库:https://github.com/agentzh/ngx_openresty

> >> 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp


> >> 教程:http://agentzh.org/misc/nginx/agentzh-nginx-tutorials-zhcn.html
>
> > --
> > Best Regards,
> > Yongkun
>
> > --
> > 邮件自: 列表"openresty",专用于技术讨论!
> > 发言: 请发邮件到 open...@googlegroups.com
> > 退订: 请发邮件至 openresty+...@googlegroups.com
> > 详情:http://groups.google.com/group/openresty
> > 官网:http://openresty.org/
> > 仓库:https://github.com/agentzh/ngx_openresty

> > 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp

Anfernee Gui

unread,
May 25, 2012, 2:51:22 AM5/25/12
to open...@googlegroups.com
我说的是scalability,不是flexibillity,你可能搞错了。
nginx/openresty很牛的,用到一个分布式系统例如PaaS中,需要很好的规划和设计,
以适应不断增长的规模。这是很重要的。关于PaaS,可以在下面这个group中讨论。


2012/5/25 Simon <big...@gmail.com>

Simon

unread,
May 25, 2012, 2:51:30 AM5/25/12
to openresty
恩,这个想法不错,每个应用一个VM,在负载均衡和应用路由上对这些VM进行管理。

新浪的SAE,我的理解是通过apache prefork来实现的,每个请求之间通过进程隔离,实现简单很多,但是对系统资源利用不高。

我觉的VM的消耗要比进程小多了。

On 5月24日, 下午5时38分, kindy <kind...@gmail.com> wrote:
> 也许,
> 可以考虑每 server 一个 VM,还是有可能的。
>
> 不过,
> 也许有强烈的需求才有意义。
>
> 消耗还是较大的。
>
>
>
>
>
>
>
>
>

> On Wed, May 23, 2012 at 6:39 PM, agentzh <agen...@gmail.com> wrote:
> > 2012/5/23 Simon <bigp...@gmail.com>:

> > > 如果是给每个请求一个VM,倒是能简单做到应用隔离,只要做好资源管理就可以了。
>
> > 那样的话运行时开销会明显增大 :)
>
> > Regards,
> > -agentzh
>
> > --
> > 邮件自: 列表“openresty”,专用于技术讨论!
> > 发言: 请发邮件到 open...@googlegroups.com
> > 退订: 请发邮件至 openresty+...@googlegroups.com
> > 详情:http://groups.google.com/group/openresty
> > 官网:http://openresty.org/
> > 仓库:https://github.com/agentzh/ngx_openresty

> > 建议: 提问的智慧http://wiki.woodpecker.org.cn/moin/AskForHelp

Anfernee Gui

unread,
May 25, 2012, 2:57:48 AM5/25/12
to open...@googlegroups.com
如果你说的VM指的是虚拟机,那我可以告诉你,一定是不可能的。建VM的代价很大,但建app是个非常轻量级的动作。
我不理解为什么VM的消耗会比进程小。新浪的SAE和CF有一些合作,他们的ruby实现就是CF。中间的那段我也没看懂。

anfernee

2012/5/25 Simon <big...@gmail.com>

Simon

unread,
May 25, 2012, 3:13:43 AM5/25/12
to openresty
这个虚拟机不是xen/kvm虚拟机,是lua虚拟机,只是一堆数据结构,所以开销比进程小

On 5月25日, 下午2时57分, Anfernee Gui <anfernee....@gmail.com> wrote:
> 如果你说的VM指的是虚拟机,那我可以告诉你,一定是不可能的。建VM的代价很大,但建app是个非常轻量级的动作。
> 我不理解为什么VM的消耗会比进程小。新浪的SAE和CF有一些合作,他们的ruby实现就是CF。中间的那段我也没看懂。
>
> anfernee
>

> 2012/5/25 Simon <bigp...@gmail.com>

kindy

unread,
May 25, 2012, 3:15:10 AM5/25/12
to open...@googlegroups.com

这里的 VM 指的是在 nginx 进程里 为 每server创建 lua VM。

so...

2012/5/25 Anfernee Gui <anfern...@gmail.com>



--

林青(Kindy Lin)

Anfernee Gui

unread,
May 25, 2012, 3:55:20 AM5/25/12
to open...@googlegroups.com
sharing the same context of vocabulary is important .. 


2012/5/25 kindy <kin...@gmail.com>



--
Best Regards,
Yongkun

agentzh

unread,
May 25, 2012, 11:18:08 AM5/25/12
to open...@googlegroups.com
2012/5/24 kindy <kin...@gmail.com>:

> 也许,
> 可以考虑每 server 一个 VM,还是有可能的。
>

我一直打算实现一个 Nginx 虚拟主机一个 Lua VM. (或者说一个 server 配置块对应一个 Lua VM)。

> 不过,
> 也许有强烈的需求才有意义。
>

主要是一般配置 Web 应用的粒度是虚拟主机,而非 Nginx 服务器实例,实现各个不同的 Web 应用在同一个 Nginx 服务器中的隔离还是有意义的。

> 消耗还是较大的。
>

相比每请求一个 Lua VM,消耗小太多了,呵呵。

Regards,
-agentzh

Simon

unread,
May 25, 2012, 9:39:08 PM5/25/12
to openresty
赞,找到共同点了,我研究一下这个

On May 25, 11:18 pm, agentzh <agen...@gmail.com> wrote:
> 2012/5/24 kindy <kind...@gmail.com>:

liyong

unread,
May 27, 2012, 8:30:50 AM5/27/12
to openresty
在语言虚拟机上做隔离,比在物理虚拟机上做隔离的成本要低的多,物理虚拟机的毎用户托管成本还是很高的,但语言虚拟机就像楼上说的,沙箱得搞定,这主要
是看语言虚拟机的灵活性了,例如SAE的php沙箱,很大程度上是通过php的配置文件php.ini配置做的应用限制,例如每个请求最长运行时间,最
大内存等。 如果lua等其他语言虚拟机不提供这个功能,就比较麻烦了,需要自己订制虚拟机才行。

On May 25, 11:18 am, agentzh <agen...@gmail.com> wrote:
> 2012/5/24 kindy <kind...@gmail.com>:
>

kindy

unread,
Jun 5, 2012, 11:25:16 AM6/5/12
to open...@googlegroups.com
有同学在搞 每 server 的 lua VM 实现了嘛?

2012/5/25 agentzh <age...@gmail.com>

Regards,
-agentzh




--

林青(Kindy Lin)

XinGuo. Feng

unread,
Jun 11, 2012, 4:38:25 AM6/11/12
to open...@googlegroups.com
我继续顶这个功能的实现吧
“一个 server 配置块对应一个 Lua VM”
去年在github上提了这个提案了。如果解决了这个问题,最起码“_G变量是进程内全局共享的”也就迎刃而解了!

在 2012年5月22日星期二UTC+8下午9时55分20秒,Simon写道:
在openresty基础上构建沙箱是否可行?

我想最大的问题在于nginx的多进程和IO复用模型,虽然性能很好,但是由于不同用户只是通过协程进行隔离,协程是非抢占式的,一旦有某个用户脚本恶
意占用CPU,那么平台怎么能识别并限制他的资源呢?

另外_G变量是进程内全局共享的,也容易成为一个薄弱点。

请各位老师指教,:)


--
Best Regard!

Wendal Chen

unread,
Jun 11, 2012, 6:49:40 AM6/11/12
to open...@googlegroups.com
一个用户一个Linux帐户, 里面跑自己的nginx进程,不就完事了?

没必要多用户共享同一个nginx进程

--



--
Wendal Chen
GuangDong China
Reply all
Reply to author
Forward
0 new messages