github.com/xgoffin/jenkins-library@v1.154.0/documentation/docs/infrastructure/overview.md (about)

     1  # Infrastructure
     2  
     3  Besides SAP specific Jenkins library steps and out-of-the-box pipelines, project "Piper" offers also documentation and tooling to start the corresponding Jenkins server with all the configuration required to run project "Piper" pipelines.
     4  
     5  The core of the Jenkins infrastructure tooling is a set of [Docker images][docker-images].
     6  There is a main Docker image containing a preconfigured Jenkins and several tooling images used in the specific project "Piper" steps.
     7  
     8  !!! info "Docker Hub rate limiting"
     9      Please be aware that Docker Hub as rate limiting active which might cause project "Piper" pipelines to fail.
    10      Refer to the [page dedicated to docker rate limiting for details and solutions][resources-docker-rate-limit].
    11  
    12  The document and the linked resources explain the various ways of starting such a Jenkins server based on these Docker images.
    13  
    14  ## Cx Server (Recommended)
    15  
    16  Cx Server is a life-cycle management tool to bootstrap a pre-configured Jenkins instance within minutes on your own (virtual) server.
    17  It uses the Docker images mentioned above.
    18  As it would be cumbersome to start the Docker image manually with all required parameters and sidecar images, this command line tool automates the bootstraping.
    19  
    20  ### Setting up a Jenkins master
    21  
    22  For the following steps you will need a server or another machine which has Docker installed and configured.
    23  
    24  To get started, initialize the Cx Server by using this `docker run` command:
    25  
    26  ```sh
    27  docker run -it --rm -u $(id -u):$(id -g) -v "${PWD}":/cx-server/mount/ ppiper/cx-server-companion:latest init-cx-server
    28  ```
    29  
    30  This creates a few files in your current working directory.
    31  The shell script `cx-server` and the configuration file `server.cfg` are of special interest.
    32  
    33  Now, you can start the Jenkins server by using the following command:
    34  
    35  ```sh
    36  chmod +x ./cx-server
    37  ./cx-server start
    38  ```
    39  
    40  For more information on the Cx Server and how to customize your Jenkins, have a look at the [Operations Guide for Cx Server][devops-docker-images-cxs-guide].
    41  
    42  ### Setting up Jenkins agents
    43  
    44  With more and more qualities checked automatically in the pipeline, more and more resources are required to handle the workload.
    45  This section shows how to scale the pipeline by adding [Jenkins build agents][build-agents].
    46  
    47  However, before setting up agents please consider also other ways to scale the build infrastructure.
    48  It might be an option to have only one Jenkins master with lots of resources (cpu cores, memory) per project or team.
    49  This has the advantage of bringing more configuration flexibility and isolation for the individual teams but has the disadvantage that parts of the configuration have to be maintained twice.
    50  Furthermore, having agents and thus network communication between the build servers increases the risk of failures.
    51  
    52  To add an agent to the Jenkins master, please make sure to fulfil the following requirements similar to the ones for the Jenkins master:
    53  
    54  - Access to a new server which runs on Linux
    55  - Docker installed on this server
    56  
    57  The connection between the master and the agents will be established via ssh.
    58  As the Jenkins master runs in a Docker container, the ssh setup steps, such as creating and storing a private/public key pair or maintaining the konwn hosts file has to be done inside this container.
    59  
    60  To execute these steps inside the container, execute the following command on the server where the Jenkins master is running:
    61  
    62  ```bash
    63  docker exec -it cx-jenkins-master bash
    64  ```
    65  
    66  Inside the container make sure to be able to access the server where the Jenkins agent should be started by running the following command. As user you should use a user which is able to execute `docker` commands, i.e. starting a docker container.
    67  
    68  ```bash
    69  ssh <docker-user>@<host/ip>
    70  ```
    71  
    72  To be able to access the agent via ssh with the command above you might need to generate a new ssh key with `ssh-keygen`, store it in the `.ssh` folder and register the public key on the agent server.
    73  You might also need to add server’s fingerprint to the list of known hosts.
    74  For more information around establishing a ssh connection please consult the [ssh documentation][ssh-documentation].
    75  
    76  To setup a new Jenkins agent, open "Manage Jenkins" > "Manage Nodes" > "New Nodes" and create a new "Permanent Agent"
    77  
    78  Please define `/var/jenkins_home` as "Remote root directory".
    79  The launch method has to be "Launch agent via execution of command on the master" and the command should be:
    80  `./var/jenkins_home/launch-jenkins-agent.sh <user> <host> [image]`.
    81  User and host should equal the values you used above to test the ssh connection.
    82  
    83  The following picture shows an example configuration.
    84  
    85  ![Agent Setup](../images/agent.png "Agent Setup")
    86  
    87  ## Kubernetes (Experimental)
    88  
    89  Hosting Jenkins master and agents means that we bind the required resources to the purpose of executing builds.
    90  There are good chances that, these resources stay idle for the most part of the day, i.e. if you have high peak loads.
    91  Autoscaling of the infrastructure solves such a problem.
    92  Instead of reserving the resources proactively, the pipeline creates the Jenkins agents dynamically on a Kubernetes cluster during the execution.
    93  Once the agent completes the dedicated task, it is deleted and the resources are freed.
    94  Project "Piper" supports running the pipeline as well as individual steps in a Kubernetes Cluster.
    95  Please note that this feature is currently only experimental.
    96  
    97  To setup the Jenkins master in Kubernetes you can use helm.
    98  The documentation to install Jenkins using helm can be found [here][jenkins-helm].
    99  
   100  To use the Jenkins image provided by project Piper, pass `ppiper/jenkins-master` as a value for the `Master.Image` command line argument while deploying Jenkins to Kubernetes.
   101  
   102  The successfully completed deployment consists of a Jenkins pod with port 80 and 50000 exposed for HTTP and internal JNLP traffic respectively.
   103  The deployment also creates two services each to listen to incoming HTTP traffic on port 80 and the internal JNLP traffic on port 50000.
   104  Please note that in this example setup, the SSL/TLS termination happens at the load balancer, hence all the traffic between a load balancer and the Jenkins pod is unencrypted.
   105  
   106  Project "Piper" needs an environment variable set in the Jenkins to run the workload in Kubernetes.
   107  In order to set the environment variable, navigate to "Manage Jenkins" > "Configure System" > "Global Properties".
   108  Add an environment variable ON_K8S and set the value to true:
   109  
   110  ![Environment Variable ON_K8S](../images/env.png "Environment Variable ON_K8S")
   111  
   112  Afterwards, you should be able to run project "Piper" pipelines in Kubernetes.
   113  
   114  ## Custom Jenkins
   115  
   116  ### On your own: Custom Jenkins Setup
   117  
   118  If you use your own Jenkins installation, you need to care for the configuration that is specific to project "Piper".
   119  This option should only be considered if you know why you need it, otherwise using the Cx Server life-cycle management makes your life much easier.
   120  If you choose to go this path, follow the [Custom Jenkins Setup guide][resources-custom-jenkins].
   121  
   122  [devops-docker-images-cxs-guide]:    https://github.com/SAP/devops-docker-cx-server/blob/master/docs/operations/cx-server-operations-guide.md
   123  [docker-images]:                     https://hub.docker.com/u/ppiper
   124  [resources-docker-rate-limit]:       docker-rate-limit.md
   125  [resources-custom-jenkins]:          customjenkins.md
   126  [build-agents]:                      https://wiki.jenkins.io/display/jenkins/distributed+builds
   127  [ssh-documentation]:                 https://www.openssh.com/manual.html
   128  [jenkins-helm]:                      https://github.com/helm/charts/tree/master/stable/jenkins