Ingress-nginx on Docker Desktop with WLS2

Standard

code…

# app.py
from flask import Flask, request
app = Flask(__name__)
@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def hello(path):
    return f"Hello from Python app!\nYou've accessed path: /{path}\n"
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)
# dockerfile
# Use an official Python runtime as a parent image
FROM python:3.8-slim-buster
# Set the working directory in the container
WORKDIR /app
# Copy the local code to the container
COPY . /app
# Install Flask
RUN pip install Flask
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Run app.py when the container launches
CMD ["python", "app.py"]
docker build -t hello-python-app:latest .
docker tag hello-python-app username/hello-python-app
docker push username/hello-python-app
# hello-python-app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  namespace: ingress-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: username/hello-python-app:latest
        ports:
        - containerPort: 8080
# hello-python-app-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: ingress-nginx
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
# ingress-nginx.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
  namespace: ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /hello1
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 80
      - path: /hello2
        pathType: Prefix
        backend:
          service:
            name: my-service
            port:
              number: 80
# main.tf
resource "helm_release" "ingress_nginx" {
  name       = "ingress-nginx"
  namespace  = "ingress-nginx"
  repository = "https://kubernetes.github.io/ingress-nginx"
  chart      = "ingress-nginx"
  create_namespace = true
}
data "local_file" "my_service_yaml" {
  filename = "${path.module}/hello-python-app-deployment.yaml"
}
data "local_file" "my_deployment_yaml" {
  filename = "${path.module}/hello-python-app-service.yaml"
}
data "local_file" "ingress_nginx_yaml" {
  filename = "${path.module}/ingress-nginx.yaml"
}
resource "kubernetes_manifest" "my_service" {
  manifest = yamldecode(data.local_file.my_service_yaml.content)
}
resource "kubernetes_manifest" "my_deployment" {
  manifest = yamldecode(data.local_file.my_deployment_yaml.content)
}
resource "kubernetes_manifest" "ingress_nginx" {
  manifest = yamldecode(data.local_file.ingress_nginx_yaml.content)
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.