You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -21,10 +21,46 @@ For that some environment variables are automatically injected:
21
21
*`JENKINS_AGENT_NAME`: the name of the Jenkins agent
22
22
*`JENKINS_NAME`: the name of the Jenkins agent (Deprecated. Only here for backwards compatibility)
23
23
24
-
Tested with [`jenkins/jnlp-slave`](https://hub.docker.com/r/jenkins/jnlp-slave),
25
-
see the [Docker image source code](https://github.com/jenkinsci/docker-jnlp-slave).
24
+
Tested with [`jenkins/inbound-agent`](https://hub.docker.com/r/jenkins/inbound-agent),
25
+
see the [Docker image source code](https://github.com/jenkinsci/docker-inbound-agent).
26
26
27
27
It is not required to run the Jenkins master inside Kubernetes.
28
+
# Generic Setup
29
+
### Prerequisites
30
+
* A running Kubernetes cluster 1.14 or later. For OpenShift users, this means OpenShift Container Platform 4.x.
31
+
* A Jenkins instance installed
32
+
* The Jenkins Kubernetes plugin installed
33
+
34
+
It should be noted that the main reason to use the global pod template definition is to migrate a huge corpus of
35
+
existing projects (incl. freestyle) to run on Kubernetes without changing job definitions. New users setting up new
36
+
Kubernetes builds should use the podTemplate step as shown in the example snippets [here](https://github.com/jenkinsci/kubernetes-plugin/pull/707)
37
+
38
+
Fill in the Kubernetes plugin configuration. In order to do that, you will open the Jenkins UI and navigate to
39
+
**Manage Jenkins -> Configure System -> Cloud -> Kubernetes** and enter in the *Kubernetes URL* and *Jenkins URL*
40
+
appropriately, this is unless Jenkins is running in Kubernetes in which case the defaults work.
41
+
42
+
Supported credentials include:
43
+
44
+
* Username/password
45
+
* Secret File (kubeconfig file)
46
+
* Secret text (Token-based authentication) (OpenShift)
47
+
* Google Service Account from private key (GKE authentication)
48
+
* X.509 Client Certificate
49
+
50
+
To test this connection is successful you can use the **Test Connection** button to ensure there is
51
+
adequate communication from Jenkins to the Kubernetes cluster, as seen below
52
+
53
+

54
+
55
+
In addition to that, in the **Kubernetes Pod Template** section, we need to configure the image that will be used to
56
+
spin up the agent pod. We do not recommend overriding the `jnlp` container except under unusual circumstances.
57
+
for your agent, you can use the default Jenkins agent image available in [Docker Hub](https://hub.docker.com). In the
58
+
‘Kubernetes Pod Template’ section you need to specify the following (the rest of the configuration is up to you):
59
+
Kubernetes Pod Template Name - can be any and will be shown as a prefix for unique generated agent’ names, which will
60
+
be run automatically during builds
61
+
Docker image - the docker image name that will be used as a reference to spin up a new Jenkins agent, as seen below
62
+
63
+

28
64
29
65
# Kubernetes Cloud Configuration
30
66
@@ -35,6 +71,12 @@ If _Kubernetes URL_ is not set, the connection options will be autoconfigured fr
35
71
36
72
When running the Jenkins master outside of Kubernetes you will need to set the credential to secret text. The value of the credential will be the token of the service account you created for Jenkins in the cluster the agents will run on.
37
73
74
+
If you check **WebSocket** then agents will connect over HTTP(S) rather than the Jenkins service TCP port.
75
+
This is unnecessary when the Jenkins master runs in the same Kubernetes cluster,
76
+
but can greatly simplify setup when agents are in an external cluster
77
+
and the Jenkins master is not directly accessible (for example, it is behind a reverse proxy).
78
+
See [JEP-222](https://jenkins.io/jep/222) for more.
79
+
38
80
### Restricting what jobs can use your configured cloud
39
81
40
82
Clouds can be configured to only allow certain jobs to use them.
@@ -78,7 +120,7 @@ Find more examples in the [examples dir](examples).
78
120
The default jnlp agent image used can be customized by adding it to the template
Multiple containers can be defined for the agent pod, with shared resources, like mounts. Ports in each container can be accessed as in any Kubernetes pod, by using `localhost`.
99
141
100
-
The `container` statement allows to execute commands directly into each container. This feature is considered **ALPHA** as there are still some problems with concurrent execution and pipeline resumption
142
+
The `container` statement allows to execute commands directly into each container.
101
143
102
144
```groovy
103
145
podTemplate(containers: [
@@ -156,13 +198,14 @@ Either way it provides access to the following fields:
156
198
***annotations** Annotations to apply to the pod.
157
199
***inheritFrom** List of one or more pod templates to inherit from *(more details below)*.
158
200
***slaveConnectTimeout** Timeout in seconds for an agent to be online *(more details below)*.
159
-
***podRetention** Controls the behavior of keeping slave pods. Can be 'never()', 'onFailure()', 'always()', or 'default()' - if empty will default to deleting the pod after `activeDeadlineSeconds` has passed.
201
+
***podRetention** Controls the behavior of keeping agent pods. Can be 'never()', 'onFailure()', 'always()', or 'default()' - if empty will default to deleting the pod after `activeDeadlineSeconds` has passed.
160
202
***activeDeadlineSeconds** If `podRetention` is set to 'never()' or 'onFailure()', pod is deleted after this deadline is passed.
161
203
***idleMinutes** Allows the Pod to remain active for reuse until the configured number of minutes has passed since the last step was executed on it.
162
204
***showRawYaml** Enable or disable the output of the raw Yaml file. Defaults to `true`
163
205
***runAsUser** The user ID to run all containers in the pod as.
164
206
***runAsGroup** The group ID to run all containers in the pod as.
165
207
***hostNetwork** Use the hosts network.
208
+
***workspaceVolume** The type of volume to use for the workspace. Can be `emptyDirWorkspaceVolume` (default), `dynamicPVC()`, `hostPathWorkspaceVolume()`, `nfsWorkspaceVolume()`, or `persistentVolumeClaimWorkspaceVolume()`.
166
209
167
210
The `containerTemplate` is a template of container that will be added to the pod. Again, its configurable via the user interface or via pipeline and allows you to set the following fields:
168
211
@@ -541,10 +584,38 @@ containerLog 'mongodb'
541
584
542
585
Also see the online help and [examples/containerLog.groovy](examples/containerLog.groovy).
543
586
587
+
# Running on OpenShift
588
+
589
+
## Random UID problem
590
+
591
+
OpenShift runs containers using a 'random' UID that is overriding what is specified in Docker images.
592
+
For this reason, you may end up with the following warning in your build
593
+
594
+
```
595
+
[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.
596
+
```
597
+
598
+
At the moment the jenkinsci agent image is not built for OpenShift and will issue this warning.
599
+
600
+
This issue can be circumvented in various ways:
601
+
* build a docker image for OpenShift in order to behave when running using an arbitrary uid.
602
+
* override HOME environment variable in the pod spec to use `/home/jenkins` and mount a volume to `/home/jenkins` to ensure the user running the container can write to it
603
+
604
+
See this [example](examples/openshift-home-yaml.groovy) configuration.
605
+
606
+
## Running with OpenShift 3
607
+
608
+
OpenShift 3 is based on an older version of Kubernetes, which is not anymore directly supported since Kubernetes plugin version 1.26.0.
609
+
610
+
To get agents working for Openshift 3, add this `Node Selector` to your Pod Templates:
611
+
```
612
+
beta.kubernetes.io/os=linux
613
+
```
614
+
544
615
# Windows support
545
616
546
617
You can run pods on Windows if your cluster has Windows nodes.
547
-
See the [example](examples/windows.groovy).
618
+
See the [example](src/main/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline/samples/windows.groovy).
548
619
549
620
# Constraints
550
621
@@ -556,7 +627,7 @@ Other containers must run a long running process, so the container does not exit
556
627
just runs something and exit then it should be overridden with something like `cat` with `ttyEnabled: true`.
557
628
558
629
**WARNING**
559
-
If you want to provide your own Docker image for the JNLP slave, you **must** name the container `jnlp` so it overrides the default one. Failing to do so will result in two slaves trying to concurrently connect to the master.
630
+
If you want to provide your own Docker image for the JNLP 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 master.
560
631
561
632
562
633
@@ -629,6 +700,33 @@ at `DEBUG` level.
629
700
630
701
kubectl get pods -o name --selector=jenkins=slave --all-namespaces | xargs -I {} kubectl delete {}
631
702
703
+
## Pipeline `sh` step hangs when multiple containers are used
704
+
To debug this you need to set `-Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true` system property
705
+
and then restart the pipeline. Most likely in the console log you will see the following:
Usually this happens when UID of the user in `jnlp` container differs from the one in other container(s).
715
+
All containers you use should have the same UID of the user, also this can be achieved by setting `securityContext`:
716
+
```yaml
717
+
apiVersion: v1
718
+
kind: Pod
719
+
spec:
720
+
securityContext:
721
+
runAsUser: 1000# default UID of jenkins user in default jnlp image
722
+
containers:
723
+
- name: maven
724
+
image: maven:3.3.9-jdk-8-alpine
725
+
command:
726
+
- cat
727
+
tty: true
728
+
```
729
+
632
730
# Building and Testing
633
731
634
732
Integration tests will use the currently configured context autodetected from kube config file or service account.
@@ -674,6 +772,21 @@ just run as
674
772
675
773
mvn clean install -Pmicrok8s
676
774
775
+
This assumes that from a pod, the host system is accessible as IP address `10.1.1.1`.
776
+
It might be some variant such as `10.1.37.1`,
777
+
in which case you would need to set `-DconnectorHost=… -Djenkins.host.address=…` instead.
778
+
To see the actual address, try:
779
+
780
+
```bash
781
+
ifdata -pa cni0
782
+
```
783
+
784
+
Or to verify the networking inside a pod:
785
+
786
+
```bash
787
+
kubectl run --rm --image=praqma/network-multitool --restart=Never --attach sh ip route | fgrep 'default via'
788
+
```
789
+
677
790
### Integration Tests in a Different Cluster
678
791
679
792
Try
@@ -719,14 +832,6 @@ Get the url to connect to with
719
832
720
833
minikube service jenkins --namespace kubernetes-plugin --url
721
834
722
-
## Running with a remote Kubernetes Cloud in AWS EKS
723
-
724
-
EKS enforces authentication to the cluster through [aws-iam-authenticator](https://docs.aws.amazon.com/eks/latest/userguide/install-aws-iam-authenticator.html). The token expires after 15 minutes
725
-
so the kubernetes client cache needs to be set to something below this by setting a [java argument](https://support.cloudbees.com/hc/en-us/articles/209715698-How-to-add-Java-arguments-to-Jenkins-), like so:
@@ -813,7 +918,7 @@ Note: the JVM will use the memory `requests` as the heap limit (-Xmx)
813
918
## Building
814
919
815
920
docker build -t csanchez/jenkins-kubernetes .
816
-
921
+
817
922
# Related Projects
818
923
819
924
* [Kubernetes Pipeline plugin](https://github.com/jenkinsci/kubernetes-pipeline-plugin): pipeline extension to provide native support for using Kubernetes pods, secrets and volumes to perform builds
In this dir you can find several pipeline examples.
3
+
In this directory you can find several Pipeline examples.
4
4
5
-
For more examples that are continuously tested, check the [`/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline`](/src/test/resources/org/csanchez/jenkins/plugins/kubernetes/pipeline)
6
-
resources directory.
5
+
For more examples that are automatically tested, check:
0 commit comments