From d5bae5f9da12b2c451a3f0468cacdd909526371d Mon Sep 17 00:00:00 2001 From: John Kenyon Date: Mon, 23 Jun 2025 16:10:34 -0700 Subject: [PATCH] Add k3d version in progress --- hello-k3d/.gitignore | 1 + hello-k3d/Dockerfile | 6 ++++++ hello-k3d/Makefile | 28 +++++++++++++++++++++++++ hello-k3d/app.py | 29 ++++++++++++++++++++++++++ hello-k3d/k8s/frontend-deployment.yaml | 26 +++++++++++++++++++++++ hello-k3d/k8s/frontend-ingress.yaml | 20 ++++++++++++++++++ hello-k3d/k8s/frontend-service.yaml | 12 +++++++++++ hello-k3d/k8s/redis-deployment.yaml | 20 ++++++++++++++++++ hello-k3d/k8s/redis-service.yaml | 12 +++++++++++ 9 files changed, 154 insertions(+) create mode 100644 hello-k3d/.gitignore create mode 100644 hello-k3d/Dockerfile create mode 100644 hello-k3d/Makefile create mode 100644 hello-k3d/app.py create mode 100644 hello-k3d/k8s/frontend-deployment.yaml create mode 100644 hello-k3d/k8s/frontend-ingress.yaml create mode 100644 hello-k3d/k8s/frontend-service.yaml create mode 100644 hello-k3d/k8s/redis-deployment.yaml create mode 100644 hello-k3d/k8s/redis-service.yaml diff --git a/hello-k3d/.gitignore b/hello-k3d/.gitignore new file mode 100644 index 0000000..699f82a --- /dev/null +++ b/hello-k3d/.gitignore @@ -0,0 +1 @@ +shoppinglist-frontend.tar diff --git a/hello-k3d/Dockerfile b/hello-k3d/Dockerfile new file mode 100644 index 0000000..cb538da --- /dev/null +++ b/hello-k3d/Dockerfile @@ -0,0 +1,6 @@ +FROM python:3.11-slim +WORKDIR /app +COPY app.py . +RUN pip install flask redis +CMD ["python", "app.py"] + diff --git a/hello-k3d/Makefile b/hello-k3d/Makefile new file mode 100644 index 0000000..1882d0c --- /dev/null +++ b/hello-k3d/Makefile @@ -0,0 +1,28 @@ + + +all: cluster image load apply restart watch + +# TODO: maybe background this? +cluster: + k3d cluster create mycluster + k3d cluster list + +image: Dockerfile + docker build -t shoppinglist-frontend:latest . + +load: + k3d image import shoppinglist-frontend:latest -c mycluster + +apply: + kubectl apply -f k8s/ + +restart: + kubectl delete pod -l app=shoppinglist-frontend + +watch: + kubectl get pods -l app=shoppinglist-frontend -w + + + + + diff --git a/hello-k3d/app.py b/hello-k3d/app.py new file mode 100644 index 0000000..70acd7c --- /dev/null +++ b/hello-k3d/app.py @@ -0,0 +1,29 @@ +# app.py +from flask import Flask, request, jsonify +import redis + +import random + +my_random_id = random.randrange(100) + +r = redis.Redis(host='redis', port=6379) +app = Flask(__name__) + +@app.route("/id", methods=["GET"]) +def id(): + return f"{my_random_id}\n" + +@app.route("/items", methods=["GET", "POST"]) +def items(): + if request.method == "POST": + item = request.json.get("item") + if item: + r.rpush("shopping_list", item) + return '', 204 + else: + items = r.lrange("shopping_list", 0, -1) + return jsonify([i.decode() for i in items]) + +if __name__ == "__main__": + app.run(host="0.0.0.0", port=5000) + diff --git a/hello-k3d/k8s/frontend-deployment.yaml b/hello-k3d/k8s/frontend-deployment.yaml new file mode 100644 index 0000000..19f9f0c --- /dev/null +++ b/hello-k3d/k8s/frontend-deployment.yaml @@ -0,0 +1,26 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: shoppinglist-frontend +spec: + replicas: 3 + selector: + matchLabels: + app: shoppinglist-frontend + template: + metadata: + labels: + app: shoppinglist-frontend + spec: + containers: + - name: frontend + image: shoppinglist-frontend:latest + imagePullPolicy: Never + ports: + - containerPort: 5000 + env: + - name: REDIS_HOST + value: redis + - name: REDIS_PORT + value: "6379" + diff --git a/hello-k3d/k8s/frontend-ingress.yaml b/hello-k3d/k8s/frontend-ingress.yaml new file mode 100644 index 0000000..7b1ec3e --- /dev/null +++ b/hello-k3d/k8s/frontend-ingress.yaml @@ -0,0 +1,20 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: shoppinglist-ingress + annotations: + nginx.ingress.kubernetes.io/rewrite-target: / +spec: + ingressClassName: nginx + rules: + - host: localhost + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: shoppinglist-frontend + port: + number: 80 + diff --git a/hello-k3d/k8s/frontend-service.yaml b/hello-k3d/k8s/frontend-service.yaml new file mode 100644 index 0000000..e43069d --- /dev/null +++ b/hello-k3d/k8s/frontend-service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: shoppinglist-frontend +spec: + selector: + app: shoppinglist-frontend + ports: + - port: 80 + targetPort: 5000 + type: NodePort + diff --git a/hello-k3d/k8s/redis-deployment.yaml b/hello-k3d/k8s/redis-deployment.yaml new file mode 100644 index 0000000..0c0a3e5 --- /dev/null +++ b/hello-k3d/k8s/redis-deployment.yaml @@ -0,0 +1,20 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: redis +spec: + replicas: 1 + selector: + matchLabels: + app: redis + template: + metadata: + labels: + app: redis + spec: + containers: + - name: redis + image: redis:7-alpine + ports: + - containerPort: 6379 + diff --git a/hello-k3d/k8s/redis-service.yaml b/hello-k3d/k8s/redis-service.yaml new file mode 100644 index 0000000..91542d0 --- /dev/null +++ b/hello-k3d/k8s/redis-service.yaml @@ -0,0 +1,12 @@ +apiVersion: v1 +kind: Service +metadata: + name: redis +spec: + selector: + app: redis + ports: + - port: 6379 + targetPort: 6379 + type: ClusterIP +