diff --git a/docker/services/netease.yaml b/docker/services/netease.yaml index ff674b8..634067b 100644 --- a/docker/services/netease.yaml +++ b/docker/services/netease.yaml @@ -11,5 +11,11 @@ services: - CAT_BACKEND_PORT=8888 - CAT_BACKEND_HOST=netease - CAT_BACKEND_ALLOWED_ORIGINS=["*"] + healthcheck: + test: ["CMD-SHELL", "curl --fail http://$$CAT_BACKEND_HOST:$$CAT_BACKEND_PORT/api/healthz || exit 1"] + interval: 2s + timeout: 5s + retries: 3 + start_period: 5s networks: - netease_network diff --git a/docker/services/streamlit.yaml b/docker/services/streamlit.yaml index 31ec899..a647efb 100644 --- a/docker/services/streamlit.yaml +++ b/docker/services/streamlit.yaml @@ -9,5 +9,11 @@ services: - "8501:8501" environment: - UI_BACKEND_URL=http://netease:8888 + healthcheck: + test: ["CMD-SHELL", "curl --fail http://streamlit:8501/api/healthz || exit 1"] + interval: 2s + timeout: 5s + retries: 3 + start_period: 5s networks: - netease_network diff --git a/k8s/configmap.yml b/k8s/configmap.yml new file mode 100644 index 0000000..c6210b2 --- /dev/null +++ b/k8s/configmap.yml @@ -0,0 +1,16 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: frontend-config +data: + UI_BACKEND_URL: "http://backend:8888" + +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: backend-config +data: + CAT_BACKEND_PORT: "8888" + CAT_BACKEND_HOST: "backend" + CAT_BACKEND_ALLOWED_ORIGINS: '["*"]' \ No newline at end of file diff --git a/k8s/deployment.yml b/k8s/deployment.yml new file mode 100644 index 0000000..7ba9ce8 --- /dev/null +++ b/k8s/deployment.yml @@ -0,0 +1,105 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: frontend + labels: + app.kubernetes.io/name: frontend +spec: + selector: + matchLabels: + app: frontend + template: + metadata: + labels: + app: frontend + spec: + containers: + - name: frontend + image: alaricle/streamlit:latest + ports: + - containerPort: 8501 + name: pod-http + resources: + requests: + memory: "64Mi" + cpu: "64m" + limits: + memory: "200Mi" + cpu: "128m" + envFrom: + - configMapRef: + name: frontend-config + livenessProbe: + httpGet: + path: /liveness + port: myport + initialDelaySeconds: 5 + periodSeconds: 10 + failureThreshold: 1 + startupProbe: + httpGet: + path: /healthz + port: myport + failureThreshold: 30 + periodSeconds: 10 + readinessProbe: + httpGet: + path: /readiness + port: myport + initialDelaySeconds: 5 + periodSeconds: 10 + failureThreshold: 1 + successThreshold: 3 +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: backend + labels: + app.kubernetes.io/name: backend +spec: + selector: + matchLabels: + app: backend + template: + metadata: + labels: + app: backend + spec: + containers: + - name: backend + image: alaricle/netease:latest + ports: + - containerPort: 8888 + name: pod-http + resources: + requests: + memory: "64Mi" + cpu: "64m" + limits: + memory: "200Mi" + cpu: "128m" + envFrom: + - configMapRef: + name: backend-config + livenessProbe: + httpGet: + path: /liveness + port: myport + initialDelaySeconds: 5 + periodSeconds: 10 + failureThreshold: 1 + startupProbe: + httpGet: + path: /healthz + port: myport + failureThreshold: 30 + periodSeconds: 10 + readinessProbe: + httpGet: + path: /readiness + port: myport + initialDelaySeconds: 5 + periodSeconds: 10 + failureThreshold: 1 + successThreshold: 3 \ No newline at end of file diff --git a/k8s/hpa.yml b/k8s/hpa.yml new file mode 100644 index 0000000..7de4217 --- /dev/null +++ b/k8s/hpa.yml @@ -0,0 +1,37 @@ +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: netease-fe-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: frontend + minReplicas: 1 + maxReplicas: 6 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 70 +--- +apiVersion: autoscaling/v2 +kind: HorizontalPodAutoscaler +metadata: + name: netease-be-hpa +spec: + scaleTargetRef: + apiVersion: apps/v1 + kind: Deployment + name: backend + minReplicas: 1 + maxReplicas: 6 + metrics: + - type: Resource + resource: + name: cpu + target: + type: Utilization + averageUtilization: 70 \ No newline at end of file diff --git a/k8s/ingress.yml b/k8s/ingress.yml new file mode 100644 index 0000000..2c4a1d1 --- /dev/null +++ b/k8s/ingress.yml @@ -0,0 +1,30 @@ +apiVersion: networking.k8s.io/v1 +kind: Ingress +metadata: + name: todo + annotations: + kubernetes.io/ingress.class: "nginx" + cert-manager.io/issuer: "letsencrypt-prod" +spec: + tls: + - hosts: + - 441781.learnk8s.jamesisme.com + secretName: todo-tls + rules: + - host: 441781.learnk8s.jamesisme.com + http: + paths: + - path: / + pathType: Prefix + backend: + service: + name: frontend + port: + name: svc-http + # - path: /api + # pathType: Prefix + # backend: + # service: + # name: backend + # port: + # name: svc-http \ No newline at end of file diff --git a/k8s/issuer.yml b/k8s/issuer.yml new file mode 100644 index 0000000..9b9d99c --- /dev/null +++ b/k8s/issuer.yml @@ -0,0 +1,18 @@ +apiVersion: cert-manager.io/v1 +kind: Issuer +metadata: + name: letsencrypt-prod +spec: + acme: + # The ACME server URL + server: https://acme-v02.api.letsencrypt.org/directory + # Email address used for ACME registration + email: ghbihuy123@gmail.com + # Name of a secret used to store the ACME account private key + privateKeySecretRef: + name: letsencrypt-prod + # Enable the HTTP-01 challenge provider + solvers: + - http01: + ingress: + class: nginx \ No newline at end of file diff --git a/k8s/service.yml b/k8s/service.yml new file mode 100644 index 0000000..a8fd585 --- /dev/null +++ b/k8s/service.yml @@ -0,0 +1,32 @@ +apiVersion: v1 +kind: Service +metadata: + name: frontend + labels: + app: frontend +spec: + type: ClusterIP + ports: + - port: 80 + name: svc-http + targetPort: pod-http + protocol: TCP + selector: + app: frontend + +--- +apiVersion: v1 +kind: Service +metadata: + name: backend + labels: + app: backend +spec: + type: ClusterIP + ports: + - port: 8888 + name: svc-http + targetPort: pod-http + protocol: TCP + selector: + app: backend \ No newline at end of file diff --git a/src/cat/api/routers/net_ease.py b/src/cat/api/routers/net_ease.py index 7f84490..d888e3a 100644 --- a/src/cat/api/routers/net_ease.py +++ b/src/cat/api/routers/net_ease.py @@ -43,3 +43,7 @@ async def upload_excel( media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", headers={"Content-Disposition": "attachment; filename=processed_salaries.xlsx"}, ) + +@router.get("/healthz") +def health_check(): + return {"status": "healthy"} \ No newline at end of file