NashTech Insights

Configure Kustomize in Kubernetes Manifest

Vikas Vashisth
Vikas Vashisth
Table of Contents
close up photo of green leaf

Hello Everyone, Welcome to the new blog and learning of Kubernetes manifest creation using the Kustomize approach.

In this blog, we will get to learn about the Kustomize where we can create dynamically pass the variables in the Azure DevOps pipeline (ADO) and call the Kubernetes manifest to deploy the application.

Steps to Configure Kustomize in ADO

Here, we are going to generate the dynamic variables for the deployment.yml of our Kubernetes through Powershell using the Kustomize approach.

  • We need to have a base structure of our kubernetes manifest files in the VCS like here we have Github :

  • Here, In the deployment file, we have defined the basic requirement for our application to get deployed. We have used variables for replica count and image to get their values from the pipeline at the runtime.

  • We have also defined our Kustomize to get the values from the defined manifest also.

  • In last we have our pipeline in which we have written the steps to get the values dynamically at the runtime.

trigger: none

name: sampleApplication-deploy

pool: <ADO-Agent-or-Self-Hosted-Agent>

parameters:
 
  - name: Namespace
    displayName: "Name of Namespace"
    type: string
    default: 'dev'   

  - name: REPLICA_COUNT_VV
    displayName: "Replica count for AKS Manifest"
    type : number
    default: '3'

  - name: IMAGE_NAME_VV
    displayName: "Image Name for AKS Manifest"
    type: string
    default: '<acr-url-of-image-with-tag>'
 

stages: 
- stage: Deploy
  displayName: Deploy stage  
  jobs:
   
    strategy:
      runOnce:
        deploy:  
          steps: 
            - checkout: self
                           
            - script: |
                  echo "Check Kubectl and Kustomize version"
                  kubectl version -o json 
                  echo "Check Kubelogin version"
                  kubelogin --version
                  echo "AZ Version"
                  az version
                  echo "Powershell Version"
                  pwsh --version
              displayName: "Dependencies Check"

            - script: |
                   az login --service-principal -u <client-id-of-your-account> -p <client-secret-of-your-account> --tenant <tenant-id-of-your-account>
              displayName: "Login through Service Principal"
      
            - script: |   
                  az account set --subscription <subscription-id-of-your-account>
                  az account show
              displayName: "Set Subscription"
       
            - script: |      
                  kubelogin convert-kubeconfig -l azurecli    
                  az aks get-credentials --name <aks-name-of-your-account> --resource-group <aks-rg-of-your-account> --admin --overwrite-existing 
              displayName: "AKS Configutation commands"
      
            - script: |
                  kubectl create namespace ${{parameters.Namespace}}
                  kubectl get namespace
              displayName: "k8 Create namespace"
              
            - task: PowerShell@2
              displayName: 'Concatenate Manifests with Rendered Manifests'
              inputs:
                targetType: 'inline'
                script: |
                  $bakeManifestsContent = Get-Content "$(System.DefaultWorkingDirectory)/k8s/base/deployment.yml" -Raw
                  $renderedManifestsContent = $bakeManifestsContent -replace '\$\{REPLICA_COUNT\}','${{parameters.REPLICA_COUNT_VV}}' `
                                                                  -replace '\$\{IMAGE_NAME\}','${{parameters.IMAGE_NAME_VV}}'

                  # Save the rendered manifests to a file
                  $renderedManifestsContent | Out-File "$(System.DefaultWorkingDirectory)/k8s/base/deployment.yml" -Encoding UTF8
                  ls
                  cat $(System.DefaultWorkingDirectory)/k8s/base/deployment.yml


            - script: |
                  kubectl apply -k $(System.DefaultWorkingDirectory)/k8s/base -n ${{parameters.Namespace}}
              displayName: "k8 Deployment"

When the Pipeline gets triggers it will override the values through the runtime parameters for the defined variables in the pipeline

Conclusion

So, In this blog, we got to learn about dynamically passing the variables and running the Kubernetes manifest for the deployment of an application using the Kustomization approach.

Kustomize helps to render the deployment as per the environment and namespace for each stage.

Vikas Vashisth

Vikas Vashisth

Vikas Vashisth is working as a Sr.DevOps Engineer at Knoldus | Part of Nashtech with having more than multiple years of experience in the field of continuous integration and delivery, infrastructure automation, and containerization enables me to build scalable, reliable, and highly available environments. I am proficient in tools such as Docker, Kubernetes, Jenkins, Ansible, and Terraform, and have experience working with cloud platforms such as AWS, GCP, and Azure.

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d