本文介紹如何在計算巢創建容器應用+RDS數據庫的場景的服務。
前提條件
已創建ACK集群。更多信息,請參見創建ACK集群。
操作流程
本示例使用一個基本的容器鏡像,并在該鏡像中預置了數據庫初始化腳本,在服務創建的過程中自動初始化數據庫并在數據庫初始化完成后,向數據庫中插入一條包含ComputeNest名字的數據。服務實例創建完成后,可以使用API名稱查詢數據庫并返回結果Hello ComputeNest
。步驟如下:
步驟一:準備容器鏡像
創建服務前,您需要先準備容器鏡像并將已準備好的容器鏡像上傳至計算巢用于服務商托管鏡像的私有鏡像倉庫中。
準備容器鏡像。
使用以下任一方法準備本地容器鏡像。
直接使用阿里云提供的容器鏡像。
docker pull compute-nest-registry.cn-hangzhou.cr.aliyuncs.com/bestpractice/springboot-demo:demo
重新編寫源代碼創建容器鏡像。
獲取鏡像源代碼,根據業務需求完成編寫。
在源代碼根目錄(即pom.xml所在目錄)下,使用Maven將程序打包為Jar包,Jar包會自動生成到target目錄中。
mvn install
構建鏡像。
完成構建后,鏡像會自動下載到您本地電腦。
docker build --build-arg JAR_FILE=target/\*.jar -t <your-image-tag> .
上傳容器鏡像至計算巢鏡像倉庫。
獲取計算巢鏡像倉庫訪問憑證、打標鏡像和推送鏡像Docker命令。
登錄計算巢控制臺。
在左側導航欄中選擇服務部署物,然后單擊創建部署物。
在創建部署物界面的部署物類型下,單擊獲取訪問憑證。在彈出的界面中獲取登錄鏡像倉庫、打標鏡像和推送鏡像的命令。
在Docker命令行工具,登錄鏡像倉庫、打標鏡像和推送鏡像。
步驟二:創建容器鏡像部署物
創建服務前,需要先完成容器鏡像部署物的創建和發布。
登錄計算巢控制臺。
在左側導航欄中,單擊服務部署物。然后在部署物管理界面,單擊創建部署物。
在創建部署物界面,填寫部署物信息、部署物內容和設置分發的信息。
配置項
配置示例
部署物名稱
springboot容器部署
部署物版本名稱
springboot容器部署版本1
部署物描述
springboot鏡像容器部署物
部署物類型
容器鏡像
選擇鏡像
部署物名稱:springbootdemo
部署物版本:latest
單擊發布部署物。
單擊發布部署物后,可在部署物管理頁面,單擊部署物名稱,進入部署物詳情界面,查看部署物的部署進度。
當部署物狀態為可用時,表示部署物發布成功。
步驟三:創建服務
登錄計算巢控制臺。
在左側導航欄中,單擊我的服務,并在我的服務頁面中選擇我創建的服務,然后單擊創建新服務。
在創建新服務界面,根據需求配置相關參數。
此處只列舉創建服務需要特別配置的參數,其他參數配置,請參見創建私有部署服務。
在錄入模板區域的模板內容處,填寫服務模板內容。此處描述模板內容中特別配置的參數,詳細的模板內容,請參見模板示例。
在容器部署時,需要拉取計算巢私有鏡像倉庫中的鏡像。ACK集群需要具備訪問計算巢私有鏡像倉庫的能力。因此在模板中定義了一個dockerconfigjson類型的Secret,并使用計算巢標識符{{ computenest::acr::dockerconfigjson }}。
ClusterApplication: DependsOn: Database Type: ALIYUN::CS::ClusterApplication Properties: ClusterId: Ref: ClusterId YamlContent: Fn::Sub: - | # 省略... apiVersion: v1 data: .dockerconfigjson: {{ computenest::acr::dockerconfigjson }} kind: Secret metadata: name: computenestrepo namespace: ${NameSpace} type: kubernetes.io/dockerconfigjson # 省略...
在模板中通過{{ computenest::acrimage::springbootdemo }}標識符將模板中的image關聯至計算巢部署物。
ClusterApplication: DependsOn: Database Type: ALIYUN::CS::ClusterApplication Properties: ClusterId: Ref: ClusterId YamlContent: Fn::Sub: - | # 省略... apiVersion: apps/v1 kind: Deployment metadata: labels: app: springboot-demo-deployment name: springboot-demo-deployment namespace: ${NameSpace} spec: progressDeadlineSeconds: 600 replicas: 2 revisionHistoryLimit: 10 selector: matchLabels: app: springboot-demo-pod template: metadata: labels: app: springboot-demo-pod spec: containers: - env: - name: DB_HOST value: ${RdsConnectString} - name: DB_USER value: ${DbUser} - name: DB_PASSWORD value: ${DbPassword} image: {{ computenest::acrimage::springbootdemo }} imagePullPolicy: Always name: springboot-demo-container ports: - containerPort: 8080 protocol: TCP imagePullSecrets: - name: computenestrepo restartPolicy: Always # 省略...
在部署應用區域的容器鏡像關聯處,關聯已創建的鏡像部署物,并選擇部署物的版本。
單擊保存服務。
測試服務。
服務保存后,您需要對創建的服務實例進行測試,保證其正常可用。更多信息,請參見測試服務功能。
發布服務。
服務測試通過后,再提交審核,審核通過后即可發布上線。更多信息,請參見上線服務。
步驟四:創建服務實例并查詢數據庫
該步驟是服務商以用戶的身份驗證服務中配置的容器鏡像是否能夠在服務實例中正確部署。
創建服務實例。
更多信息,請參考創建私有部署服務實例。
服務實例部署成功,則表示鏡像部署物也已經在服務實例中部署成功。
查詢數據庫。
使用kubectl命令行工具查詢LoadBalancer訪問端點,并使用獲取到的公網IP訪問應用,并查看返回結果。
模板示例
您可以通過如下內容,查看本場景中必須的ROS資源和函數。
本場景詳細的模板示例如下。
ROSTemplateFormatVersion: '2015-09-01'
Description:
en: A simple demo that deploys a RDS instance and a container-base app into ack. The app use RDS instance as a persistence storage.
zh-cn: 新建一個RDS實例并向ACK中部署容器應用。容器應用使用RDS實例作為數據庫。
Parameters:
ZoneId:
Type: String
AssociationProperty: ALIYUN::ECS::Instance:ZoneId
Description:
en: Availability zone ID,<br><b>note: <font color='blue'>Before selecting, please confirm that the Availability Zone supports the specification of creating ECS resources</font></b>
zh-cn: 可用區ID
Label:
en: VSwitch Available Zone
zh-cn: 可用區
VpcId:
Type: String
AssociationProperty: ALIYUN::ECS::VPC::VPCId
VSwitchId:
Type: String
AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId
AssociationPropertyMetadata:
VpcId: ${VpcId}
ZoneId: ${ZoneId}
PayType:
AssociationProperty: ChargeType
Type: String
Label:
en: ECS Instance Charge Type
zh-cn: 付費類型
Default: PostPaid
AllowedValues:
- PostPaid
- PrePaid
PayPeriod:
Type: Number
Description:
en: The subscription period. Unit is months.
zh-cn: 購買時長,單位(月)
Label:
en: The subscription period. Unit is months.
zh-cn: 購買時長,單位(月)
Default: 1
AllowedValues:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 12
- 24
- 36
AssociationPropertyMetadata:
Visible:
Condition:
Fn::Or:
- Fn::Equals:
- ${PayType}
- PrePaid
- Fn::Equals:
- ${PayType}
- undefined
RdsInstanceClass:
Label: MySQL 實例規格
Type: String
Description:
zh-cn: 根據數據庫引擎的類型和可用的區域支持選擇實例規格
en: 'Select the instance specification based on the type of database engine and the available area support'
Default: rds.mysql.s2.large
RdsInstanceStorage:
Label: 磁盤大小
Type: Number
Description:
zh-cn: RDS 實例大小,范圍為 20 - 2000,每 5 個增量,單位為 GB
en: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
MinValue: 20
MaxValue: 2000
ConstraintDescription: The size range of RDS instances is 20 - 2000, Incrementing in every 5, unit GB
Default: 30
RdsAccountName:
Type: String
Label:
zh-cn: 用戶名稱
en: Account Name
Description:
zh-cn: MySQL 管理員用戶名稱
en: Account Name
Default: db_root
RdsAccountPassword:
Type: String
NoEcho: true
Label:
zh-cn: 用戶密碼
en: DB Account Password
Description:
zh-cn: |-
長度為8~32個字符。由大寫英文字母、小寫英文字母、數字、特殊字符中的任意三種組成。支持的特殊字符如下:
!@#$&%^*()_+-= 。
en: |-
The length is 8 ~ 32 characters. It is composed of uppercase English letters, lowercase English letters, numbers and special characters. The special characters supported are as follows:
!@#$& amp;%^* ()_+-= .
ClusterId:
Type: String
Description:
en: >-
The ID of Kubernetes ClusterId in which application deployed.
zh-cn: >-
部署應用程序的K8s集群ID
AllowedPattern: '[0-9a-z]+$'
Label:
en: Kubernetes ClusterId
zh-cn: K8s集群ID
AssociationProperty: ALIYUN::CS::Cluster::ClusterId
ConstraintDescription:
en: >-
must be lowercase letters or numbers
zh-cn: '集群ID必須由小寫字母或者數字組成'
NameSpace:
Type: String
Description:
en: >-
The NameSpace in which application deployed.
zh-cn: >-
部署應用程序的K8s命名空間
Resources:
EcsSecurityGroup:
Type: ALIYUN::ECS::SecurityGroup
Properties:
VpcId:
Ref: VpcId
SecurityGroupIngress:
- PortRange: '-1/-1'
Priority: 1
SourceCidrIp: 0.0.0.0/0
IpProtocol: all
NicType: intranet
SecurityGroupEgress:
- PortRange: '-1/-1'
Priority: 1
IpProtocol: all
DestCidrIp: 0.0.0.0/0
NicType: intranet
DBInstance:
Type: ALIYUN::RDS::DBInstance
Properties:
ZoneId:
Ref: ZoneId
VPCId:
Ref: VpcId
VSwitchId:
Ref: VSwitchId
Engine: MySQL
EngineVersion: "5.7"
DBInstanceClass:
Ref: RdsInstanceClass
DBInstanceStorage:
Ref: RdsInstanceStorage
PayType:
Ref: PayType
PeriodType: Month
Period:
Ref: PayPeriod
SecurityIPList: '0.0.0.0/0'
RdsAccount:
DependsOn: DBInstance
Type: ALIYUN::RDS::Account
Properties:
DBInstanceId:
Ref: DBInstance
AccountType: Super
AccountName:
Ref: RdsAccountName
AccountPassword:
Ref: RdsAccountPassword
RdsParameter:
Type: ALIYUN::RDS::DBInstanceParameterGroup
Properties:
DBInstanceId:
Ref: DBInstance
Parameters:
- Key: innodb_large_prefix
Value: 'ON'
- Key: innodb_adaptive_flushing_lwm
Value: '10'
Database:
Type: ALIYUN::RDS::Database
Properties:
CharacterSetName: UTF8
DBInstanceId:
Ref: DBInstance
DBName: springboot_demo
DependsOn:
- DBInstance
ClusterApplication:
DependsOn: Database
Type: ALIYUN::CS::ClusterApplication
Properties:
ClusterId:
Ref: ClusterId
YamlContent:
Fn::Sub:
- |
apiVersion: v1
kind: Namespace
metadata:
name: ${NameSpace}
---
apiVersion: v1
data:
.dockerconfigjson: {{ computenest::acr::dockerconfigjson }}
kind: Secret
metadata:
name: computenestrepo
namespace: ${NameSpace}
type: kubernetes.io/dockerconfigjson
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: springboot-demo-deployment
name: springboot-demo-deployment
namespace: ${NameSpace}
spec:
progressDeadlineSeconds: 600
replicas: 2
revisionHistoryLimit: 10
selector:
matchLabels:
app: springboot-demo-pod
template:
metadata:
labels:
app: springboot-demo-pod
spec:
containers:
- env:
- name: DB_HOST
value: ${RdsConnectString}
- name: DB_USER
value: ${DbUser}
- name: DB_PASSWORD
value: ${DbPassword}
image: {{ computenest::acrimage::springbootdemo }}
imagePullPolicy: Always
name: springboot-demo-container
ports:
- containerPort: 8080
protocol: TCP
imagePullSecrets:
- name: computenestrepo
restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
labels:
app: springboot-demo-svc
name: springboot-demo-svc
namespace: ${NameSpace}
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: springboot-demo-pod
type: LoadBalancer
- NameSpace:
Ref: NameSpace
DbUser:
Ref: RdsAccountName
DbPassword:
Ref: RdsAccountPassword
RdsConnectString:
Fn::GetAtt:
- DBInstance
- InnerConnectionString
AppExternalIp:
Type: DATASOURCE::CS::ClusterApplicationResources
Properties:
Kind: Service
Name: springboot-demo-svc
ClusterId:
Ref: ClusterApplication
Namespace:
Ref: NameSpace
JsonPath: $.status.loadBalancer.ingress
FirstMatch: true
DependsOn: ClusterApplication
Outputs:
ExternalIp:
Value:
Ref: AppExternalIp
Metadata:
ALIYUN::ROS::Interface:
ParameterGroups:
- Parameters:
- VpcId
- ZoneId
- VSwitchId
Label:
en: Network Configuration
zh-cn: 網絡參數配置
- Parameters:
- ClusterId
- NameSpace
Label:
en: ACK Configuration
zh-cn: ACK參數配置
- Parameters:
- RdsInstanceClass
- RdsInstanceStorage
- RdsAccountName
- RdsAccountPassword
Label:
en: Database Configuration
zh-cn: 數據庫參數配置
- Parameters:
- PayType
- PayPeriod
Label:
en: Payment Configuration
zh-cn: 支付配置