创建和使用数据库
本教程将介绍如何通过 Terraform 部署阿里云 RDS (和 OSS)。
⚠️ 请确认管理员已经安装了 云资源插件。
下面我们以阿里云关系型数据库(RDS)的例子,作为示例进行讲解。
首先请直接复制一个编写好的应用部署计划,在命令行中执行:
cat <<EOF | vela up -f -apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: webappspec:components:- name: rds-servertype: webserviceproperties:image: zzxwill/flask-web-application:v0.3.1-crossplaneports: 80- name: sample-dbtype: alibaba-rdsproperties:instance_name: sample-dbaccount_name: oamtestpassword: U34rfwefwefffakedwriteConnectionSecretToRef:name: db-connEOF
可以看到,我们使用了一个 webservice 组件作为 RDS 即将对外的服务器,而名称是 sample-db 的 alibaba-rds 组件则承载起去拉起云资源的责任,数据库相关访问信息被写 db-conn 中。
一般云资源的拉起,会消耗比较多的时间,比如这里的 RDS 就大约需要 15 分钟左右,我们可以看到它从渲染、健康检查到正常运行的全过程:
$ vela lsAPP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIMEwebapp rds-server webservice service-binding rendering 2021-08-30 20:04:03 +0800 CST└─ sample-db alibaba-rds rendering 2021-08-30 20:04:03 +0800 CSTwebapp rds-server webservice service-binding healthChecking healthy 2021-08-30 20:04:03 +0800 CST└─ sample-db alibaba-rds healthChecking unhealthy Cloud resources are being provisioned. 2021-08-30 20:04:03 +0800 CSTwebapp rds-server webservice service-binding running healthy 2021-08-30 20:04:03 +0800 CST└─ sample-db alibaba-rds running healthy Cloud resources are deployed and ready to use. 2021-08-30 20:04:03 +0800 CST
有了 RDS 的服务器,又有了正常运行的云资源,是时候让它们之间映射起来了:使用运维特征 service-binding。我们对 YAML 文件进行更新后,再次部署:
cat <<EOF | vela up -f -apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: webappspec:components:- name: rds-servertype: webserviceproperties:image: zzxwill/flask-web-application:v0.3.1-crossplaneports: 80traits:- type: service-bindingproperties:envMappings:# 环境变量与 db-conn 密钥形成映射DB_PASSWORD:secret: db-connendpoint:secret: db-connkey: DB_PUBLIC_HOSTusername:secret: db-connkey: DB_USER- name: sample-dbtype: alibaba-rdsproperties:instance_name: sample-dbaccount_name: oamtestpassword: U34rfwefwefffakedwriteConnectionSecretToRef:name: db-connEOF
可以看到,db-conn 负责将密钥的账户、密码等信息转发给 rds-server 这个组件来使用。

自定义云资源
如果我们提供的开箱即用云资源没有覆盖你的研发需求,你依然可以通过灵活的Terraform 组件去自定义业务所需要的云资源。