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 @@
# Ch05 lab
Run the app:
```
kubectl apply -f lab/todo-list
```
Check the pods - the proxy fails. Logs tell you why:
```
kubectl get pods
kubectl logs -l app=todo-proxy-lab
```
The proxy pod needs the cache directory to exist before it starts. So there are two volumes to provide:
- in the proxy a volume to mount to `/data/nginx/cache` (from the logs)
- in the web app a mount to `/data` (from the env settings in the pod spec).
## Sample solution
I'm using PVCs with no storage class defined, so they will use the default provisioner. These YAML files contain the PVC spec and updated deployment specs:
- [proxy.yaml](solution/proxy.yaml)
- [web.yaml](solution/web.yaml)
Deploy the updates and check volumes:
```
kubectl apply -f lab/solution/
kubectl get pvc
kubectl get pv
```
Find the URL for the proxy:
```
kubectl get svc todo-proxy-lab -o jsonpath='http://{.status.loadBalancer.ingress[0].*}:8082'
```
Browse, add some items, delete the pods:
```
kubectl delete pod -l app=todo-proxy-lab
kubectl delete pod -l app=todo-web-lab
```
Refresh your browser, and you should see your original data.

View File

@@ -0,0 +1,45 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: todo-proxy-lab-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 100Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-proxy-lab
labels:
app: todo-proxy-lab
spec:
selector:
matchLabels:
app: todo-proxy-lab
template:
metadata:
labels:
app: todo-proxy-lab
spec:
containers:
- image: nginx:1.17-alpine
name: nginx
ports:
- containerPort: 80
name: http
volumeMounts:
- name: config
mountPath: "/etc/nginx/"
readOnly: true
- name: cache-volume
mountPath: /data/nginx/cache
volumes:
- name: config
configMap:
name: todo-proxy-lab-configmap
- name: cache-volume
persistentVolumeClaim:
claimName: todo-proxy-lab-pvc

View File

@@ -0,0 +1,39 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: todo-web-lab-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 200Mi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-web-lab
spec:
selector:
matchLabels:
app: todo-web-lab
template:
metadata:
labels:
app: todo-web-lab
spec:
containers:
- name: web
image: kiamol/ch04-todo-list
env:
- name: Database__Provider
value: Sqlite
- name: ConnectionStrings__ToDoDb
value: "Filename=/data/todo-list-lab.db"
volumeMounts:
- name: data
mountPath: /data
volumes:
- name: data
persistentVolumeClaim:
claimName: todo-web-lab-pvc

View File

@@ -0,0 +1,77 @@
apiVersion: v1
kind: Service
metadata:
name: todo-proxy-lab
labels:
app: todo-proxy-lab
spec:
ports:
- port: 8082
targetPort: 80
selector:
app: todo-proxy-lab
type: LoadBalancer
---
apiVersion: v1
kind: ConfigMap
metadata:
name: todo-proxy-lab-configmap
data:
nginx.conf: |-
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
proxy_pass http://todo-web-lab;
proxy_set_header Host $host;
proxy_cache STATIC;
proxy_cache_valid 200 1d;
add_header X-Cache $upstream_cache_status;
add_header X-Host $hostname;
}
}
}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-proxy-lab
labels:
app: todo-proxy-lab
spec:
selector:
matchLabels:
app: todo-proxy-lab
template:
metadata:
labels:
app: todo-proxy-lab
spec:
containers:
- image: nginx:1.17-alpine
name: nginx
ports:
- containerPort: 80
name: http
volumeMounts:
- name: config
mountPath: "/etc/nginx/"
readOnly: true
volumes:
- name: config
configMap:
name: todo-proxy-lab-configmap

View File

@@ -0,0 +1,33 @@
apiVersion: v1
kind: Service
metadata:
name: todo-web-lab
spec:
ports:
- port: 80
targetPort: 80
selector:
app: todo-web-lab
type: ClusterIP
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-web-lab
spec:
selector:
matchLabels:
app: todo-web-lab
template:
metadata:
labels:
app: todo-web-lab
spec:
containers:
- name: web
image: kiamol/ch04-todo-list
env:
- name: Database__Provider
value: Sqlite
- name: ConnectionStrings__ToDoDb
value: "Filename=/data/todo-list-lab.db"