Sumanth Kannedari
Sumanth Kannedari

Reputation: 1

Run Kubectl command on AKS

I have deployed a container image onto AKS successfully.

Now I want to run a command and a json file on the AKS using the pipeline once the container image is deployed onto AKS.

Upvotes: 0

Views: 4528

Answers (1)

iamattiq1991
iamattiq1991

Reputation: 1276

First of all you need to install azure cli & kubectl on your system.

Install Azure Cli https://learn.microsoft.com/en-us/cli/azure/install-azure-cli

Install Kubectl https://kubernetes.io/docs/tasks/tools/

As far kubectl is installed, verify its version

kubectl version --client --short
Client Version: v1.23.1

The version in your case might be different.

Now is the time to get AKS credentials (kubeconfig) file to interact with AKS cluster.

az login

provide the credentials for azure AD.

az account set --subscription {subscription_id}

az aks get-credentials --resource-group MyAKSResoucceGroup --name MyAksCluster

Verify if cluster is connected

 kubectl config current-context
  MyAksCluster

You can play around with AKS and run all commands you want to run. Here is the cheatsheet or kubectl.

Kubectl Cheat-Sheet https://www.bluematador.com/learn/kubectl-cheatsheet

In order to run commands using Azure DevOps on you need to create service connection in Azure DevOps to authenticate Azure DevOps with AKS.

Project Settings --> Service Connections --> New Kubernetes Service Connection --> Azure Subscription

enter image description here

Now you can run the kubernetes commands on this AKS using built in kubernetes task or using bash|powershell commands inside your pipeline.

Hope that helps you.

e:g

- task: Kubernetes@1
  inputs:
    connectionType: 'Kubernetes Service Connection'
    kubernetesServiceEndpoint: '12345'
    namespace: 'default'
    command: 'apply'
    useConfigurationFile: true
    configurationType: 'inline'
    inline: 'abcd'
    secretType: 'dockerRegistry'
    containerRegistryType: 'Azure Container Registry'

Upvotes: 3

Related Questions