Testing Docker builds outside the cluster

No matter whether you choose to use static VMs or you decided to create them dynamically in AWS or GCE, the steps to test them are the same. From Jenkins' perspective, all that matters is that there are agent nodes with the labels docker.

We'll modify our Pipeline to use the node labeled docker.

 1  open "http://$JENKINS_ADDR/job/my-k8s-job/configure"

Please click the Pipeline tab and replace the script with the one that follows.

 1  podTemplate( 
 2      label: "kubernetes",
 3      namespace: "go-demo-3-build", 
 4      serviceAccount: "build", 
 5      yaml: """ 
 6  apiVersion: v1 
 7  kind: Pod 
 8  spec: 
 9    containers: 
10    - name: kubectl 
11      image: vfarcic/kubectl 
12      command: ["sleep"] 
13      args: ["100000"] 
14    - name: oc 
15      image: vfarcic/openshift-client 
16      command: ["sleep"] 
17      args: ["100000"] 
18    - name: golang 
19      image: golang:1.12 
20      command: ["sleep"] 
21      args: ["100000"] 
22    - name: helm 
23      image: vfarcic/helm:2.8.2 
24      command: ["sleep"] 
25      args: ["100000"] 
26  """ 
27  ) { 
28      node("docker") { 
29          stage("docker") { 
30              sh "sudo docker version" 
31          } 
32      } 
33      node("kubernetes") { 
34          container("kubectl") { 
35              stage("kubectl") { 
36                  sh "kubectl version" 
37              } 
38          } 
39          container("oc") { 
40              stage("oc") { 
41                  sh "oc version" 
42              } 
43          } 
44          container("golang") { 
45              stage("golang") { 
46                  sh "go version" 
47              } 
48          } 
49          container("helm") {

50              stage("helm") { 
52                  sh "helm version --tiller-namespace go-demo-3-build" 
53              } 
54          } 
55      } 
56  }
If you prefer to copy and paste, the job is available in the my-k8s-job-docker.groovy (https://gist.github.com/vfarcic/fbf9fc6611fe400c7950f43cfc89f406) Gist.

The only notable difference, when compared with the previous version of the job, is that we added the second node segment. Most of the steps will be executed inside the kubernetes node that hosts a few containers. The new node is called docker and will be in charge of the steps that require Docker server. Depending on the path you took, that node might be a static VM, a dynamically created (and destroyed) node in AWS or GCE, or something entirely different. From job's perspective, it does not matter how is that node created, but that it exists or that it will be created on demand. The job will request nodes docker and kubernetes, and it is up to Jenkins' internal configuration to figure out how to get them.

Please click the Save button to persist the updated job.

Next, we'll open the job in BlueOcean and run it as a way to confirm that everything works as expected.

 1  open
"http://$JENKINS_ADDR/blue/organizations/jenkins/my-k8s-job/activity"

Press the Run button, followed with a click on the row of the new build. Wait until all the stages are executed.

Figure 6-11: Jenkins job for testing tools

This time, everything worked, and the build is green. We managed to run the steps in a different Namespace without sacrificing security while keeping docker commands outside the Kubernetes cluster in a separate node.

Figure 6-12: Jenkins external VMs for building container images

Now that we know what we want to accomplish, we'll switch our attention to full automation of Jenkins installation and setup.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.174.55