Jenkins Kubernetes Plugin , how create new pod - unix1998/technical_notes GitHub Wiki

When using the Jenkins Kubernetes plugin to dynamically create pods, Jenkins will not necessarily create the pods on the same virtual host (VM) as the Jenkins master. Instead, the pods will be created on the nodes (which can be virtual or physical machines) within the Kubernetes cluster that Jenkins is configured to use. Here’s a detailed explanation:

Kubernetes Cluster Context

  1. Jenkins Master on Kubernetes:

    • If Jenkins itself is running as a pod within a Kubernetes cluster, it interacts with the Kubernetes API to manage the creation of additional pods for running agents (slaves).
  2. Pod Scheduling:

    • The Kubernetes scheduler determines where to create the pods based on resource availability, node selectors, taints, tolerations, and other scheduling policies. Jenkins does not control which specific node the pod will run on; it simply requests a pod to be created.
  3. Jenkins Kubernetes Plugin Configuration:

    • The Jenkins Kubernetes plugin is configured to interact with the Kubernetes API of your cluster. This means that any pods defined by podTemplate will be created somewhere within the cluster, not necessarily on the same node as the Jenkins master pod.

Example Pipeline Execution:

podTemplate(containers: [
    containerTemplate(name: 'maven', image: 'maven:3.3.9-jdk-8-alpine', ttyEnabled: true, command: 'cat'),
    containerTemplate(name: 'golang', image: 'golang:1.8.0', ttyEnabled: true, command: 'cat')
]) {

    node(POD_LABEL) {
        stage('Get a Maven project') {
            git url: 'https://github.com/your-org/your-maven-project.git'
            container('maven') {
                stage('Build a Maven project') {
                    sh 'mvn -B clean install'
                }
            }
        }

        stage('Get a Golang project') {
            git url: 'https://github.com/your-org/your-golang-project.git'
            container('golang') {
                stage('Build a Go project') {
                    sh """
                    mkdir -p /go/src/github.com/hashicorp
                    ln -s `pwd` /go/src/github.com/hashicorp/terraform
                    cd /go/src/github.com/hashicorp/terraform && make core-dev
                    """
                }
            }
        }
    }
}

Key Points:

  1. Pod Creation:

    • The podTemplate block defines the configuration for the pod, which includes the containers needed for the build.
    • When the pipeline reaches the node(POD_LABEL) block, Jenkins requests Kubernetes to create a pod matching the defined template.
  2. Pod Scheduling:

    • Kubernetes schedules the pod on an available node within the cluster. This node is selected based on the cluster's scheduling policies and available resources.
  3. Pod Execution:

    • The stages defined within the node(POD_LABEL) block are executed within the created pod.
    • Each container block specifies which container within the pod to use for executing the subsequent commands.

Summary:

  • Jenkins does not control specific node placement: The pods are created on any node within the Kubernetes cluster, not necessarily on the same virtual host as the Jenkins master.
  • Kubernetes Scheduling: The Kubernetes scheduler handles where the pods will be created based on its own policies and resource availability.
  • Isolated and Consistent Build Environments: Each pod provides an isolated environment for the Jenkins agent, ensuring a consistent and reproducible build process.

By leveraging the Kubernetes cluster, Jenkins can scale and manage build environments dynamically, utilizing the resources of the entire cluster rather than being confined to a single host.