Search code examples
azurekubernetesgithub-actionsazure-akskubeconfig

AKS unable to recognize any kubectl commands


I'm currently trying to deploy two microservices to my AKS cluster using github actions.
(I've already tried deploying it to minikube, which worked flawlessly).

The process:

  1. Build docker images and push to dockerhub
  2. Login using azure/login@1.4.6
  3. Setup kubectl using azure/setup-kubectl@v2.0
  4. Deploy the microservices simply using kubectl apply -f ./service-1/k8s

Note: ./service-1/k8s/ includes service.yaml, ingress.yaml & deployment.yaml, which all work correctly when deployed to minikube using the exact same process (minus az-specific steps).

Now then, the problem arises up on using (any) kubectl command. The error in the console is the following:

E0118 [time]    1792 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0118 [time]    1792 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
E0118 [time]    1792 memcache.go:265] couldn't get current server API group list: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
unable to recognize "service-1/k8s/deployment.yaml": Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
unable to recognize "service-1/k8s/ingress.yaml": Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
unable to recognize "service-1/k8s/service.yaml": Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused

Sadly I have no idea what this error means.

Thanks for your help!


Solution

  • The errors you're encountering indicate that kubectl is trying to communicate with a Kubernetes cluster API server at localhost:8080, which is the default when no context is set, and it's failing to connect. This suggests that kubectl is not configured with the correct context to your AKS cluster.

    In your GitHub Actions workflow, after setting up kubectl with azure/setup-kubectl@v2.0, you need to ensure that kubectl is configured to use the credentials for your AKS cluster. Typically, this is done by using the az aks get-credentials command with Azure CLI, which automatically sets the correct context in kubectl to communicate with your AKS cluster.

    Here's what you need to do in your GitHub Actions workflow to correct this:

    After logging in with azure/login@1.4.6, fetch the credentials for your AKS cluster using Azure CLI:

    - name: Get AKS credentials
      run: az aks get-credentials --resource-group <YourResourceGroupName> --name <YourClusterName> --overwrite-existing
    

    Confirm that the kubectl context has been set to your AKS cluster:

    - name: Check kubectl context
      run: kubectl config current-context
    

    enter image description here

    Apply your Kubernetes manifests with kubectl:

    - name: Deploy to AKS
      run: kubectl apply -f ./service-1/k8s/
    

    enter image description here

    Here, I have already added the $rg, $clustername and $creds seperately in variables. Now I don't know If you're using a service principal to authenticate with Azure, if so, then ensure that the service principal has the necessary permissions to fetch credentials for the AKS cluster and perform the required operations.

    az ad sp create-for-rbac --name "APIM" --scope /subscriptions/<yoursubscriptionId>/resourceGroups/<yourRG> --role Contributor --json-auth
    

    enter image description here

    Verify that the Azure credentials stored in GitHub Secrets ($creds) have the correct format and contain all required fields (clientId, clientSecret, subscriptionId, tenantId).

    enter image description here

    and last but not least, verify that the Kubernetes API server is running by checking the status of the kube-apiserver pod using the

    kubectl get pods -n kube-system
    

    enter image description here

    By ensuring kubectl is correctly pointed at your AKS cluster, it should resolve the connection issues and allow your deployments to proceed.

    Kindly refer to below documentation for ease as it has demonstrated step by step process with example-

    Deploy to Azure Kubernetes Services using GitHub Actions

    Official MS Doc to build, test and deploy container images to AKS using github action

    Similar issue resolution