This project shows how to run Jenkins master and slave(s) in Docker solution.
- Docker
-
Run Jenkins machine through docker-compose
$docker-compose up
-
Open http://docker-host-ip-address:8080 from web browser.
If you run this docker-jenkins for the first time, please do this step to be able to create first admin account:
Read and copy the initial admin password from the logs and paste it in the Getting started page
You are able to have a Jenkins slave(s) / node(s) on demand through docker.
-
Enable Docker Engine API on the machine which has Docker installed. Example steps on Ubuntu 16.04:
sudo nano /lib/systemd/system/docker.service Replace with -> ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:4243 sudo systemctl daemon-reload sudo systemctl restart docker curl http://localhost:4243/version
-
Install Docker plugin in Jenkins (under Manage Jenkins -> Manage Plugins)
-
Configure the connection between jenkins master and Docker machine (under Manage Jenkins -> Configure System -> Cloud). Sample configuration:
Name: cloud1 Docker Host URI: tcp://xxx.xxx.xxx.xxx:xxx Enabled: true
-
Configure Docker agent template in the same page. Sample configuration:
Label: java Enabled: true Name: docker-openjdk DockerImage: openjdk Connect method: Attach Docker container Pull strategy: Pull all images every time
-
Based on above configuration, you can create a job with option "Restrict where this project can be run: java"
You are able to have a Jenkins slave(s) / node(s) on demand through kubernetes but you need to have kubernetes cluster ready before.
-
You need to create jenkins namespace and needed components like ServiceAccount and RoleBinding (run the following command on k8s master node), you need to run jenkins-cluster.yml
kubectl apply -f my-jenkins.yml
-
Copy token of created service account:
kubectl describe secret $(kubectl describe serviceaccount jenkins-user --namespace=jenkins-cluster | grep Token | awk '{print $2}') --namespace=jenkins-cluster
-
Get the url of kubernetes master (you need to run the command on k8s master node):
kubectl cluster-info
-
Install Kubernetes plugin
-
Create secret text by going to manage jenkins -> manage credentials -> press one item of the "store" e.g. "jenkins" -> "global credentials" -> "add credentials" -> choose "system" and paste the output from step 2 to "secret" field
-
Configure the connection between jenkins master and kubernetes cluster (under Manage Jenkins -> Configure System -> Cloud). Sample configuration:
Name: team-k8s Kubernetes cluster: https://xx.xx.xx.xx:6443 (you get this from step 2) Disable https certificate check: marked Kubernetes Namespace: jenkins-cluster Credentials: xxx (choose the credentials id from step 4) Direct Connection: marked and press "Test Connection"
-
Continue to configure the Pod template (it is on the same page) if the connection is successfull:
Name: k8s-java-openjdk Namespace: jenkins-cluster Label: k8s-java-pod Containers: Name: k8s-java-container Docker image: openjdk ImagePullSecrets: Name: xxx (optional) Node Selector: kubernetes.io/hostname=node1 -> you can get through kubectl get node --show-labels
-
On pipeline script you need to specify the node and the container name, example:
node('k8s-java-pod') { // do something if needed conainer('k8s-java-container') { // do something here } }
-
List of Docker images that can be used as Jenkins slave / node