背景:想将项目放到公网上,需要购买云服务器,可能还需要购买域名,所以研究了下serverless,正好解决了这两问题。
前提条件
- 阿里云开通了函数计算
- 阿里云开通了容器镜像服务
- 非必须,使用docker部署时需要
- 阿里云开通了文件存储NAS
- 非必须,需要持久化数据库等数据时需要
- 阿里云开通了对象存储OSS
- 非必须,需要持久化图片视频资源等数据时需要
部署配置
- 以部署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
部署方式
- 在阿里云函数计算中添加应用的方式
- 需要个git仓库,将配置放上去,好处是可以自动部署,只需要关注代码编写即可
- 直接使用命令部署
- 需要安装
s
命令,安装Serverless Devs和Docker - 命令:
s deploy
,需要通过具有相应权限的访问key和访问secret进行登录
- 需要安装
上面两种方式均需要开通对应的权限,例如函数计算相关权限、容器镜像相关的权限等
至此,部署已经结束了,命令部署的话,可以从控制台中获得自动生成的域名进行访问,页面部署的话,也可以在对应页面找到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