全部产品
容器服务

基于Swarm mode 的极简Serverless实践

更新时间:2017-08-01 09:55:52   分享:   

函数计算(FaaS)是目前最新的云服务模式,阿里云容器服务基于 swarm mode 集群上实现了一个极简的Serverless 框架,支持将任意 Unix 进程作为函数实现来对外提供服务。更多扩展知识,可参考 阿里云函数计算。``

架构原理

在 FaaS 原型系统中,包括以下几个模块,其中:

  1. 任何进程都可以转化成为一个函数,并利用Docker镜像进行打包和交付。
  2. 利用 Docker Swarm 集群的资源调度和routing mesh的负载均衡能力简洁地实现了函数的调度能力。其中每个函数对应一个Docker集群中的服务。
  3. 基于 Prometheus 实现函数调用监控和自动伸缩。

1

其设计架构非常简单,其中:

  • API Gateway 负责接受服务调用,路由请求到后端函数实现,并采集服务调用的指标发送给 Prometheus。 Prometheus 则会根据一段时间内服务调用的次数,回调API Gateway 来动态伸缩服务容器实例数量。
  • Function Watchdog 将HTTP请求转发为进程调用,并将请求数据通过 STDIN 传递给进程,而将进程的 STDOUT 作为 HTTP 响应的结果返回给调用者。将函数进程和Function Watchdog打包成一个容器镜像进行部署。其调用流程如下:

    4

本地安装 FaaS

  1. 首先您需要准备一个本地的Docker Swarm集群,如果没有,可以安装最新 Docker Engine 并执行下面命令:

    docker swarm init

  2. 执行如下命令来部署 FaaS

    1. git clone https://github.com/alexellis/faas
    2. cd faas
    3. ./deploy_stack.sh
  3. 部署完成后,您可以通过如下命令检查 FaaS 的状态

    1. $ docker stack services func
    2. ID NAME MODE REPLICAS IMAGE
    3. 1a8b2tb19ulk func_gateway replicated 1/1 functions/gateway:0.5.6
    4. 4jdexem6kppg func_webhookstash replicated 1/1 functions/webhookstash:latest
    5. 9ju4er5jur9l func_wordcount replicated 1/1 functions/alpine:health
    6. e190suippx7i func_markdown replicated 1/1 alexellis2/faas-markdownrender:latest
    7. l70j4c7kf99t func_alertmanager replicated 1/1 functions/alertmanager:latest
    8. mgujgoa2u8f3 func_decodebase64 replicated 1/1 functions/alpine:health
    9. o44asbnhqbda func_hubstats replicated 1/1 alexellis2/faas-dockerhubstats:latest
    10. q8rx49ow3may func_echoit replicated 1/1 functions/alpine:health
    11. t1ao5psnsj0s func_base64 replicated 1/1 functions/alpine:health
    12. vj5z7rpdlo48 func_prometheus replicated 1/1 functions/prometheus:latest
    13. xmwzd4z7l4dv func_nodeinfo replicated 1/1 functions/nodeinfo:latest
  4. 随后通过浏览器来访问 http://127.0.0.1:8080/ui FaaS

    1

在阿里云上测试 FaaS

限制条件

需要具备创建 swarm mode 集群的条件。

  • 默认情况下,您最多可以创建 5 个集群(所有地域下),每个集群中最多可以添加 20 个节点。如果您需要创建更多的集群或添加更多的节点,请提交 工单申请。
  • 随集群一同创建的负载均衡实例只支持按量付费的方式。
  • 用户账户需有 100 元的余额并通过实名认证,否则无法创建按量付费的 ECS 实例和负载均衡。

