Skip to content

Commit bc13206

Browse files
committed
lesson 6
1 parent 5b910c1 commit bc13206

14 files changed

+494
-0
lines changed

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@
1111
- [урок про volumes](lesson-3/)
1212
- [урок про StatefullSet и CertManager](lesson-4/)
1313
- [урок про Advanced Scheduling](lesson-5/)
14+
- [урок про Advanced Scheduling Part 2](lesson-6/)

lesson-5/3/3-deploy-v2.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ spec:
1717
containers:
1818
- image: luksa/kubia:v2
1919
name: nodejs
20+
strategy:
21+
type: RollingUpdate
22+
rollingUpdate:
23+
maxSurge: 1
24+
maxUnavailable: 0

lesson-6/1/01-dep.yaml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
apiVersion: v1
3+
kind: Namespace
4+
metadata:
5+
name: demo-ns
6+
---
7+
apiVersion: apps/v1
8+
kind: Deployment
9+
metadata:
10+
name: probe
11+
namespace: demo-ns
12+
labels:
13+
app: probe
14+
spec:
15+
replicas: 1
16+
selector:
17+
matchLabels:
18+
app: probe
19+
template:
20+
metadata:
21+
name: probe
22+
labels:
23+
app: probe
24+
spec:
25+
containers:
26+
- name: probe
27+
image: nar3k/probe
28+
readinessProbe:
29+
httpGet:
30+
path: /healthz
31+
port: 8080
32+
periodSeconds: 10
33+
---
34+
apiVersion: v1
35+
kind: Service
36+
metadata:
37+
name: probe
38+
namespace: demo-ns
39+
spec:
40+
selector:
41+
app: probe
42+
ports:
43+
- protocol: TCP
44+
port: 80
45+
targetPort: 8080
46+
type: LoadBalancer

lesson-6/1/readme.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Probes
2+
3+
```
4+
cd lesson-6/1
5+
```
6+
7+
Создадим deployment с пробами:
8+
9+
```sh
10+
kubectl apply -f 01-dep.yaml
11+
```
12+
13+
Запустим в фоне поиск по приложению
14+
```
15+
watch kubectl describe svc -n demo-ns
16+
```
17+
18+
Посылаем curl по IP адресу балансера
19+
20+
```
21+
URL=$(kubectl get svc probe -n demo-ns -o json | jq -r .status.loadBalancer.ingress[0].ip)
22+
curl $URL/healthz
23+
```
24+
25+
26+
1) сначала он не будет приходить ( потому что нода не стала еще ready )
27+
2) через 30 секунд в Endpoints появятся узлы и запросы начнут приходить
28+
3) через минуту запросы перестанут приходить совсем и ноды продадут из endpoint
29+
30+
4) заглянем в логи что случилось
31+
32+
```
33+
kubectl describe po -n demo-ns
34+
```
35+
36+
Увидим сообщения о проблемах со readyness probe
37+
38+
39+
Удалим лабу
40+
41+
```
42+
kubectl delete -f 01-dep.yaml
43+
```

lesson-6/1/service/Dockerfile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM golang:latest
2+
RUN mkdir /app
3+
ADD . /app/
4+
WORKDIR /app
5+
RUN go build -o main .
6+
CMD ["/app/main"]

