Deploying an Alibaba Cloud ACK Kubernetes Cluster with Terraform
This tutorial walks through provisioning an Alibaba Cloud ACK Kubernetes cluster using Terraform, covering provider setup, VPC and VSwitch creation, managed Kubernetes resource definition, Jenkins deployment, DNS configuration, and cleanup procedures, with full code examples.
This guide demonstrates how to provision an Alibaba Cloud Container Service for Kubernetes (ACK) cluster using Terraform, covering provider configuration, RAM credentials, VPC and VSwitch creation, managed Kubernetes resource definition, and deployment of a Jenkins application.
It starts with initializing the AliCloud and Kubernetes providers, exporting access keys, then defines a VPC (cidr 172.16.0.0/12) and a VSwitch in zone cn-zhangjiakou-a.
The ACK cluster is created with resource "alicloud_cs_managed_kubernetes" "k8s" { name = var.cluster_name version = local.cluster_version cluster_spec = "ack.standard" availability_zone = "cn-zhangjiakou-a" service_cidr = local.service_cidr pod_cidr = local.pod_cidr new_nat_gateway = true load_balancer_spec = "slb.s1.small" slb_internet_enabled = true password = "Password123.com" node_port_range = "30000-32767" os_type = "Linux" platform = "CentOS" worker_number = 1 worker_instance_types = ["ecs.g6.xlarge"] worker_vswitch_ids = [alicloud_vswitch.vsw.id] worker_disk_category = "cloud_efficiency" worker_disk_size = 40 runtime = { name = "docker"; version = "19.03.5" } // optional addons omitted for brevity } , specifying version, service and pod CIDRs, NAT gateway, load balancer, password, node settings, Docker runtime, and optional addons.
Standard Terraform workflow commands ( terraform fmt , terraform validate , terraform plan , terraform apply , terraform apply -auto-approve ) are run to apply the configuration.
After the cluster is ready, a Jenkins deployment and service are defined with resource "kubernetes_deployment_v1" "jenkins" { provider = kubernetes.clustera metadata { name = "jenkins"; labels = { app = "jenkins" }; namespace = kubernetes_namespace.jenkins.id } spec { replicas = 1 selector { match_labels = { app = "jenkins" } } template { metadata { labels = { app = "jenkins" } } spec { container { name = "jenkins"; image = "jenkins/jenkins:2.332.2-centos7-jdk8"; ports { container_port = 8080 } resources { limits = { cpu = "1000m"; memory = "4096Mi" } requests = { cpu = "250m"; memory = "1024Mi" } } } } } } } and resource "kubernetes_service_v1" "jenkins" { provider = kubernetes.clustera metadata { name = "jenkins-service"; namespace = kubernetes_namespace.jenkins.id } spec { selector = { app = kubernetes_deployment_v1.jenkins.metadata[0].labels.app } port { port = 8080; target_port = 8080 } type = "ClusterIP" } } , followed by an ingress rule to expose it.
Additional resources such as a DNS A record ( resource "alicloud_dns_record" "record" { name = "zeyang.site"; host_record = "jenkins"; type = "A"; value = kubernetes_ingress_v1.jenkins_ingress.status[0].load_balancer[0].ingress[0].ip } ) and a namespace are created, and the guide shows how to destroy the infrastructure with terraform destroy .
An optional ASK (serverless Kubernetes) extension is provided, illustrating how to import an existing ASK cluster into Terraform state ( terraform import alicloud_cs_serverless_kubernetes.main c995c50e1efa54eb9a1b03c8e41df22e5 ) and manage its VPC, VSwitch, and serverless cluster resources using resource "alicloud_cs_serverless_kubernetes" "main" { ... } .
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.