使用serverless超低成本快速部署开源项目

使用serverless超低成本快速部署开源项目

背景:想将项目放到公网上,需要购买云服务器,可能还需要购买域名,所以研究了下serverless,正好解决了这两问题。

前提条件

部署配置

  • 以部署nocobase为例(其他项目类似操作,以docker镜像或源码编写dockerfile文件生成新的镜像镜像部署)
# s.yaml
edition: 1.0.0
name: nocobase-app
access: "default"

vars: # 全局变量
  region: "cn-hangzhou"
  image_namespace: "nocobase-namespace"
  image_name: "nocobase-function"
  service:
    name: "nocobase-service"
    nasConfig: auto # 无需持久化数据库的话,去掉这个配置,就不需要再开通NAS服务了

services:
  nocobaseService:
    component: fc
    actions:
      pre-deploy:
        - component: fc build --use-docker --dockerfile ./Dockerfile
    props:
      region: ${vars.region}
      service: ${vars.service}
      function:
        name: "nocobase-function"
        runtime: custom-container
        timeout: 60
        memorySize: 512
        instanceConcurrency: 100
        caPort: 13000
        customContainerConfig:
          image: "registry.${vars.region}.aliyuncs.com/${vars.image_namespace}/${vars.ima\
            ge_name}:v1.0"
      triggers:
        - name: httpTrigger
          type: http
          config:
            authType: anonymous
            methods:
              - GET
              - POST
              - PUT
              - DELETE
              - HEAD
              - OPTIONS
      customDomains: # 自定义域名
        - domainName: Auto # 这里代表自动生成自定义域名
          protocol: HTTP
          routeConfigs:
            - path: /*

# Dockerfile
FROM nocobase/nocobase:main
ARG LOCAL_STORAGE_BASE_URL=/storage/uploads

ENV DB_DIALECT=sqlite
ENV DB_STORAGE=/mnt/auto/nocobase/storage/db/nocobase.sqlite

COPY . .

EXPOSE 80 13000

部署方式

  1. 在阿里云函数计算中添加应用的方式
    • 需要个git仓库,将配置放上去,好处是可以自动部署,只需要关注代码编写即可
  2. 直接使用命令部署

上面两种方式均需要开通对应的权限,例如函数计算相关权限、容器镜像相关的权限等

至此,部署已经结束了,命令部署的话,可以从控制台中获得自动生成的域名进行访问,页面部署的话,也可以在对应页面找到URL进行访问。

额外话题(加餐)

  • 使用命令生成上面的配置文件,无需手动编写。

    • s init start-nocobase -d start-nocobase
    • 执行命令后,按提示操作即可,进入到生成的配置目录下cd start-nocobase
    • 然后执行s deploy命令进行项目部署
  • 说到这,应该会有人好奇上面的命令是怎么生成配置的吧,这就要说到serverless registry了。

  • 每个人都可以打包应用到serverless registry中,以下为打包的相关配置。

  • 随便建个目录,这里以serverless目录为例。

# serverless/publish.yaml
Type: Application
Name: start-nocobase
Provider:
  - 阿里云
Version: 0.1.16
Description: 快速部署一个 nocobase 函数到阿里云函数计算
HomePage: https://github.com/183461750/nocobase
Tags:
  - 函数计算
  - 全栈应用
Category: 全栈应用
Service:
  函数计算:
    Authorities:
      - AliyunFCFullAccess
      - AliyunContainerRegistryFullAccess
    Runtime: custom container
Parameters:
  type: object
  additionalProperties: false # 不允许增加其他属性
  required: # 必填项
    - region
    - serviceName
    - functionName
    - imageNamespace
  properties:
    region:
      title: 地域
      type: string
      default: cn-hangzhou
      description: 创建应用所在的地区
      enum:
        - cn-beijing
        - cn-hangzhou
        - cn-shanghai
        - cn-qingdao
        - cn-zhangjiakou
        - cn-huhehaote
        - cn-shenzhen
        - cn-chengdu
        - cn-hongkong
        - ap-southeast-1
        - ap-southeast-2
        - ap-southeast-3
        - ap-southeast-5
        - ap-northeast-1
        - eu-central-1
        - eu-west-1
        - us-west-1
        - us-east-1
        - ap-south-1
    serviceName:
      title: 服务名
      type: string
      default: nocobase-service
      description: 服务名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间
    functionName:
      title: 函数名
      type: string
      default: nocobase-function
      description: 函数名称,只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-64 之间
    imageNamespace:
      title: 镜像仓库的命名空间
      type: string
      default: nocobase-namespace
      description: 镜像仓库的命名空间,需要在 https://cr.console.aliyun.com/ 中开通服务、创建镜像仓库的命名空间以及设置访问凭证
# serverless/src/s.yaml
edition: 1.0.0
name: nocobase-app
access: "{{ access }}"

vars: # 全局变量
  region: "{{ region }}"
  image_namespace: "{{ imageNamespace }}"
  image_name: "{{ functionName }}"
  service:
    name: "{{ serviceName }}"
    nasConfig: auto

services:
  nocobaseService:
    component: fc
    actions:
      pre-deploy:
        - component: fc build --use-docker --dockerfile ./Dockerfile
    props:
      region: ${vars.region}
      service: ${vars.service}
      function:
        name: "{{ functionName }}"
        runtime: custom-container
        timeout: 60
        memorySize: 512
        instanceConcurrency: 100
        caPort: 13000
        customContainerConfig:
          image: "registry.${vars.region}.aliyuncs.com/${vars.image_namespace}/${vars.image_name}:v1.0"
      triggers:
        - name: httpTrigger
          type: http
          config:
            authType: anonymous
            methods:
              - GET
              - POST
              - PUT
              - DELETE
              - HEAD
              - OPTIONS
      customDomains:
        - domainName: Auto
          protocol: HTTP
          routeConfigs:
            - path: /*
              
FROM nocobase/nocobase:main
ARG LOCAL_STORAGE_BASE_URL=/storage/uploads

ENV DB_DIALECT=sqlite
ENV DB_STORAGE=/mnt/auto/nocobase/storage/db/nocobase.sqlite

COPY . .

EXPOSE 80 13000
  • 然后,执行以下命令即可发布package到serverless registry中
# 登录
s cli registry login
# 发布
s cli registry publish
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

随猿Fa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值