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,53 @@
# Ch07 lab
Run the app:
```
kubectl apply -f lab/pi/
```
Check the app and see it's broken:
```
kubectl describe pod -l app=pi-web
```
> The startup command for the app container uses a script which doesn't exist.
## Sample Solution
My updated [Deployment](solution/web.yaml) for the web app uses multiple containers.
Init containers:
- init container `init-1` writes the startup script file in an EmptyDir volume
- `init-2` makes the startup script executable
- `init-3` writes a text file with a fake app version.
App container:
- mounts the EmptyDir volume and runs the script at startup; serves the app on port 80.
Sidecar:
- runs a simple NCat HTTP server, serving the version number text file on port 8080.
Run the update and browse to your Service on port 8070 for Pi and 8071 for the version:
```
kubectl apply -f lab/solution/
```
> This is not the most efficient way to do this! It's just an example which makes use of multi-container Pods.
## Teardown
Delete the lab resources by their labels:
```
kubectl get all -l kiamol=ch07-lab
kubectl delete all -l kiamol=ch07-lab
```

View File

@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: pi-web
labels:
kiamol: ch07-lab
spec:
ports:
- port: 8070
targetPort: 80
name: http
selector:
app: pi-web
type: LoadBalancer

View File

@@ -0,0 +1,23 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: pi-web
labels:
kiamol: ch07-lab
spec:
replicas: 2
selector:
matchLabels:
app: pi-web
template:
metadata:
labels:
app: pi-web
spec:
containers:
- image: kiamol/ch05-pi
command: ["/scripts/startup.sh"]
name: web
ports:
- containerPort: 80
name: http

View File

@@ -0,0 +1,14 @@
apiVersion: v1
kind: Service
metadata:
name: pi-web-version
labels:
kiamol: ch07-lab
spec:
ports:
- port: 8071
targetPort: 8080
name: http
selector:
app: pi-web
type: LoadBalancer

View File

@@ -0,0 +1,58 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: pi-web
labels:
kiamol: ch07-lab
spec:
replicas: 2
selector:
matchLabels:
app: pi-web
template:
metadata:
labels:
app: pi-web
spec:
initContainers:
- name: init-script-1
image: kiamol/ch03-sleep
command: ['sh', '-c', "echo '#!/bin/sh\ndotnet Pi.Web.dll -m web' > /init/startup.sh"]
volumeMounts:
- name: init
mountPath: "/init"
- name: init-script-2
image: kiamol/ch03-sleep
command: ['sh', '-c', "chmod +x /init/startup.sh"]
volumeMounts:
- name: init
mountPath: "/init"
- name: init-version
image: kiamol/ch03-sleep
command: ['sh', '-c', "echo 'ch07-lab' > /init/version.txt"]
volumeMounts:
- name: init
mountPath: "/init"
containers:
- name: web
image: kiamol/ch05-pi
command: ["/init/startup.sh"]
ports:
- containerPort: 80
name: http
volumeMounts:
- name: init
mountPath: "/init"
- name: server
image: kiamol/ch03-sleep
command: ['sh', '-c', 'while true; do echo -e "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 9\n\n$(cat /init/version.txt)" | nc -l -p 8080; done']
ports:
- containerPort: 8080
name: http
volumeMounts:
- name: init
mountPath: "/init"
volumes:
- name: init
emptyDir: {}