Jenkins
Kubernetes
declarative pipeline
pod template
CI/CD

Jenkins Kubernetes Plugin declarative pipeline and pod template inheritance

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

Pod template inheritance in the Jenkins Kubernetes plugin is a way to reuse a common agent definition instead of repeating the same YAML in every pipeline. In declarative pipelines, the usual mechanism is inheritFrom, which lets a job extend a named base template with a small set of pipeline-specific changes.

Define a Stable Base Template

Inheritance works best when the parent template contains only genuinely shared settings. Typical candidates are the service account, shared volumes, workspace settings, and one or two standard tool containers.

For example, a Jenkins administrator might define a base template named maven-base in the Kubernetes cloud configuration. A pipeline can then inherit from it instead of restating the entire pod spec.

Extend the Base from a Declarative Pipeline

The child pipeline can add a container, override the default container, or attach additional YAML. A simple example looks like this:

groovy
1pipeline {
2    agent {
3        kubernetes {
4            inheritFrom 'maven-base'
5            defaultContainer 'maven'
6            yaml '''
7apiVersion: v1
8kind: Pod
9spec:
10  containers:
11    - name: maven
12      image: maven:3.9.9-eclipse-temurin-21
13      command:
14        - cat
15      tty: true
16    - name: docker
17      image: docker:27-cli
18      command:
19        - cat
20      tty: true
21'''
22        }
23    }
24
25    stages {
26        stage('Build') {
27            steps {
28                container('maven') {
29                    sh 'mvn -version'
30                }
31            }
32        }
33    }
34}

The intent is clear: inherit the baseline pod from Jenkins, then add or override only what this pipeline needs. That keeps the Jenkinsfile readable and makes shared agent changes easier to roll out.

Keep Merge Behavior Predictable

Inheritance is helpful, but it is not magic. The final pod depends on how the parent template was defined and which fields the child restates. Some fields merge intuitively, while others behave more like replacement than deep merge.

That is why simple inheritance trees are easier to maintain. One base template plus a small child override is usually better than stacking several parents and expecting the final pod to be obvious.

Container names deserve special attention. If the parent already defines a maven container and the child also defines maven, you need to know which definition wins in your configuration. Teams that treat container names casually often end up debugging the wrong image rather than the pipeline logic itself.

Debug the Generated Pod, Not Just the Jenkinsfile

When inheritance does not behave the way you expect, inspect the pod that Jenkins actually created in Kubernetes. The source of truth is the generated pod spec, not your mental model of how the merge should work.

Useful checks include:

  • Which containers ended up in the final pod
  • Which container is the default one for shell steps
  • Whether the expected volumes, environment variables, and service account were inherited

This is also where duplicate mounts, missing containers, or overridden values become visible. Debugging only the Jenkinsfile often hides the real issue because the problem lives in the final merged agent definition.

Common Pitfalls

The most common mistake is putting too much unrelated tooling into the base template. That turns inheritance into a dependency burden rather than a simplification.

Another issue is assuming every field participates in a perfect deep merge. In practice, you should verify the resulting pod instead of relying on intuition.

Teams also get into trouble by using unclear container names. If the base and child templates both define containers with the same name, be explicit about which one is supposed to run the build steps.

Summary

  • Use inheritFrom to reuse a named base Kubernetes pod template in a declarative Jenkins pipeline.
  • Keep the parent template focused on shared infrastructure, not every tool every project might need.
  • Add pipeline-specific containers and YAML only where they are actually required.
  • Verify the generated pod in Kubernetes when inheritance results are unclear.
  • Simple inheritance structures are easier to reason about than heavily layered ones.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.