How to deploy to a (local) Kubernetes cluster using Jenkins

user538578964

This question is somewhat related to one of my previous questions as in it gives a clearer idea on what I am trying to achieve.. This question is about an issue I ran into when trying to achieve the task in that previous question...

I am trying to test if my kubectl works from within the Jenkins container. When I start up my Jenkins container I use the following command:

docker run \ 
    -v /home/student/Desktop/jenkins_home:/var/jenkins_home \
    -v $(which kubectl):/usr/local/bin/kubectl \ #bind docker host binary to docker container binary
    -v ~/.kube:/home/jenkins/.kube \ #docker host kube config file stored in /.kube directory. Binding this to $HOME/.kube in the docker container
    -v /var/run/docker.sock:/var/run/docker.sock \ 
    -v $(which docker):/usr/bin/docker -v ~/.kube:/home/root/.kube \ 
    --group-add 998 
    -p 8080:8080 -p 50000:50000 
    -d --name jenkins jenkins/jenkins:lts

The container starts up and I can login/create jobs/run pipeline scripts all no issue.

I created a pipeline script just to check if I can access my cluster like this:

pipeline {
    agent any
    stages {
        stage('Kubernetes test') {
            steps {
                sh "kubectl cluster-info"
            }
        }
    }
}

When running this job, it fails with the following error:

+ kubectl cluster-info // this is the step

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
error: the server doesn't have a resource type "services"

Thanks!

Janshair Khan

I'm not getting why there is:

-v $(which kubectl):/usr/local/bin/kubectl -v ~/.kube:/home/jenkins/.kube

/usr/local/bin/kubectl is a kubectl binary and ~/.kube:/home/jenkins/.kube should be the location where the kubectl binary looks for the cluster context file i.e. kubeconfig. First, you should make sure that the kubeconfig is mounted to the container at /home/jenkins/.kube and is accessible to kubectl binary. After appropriate volume mounts, you can verify by creating a session in the jenkins container with docker container exec -it jenkins /bin/bash and test with kubectl get svc. Make sure you have KUBECONFIG env var set in the session with:

export KUBECONFIG=/home/jenkins/.kube/kubeconfig

Before you run the verification test and

withEnv(["KUBECONFIG=$HOME/.kube/kubeconfig"]) {
// Your stuff here
}

In your pipeline code. If it works with the session, it should work in the pipeline as well.

I would personally recommend to create a custom Docker image for Jenkins which will contain kubectl binary and other utilities necessary (such as aws-iam-authenticator for AWS EKS IAM-based authentication) for working with Kubernetes cluster. This creates isolation between your host system binaries and your Jenkins binaries.

Below is the Dockerfile I'm using which contains, helm, kubectl and aws-iam-authenticator.

# This Dockerfile contains Helm, Docker client-only, aws-iam-authenticator, kubectl with Jenkins LTS.

FROM jenkins/jenkins:lts
USER root

ENV VERSION v2.9.1
ENV FILENAME helm-${VERSION}-linux-amd64.tar.gz
ENV HELM_URL https://storage.googleapis.com/kubernetes-helm/${FILENAME}
ENV KUBE_LATEST_VERSION="v1.11.0"

# Install the latest Docker CE binaries
RUN apt-get update && \
    apt-get -y install apt-transport-https \
      ca-certificates \
      curl \
      gnupg2 \
      software-properties-common && \
    curl -fsSL https://download.docker.com/linux/$(. /etc/os-release; echo "$ID")/gpg > /tmp/dkey; apt-key add /tmp/dkey && \
    add-apt-repository \
      "deb [arch=amd64] https://download.docker.com/linux/$(. /etc/os-release; echo "$ID") \
      $(lsb_release -cs) \
      stable" && \
   apt-get update && \
   apt-get -y install docker-ce \
   && curl -o /tmp/$FILENAME ${HELM_URL} \
   && tar -zxvf /tmp/${FILENAME} -C /tmp \
   && mv /tmp/linux-amd64/helm /bin/helm \
   && rm -rf /tmp/linux-amd64/helm \
   && curl -L https://storage.googleapis.com/kubernetes-release/release/${KUBE_LATEST_VERSION}/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl \
   && chmod +x /usr/local/bin/kubectl \
   && curl -L https://amazon-eks.s3-us-west-2.amazonaws.com/1.11.5/2018-12-06/bin/linux/amd64/aws-iam-authenticator -o /usr/local/bin/aws-iam-authenticator \
   && chmod +x /usr/local/bin/aws-iam-authenticator

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to deploy a ConfigMap using kubernetes client Api

分類Dev

How to create a user in a Kubernetes cluster?

