本文介绍如何创建一个包含 MySQL、VPC 和对应子网的组合资源
XMySQL
。为了构建XMySQL
资源,需要创建并定义xrd.yaml
、composition.yaml
、xmysql.yaml
和secret.yaml,
然后执行这三个配置文件。定义 XRD
首先我们需要创建自定义 API,即为后面创建的复合资源,定义该复合资源对应的参数和类型。
我们在
xrd.yaml
定义了 MySQL 相关的配置(例如规格,内存,计费类型等)和 VPC、子网资源配置。其中
vpcName
,subnetName
,availabilityZone
为必选参数,用于创建 VPC 和对应子网。# xrd.yamlapiVersion: apiextensions.crossplane.io/v1kind: CompositeResourceDefinitionmetadata:name: xmysqls.crd.tencentcloud.crossplane.iospec:group: crd.tencentcloud.crossplane.ionames:kind: XMySQLplural: xmysqlsversions:- name: v1alpha1served: truereferenceable: trueschema:openAPIV3Schema:type: objectproperties:spec:type: objectproperties:vpcName:type: stringsubnetName:type: stringavailabilityZone:type: stringinstanceName:type: stringdefault: test-crossplane-mysql # 实例默认值chargeType:type: stringdefault: POSTPAID # 默认按量计费intranetPort:type: integerdefault: 3306 # 默认端口parameters:type: objectproperties:maxConnections:type: integerdefault: 1000 # 默认最大链接数character_set_server:type: stringdefault: utf8 # 默认字符集volumeSize:type: integerdefault: 200 # 默认容量memSize:type: integerdefault: 4000 # 默认内存值sgId:type: stringsecurityGroups:type: arrayitems:type: stringrequired:- vpcName- subnetName- availabilityZonestatus:description: A Status represents the observed stateproperties:share:description: Freeform field containing status informationtype: objectx-kubernetes-preserve-unknown-fields: truetype: object
定义 Composition
接着,在
composition.yaml
中定义复合资源,包括一个 VPC,一个子网,和在该 VPC 下的 MySQL 实例。用户可在资源创建后,通过
status.share
获取 MySQL 的实例 Id。# composition.yamlapiVersion: apiextensions.crossplane.io/v1kind: Compositionmetadata:name: composition-mysqlspec:compositeTypeRef:apiVersion: crd.tencentcloud.crossplane.io/v1alpha1kind: XMySQLresources:# vpc 配置- name: vpcbase:apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1kind: VPCspec:forProvider:cidrBlock: "10.1.0.0/16"patches:- fromFieldPath: spec.vpcNametoFieldPath: spec.forProvider.name- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.vpcId# 子网配置- name: subnetbase:apiVersion: vpc.tencentcloud.crossplane.io/v1alpha1kind: Subnetspec:forProvider:cidrBlock: "10.1.2.0/24"patches:- fromFieldPath: status.share.vpcIdtoFieldPath: spec.forProvider.vpcId- fromFieldPath: spec.subnetNametoFieldPath: spec.forProvider.name- fromFieldPath: spec.availabilityZonetoFieldPath: spec.forProvider.availabilityZone- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.subnetId- name: mysqlbase:apiVersion: mysql.tencentcloud.crossplane.io/v1alpha1kind: Instancespec:forProvider:rootPasswordSecretRef: # password 配置key: passwordnamespace: crossplane-systemname: mysql-root-passwordpatches:- fromFieldPath: status.share.vpcId # 使用上面创建的 VPCtoFieldPath: spec.forProvider.vpcId- fromFieldPath: status.share.subnetId # 使用上面创建的子网toFieldPath: spec.forProvider.subnetId- fromFieldPath: spec.securityGroupstoFieldPath: spec.forProvider.securityGroups- fromFieldPath: spec.chargeTypetoFieldPath: spec.forProvider.chargeType- fromFieldPath: spec.availabilityZonetoFieldPath: spec.forProvider.availabilityZone- fromFieldPath: spec.instanceNametoFieldPath: spec.forProvider.instanceName- fromFieldPath: spec.volumeSizetoFieldPath: spec.forProvider.volumeSize- fromFieldPath: spec.memSizetoFieldPath: spec.forProvider.memSize- fromFieldPath: spec.intranetPorttoFieldPath: spec.forProvider.intranetPort- fromFieldPath: spec.parameterstoFieldPath: spec.forProvider.parameters- type: ToCompositeFieldPathfromFieldPath: status.atProvider.idtoFieldPath: status.share.mysqlId # 实例 Id
然后,在
secret.yaml
中定义 MySQL 实例的密码:# secret.yamlapiVersion: v1kind: Secretmetadata:name: mysql-root-passwordnamespace: crossplane-systemstringData:password: my-secure-password
创建 XMySQL 资源
现在,在
xmysql.yaml
中为复合资源设置子网和实例配置。# xmysql.yamlapiVersion: crd.tencentcloud.crossplane.io/v1alpha1kind: XMySQLmetadata:name: xmysql-example # 复合资源前缀namespace: crossplane-systemspec:# 指定子网和可用区vpcName: test-crossplane-vpcsubnetName: test-crossplane-subnetavailabilityZone: ap-guangzhou-3# 实例配置volumeSize: 30memSize: 2000instanceName: test-crossplane-mysql-instance# 安全组配置securityGroups:- sg-xxxxxx
使用
kubectl apply
命令依次执行上述配置文件,完成资源的创建。kubectl
apply -f xrd.yamlkubectl
apply -f secret.yamlkubectl
apply -f composition.yamlkubectl
apply -f xmysql.yaml
执行结果如下:
创建了前缀为
xmysql-example-
的 VPC,子网和 MySQL 实例。❯kubectl
get managedNAME READY SYNCED EXTERNAL-NAME AGEinstance.mysql.tencentcloud.crossplane.io/xmysql-example-bqg56 True True cdb-071eyvvt 3m6sNAME READY SYNCED EXTERNAL-NAME AGEsubnet.vpc.tencentcloud.crossplane.io/xmysql-example-b5jmh True True subnet-qmac9t04 16mNAME READY SYNCED EXTERNAL-NAME AGEvpc.vpc.tencentcloud.crossplane.io/xmysql-example-pc7dt True True vpc-124cxb6p 16m





