forked from mschmidt712/kubernetes-ci-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
steps.yml
213 lines (144 loc) · 12.4 KB
/
steps.yml
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
parts:
- name: Part 1
intro: In this part we will setup a local cluster with minikube, deploy a public image from dockerhub, customize that image, and then finally deploy it inside our local cluster.
steps:
- cap: Start up the Kubernetes cluster with Minikube, giving it some extra resources.
com: minikube start --memory 8000 --cpus 2 --kubernetes-version v1.6.0
- cap: Enable the Minikube add-ons Heapster and Ingress.
com: minikube addons enable heapster; minikube addons enable ingress
- cap: Wait 20 seconds, and then view the Minikube Dashboard, a web UI for managing deployments.
com: sleep 20; minikube service kubernetes-dashboard --namespace kube-system
- cap: Deploy the public nginx image from DockerHub into a pod. Nginx is an open source web server that will automatically download from Docker Hub if it’s not available locally.
com: kubectl run nginx --image nginx --port 80
- cap: Create a service for deployment. This will expose the nginx pod so you can access it with a web browser.
com: kubectl expose deployment nginx --type NodePort --port 80
- cap: Launch a web browser to test the service. The nginx welcome page displays, which means the service is up and running.
com: minikube service nginx
- cap: Set up the cluster registry by applying a .yml manifest file.
com: kubectl apply -f manifests/registry.yml
- cap: Wait for the registry to finish deploying. Note that this may take several minutes.
com: kubectl rollout status deployments/registry
- cap: View the registry user interface in a web browser.
com: minikube service registry-ui
- cap: Let’s make a change to an HTML file in the cloned project. Running the command below will open /applications/hello-kenzan/index.html in the nano text editor. Change some text inside one of the <p> tags. For example, change “Hello from Kenzan!” to “Hello from Me!”. When you’re done, press Ctrl+X to close the file, type Y to confirm the filename, and press Enter to write the changes to the file.
com: nano applications/hello-kenzan/index.html
- cap: Now let’s build an image, giving it a special name that points to our local cluster registry.
com: docker build -t 127.0.0.1:30400/hello-kenzan:latest -f applications/hello-kenzan/Dockerfile applications/hello-kenzan
- cap: We’ve built the image, but before we can push it to the registry, we need to set up a temporary proxy. By default the Docker client can only push to HTTP (not HTTPS) via localhost. To work around this, we’ll set up a container that listens on 127.0.0.1:30400 and forwards to our cluster.
com: docker stop socat-registry; docker rm socat-registry; docker run -d -e "REGIP=`minikube ip`" --name socat-registry -p 30400:5000 chadmoon/socat:latest bash -c "socat TCP4-LISTEN:5000,fork,reuseaddr TCP4:`minikube ip`:30400"
- cap: With our proxy container up and running, we can now push our image to the local repository.
com: docker push 127.0.0.1:30400/hello-kenzan:latest
- cap: The proxy’s work is done, so you can go ahead and stop it.
com: docker stop socat-registry;
- cap: With the image in our cluster registry, the last thing to do is apply the manifest to create and deploy the hello-kenzan pod based on the image.
com: kubectl apply -f applications/hello-kenzan/k8s/deployment.yaml
- cap: Launch a web browser and view the service.
com: minikube service hello-kenzan
- name: Part 2
intro: In this part we will Setup Jenkins, and setup an automated pipeline to build, push and deploy our custom appliction.
steps:
- cap: Install Jenkins, which we’ll use to create our automated CI/CD pipeline. It will take the pod a minute or two to roll out.
com: kubectl apply -f manifests/jenkins.yml; kubectl rollout status deployment/jenkins
- cap: Open the Jenkins UI in a web browser.
com: minikube service jenkins
- cap: Display the Jenkins admin password with the following command, and right-click to copy it. IMPORTANT: BE CAREFUL NOT TO PRESS CTRL-C TO COPY THE PASSWORD AS THIS WILL STOP THE SCRIPT.
com: kubectl exec -it `kubectl get pods --selector=app=jenkins --output=jsonpath={.items..metadata.name}` cat /root/.jenkins/secrets/initialAdminPassword
- cap: Switch back to the Jenkins UI. Paste the Jenkins admin password in the box and click Continue. Click Install suggested plugins and wait for the process to complete.
com: echo ''
- cap: Create an admin user and credentials, and click Save and Finish. (Make sure to remember these credentials as you will need them for repeated logins.) Click Start using Jenkins.
com: echo ''
- cap: We now want to create a new pipeline for use with our Hello-Kenzan app. On the left, click New Item. Enter the item name as "Hello-Kenzan Pipeline", select Pipeline, and click OK.
com: echo ''
- cap: Under the Pipeline section at the bottom, change the Definition to be "Pipeline script from SCM".
com: echo ''
- cap: Change the SCM to Git.
com: echo ''
- cap: Change the Repository URL to be the URL of your forked Git repository, such as https://github.com/[GIT USERNAME]/kubernetes-ci-cd. Click Save. On the left, click Build Now to run the new pipeline.
com: echo ''
- cap: Now view the Hello-Kenzan application.
com: minikube service hello-kenzan
- cap: Push a change to your fork. Run job again. View the changes.
com: minikube service hello-kenzan
- name: Part 3
intro: This part will have us setup the various applications that will present the crossword puzzle. We will run a sample etcd cluster as a cache, a pages application containing the front-end, a crossword server using mongodb, and a monitoring and scaling server application.
steps:
- cap: Start the etcd operator and service on the cluster. You may notice errors showing up as it is waiting to start up the cluster. This is normal until it starts.
com: scripts/etcd.sh
- cap: Now that we have an etcd service, we need an etcd client. The following command will set up a directory within etcd for storing key-value pairs, and then run the etcd client.
com: kubectl create -f manifests/etcd-job.yml
- cap: Check the status of the job in step 2 to make sure it deployed.
com: kubectl describe jobs/etcd-job
- cap: The crossword application is a multi-tier application whose services depend on each other. We will create three services in Kubernetes ahead of time, so that the deployments are aware of them.
com: kubectl apply -f manifests/all-services.yml
- cap: Now we're going to walk through an initial build of the monitor-scale service.
com: docker build -t 127.0.0.1:30400/monitor-scale:`git rev-parse --short HEAD` -f applications/monitor-scale/Dockerfile applications/monitor-scale
- cap: Set up a proxy so we can push the monitor-scale Docker image we just built to our cluster's registry.
com: docker stop socat-registry; docker rm socat-registry; docker run -d -e "REGIP=`minikube ip`" --name socat-registry -p 30400:5000 chadmoon/socat:latest bash -c "socat TCP4-LISTEN:5000,fork,reuseaddr TCP4:`minikube ip`:30400"
- cap: Push the monitor-scale image to the registry.
com: docker push 127.0.0.1:30400/monitor-scale:`git rev-parse --short HEAD`
- cap: The proxy’s work is done, so go ahead and stop it.
com: docker stop socat-registry
- cap: Open the registry UI and verify that the monitor-scale image is in our local registry.
com: minikube service registry-ui
- cap: Create the monitor-scale deployment and service.
com: sed 's#127.0.0.1:30400/monitor-scale:latest#127.0.0.1:30400/monitor-scale:'`git rev-parse --short HEAD`'#' applications/monitor-scale/k8s/deployment.yaml | kubectl apply -f -
- cap: Wait for the monitor-scale deployment to finish.
com: kubectl rollout status deployment/monitor-scale
- cap: View pods to see the monitor-scale pod running.
com: kubectl get pods
- cap: View services to see the monitor-scale service.
com: kubectl get services
- cap: View ingress rules to see the monitor-scale ingress rule.
com: kubectl get ingress
- cap: View deployments to see the monitor-scale deployment.
com: kubectl get deployments
- cap: We will run a script to bootstrap the puzzle and mongo services, creating Docker images and storing them in the local registry. The puzzle.sh script runs through the same build, proxy, push, and deploy steps we just ran through manually for both services.
com: scripts/puzzle.sh
- cap: Check to see if the puzzle and mongo services have been deployed.
com: kubectl rollout status deployment/puzzle
- cap: Bootstrap the kr8sswordz frontend web application. This script follows the same build proxy, push, and deploy steps that the other services followed.
com: scripts/kr8sswordz-pages.sh
- cap: Check to see if the frontend has been deployed.
com: kubectl rollout status deployment/kr8sswordz
- cap: Check out all the pods that are running.
com: kubectl get pods
- cap: Start the web application in your default browser. You may have to refresh your browser so that the puzzle appears properly.
com: minikube service kr8sswordz
- name: Part 4
intro: In this part we will return to our Jenkins instance and setup a pipeline for the kr8sswordz application.
steps:
- cap: Enter the following command to open the Jenkins UI in a web browser. Log in to Jenkins using the username and password you previously set up.
com: minikube service jenkins
- cap: We’ll want to create a new pipeline for the puzzle service that we previously deployed. On the left in Jenkins, click New Item.
com: echo ''
- cap: Enter the item name as "Puzzle-Service", click Pipeline, and click OK.
com: echo ''
- cap: Under the Build Triggers section, select Poll SCM. For the Schedule, enter the the string H/5 * * * * which will poll the Git repo every 5 minutes for changes.
com: echo ''
- cap: In the Pipeline section, change the Definition to "Pipeline script from SCM". Set the SCM property to GIT. Set the Repository URL to your forked repo (created in Part 2), such as https://github.com/[GIT USERNAME]/kubernetes-ci-cd.git. Set the Script Path to applications/puzzle/Jenkinsfile
com: echo ''
- cap: When you are finished, click Save. On the left, click Build Now to run the new pipeline. You should see it successfully run through the build, push, and deploy steps in a few minutes.
com: echo ''
- cap: View the Kr8sswordz application.
com: minikube service kr8sswordz
- cap: Spin up several instances of the puzzle service by moving the slider to the right and clicking Scale. For reference, click on the Submit button, noting that the green hit does not register on the puzzle services.
com: echo ''
- cap: Edit applications/puzzle/common/models/crossword.js in the nano editor. You'll see a commented section on lines 42-43 that indicates to uncomment a specific line. Uncomment line 43 by deleting the forward slashes. Press Ctrl+X to close the file, type Y to confirm the filename, and press Enter to write the changes to the file.
com: nano applications/puzzle/common/models/crossword.js
- cap: Commit and push the change to your forked Git repo.
com: echo ''
- cap: In Jenkins, open up the Puzzle-Service pipeline and wait until it triggers a build. It should trigger every 5 minutes.
com: echo ''
- cap: After it triggers, observe how the puzzle services disappear in the Kr8sswordz Puzzle app, and how new ones take their place.
com: echo ''
- cap: Try clicking Submit to test that hits now register as light green.
com: echo ''
# - name: Part 5
# intro: Spinnaker
# steps:
# - cap: Initialize Helm
# com: helm init; sleep 10; kubectl --namespace kube-system rollout status deployment/tiller-deploy
# - cap: Install Chart
# com: helm install --name spinnaker applications/spinnaker-helm/spinnaker-chart
# - cap: Launch Deck
# com: minikube service deck