Kubernetes Application: CI/CD pipeline

Introduction to Jenkins

1
2
3
$ cd
$ bash /local/repository/launch_network.sh
$ kubectl get nodes
1
$ bash /local/repository/launch_jenkins.sh
1
$ kubectl get nodes -o wide
1
$ kubectl exec $(kubectl get pods -n jenkins | grep jenkins | awk '{print $1}') -n jenkins -- cat /var/jenkins_home/secrets/initialAdminPassword
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
## Configure Jenkins


- Click on `Install selected plugins`

:::{image} ../fig/csc603/07-kubernetes/05.png
:alt: Install selected plugins for Kubernetes
:class: bg-primary mb-1
:height: 500px
:align: center
:::

- You can either try to create the first admin user or Skip and continue
as admin. 

:::{image} ../fig/csc603/07-kubernetes/06.png
:alt: Setup admin user
:class: bg-primary mb-1
:height: 500px
:align: center
:::

- Click `Save and Finish`,

:::{image} ../fig/csc603/07-kubernetes/07.png
:alt: Accept instance configuration 
:class: bg-primary mb-1
:height: 500px
:align: center
:::

- Click `Start using Jenkins`

:::{image} ../fig/csc603/07-kubernetes/08.png
:alt: Start using Jenkins
:class: bg-primary mb-1
:height: 500px
:align: center
:::

- This is the Jenkins Dashboard

:::{image} ../fig/csc603/07-kubernetes/09.png
:alt: Jenkins Dashboard
:class: bg-primary mb-1
:height: 500px
:align: center
:::



- Explore the Jenkins Dashboard and figure out how to install two additional plugins:
  - Kubernetes
  - SSH Agent

:::{image} ../fig/csc603/07-kubernetes/jenkins-extra-agents.png
:alt: Installing additional Kubernetes and SSH Agent plugins
:class: bg-primary mb-1
:height: 200px
:align: center
:::




- On the CloudLab head node, run `ssh-keygen` (do not enter any password when asked). 
- Run `cat .ssh/id_rsa.pub >> .ssh/authorized_keys`
- Run `cat ~/.ssh/id_rsa` and copy the displayed text, including the starting 
and ending dashes without any extra spaces.  
- On Jenkins Dashboard, go to `Manage Jenkins`/`Manage Credentials`.
  - Click on `Jenkins` under `Stores scoped to Jenkins`, then `Global credentials (unrestricted)`.
  - Click on `Add Credentials`. 
- Fill in the boxes as follows:
  - `Kind`: SSH Username with private name
  - `Scope`: Global (Jenkins, nodes, items, all child items, etc)
  - `ID`: cloudlab
  - `Username`: Enter your CloudLab login username here. 
  - `Private Key`: Check `Enter directly`, click `Add`, then paster the previously
  copied private key to this box. 
  - Click `Create`. 



- On Jenkins Dashboard, go to `Manage Jenkins`/`Manage Nodes and Clouds`.
  - Click on the gear icon for `Built-In Node` 
- Fill in the boxes as follows:
  - `Number of executors`: 1
  - `Labels`: deploy
  - `Usage`: Only build jobs with label expressions matching this node



- On Jenkins Dashboard, go to `Manage Jenkins`/`Manage Nodes and Clouds`/`Configure Clouds`.
- Select `Kubernetes` from `Add a new cloud` dropbox. 
- Click on `Kubernetes Cloud Details`.
- Fill in the boxes as follows:
  - `Kubernetes Name`: kubernetes
  - `Kubernetes URL`: Information of the `Kubernetes control plane` gotten from 
  running `kubectl cluster-info` on the CloudLab head node.
  - Check `Direction Connection` box.
  - Click `Test Connection` to confirm connection. 




- Continue on the `Configure Clouds` from the previous slide.
- Click `Pod Templates` then `Add Pod Template` then `Pod Template details`
- Fill in the boxes as follows:
  - `Name`: agent-template
  - `Namespace`: jenkins
  - `Usage`: Only build jobs with label expressions matching this node
  - **Do not add container yet**
  - Click on `Add Volume`:
    - Select `Host Path Volume`
    - Enter `/var/run/docker.sock` for both `Host path` and `Mount path`. 
    - *This is to enable the building and pushing of Docker images*. 


In the scope of `Pod Template`
- Click `Add Container`
- Fill in the boxes as follows:
  - `Container Template Name`: golang
  - `Docker image`: golang
- Click `Add Container`
- Fill in the boxes as follows:
  - `Container Template Name`: gcc
  - `Docker image`: gcc:latest
- Click `Add Container`
  - `Container Template Name`: docker
  - `Docker image`: docker
- Click `Add Environment Variable` for the `docker` container template
  - Prior to this, go to `hub.docker.com` and login to your Docker Hub account. 
    - Go to Account Settings
    - Go to `Security`. 
    - Click on `New Access Token`. 
    - Enter a short description for this token, allow `Access permission` to 
    be `Read, Write, Delete`, and then click `Generate`. 
    - Store this key some where safe. 
   - First environment variable:
     - `Key`: DOCKER_TOKEN
     - `Value`: the access token copied from before. 
   - Second environment variable:
     - `Key`: DOCKER_REGISTRY
     - `Value`: YOUR_DOCKERHUB_USERNAME/go_server
   - Third environment variable:
     - `Key`: DOCKER_USER
     - `Value`: YOUR_DOCKERHUB_USERNAME
>
- Click `Apply` and then `Save`. 

Launch pipelines

```