What is a Serverless
What is Serverless? It literally means Serverless. As defined by CNCF, Serverless refers to the concept of building and running applications that do not require server administration.
CloudFlare defines it as:
Serverless computing is an approach to providing back-end services on demand. Serverless providers allow users to write and deploy code without worrying about the underlying infrastructure. Companies that get back-end services from a serverless vendor will calculate fees based on them, rather than having to retain and pay for a fixed amount of bandwidth or number of servers because the service scales automatically. Note that despite being called serverless, physical servers are still used, but developers do not need to know about them.
Three stages of operation and maintenance:
Physical server
Cloud server
Serverless
Is ServerLess the next cloud?
Advantages: light operation and maintenance, low cost, flexible expansion, payment by volume
Disadvantages: Need to use cloud service provider supporting services, cold start
Practice: Deploy a Serverless application with Vue+Egg+PostgreSql
1. You need to be there firstTencent cloudRegistered account
2. Installserverless framework
To deploy the complete project, we will not only discuss cloud functions here, but use the Serverless framework directly
npm install -g serverless
Copy the code
- This section mainly refers to the best practice Fullstack-app. In order to better understand the configuration and operation mechanism of each component, each component is separated and deployed
Deployment of 3.Private network VPC
Private network A VPC connects various cloud services so that back-end services and databases can connect to databases on the same private network
mkdir vpc; cd vpc
touch serverless.yaml
Copy the code
serverless.yaml
Is a configuration file for the Serverless Framework
The general structure is
Common parts, such as component, name, and so on
Inputs: Specifies the field required for deploying the component
Outputs: Output field after successful deployment
Complete VPC configuration
component: vpc # (Mandatory) Component name, VPC
name: serverless-vpc # (Mandatory) Instance name
inputs:
region: ${env:REGION} # VPC region
zone: ${env:ZONE} This parameter specifies the region where the VPC is located
vpcName: ${name} # VPC name
subnetName: ${name} The name of the Subnet
Copy the code
The deployment of
serverless deploy
Copy the code
Verify the identity and scan the wechat signal associated with Tencent Cloud
After the success of the deployment can examine the deployment was successful in serverless.cloud.tencent.com/ components
Deployment of 4.PostgreSql database
As for why not use mysql, because Tencent cloud now only PGSQL launched serverless version
mkdir db; cd db
touch serverless.yaml
Copy the code
The postgreSql configuration is complete
component: postgresql #(required) References the name of component, currently using the PostgresQL component
name: serverless-db # (Mandatory) Name of the instance created by this PostgresQL component
inputs:
region: ${env:REGION}
zone: ${env:ZONE}
dBInstanceName: ${name} The database instance name must be unique to a user
vpcConfig:
vpcId: vpc-xxx Add the outputs. VpcId of the newly deployed VPC.
subnetId: subnet-xxx Add outputs. SubnetId to the newly deployed VPC
extranetAccess: true # Whether to enable serverlessDB outlaw network access
Copy the code
Serverless.cloud.tencent.com/apps/server…
The deployment of
serverless deploy
Copy the code
Verify the identity and scan the wechat signal associated with Tencent Cloud
After the success of the deployment can examine the deployment was successful in serverless.cloud.tencent.com/ components
-
Use Navicat to connect
Fill in the serverless.cloud.tencent.com/apps/server…
5. Deploy the back-end Eggjs
The back-end API uses egg.js + Sequelize, where ready-made cases can be used
git clone https://gitee.com/logicadi/serverless-egg.git
Copy the code
Modify serverless. Yaml Configuration View the full configuration
component: egg
name: serverless-api
inputs:
src:
src: . /
exclude: Excluded files or directories
- .env
- node_modules
functionName: ${name}
region: ${env:REGION}
runtime: Nodejs10.15
functionConf: Function configuration is relevant
timeout: 30
vpcConfig:
vpcId: vpc-xxx Add elsik.vpcid to the newly deployed VPC
subnetId: subnet-xxx Add outputs. SubnetId to the newly deployed VPC
environment: # Environment variables
variables:
PG_CONNECT_STRING: postgresql://xxx # fill in here just deploy the db outputs. Private. The connectionString
apigatewayConf: API gateway configuration
enableCORS: true
protocols:
- http
- https
Copy the code
The deployment of
serverless deploy
Copy the code
Click the link from the panel to open the API home page and find an error because there are no dependencies installed
The node_modules folder often causes upload failures, so it needs to be excluded here, and can be handled in two ways:
- Usage Layer management
- Online Installation dependencies
Here use method 2, log in Tencent cloud. Enable automatic installation dependency and click Deploy
Just open the API home page after deployment
- Initializing the database
// Local installation depends on NPM installCopy the code
Initialize the database using Sequelize
// database/config.json
{
"development": {
"url": "postgresql://xxx"./ / db network address db outputs. The public. The connectionString
"dialect": "postgres"}}Copy the code
Executing database scripts
Update database
npx sequelize db:migrate
If you have problems and need to roll back a change, use 'db:migrate:undo' to roll back a change
# npx sequelize db:migrate:undo
You can use 'db:migrate:undo:all' to revert to the initial state
# npx sequelize db:migrate:undo:all
Copy the code
The table has been built
6. Deploy the front-end Vue
An existing template is recommended. Front-end static files are used for COS deployment of object storage
git clone https://gitee.com/logicadi/serverless-vue.git
Copy the code
Website component configuration Full configuration
component: website
name: serverless-vue
inputs:
region: ${env:REGION}
bucketName: ${name} # COS Bucket name. Capital letters are not allowed. If you do not add the AppId suffix, it will be added for you by default.
src:
src: . /
hook: npm run build # Hook script. Execute before your project code is uploaded.
dist: ./dist
envPath: . /
index: index.html # website index page
error: index.html
env: Configure front-end environment variables
apiUrl: https://xxx Enter the back-end API address here
Copy the code
// Deploy serverlessCopy the code
Open the page, you can operate the corresponding user add, delete, change and check
Refer to the link
Tencent Cloud Serverless console
Tencent Cloud Serverless Framework document
Deploy Vue + Express + PostgreSQL full-stack website