Skip to content

Commit 93ea4bb

Browse files
authored
Update README with agent injection in mind (#1606)
* Update README with agent injection in mind Following #1602, there are several sentences in the README that can be made more generic, avoiding references to `jnlp`. As it is still the default value, I couldn't remove all references, but I think it is slightly clearer now. * Fix review and refresh pod template screenshot * Refresh
1 parent 9a8dca4 commit 93ea4bb

6 files changed

+45
-50
lines changed

README.md

+45-50
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,15 @@ Feel free to enable it and provide feedback about this functionality.
9292
## Static pod templates
9393

9494
In addition to that, in the **Kubernetes Pod Template** section, we need to configure the image that will be used to
95-
spin up the agent pod. We do not recommend overriding the `jnlp` container except under unusual circumstances.
96-
For your agent, you can use the default Jenkins agent image available in [Docker Hub](https://hub.docker.com). In the
97-
‘Kubernetes Pod Template’ section you need to specify the following (the rest of the configuration is up to you):
95+
spin up the agent pod. The Jenkins agent runs by default in a container named `jnlp` (historical name, kept for backward compatibility).
96+
97+
You may use it, use a different agent container name and/or provide your own container image.
98+
99+
To avoid drift between the Jenkins controller and your agent, we recommend *not* to include the agent JAR in your image, but instead use the *Inject Jenkins agent in agent container* checkbox.
100+
101+
The provided container image must have a JRE installed that is compatible with the Java version required by the Jenkins controller.
102+
103+
In the ‘Kubernetes Pod Template’ section you need to specify the following (the rest of the configuration is up to you):
98104
Kubernetes Pod Template Name - can be any and will be shown as a prefix for unique generated agent’ names, which will
99105
be run automatically during builds
100106
Docker image - the docker image name that will be used as a reference to spin up a new Jenkins agent, as seen below
@@ -115,9 +121,13 @@ use this cloud configuration you will need to add it in the jobs folder's config
115121
# Usage
116122
## Overview
117123

118-
The Kubernetes plugin allocates Jenkins agents in Kubernetes pods. Within these pods, there is always one special
119-
container `jnlp` that is running the Jenkins agent. Other containers can run arbitrary processes of your choosing,
120-
and it is possible to run commands dynamically in any container in the agent pod.
124+
The Kubernetes plugin allocates Jenkins agents in Kubernetes pods.
125+
126+
Within these pods, there is always one special container that is running the Jenkins agent.
127+
128+
Its name defaults to `jnlp`, but you can override the name with one of your choosing in pod template configuration.
129+
130+
Other containers can run arbitrary processes of your choosing, and it is possible to run commands dynamically in any container in the agent pod using the `container` step (pipeline only).
121131

122132
## Using a label
123133

@@ -146,10 +156,9 @@ podTemplate {
146156
}
147157
```
148158

149-
Commands will be executed by default in the `jnlp` container, where the Jenkins agent is running.
150-
(The `jnlp` name is historical and is retained for compatibility.)
159+
Commands will be executed by default in the designated agent container, where the Jenkins agent is running.
151160

152-
This will run in the `jnlp` container:
161+
This will run in the agent container:
153162
```groovy
154163
podTemplate {
155164
node(POD_LABEL) {
@@ -162,7 +171,7 @@ podTemplate {
162171

163172
Find more examples in the [examples dir](examples).
164173

165-
The default jnlp agent image used can be customized by adding it to the template
174+
The default agent image used can be customized by adding it to the template
166175

167176
```groovy
168177
containerTemplate(name: 'jnlp', image: 'jenkins/inbound-agent', args: '${computer.jnlpmac} ${computer.name}'),
@@ -427,7 +436,7 @@ kind: Pod
427436
spec:
428437
containers:
429438
- name: maven
430-
image: maven:3.8.1-jdk-8
439+
image: maven:3.9.9-eclipse-temurin-17
431440
command:
432441
- sleep
433442
args:
@@ -462,29 +471,27 @@ See [Defining a liveness command](https://kubernetes.io/docs/tasks/configure-pod
462471
## Overview
463472

464473
A pod template may or may not inherit from an existing template.
465-
This means that the pod template will inherit node selector, service account, image pull secrets, container templates
466-
and volumes from the template it inherits from.
467474

468-
**yaml** is merged according to the value of `yamlMergeStrategy`.
475+
Depending on fields, the inheritance behaviour can vary. For simple values (strings), the child template will override the parent template. However for complex values (lists, maps), the child template will merge with the parent template.
469476

470-
**Service account** and **Node selector** when are overridden completely substitute any possible value found on the 'parent'.
471-
472-
**Container templates** that are added to the podTemplate, that has a matching containerTemplate (a container template
477+
* **yaml** is merged according to the value of `yamlMergeStrategy` specified in the child pod template.
478+
* **Service account** and **Node selector** when are overridden completely substitute any possible value found on the 'parent'.
479+
* **Container templates** that are added to the podTemplate, that has a matching containerTemplate (a container template
473480
with the same name) in the 'parent' template, will inherit the configuration of the parent containerTemplate.
474481
If no matching container template is found, the template is added as is.
475-
476-
**Volume** inheritance works exactly as **Container templates**.
477-
478-
**Image Pull Secrets** are combined (all secrets defined both on 'parent' and 'current' template are used).
482+
* **Volume** inheritance works exactly as **Container templates**.
483+
* **Image Pull Secrets** are combined (all secrets defined both on 'parent' and 'current' template are used).
479484

480485
In the example below, we will inherit from a pod template we created previously, and will just override the version of
481-
`maven` so that it uses jdk-11 instead:
486+
`maven` so that it uses Java 21 instead:
482487

483-
![image](images/mypod.png)
488+
![image](images/mypod-1-general.png)
489+
![image](images/mypod-2-golang.png)
490+
![image](images/mypod-3-maven.png)
484491

485492
```groovy
486493
podTemplate(inheritFrom: 'mypod', containers: [
487-
containerTemplate(name: 'maven', image: 'maven:3.8.1-jdk-11')
494+
containerTemplate(name: 'maven', image: 'maven:3.9.9-eclipse-temurin-21')
488495
]) {
489496
node(POD_LABEL) {
490497
@@ -503,7 +510,7 @@ pipeline {
503510
spec:
504511
containers:
505512
- name: maven
506-
image: maven:3.8.1-jdk-11
513+
image: maven:3.9.9-eclipse-temurin-21
507514
'''
508515
509516
}
@@ -520,22 +527,22 @@ Also, the `golang` container will be added as defined in the 'parent' template.
520527

521528
## Multiple Pod template inheritance
522529

523-
Field `inheritFrom` may refer a single podTemplate or multiple separated by space. In the later case each template will
530+
Field `inheritFrom` may refer a single pod template or multiple separated by space. In the later case each template will
524531
be processed in the order they appear in the list *(later items overriding earlier ones)*.
525532
In any case if the referenced template is not found it will be ignored.
526533

527534

528535
## Nesting Pod templates
529536

530-
Field `inheritFrom` provides an easy way to compose podTemplates that have been pre-configured. In many cases it would
531-
be useful to define and compose podTemplates directly in the pipeline using groovy.
537+
Field `inheritFrom` provides an easy way to compose pod templates that have been pre-configured. In many cases it would
538+
be useful to define and compose pod templates directly in the pipeline using groovy.
532539
This is made possible via nesting. You can nest multiple pod templates together in order to compose a single one.
533540

534541
The example below composes two different pod templates in order to create one with maven and docker capabilities.
535542

536543
```groovy
537-
podTemplate(containers: [containerTemplate(image: 'docker', name: 'docker', command: 'cat', ttyEnabled: true)]) {
538-
podTemplate(containers: [containerTemplate(image: 'maven', name: 'maven', command: 'cat', ttyEnabled: true)]) {
544+
podTemplate(containers: [containerTemplate(image: 'docker', name: 'docker', command: 'sleep', args: '99d')]) {
545+
podTemplate(containers: [containerTemplate(image: 'maven', name: 'maven', command: 'sleep', args: '99d')]) {
539546
node(POD_LABEL) { // gets a pod with both docker and maven
540547
541548
}
@@ -618,7 +625,7 @@ pipeline {
618625
spec:
619626
containers:
620627
- name: maven
621-
image: maven:alpine
628+
image: maven:3.9.9-eclipse-temurin-17
622629
command:
623630
- cat
624631
tty: true
@@ -671,7 +678,7 @@ pipeline {
671678
//cloud 'kubernetes'
672679
containerTemplate {
673680
name 'maven'
674-
image 'maven:3.8.1-jdk-8'
681+
image 'maven:3.9.9-eclipse-temurin-17'
675682
command 'sleep'
676683
args '99d'
677684
}
@@ -684,7 +691,7 @@ pipeline {
684691
```
685692

686693
Run steps within a container by default. Steps will be nested within an implicit `container(name) {...}` block instead
687-
of being executed in the jnlp container.
694+
of being executed in the agent container.
688695

689696
```groovy
690697
pipeline {
@@ -760,7 +767,7 @@ pipeline {
760767
spec:
761768
containers:
762769
- name: maven
763-
image: maven:3.8.1-jdk-8
770+
image: maven:3.9.9-eclipse-temurin-17
764771
command:
765772
- sleep
766773
args:
@@ -822,7 +829,7 @@ OpenShift runs containers using a _random_ UID that is overriding what is specif
822829
For this reason, you may end up with the following warning in your build
823830

824831
```
825-
[WARNING] HOME is set to / in the jnlp container. You may encounter troubles when using tools or ssh client. This usually happens if the uid doesnt have any entry in /etc/passwd. Please add a user to your Dockerfile or set the HOME environment variable to a valid directory in the pod template definition.
832+
[WARNING] HOME is set to / in the agent container. You may encounter troubles when using tools or ssh client. This usually happens if the uid doesnt have any entry in /etc/passwd. Please add a user to your Dockerfile or set the HOME environment variable to a valid directory in the pod template definition.
826833
```
827834
828835
At the moment the jenkinsci agent image is not built for OpenShift and will issue this warning.
@@ -847,18 +854,6 @@ beta.kubernetes.io/os=linux
847854
You can run pods on Windows if your cluster has Windows nodes.
848855
See the [example](src/main/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/samples/windows.groovy).
849856
850-
# Constraints
851-
852-
Multiple containers can be defined in a pod.
853-
One of them is automatically created with name `jnlp`, and runs the Jenkins JNLP agent service, with args `${computer.jnlpmac} ${computer.name}`,
854-
and will be the container acting as Jenkins agent.
855-
856-
Other containers must run a long running process, so the container does not exit. If the default entrypoint or command
857-
just runs something and exit then it should be overridden with something like `cat` with `ttyEnabled: true`.
858-
859-
**WARNING**
860-
If you want to provide your own Docker image for the inbound agent, you **must** name the container `jnlp` so it overrides the default one. Failing to do so will result in two agents trying to concurrently connect to the controller.
861-
862857
# Configuration on minikube
863858
864859
Create and start [minikube](https://github.com/kubernetes/minikube)
@@ -931,7 +926,7 @@ touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt
931926
touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
932927
touch: /home/jenkins/agent/workspace/thejob@tmp/durable-e0b7cd27/jenkins-log.txt: Permission denied
933928
```
934-
Usually this happens when UID of the user in `jnlp` container differs from the one in another container(s).
929+
Usually this happens when UID of the user in agent container differs from the one in another container(s).
935930
All containers you use should have the same UID of the user, also this can be achieved by setting `securityContext`:
936931
```yaml
937932
apiVersion: v1
@@ -941,7 +936,7 @@ spec:
941936
runAsUser: 1000 # default UID of jenkins user in agent image
942937
containers:
943938
- name: maven
944-
image: maven:3.8.1-jdk-8
939+
image: maven:3.9.9-eclipse-temurin-17
945940
command:
946941
- cat
947942
tty: true
@@ -967,7 +962,7 @@ RUN keytool -noprompt -storepass changeit -cacerts \
967962
USER jenkins
968963
```
969964

970-
Then, use it as the `jnlp` container for the pod template as usual. No command or args need to be specified.
965+
Then, use it as the agent container for the pod template as usual. No command or args need to be specified.
971966

972967
> **Notes:**
973968
>

images/mypod-1-general.png

59.5 KB
Loading

images/mypod-2-golang.png

71.7 KB
Loading

images/mypod-3-maven.png

73 KB
Loading

images/mypod.png

-129 KB
Binary file not shown.

images/pod-template-configuration.png

-128 KB
Loading

0 commit comments

Comments
 (0)