How to connect to a specific pod in AKS


Description

The article describes how to connect to a specific pod in Azure Kubernetes Service (AKS) cluster using PowerShell.

Solution

To connect to a specific pod in Azure Kubernetes Service (AKS), consider the following steps:

  1. Install the Azure Az PowerShell module as described here:
    https://learn.microsoft.com/en-us/powershell/azure/new-azureps-module-az
  2. Run Windows PowerShell as Administrator.
  3. Execute the az login command.
    You should get prompted to authorize a connection to Azure using your browser.
  4. Use Azure Portal to locate the resource (Kubernetes service) you are connecting to.
    You might need to change your identity to access certain directories, see
    https://learn.microsoft.com/en-us/azure/devtest/offer/how-to-change-directory-tenants-visual-studio-azure
  5. Click Connect, then use PowerShell to execute the suggested commands. For example:
    az account set --subscription <subscription_id>
    az aks get-credentials --resource-group <resource_group_name> --name <aks_name>
    This is an important step as it sets the current Azure Resource Group for all subsequent commands.
  6. Use PowerShell command:
    kubectl get deployments --all-namespaces=true
    to list all deployments and find the necessary namespace.
  7. Identify the pod name using the namespace and label, e.g.
    kubectl get pods -l app.kubernetes.io/name=cm -namespace <namespace>

 

To start a PowerShell session in the context of a specific pod use:

kubectl exec -it <pod_name> -namespace <namespace> -- powershell