forked from quiccklabs/Labs_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathManage Kubernetes in Google Cloud: Challenge Lab
220 lines (159 loc) · 4.55 KB
/
Manage Kubernetes in Google Cloud: Challenge Lab
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
214
215
216
217
export REPO_NAME=
export CLUSTER_NAME=
export NAMESPACE=
export INTERVAL=
export SERVICE_NAME=
gcloud config set compute/zone us-central1-a
gcloud container clusters create $CLUSTER_NAME \
--release-channel regular \
--cluster-version 1.25.6-gke.1000 \
--num-nodes 3 \
--min-nodes 2 \
--max-nodes 6 \
--enable-autoscaling --no-enable-ip-alias
gcloud container clusters update $CLUSTER_NAME --enable-managed-prometheus --zone us-central1-a
kubectl create ns $NAMESPACE
gsutil cp gs://spls/gsp510/prometheus-app.yaml .
cat > prometheus-app.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-test
labels:
app: prometheus-test
spec:
selector:
matchLabels:
app: prometheus-test
replicas: 3
template:
metadata:
labels:
app: prometheus-test
spec:
nodeSelector:
kubernetes.io/os: linux
kubernetes.io/arch: amd64
containers:
- image: nilebox/prometheus-example-app:latest
name: prometheus-test
ports:
- name: metrics
containerPort: 1234
command:
- "/main"
- "--process-metrics"
- "--go-metrics"
EOF
kubectl -n $NAMESPACE apply -f prometheus-app.yaml
gsutil cp gs://spls/gsp510/pod-monitoring.yaml .
cat > pod-monitoring.yaml <<EOF
apiVersion: monitoring.googleapis.com/v1alpha1
kind: PodMonitoring
metadata:
name: prometheus-test
labels:
app.kubernetes.io/name: prometheus-test
spec:
selector:
matchLabels:
app: prometheus-test
endpoints:
- port: metrics
interval: $INTERVAL
EOF
kubectl -n $NAMESPACE apply -f pod-monitoring.yaml
gsutil cp -r gs://spls/gsp510/hello-app/ .
export PROJECT_ID=$(gcloud config get-value project)
export REGION="us-central1"
cd ~/hello-app
gcloud container clusters get-credentials $CLUSTER_NAME --zone us-central1-a
kubectl -n $NAMESPACE apply -f manifests/helloweb-deployment.yaml
cd manifests/
cat > helloweb-deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: helloweb
labels:
app: hello
spec:
selector:
matchLabels:
app: hello
tier: web
template:
metadata:
labels:
app: hello
tier: web
spec:
containers:
- name: hello-app
image: us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
ports:
- containerPort: 8080
resources:
requests:
cpu: 200m
# [END container_helloapp_deployment]
# [END gke_manifests_helloweb_deployment_deployment_helloweb]
---
EOF
cd ..
kubectl delete deployments helloweb -n $NAMESPACE
kubectl -n $NAMESPACE apply -f manifests/helloweb-deployment.yaml
cat > main.go <<EOF
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func main() {
// register hello function to handle all requests
mux := http.NewServeMux()
mux.HandleFunc("/", hello)
// use PORT environment variable, or default to 8080
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// start the web server on port and accept requests
log.Printf("Server listening on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, mux))
}
// hello responds to the request with a plain-text "Hello, world" message.
func hello(w http.ResponseWriter, r *http.Request) {
log.Printf("Serving request: %s", r.URL.Path)
host, _ := os.Hostname()
fmt.Fprintf(w, "Hello, world!\n")
fmt.Fprintf(w, "Version: 2.0.0\n")
fmt.Fprintf(w, "Hostname: %s\n", host)
}
// [END container_hello_app]
// [END gke_hello_app]
EOF
export PROJECT_ID=$(gcloud config get-value project)
export REGION="us-central1"
cd ~/hello-app/
gcloud auth configure-docker $REGION-docker.pkg.dev --quiet
docker build -t $REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/hello-app:v2 .
docker push $REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/hello-app:v2
kubectl set image deployment/helloweb -n $NAMESPACE hello-app=$REGION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/hello-app:v2
kubectl expose deployment helloweb -n $NAMESPACE --name=$SERVICE_NAME --type=LoadBalancer --port 8080 --target-port 8080
cd ..
kubectl -n $NAMESPACE apply -f pod-monitoring.yaml
Task 4. Create a logs-based metric and alerting policy
Create a logs-based metric
1. Return to the Cloud Console, and from the Navigation menu open Logging then click Logs Explorer.
2. Enable Show query and in the Query builder box, add the following query:
resource.type="k8s_pod"
severity=WARNING
3. Click Run Query.
4. Click Create Metric.
5. Use the following details to configure your metric:
Metric type: Counter
Log Metric Name : pod-image-errors
6. Click Create Metric.