Creating Alibaba Cloud Kubernetes Service (ACK) Using Azure DevOps and Terraform

In this blog post, we’ll walk through the process of setting up an Alibaba Cloud Kubernetes Service (ACK) using Azure DevOps and Terraform. We’ll cover the essential Terraform configuration files: providers.tf, main.tf, variables.tf, output.tf, and Kubernetes manifests: deployment.yaml and services.yaml. Additionally, we’ll discuss how to integrate Azure DevOps with Alibaba Cloud.

Prerequisites

  • Alibaba Cloud account with necessary permissions to manage ACK.
  • Azure DevOps account.
  • Terraform installed on your local machine.
  • Alibaba Cloud CLI installed and configured on your local machine.

Step 1: Setting Up Terraform Configuration Files

providers.tf

This file specifies the providers required for Terraform to interact with Alibaba Cloud.

provider "alicloud" {
  region = var.alicloud_region
}

variables.tf

Define the variables used in the Terraform configuration.

variable "alicloud_region" {
  description = "The Alibaba Cloud region to deploy resources"
  default     = "cn-hangzhou"
}

variable "cluster_name" {
  description = "The name of the ACK cluster"
  default     = "my-ack-cluster"
}

variable "node_instance_type" {
  description = "ECS instance type for the nodes"
  default     = "ecs.c6.large"
}

variable "node_count" {
  description = "Number of worker nodes"
  default     = 3
}

main.tf

This file contains the main configuration for creating the ACK cluster and node group.

resource "alicloud_cs_kubernetes" "ack_cluster" {
  name     = var.cluster_name
  region_id = var.alicloud_region
  instance_type = var.node_instance_type
  worker_instance_types = [var.node_instance_type]
  worker_number = var.node_count
  vswitch_ids = [alicloud_vswitch.main.id]
  new_nat_gateway = true
  install_cloud_monitor = true
}

resource "alicloud_vswitch" "main" {
  vpc_id = alicloud_vpc.main.id
  cidr_block = "172.16.0.0/24"
  availability_zone = "cn-hangzhou-b"
}

resource "alicloud_vpc" "main" {
  cidr_block = "172.16.0.0/16"
}

output.tf

Output the necessary information after the resources are created.

output "cluster_name" {
  value = alicloud_cs_kubernetes.ack_cluster.name
}

output "cluster_id" {
  value = alicloud_cs_kubernetes.ack_cluster.id
}

output "cluster_endpoint" {
  value = alicloud_cs_kubernetes.ack_cluster.api_server_internet
}

Step 2: Kubernetes Manifests

deployment.yaml

Define the deployment for your application.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image:latest
        ports:
        - containerPort: 80

services.yaml

Define the service to expose your application.

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: my-app

Step 3: Integrating Azure DevOps with Alibaba Cloud

  • Create an Access Key in Alibaba Cloud:
    • Log in to the Alibaba Cloud Console.
    • Navigate to the RAM (Resource Access Management) Console.
    • Create a new RAM user with programmatic access.
    • Attach the necessary policies to the RAM user (e.g., AliyunCSFullAccess for full access to ACK).
    • Generate an Access Key ID and Secret Access Key for the RAM user.
    • Save the Access Key ID and Secret Access Key securely.
  • Create a Service Connection in Azure DevOps:
    • Navigate to your Azure DevOps project.
    • Go to Project Settings > Service connections.
    • Click on “New service connection” and select “Generic”.
    • Fill in the details:
      • Connection name: A descriptive name for your service connection.
      • Server URL: https://cs.aliyuncs.com (ACK API endpoint).
      • Username: Your Alibaba Cloud Access Key ID.
      • Password/Token: Your Alibaba Cloud Secret Access Key.
    • Verify the connection and save it.

Step 4: Setting Up Azure DevOps Pipeline

  • Create a new pipeline in Azure DevOps.
  • Connect your repository containing the Terraform and Kubernetes configuration files.
  • Add tasks to the pipeline to:
    • Install Terraform.
    • Initialize Terraform.
    • Apply Terraform configuration.
    • Deploy Kubernetes manifests using kubectl.

Here’s an example of a simple Azure DevOps pipeline YAML:

trigger:
- main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: TerraformInstaller@0
  inputs:
    terraformVersion: '1.0.0'

- script: |
    terraform init
    terraform apply -auto-approve
  displayName: 'Run Terraform'

- task: Kubernetes@1
  inputs:
    connectionType: 'Kubernetes Service Connection'
    kubernetesServiceEndpoint: '<your-k8s-service-connection>'
    namespace: 'default'
    command: 'apply'
    useConfigurationFile: true
    configuration: '$(Pipeline.Workspace)/manifests/deployment.yaml'
    arguments: '-f $(Pipeline.Workspace)/manifests/services.yaml'

Conclusion

By following these steps, you can set up an Alibaba Cloud ACK cluster using Terraform and deploy your application using Azure DevOps. This approach ensures a consistent and repeatable process for managing your Kubernetes infrastructure and deployments.

Feel free to customize the configurations and pipeline according to your specific requirements. Happy deploying! 

If you have any questions or need further assistance, let me know!

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to get latest build ID from Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?