lesson-6/1/service/main.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
/*
4+
Copyright 2014 The Kubernetes Authors.
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// A simple server that is alive for 10 seconds, then reports unhealthy for
17+
// the rest of its (hopefully) short existence.
18+
19+
import (
20+
"fmt"
21+
"log"
22+
"net/http"
23+
"net/url"
24+
"time"
25+
)
26+
27+
func main() {
28+
started := time.Now()
29+
http.HandleFunc("/started", func(w http.ResponseWriter, r *http.Request) {
30+
w.WriteHeader(200)
31+
data := (time.Since(started)).String()
32+
w.Write([]byte(data))
33+
})
34+
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
35+
duration := time.Since(started)
36+
if duration.Seconds() < 30 || duration.Seconds() > 60 {
37+
w.WriteHeader(500)
38+
w.Write([]byte(fmt.Sprintf("error: %v", duration.Seconds())))
39+
} else {
40+
w.WriteHeader(200)
41+
w.Write([]byte(fmt.Sprintf("ok: %v", duration.Seconds())))
42+
}
43+
})
44+
http.HandleFunc("/redirect", func(w http.ResponseWriter, r *http.Request) {
45+
loc, err := url.QueryUnescape(r.URL.Query().Get("loc"))
46+
if err != nil {
47+
http.Error(w, fmt.Sprintf("invalid redirect: %q", r.URL.Query().Get("loc")), http.StatusBadRequest)
48+
return
49+
}
50+
http.Redirect(w, r, loc, http.StatusFound)
51+
})
52+
log.Fatal(http.ListenAndServe(":8080", nil))
53+
}

lesson-6/2/02-dep.yaml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
apiVersion: v1
3+
kind: Namespace
4+
metadata:
5+
name: demo-ns
6+
---
7+
apiVersion: apps/v1
8+
kind: Deployment
9+
metadata:
10+
name: nginx
11+
namespace: demo-ns
12+
labels:
13+
app: nginx
14+
spec:
15+
replicas: 1
16+
selector:
17+
matchLabels:
18+
app: nginx
19+
template:
20+
metadata:
21+
name: nginx
22+
labels:
23+
app: nginx
24+
spec:
25+
containers:
26+
- name: nginx
27+
image: nginx
28+
resources:
29+
requests:
30+
memory: "64Mi"
31+
cpu: "250m"
32+
limits:
33+
memory: "100Gi"
34+
cpu: "50000m"

lesson-6/2/Readme.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2+
```
3+
cd ../2/
4+
```
5+
6+
Создадим deployment c реквестами и лимитами:
7+
8+
```sh
9+
kubectl apply -f 02-dep.yaml
10+
```
11+
12+
посмотрим на какой ноде запустилось приложение
13+
14+
```
15+
NODENAME=$(kubectl get po -o wide -n demo-ns -o=custom-columns=NAME:.metadata.name,node:.spec.nodeName -o json | jq -r .items[0].spec.nodeName)
16+
kubectl describe node $NODENAME
17+
```
18+
19+
Увидим overommit
20+
21+
```
22+
Allocated resources:
23+
(Total limits may be over 100 percent, i.e., overcommitted.)
24+
Resource Requests Limits
25+
-------- -------- ------
26+
cpu 660m (16%) 50800m (1295%)
27+
memory 196Mi (7%) 103100Mi (3801%)
28+
ephemeral-storage 0 (0%) 0 (0%)
29+
Events: <none>
30+
```
31+
32+
Посмотрим соседнюю ноду
33+
34+
```
35+
$ kubectl get nodes
36+
NAME STATUS ROLES AGE VERSION
37+
cl14607bcn1714k4v3im-ahyn Ready <none> 13d v1.17.8
38+
39+
$ kubectl describe node cl14607bcn1714k4v3im-ahyn
40+
```
41+
В ней нет overcommit
42+
43+
```
44+
Allocated resources:
45+
(Total limits may be over 100 percent, i.e., overcommitted.)
46+
Resource Requests Limits
47+
-------- -------- ------
48+
cpu 610m (15%) 800m (20%)
49+
memory 132Mi (4%) 700Mi (25%)
50+
ephemeral-storage 0 (0%) 0 (0%)
51+
```
52+
53+
54+
Удалим лабу
55+
56+
```
57+
kubectl delete -f 02-dep.yaml
58+
```

lesson-6/3/03-dep-hpa.yaml

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
---
3+
apiVersion: apps/v1
4+
kind: Deployment
5+
metadata:
6+
name: nginx
7+
namespace: demo-ns
8+
labels:
9+
app: nginx
10+
spec:
11+
replicas: 1
12+
selector:
13+
matchLabels:
14+
app: nginx
15+
template:
16+
metadata:
17+
name: nginx
18+
labels:
19+
app: nginx
20+
spec:
21+
containers:
22+
- name: nginx
23+
image: k8s.gcr.io/hpa-example
24+
---
25+
apiVersion: v1
26+
kind: Service
27+
metadata:
28+
name: nginx
29+
namespace: demo-ns
30+
spec:
31+
selector:
32+
app: nginx
33+
ports:
34+
- protocol: TCP
35+
port: 80
36+
targetPort: 80
37+
type: LoadBalancer
38+
---
39+
apiVersion: autoscaling/v1
40+
kind: HorizontalPodAutoscaler
41+
metadata:
42+
name: nginx
43+
namespace: demo-ns
44+
spec:
45+
scaleTargetRef:
46+
apiVersion: apps/v1
47+
kind: Deployment
48+
name: nginx
49+
minReplicas: 1
50+
maxReplicas: 3
51+
targetCPUUtilizationPercentage: 50

lesson-6/3/03-ns-limitRange.yaml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
apiVersion: v1
3+
kind: Namespace
4+
metadata:
5+
name: demo-ns
6+
---
7+
apiVersion: v1
8+
kind: LimitRange
9+
metadata:
10+
name: cpu-limit-range
11+
namespace: demo-ns
12+
spec:
13+
limits:
14+
- default:
15+
cpu: 0.5
16+
defaultRequest:
17+
cpu: 0.25
18+
type: Container

lesson-6/3/Readme.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
## HorizontalPodAutoscaler
2+
```
3+
cd ../3/
4+
```
5+
6+
Создадим ns и limitRange :
7+
8+
```sh
9+
kubectl apply -f 03-ns-limitRange.yaml
10+
```
11+
12+
Создадим deployment и HPA
13+
14+
```sh
15+
kubectl apply -f 03-dep-hpa.yaml
16+
```
17+
18+
Запустим в окне
19+
20+
```sh
21+
watch kubectl get pod,svc,hpa -n demo-ns
22+
```
23+
24+
Посылаем curl по IP адресу балансера
25+
26+
```sh
27+
URL=$(kubectl get svc nginx -n demo-ns -o json | jq -r .status.loadBalancer.ingress[0].ip)
28+
while true; do wget -q -O- http://$URL; done
29+
```
30+
Отпустим нагрузку
31+
32+
33+
Ждем когда деплоймент смаштабируется вверх и порадуемся
34+
35+
Посмотрим лог
36+
37+
```sh
38+
kubectl describe horizontalpodautoscaler.autoscaling/nginx -n demo-ns
39+
```
40+
41+
```sh
42+
Conditions:
43+
Type Status Reason Message
44+
---- ------ ------ -------
45+
AbleToScale True ScaleDownStabilized recent recommendations were higher than current one, applying the highest recent recommendation
46+
ScalingActive True ValidMetricFound the HPA was able to successfully calculate a replica count from cpu resource utilization (percentage of request)
47+
ScalingLimited True TooManyReplicas the desired replica count is more than the maximum replica count
48+
```
49+
50+
51+
## Переходим к следущей лабе

0 commit comments

Comments
 (0)