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,56 @@
# Ch08 lab
Run the app:
```
kubectl apply -f lab/nginx/
```
Get the URL and browse:
```
kubectl get svc nginx -o jsonpath='http://{.status.loadBalancer.ingress[0].*}:8088'
```
> It works, but its just a single Pod writing logs to an EmptyDir.
## Sample Solution
My [StatefulSet](solution/nginx-statefulset.yaml) runs three Pods, with volume claim templates for storage.
```
kubectl apply -f lab/solution/nginx-statefulset.yaml
```
> Make lots of calls to the web app
I used this in Powershell:
```
for($i = 0; $i -lt 100; $i++) { curl http://localhost:8088 | Out-Null }
```
Then the [Job](solution/disk-calc-job.yaml) is configured to mount all of the PVCs used in the StatefulSet Pods.
```
kubectl apply -f lab/solution/disk-calc-job.yaml
```
When I check the logs I see this:
```
PS>kubectl logs -l job-name=disk-calc
32.0K /nginx0/access.log
24.0K /nginx1/access.log
40.0K /nginx2/access.log
```
## Teardown
Delete the lab resources by their labels:
```
kubectl delete all -l kiamol=ch08-lab
kubectl delete pvc -l kiamol=ch08-lab
```

View File

@@ -0,0 +1,23 @@
apiVersion: v1
kind: Job
metadata:
name: nginx
labels:
kiamol: ch08-lab
app: nginx
spec:
containers:
- image: kiamol/ch03-sleep
name: calc
command: ['sh', '-c', 'du -h /nginx1/access.log /nginx2/access.log /nginx3/access.log']
ports:
- containerPort: 80
volumeMounts:
- name: nginx1
mountPath: /nginx1/
- name: nginx2
mountPath: /nginx2/
- name: nginx3
mountPath: /nginx3/
# volumes:
# ?

View File

@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
kiamol: ch08-lab
spec:
type: LoadBalancer
ports:
- port: 8088
targetPort: 80
selector:
app: nginx

View File

@@ -0,0 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
kiamol: ch08-lab
app: nginx
spec:
containers:
- image: nginx:1.17-alpine
name: nginx
ports:
- containerPort: 80
volumeMounts:
- name: logs
mountPath: /var/log/nginx/
volumes:
- name: logs
emptyDir: {}

View File

@@ -0,0 +1,33 @@
apiVersion: batch/v1
kind: Job
metadata:
name: disk-calc
labels:
kiamol: ch08-lab
spec:
template:
spec:
restartPolicy: Never
containers:
- image: kiamol/ch03-sleep
name: calc
command: ['sh', '-c', 'du -h /nginx0/access.log /nginx1/access.log /nginx2/access.log']
ports:
- containerPort: 80
volumeMounts:
- name: nginx0
mountPath: /nginx0/
- name: nginx1
mountPath: /nginx1/
- name: nginx2
mountPath: /nginx2/
volumes:
- name: nginx0
persistentVolumeClaim:
claimName: logs-nginx-stateful-0
- name: nginx1
persistentVolumeClaim:
claimName: logs-nginx-stateful-1
- name: nginx2
persistentVolumeClaim:
claimName: logs-nginx-stateful-2

View File

@@ -0,0 +1,50 @@
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
kiamol: ch08-lab
spec:
type: LoadBalancer
ports:
- port: 8088
targetPort: 80
selector:
app: nginx-stateful
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-stateful
labels:
kiamol: ch08-lab
spec:
selector:
matchLabels:
app: nginx-stateful
serviceName: nginx-stateful
replicas: 3
template:
metadata:
labels:
app: nginx-stateful
spec:
containers:
- image: nginx:1.17-alpine
name: nginx
ports:
- containerPort: 80
volumeMounts:
- name: logs
mountPath: /var/log/nginx/
volumeClaimTemplates:
- metadata:
name: logs
labels:
kiamol: ch08-lab
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Mi