Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running kubectl command from jenkins container

I installed minikube on a VirtualBox(ubuntu) to simulate a deploy environment and used Jenkins pipeline. On my jenkins container i also installed kubectl command to control minikube.

So i create the following stage:

stage("k8s command test") {                        
        withCredentials([usernamePassword(credentialsId: 'cbcba826-ef01-4b18-856f-e6dc4eb27c1f', usernameVariable: 'my-user', passwordVariable: 'PASSWORD')]) {
            sh """
                kubectl config view
                kubectl create -f /home/my-user/file-svc.yml
            """
        }
    }

First thing my cluster seems to be empty in output i have :

+ kubectl config view
apiVersion: v1
clusters: []
contexts: []
current-context: ""
kind: Config
preferences: {}
users: []

if i check .kube/config i have the following config :

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /home/my-user/.minikube/ca.crt
    server: https://127.0.0.1:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    as-user-extra: {}
    client-certificate: /home/my-user/.minikube/client.crt
    client-key: /home/my-user/.minikube/client.key

As i'm new on kubernetes i need help to understand how to make possible the communication between jenkins and minikube.

consequently kubectl create -f /home/my-user/file-svc.yml raised an error about authentication. I read many article kubectl need certificate to be connected to the API server how can i do that in jenkins

like image 995
Lionel Piroche Avatar asked Nov 28 '17 16:11

Lionel Piroche


2 Answers

It seems KUBECONFIG environment variable is not assigned to the correct config file.Therefore, you need to assign it first by following the command

export KUBECONFIG=~/.kube/config

Now kubectl can communicate with kube-api server. However, It needs to show its identity and capability to take actions on k8s resources, therefore, it will take client.crt and client.key files whenever it communicate with kube-api server. Now it can perform following tasks

kubectl apply -f deployment.yaml
kubectl get pods
kubectl get nodes 

Solutions to your problem

Jenkins needs to have these certificate and keys in order to perform operations such as create, apply, get ,watch etc.

like image 52
Suresh Vishnoi Avatar answered Oct 21 '22 19:10

Suresh Vishnoi


thank for your help, finaly i set cluster on jenkins and use --insecure-skip-tls-verify=true to avoid authentication as it's a test machine

kubectl config set-cluster minikube --server=https://127.0.0.1:8443 --insecure-skip-tls-verify=true
kubectl config set-context minikube --cluster=minikube --user=minikube
kubectl config use-context minikube 

And both container start on the same network

like image 25
Lionel Piroche Avatar answered Oct 21 '22 18:10

Lionel Piroche