user1938143
user1938143

Reputation: 1184

How to use Pod yaml file in Jenkins to create container

I use a POD yaml file to create a pod in Kubernetes.

My yaml file is simple and looks like this:

kind: Pod
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true

but I got this Error message:

nodes are available: 33 node(s) didn't match node selector.

I run this pod file in Jenkins pipeline, so that a Kaniko can be installed to build a docker image.

any solutions?

Upvotes: 0

Views: 2240

Answers (2)

Hirshberg
Hirshberg

Reputation: 99

you can try this as an example:

pipeline {
  agent {
    kubernetes {
      yaml """
apiVersion: v1
kind: Pod
metadata:
  labels:
    jenkins: worker
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    command: ["/busybox/cat"]
    tty: true
    volumeMounts:
      - name: dockercred
        mountPath: /root/.docker/
  volumes:
  - name: dockercred
    secret:
      secretName: dockercred
"""
    }
  }
  stages {
    stage('Stage 1: Build with Kaniko') {
      steps {
        container('kaniko') {
          sh '/kaniko/executor --context=git://github.com/repository/project.git \
                  --destination=repository/image:tag \
                  --insecure \
                  --skip-tls-verify  \
                  -v=debug'
        }
      }
    }  
  }
}

The secret I mount is the docker credentials to be used by Kaniko.

Upvotes: 1

user15659347
user15659347

Reputation:

You are missing several required keys in your YAML file.

  1. apiVersion key - api version for Pod is currently v1
  2. metadata key - Data that helps uniquely identify the object, including a name string, UID, and optional namespace

You can read more on creating static pods in Kubernetes docs, and if you want some examples for kaniko pod, they are available here.

So, minimal correct pod YAML should looke like this:

kind: Pod
apiVersion: v1
metadata:
  # specify your pod name
  name: <pod-name>
spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true

Addressing the problem: You can assign which pod should run on which node with use of the nodeSelector key. You need to specify it under spec. For example:

spec:
  containers:
  - name: kaniko
    image: gcr.io/kaniko-project/executor:debug
    imagePullPolicy: Always
    command:
    - /busybox/cat
    tty: true
  #here it is
  nodeSelector:
    # and here is node label
    <label-name>: <label-value>

You can find your node labels with

kubectl describe node <node-name>

or add a label to it with

kubectl label node <node-name> <label-name>=<label-value>

You can find more on this in the docs.

Upvotes: 1

Related Questions