This commit is contained in:
2024-02-20 17:15:27 +08:00
committed by huty
parent 6706e1a633
commit 34158042ad
1529 changed files with 177765 additions and 0 deletions

View File

@@ -0,0 +1,73 @@
# ch19 lab
## Setup
Deploy metrics-server if you need it (check with `kubectl top nodes` - no stats means you need it):
```
kubectl apply -f metrics-server/
```
Run the app:
```
kubectl apply -f lab/pi/
```
Confirm the metrics are coming through:
```
kubectl top pods -l app=pi-web-lab
```
> Browse to the app and check the CPU spikes - e.g. http://localhost:8032/?dp=100000
## Sample Solution
You need to label your node to indicate it's in the EU region - you can use any key and value for this, but you'll need to use the same in your affinity rules:
```
kubectl label node --all kiamol.net/region=eu
```
### Pod with affinity rules
The updated deployment in [solution/pi.yaml](./solution/pi.yaml) adds these settings:
- **node affinity** - require to run on nodes with region=eu
- **pod anti-affinity** - prefer to run on nodes without any other Pi pods
- **resources** - add memory request for the HPA to use
- **replicas** - start with 2 as that's the desired minimum
```
kubectl apply -f lab/solution/pi.yaml
```
> You'll have two Pods running; browse to the app in a few tabs and both will spike CPU
### HPA for scaling on CPU
The HPA spec in [solution/hpa-cpu.yaml](./solution/hpa-cpu.yaml) scales from 2 to 5 Pods based on target CPU utilization of 50%.
```
kubectl apply -f lab/solution/hpa-cpu.yaml
```
> Make lots of browser requests in different tabs (or adapt the `ch19/loadpi` script in) and you'll see the Pods scale up to a maximum of five replicas:
![Horizontal pod autoscaling in action](./solution/hpa.png)
## Teardown
Delete all the resources:
```
kubectl delete all,hpa -l kiamol=ch19-lab
```
And metrics-server if you deployed it:
```
kubectl delete -f metrics-server/
```

View File

@@ -0,0 +1,40 @@
apiVersion: v1
kind: Service
metadata:
name: pi-web
labels:
kiamol: ch19-lab
spec:
ports:
- port: 8032
targetPort: http
selector:
app: pi-web-lab
type: LoadBalancer
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pi-web-lab
labels:
kiamol: ch19-lab
spec:
selector:
matchLabels:
app: pi-web-lab
replicas: 1
template:
metadata:
labels:
app: pi-web-lab
spec:
containers:
- image: kiamol/ch05-pi
command: ["dotnet", "Pi.Web.dll", "-m", "web"]
name: web
ports:
- containerPort: 80
name: http
resources:
limits:
cpu: 200m

View File

@@ -0,0 +1,14 @@
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: pi-cpu
labels:
kiamol: ch19-lab
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: pi-web-lab
minReplicas: 2
maxReplicas: 5
targetCPUUtilizationPercentage: 50

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

View File

@@ -0,0 +1,48 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: pi-web-lab
labels:
kiamol: ch19-lab
spec:
selector:
matchLabels:
app: pi-web-lab
replicas: 2
template:
metadata:
labels:
app: pi-web-lab
spec:
containers:
- image: kiamol/ch05-pi
command: ["dotnet", "Pi.Web.dll", "-m", "web"]
name: web
ports:
- containerPort: 80
name: http
resources:
limits:
cpu: 200m
requests:
cpu: 100m
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: kiamol.net/region
operator: In
values:
- eu
podAntiAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
podAffinityTerm:
labelSelector:
matchExpressions:
- key: app
operator: In
values:
- pi-web-lab
topologyKey: "kubernetes.io/hostname"