操作步骤

  1. 创建 Swarm mode 集群。
    FaaS 基于Docker Swarm mode集群进行部署的,您首先需要在阿里云容器服务创建一个 Swarm mode 集群。

    1

  2. 使用编排模板创建应用,参见 使用编排模板创建应用

    编排示例如下:

    1. version: "3"
    2. services:
    3. # Core API services are pinned, HA is provided for functions.
    4. gateway:
    5. volumes:
    6. - "/var/run/docker.sock:/var/run/docker.sock"
    7. ports:
    8. - 8080:8080
    9. labels:
    10. aliyun.routing.port_8080: faas
    11. image: functions/gateway:0.5.6
    12. networks:
    13. - functions
    14. environment:
    15. dnsrr: "true" # Temporarily use dnsrr in place of VIP while issue persists on PWD
    16. deploy:
    17. placement:
    18. constraints: [node.role == manager]
    19. prometheus:
    20. image: functions/prometheus:latest # autobuild from Dockerfile in repo.
    21. command: "-config.file=/etc/prometheus/prometheus.yml -storage.local.path=/prometheus -storage.local.memory-chunks=10000 --alertmanager.url=http://alertmanager:9093"
    22. ports:
    23. - 9090:9090
    24. depends_on:
    25. - gateway
    26. - alertmanager
    27. labels:
    28. aliyun.routing.port_9090: prometheus
    29. environment:
    30. no_proxy: "gateway"
    31. networks:
    32. - functions
    33. deploy:
    34. placement:
    35. constraints: [node.role == manager]
    36. alertmanager:
    37. image: functions/alertmanager:latest # autobuild from Dockerfile in repo.
    38. environment:
    39. no_proxy: "gateway"
    40. command:
    41. - '-config.file=/alertmanager.yml'
    42. networks:
    43. - functions
    44. ports:
    45. - 9093:9093
    46. deploy:
    47. placement:
    48. constraints: [node.role == manager]
    49. # Sample functions go here.
    50. # Service label of "function" allows functions to show up in UI on http://gateway:8080/
    51. webhookstash:
    52. image: functions/webhookstash:latest
    53. labels:
    54. function: "true"
    55. depends_on:
    56. - gateway
    57. networks:
    58. - functions
    59. environment:
    60. no_proxy: "gateway"
    61. https_proxy: $https_proxy
    62. # Pass a username as an argument to find how many images user has pushed to Docker Hub.
    63. hubstats:
    64. image: alexellis2/faas-dockerhubstats:latest
    65. labels:
    66. function: "true"
    67. depends_on:
    68. - gateway
    69. networks:
    70. - functions
    71. environment:
    72. no_proxy: "gateway"
    73. https_proxy: $https_proxy
    74. # Node.js gives OS info about the node (Host)
    75. nodeinfo:
    76. image: functions/nodeinfo:latest
    77. labels:
    78. function: "true"
    79. depends_on:
    80. - gateway
    81. networks:
    82. - functions
    83. environment:
    84. no_proxy: "gateway"
    85. https_proxy: $https_proxy
    86. # Uses `cat` to echo back response, fastest function to execute.
    87. echoit:
    88. image: functions/alpine:health
    89. labels:
    90. function: "true"
    91. depends_on:
    92. - gateway
    93. networks:
    94. - functions
    95. environment:
    96. fprocess: "cat"
    97. no_proxy: "gateway"
    98. https_proxy: $https_proxy
    99. # Counts words in request with `wc` utility
    100. wordcount:
    101. image: functions/alpine:health
    102. labels:
    103. function: "true"
    104. com.faas.max_replicas: "10"
    105. depends_on:
    106. - gateway
    107. networks:
    108. - functions
    109. environment:
    110. fprocess: "wc"
    111. no_proxy: "gateway"
    112. https_proxy: $https_proxy
    113. # Calculates base64 representation of request body.
    114. base64:
    115. image: functions/alpine:health
    116. labels:
    117. function: "true"
    118. depends_on:
    119. - gateway
    120. networks:
    121. - functions
    122. environment:
    123. fprocess: "base64"
    124. no_proxy: "gateway"
    125. https_proxy: $https_proxy
    126. # Decodes base64 representation of request body.
    127. decodebase64:
    128. image: functions/alpine:health
    129. labels:
    130. function: "true"
    131. depends_on:
    132. - gateway
    133. networks:
    134. - functions
    135. environment:
    136. fprocess: "base64 -d"
    137. no_proxy: "gateway"
    138. https_proxy: $https_proxy
    139. # Converts body in (markdown format) -> (html)
    140. markdown:
    141. image: alexellis2/faas-markdownrender:latest
    142. labels:
    143. function: "true"
    144. depends_on:
    145. - gateway
    146. networks:
    147. - functions
    148. environment:
    149. no_proxy: "gateway"
    150. https_proxy: $https_proxy
    151. networks:
    152. functions:
    153. driver: overlay

    和本地部署相比只是增加了两个 label,定义了 API Gatway 和 Prometheus 的路由

    • aliyun.routing.port_8080: faas : API Gatway 的虚拟域名
    • aliyun.routing.port_9090: prometheus: prometheus 服务的虚拟域名
  3. 查看应用的 路由列表

    2

  4. 单击路由地址,访问 FaaS 的 API Gateway 和 Prometheus 服务界面。

    3

    4

后续

你可以在这个基础上,测试服务的伸缩性,具体请参考:

本文导读目录
本文导读目录
以上内容是否对您有帮助?