Monday, October 24, 2022

Deploy AKS Cluster with GitHub Actions

GitHub actions facilitate automation of software workflows to enable continuous delivery and deployment. It has facility to customize your own workflows or utilize built-in workflows. This blog explains how to deploy Azure Kubernetes Cluster with Microsoft Create AKS Cluster action in GitHub Marketplace.

Pre-requisites:

  • GitHub repository
  • Basic knowledge on YAML build pipelines.

Let's get started.

Go to Actions section of the repository and select "set up a workflow yourself" option to create a new action pipeline.

It will open empty yaml file where we add pipeline components. Before adding actions to pipeline, we need to define a name for the pipeline, triggers which tells when to run pipeline and runner which runs the pipeline. Add following script block to do steps explained above.


name: AKS Deployment

on:

  push:

    branches: [ "main" ]

  pull_request:

    branches: [ "main" ]

jobs:

  build:

    runs-on: ubuntu-latest

Now we can add actions to pipeline. Search for Create AKS Cluster action in marketplace and add its content to pipeline. 

ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_SUBSCRIPTION_ID, ARM_TENANT_ID are used to build up a connection with Azure. These values can be kept as secrets and use in action pipeline as explained in this blog. RESOURCE_GROUP_NAME is the resource group of AKS cluster. use CLUSTER_NAME to give name for the cluster, define cluster size as dev or test using CLUSTER_SIZE.

This action uses terraform internally. So, we need to add STORAGE_ACCOUNT_NAME, STORAGE_CONTAINER_NAME, and STORAGE_ACCESS_KEY where it keeps terraform state file.

Give ACTION_TYPE as create to deploy a cluster. If you want to create Azure Container Registry with same name as cluster, give value true for CREATE_ACR

    steps:

    - name: Create AKS Cluster

      uses: Azure/aks-create-action@1.1

      with:

      # Client ID for Service Principal

        ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }}

      # Client Secret for Service Principal

        ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }}

      # Subscription for Service Principal

        ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }}

      # Tenant for Service Principal

        ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }}

      # Resource Group for cluster

        RESOURCE_GROUP_NAME: demo-dev-rg

      # Name for cluster

        CLUSTER_NAME: akstestcluster01

      # Size of cluster (dev or test)

        CLUSTER_SIZE: dev

      # Name for Storage Account

        STORAGE_ACCOUNT_NAME: clusterstoragedemo

      # Name for Storage Container

        STORAGE_CONTAINER_NAME: testcluster

      # Access Key for Storage Container

        STORAGE_ACCESS_KEY: ${{ secrets.STORAGE_ACCESS_KEY }}

      # create or destroy

        ACTION_TYPE: create

      # create ACR with cluster name

        CREATE_ACR: true


Run the action pipeline to deploy AKS cluster. You can find details of the resources going to be created in build pipeline as below.


Go to Azure portal and go to relevant resource group. You can find AKS cluster and Azure Container Registry created.




No comments:

Post a Comment