分類Dev

Docker image push from Jenkins to Dockerhub and deployment to Kubernetes cluster

分類Dev

Upgrade Kubernetes Cluster using Terraform's provisioner

分類Dev

How to deploy Kafka Stream applications on Kubernetes?

分類Dev

How to deploy tomcat web apps with Jenkins installed

分類Dev

Kubernetes - How to access nginx load balancing from outside the cluster using a NodePort service

分類Dev

How to use kubeadm to create kubernetes cluster?

分類Dev

how to configure already running cluster in kubernetes

分類Dev

How to remove GPU in Google Kubernetes cluster (GKE)

分類Dev

How to find available resources in a Kubernetes Cluster level?

分類Dev

How to cluster in Nodejs - just local calculations

分類Dev

How to deploy new changes of my flow to Apache Flink cluster?

分類Dev

How to use local docker containers with Kubernetes

分類Dev

How to (properly) Deploy MongoDB on Kubernetes and Access it from Another Pod/Job?

分類Dev

How should I deploy Persistent Volume(PV) for JupyterHub on Kubernetes?

分類Dev

Kafka Producer deployed on Kubernetes not able to produce to Kafka cluster running on local machine

分類Dev

How to create a GCP Kubernetes Engine cluster spanning two regions?

分類Dev

How to enable Client Certificate in Google Kubernetes Engine Cluster

分類Dev

using docker-compose on a kubernetes instance with jenkins - mounting empty volumes

分類Dev

Reset Kubernetes cluster

分類Dev

How to add an Azure AKS Kubernetes Cluster self-signed CA to GitLab CI/CD Kubernetes integration?

分類Dev

How to implement cluster of points with using BaiduMap SDK?

分類Dev

how to connect to database service using cluster ip

分類Dev

kube-dns は「kubernetes.default.svc.cluster.local」を解決できません

分類Dev

How to make Helm work on local single instance Kubernetes?

分類Dev

How to get all Kubernetes Deployment objects using kubernetes java client?

分類Dev

I want to ping (icmp) monitor the worker nodes that make up the kubernetes cluster without using the internal IP of the node

分類Dev

Kubernetes Cluster Context with Multiple Namespaces

Related 関連記事

  1. 1

    How to deploy a ConfigMap using kubernetes client Api

  2. 2

    How to create a user in a Kubernetes cluster?

  3. 3

    Docker image push from Jenkins to Dockerhub and deployment to Kubernetes cluster

  4. 4

    Upgrade Kubernetes Cluster using Terraform's provisioner

  5. 5

    How to deploy Kafka Stream applications on Kubernetes?

  6. 6

    How to deploy tomcat web apps with Jenkins installed

  7. 7

    Kubernetes - How to access nginx load balancing from outside the cluster using a NodePort service

  8. 8

    How to use kubeadm to create kubernetes cluster?

  9. 9

    how to configure already running cluster in kubernetes

  10. 10

    How to remove GPU in Google Kubernetes cluster (GKE)

  11. 11

    How to find available resources in a Kubernetes Cluster level?

  12. 12

    How to cluster in Nodejs - just local calculations

  13. 13

    How to deploy new changes of my flow to Apache Flink cluster?

  14. 14

    How to use local docker containers with Kubernetes

  15. 15

    How to (properly) Deploy MongoDB on Kubernetes and Access it from Another Pod/Job?

  16. 16

    How should I deploy Persistent Volume(PV) for JupyterHub on Kubernetes?

  17. 17

    Kafka Producer deployed on Kubernetes not able to produce to Kafka cluster running on local machine

  18. 18

    How to create a GCP Kubernetes Engine cluster spanning two regions?

  19. 19

    How to enable Client Certificate in Google Kubernetes Engine Cluster

  20. 20

    using docker-compose on a kubernetes instance with jenkins - mounting empty volumes

  21. 21

    Reset Kubernetes cluster

  22. 22

    How to add an Azure AKS Kubernetes Cluster self-signed CA to GitLab CI/CD Kubernetes integration?

  23. 23

    How to implement cluster of points with using BaiduMap SDK?

  24. 24

    how to connect to database service using cluster ip

  25. 25

    kube-dns は「kubernetes.default.svc.cluster.local」を解決できません

  26. 26

    How to make Helm work on local single instance Kubernetes?

  27. 27

    How to get all Kubernetes Deployment objects using kubernetes java client?

  28. 28

    I want to ping (icmp) monitor the worker nodes that make up the kubernetes cluster without using the internal IP of the node

  29. 29

    Kubernetes Cluster Context with Multiple Namespaces

ホットタグ

アーカイブ