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
Ok so basically we need to do "az login" within Jenkins before we call "kubectl"
sample code
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.