Terraform lets you define infrastructure as code to create a Container Service for Kubernetes (ACK) cluster, import it into Enterprise Distributed Application Service (EDAS), and deploy an application -- all in a single, automated workflow.
How it works
The deployment chains three Terraform resources:
alicloud_cs_managed_kubernetes-- Creates a managed Kubernetes cluster in ACK.alicloud_edas_k8s_cluster-- Imports the ACK cluster into EDAS.alicloud_edas_k8s_application-- Deploys an application to the EDAS-managed cluster.
Terraform interfaces
Data sources
| Data source | Description |
|---|---|
| alicloud_edas_applications | Query EDAS applications, including both Elastic Compute Service (ECS) and ACK applications |
| alicloud_edas_clusters | Query EDAS clusters, including both ECS and ACK clusters |
Resources
| Resource | Description |
|---|---|
| alicloud_edas_k8s_cluster | Import an ACK cluster into EDAS |
| alicloud_edas_k8s_application | Create and deploy an application in an EDAS-managed ACK cluster |
Configuration example
This example creates a managed Kubernetes cluster in ACK, imports it into EDAS, and deploys a container-based application. Modify configuration parameters as needed.
Terraform supports various ACK cluster types. This example uses a managed Kubernetes cluster.
This example assumes that you have already defined VPC and vSwitch resources (for example, alicloud_vpc and alicloud_vswitch) and an alicloud_instance_types data source in your configuration. For details, see the Alibaba Cloud Terraform provider documentation.
Step 1: Create a managed Kubernetes cluster
resource "alicloud_cs_managed_kubernetes" "default" {
name = var.name
worker_instance_types = [data.alicloud_instance_types.default.instance_types.0.id]
worker_vswitch_ids = [alicloud_vswitch.default.id]
worker_number = "1"
password = "Test12345"
pod_cidr = "172.20.0.0/16"
service_cidr = "172.21.0.0/20"
worker_disk_size = "50"
worker_disk_category = "cloud_ssd"
worker_data_disk_size = "20"
worker_data_disk_category = "cloud_ssd"
worker_instance_charge_type = "PostPaid"
slb_internet_enabled = "true"
}The following table describes the key parameters:
| Parameter | Description | Example |
|---|---|---|
name | Cluster name | Referenced from a variable |
worker_instance_types | ECS instance type for worker nodes | ecs.g6.large |
worker_vswitch_ids | vSwitch ID for worker nodes | Referenced from an alicloud_vswitch resource |
password | SSH login password for worker nodes | Test12345 |
pod_cidr | CIDR block for pod IP addresses. Must not overlap with VPC or service CIDR ranges. | 172.20.0.0/16 |
service_cidr | CIDR block for service IP addresses. Must not overlap with VPC or pod CIDR ranges. | 172.21.0.0/20 |
Step 2: Import the cluster into EDAS
resource "alicloud_edas_k8s_cluster" "default" {
cs_cluster_id = "xxxx-xxx-xxx"
}This resource imports the ACK cluster created in Step 1 into EDAS. After the import, EDAS can manage application deployments on this cluster.
Step 3: Deploy an application
resource "alicloud_edas_k8s_application" "default" {
application_name = "shesheng-test-k8s"
cluster_id = "45bc8232-3233-46be-ab20-6d4a9b506d5c"
replicas = 1
package_type = "Image"
image_url = "cr.registry.cloud.ste2.com/test/gw-provider:1028"
command = "/bin/sh"
command_args = ["-c", "while true; do echo hello; sleep 1000;done"]
}The following table describes the key parameters:
| Parameter | Description |
|---|---|
application_name | Name of the application in EDAS |
cluster_id | ID of the EDAS-managed cluster from Step 2 |
replicas | Number of pod replicas |
package_type | Deployment package type. Set to Image for container image deployments. |
image_url | Full URL of the container image |
command | Entrypoint command for the container |
command_args | Arguments passed to the entrypoint command |
Deploy the infrastructure
Run the following commands to initialize and apply the Terraform configuration:
Initialize the Terraform working directory.
terraform initExpected output:
Initializing provider plugins... - Installing aliyun/alicloud... Terraform has been successfully initialized!Preview the changes.
terraform planApply the configuration to create all resources.
terraform applyWhen prompted, type
yesto confirm. Cluster creation typically takes 10 to 15 minutes.Expected output:
Apply complete! Resources: X added, 0 changed, 0 destroyed.
Clean up resources
To avoid ongoing charges, destroy all resources created by this configuration when they are no longer needed:
terraform destroyWhen prompted, type yes to confirm. This removes the EDAS application, the EDAS cluster import, and the ACK cluster.
terraform destroy permanently deletes all resources managed by this configuration. Back up any important data before you run this command.
Related information
EDAS K8s cluster resource reference on Terraform Registry
EDAS K8s application resource reference on Terraform Registry