skip to Main Content

I am trying to call simple kubectl commands from Jenkins for my Azure setup.

Jenkins is installed jumpbox which is connected to AKS and ACR. kubectl commands run perfectly fine in the terminal however fail when called from Jenkins.

Below is the Jenkins script

        stage('Deploy Image') {
            steps {
                echo 'Deploying Image to AKS ....'
                withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: '', namespace: 'icx-preprod', restrictKubeConfigAccess: false, serverUrl: '') 
                // sh 'kubectl delete statetfulset dpms -n icx-preprod'
                sh 'kubectl apply -f /home/icx-user/nonwebpa-yamls/rt/rt.yaml -n preprod'
            }
        }

and errors are

+ kubectl apply -f /home/icx-user/nonwebpa-yamls/rt/rt.yaml -n preprod
Error from server (NotFound): the server could not find the requested resource

and

+ kubectl get pods -n preprod
E1010 07:30:08.881057   47786 memcache.go:265] couldn't get current server API group list: invalid character '<' looking for beginning of value
E1010 07:30:08.897305   47786 memcache.go:265] couldn't get current server API group list: invalid character '<' looking for beginning of value
E1010 07:30:08.910836   47786 memcache.go:265] couldn't get current server API group list: invalid character '<' looking for beginning of value
E1010 07:30:08.931700   47786 memcache.go:265] couldn't get current server API group list: invalid character '<' looking for beginning of value
E1010 07:30:08.950612   47786 memcache.go:265] couldn't get current server API group list: invalid character '<' looking for beginning of value
error: invalid character '<' looking for beginning of value
[Pipeline] }
[kubernetes-cli] kubectl configuration cleaned up

2

Answers


  1. Chosen as BEST ANSWER

    Ok so basically we need to do "az login" within Jenkins before we call "kubectl"

    sample code

                        // Login to Azure CLI using Service Principal
                        sh 'az login --service-principal -u <client_id> -p <client_secret> -t <tenant_id>'
                        sh 'az account set --subscription <subscription_name>'
                        // Connect with kube cluster
                        sh 'az aks get-credentials --overwrite-existing --resource-group <resource_group_name> --name <cluster_name>'
                        sh 'kubectl get pods -n <namespace>'
    

  2. Basically K8s api-server is not reachable from jenkins is because you’ve not configure the right k8s api in jenkins or the plugin that you’re using is deprecated. You can also use kube-config in jenkins server to interact to kubernetes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search