https://pulumi.com logo
#python
Title
# python
c

cool-nail-39287

08/30/2023, 9:10 AM
Hi all, I'm pretty new to Pulumi (and also Kubernetes). I've spent the last few days trying to deploy a backend and a frontend into a local Minikube cluster. The frontend is just a web app sending API requests to the backend and receiving and rendering responses. Unfortunately, since Minikube doesn't accept a LoadBalancer, to access the frontend locally I need to port-forward it - but when I do that it's not able to communicate with the backend anymore. I tried to implement an Ingress to overcome this issue but I've hit a wall there because after implementing it I can't access the frontend service anymore via port-forwarding (I can port-forward and access the respective pod though, which means there's something wrong with the Ingress I guess). Any suggestions/past experiences doing that? My code is below
Copy code
import pulumi
from pulumi_kubernetes.apps.v1 import Deployment, DeploymentSpecArgs
from pulumi_kubernetes.core.v1 import (
    PodTemplateSpecArgs,
    ContainerArgs,
    PodSpecArgs,
    ContainerPortArgs,
    ServiceSpecArgs,
    Service,
    ServicePortArgs,
)
from pulumi_kubernetes.meta.v1 import LabelSelectorArgs, ObjectMetaArgs
from pulumi_kubernetes.networking.v1 import (
    Ingress,
    IngressSpecArgs,
    HTTPIngressPathArgs,
    IngressBackendArgs,
    IngressRuleArgs,
    HTTPIngressRuleValueArgs,
    IngressServiceBackendArgs,
    ServiceBackendPortArgs,
)


from pulumi_kubernetes import Provider
from pulumi_docker import Image


config = pulumi.Config()

is_minikube = config.require_bool("isMinikube")

app_name = "my-app"
app_labels = {"app": app_name}
frontend_app_name = f"{app_name}-frontend"
backend_app_name = f"{app_name}-backend"
frontend_app_labels = {"app": frontend_app_name}
backend_app_labels = {"app": backend_app_name}

# create images
backend_image_name = "localhost/backend:latest"
backend_image = Image(
    "backend-image",
    image_name=backend_image_name,
    build="../backend",
    skip_push=True,
)
frontend_image_name = "localhost/frontend:latest"
frontend_image = Image(
    "frontend-image",
    image_name=frontend_image_name,
    build="../frontend",
    skip_push=True,
)


# create provider
k8s_provider = Provider("my-provider")

# Deployments
backend_deployment = Deployment(
    "backend",
    spec=DeploymentSpecArgs(
        replicas=1,
        selector=LabelSelectorArgs(match_labels=app_labels),
        template=PodTemplateSpecArgs(
            metadata=ObjectMetaArgs(labels=app_labels),
            spec=PodSpecArgs(
                containers=[
                    ContainerArgs(
                        name="backend",
                        image=backend_image_name,
                        ports=[ContainerPortArgs(container_port=8000)],
                        image_pull_policy="Never",
                    )
                ]
            ),
        ),
    ),
    opts=pulumi.ResourceOptions(provider=k8s_provider),
)

frontend_deployment = Deployment(
    "frontend",
    spec=DeploymentSpecArgs(
        replicas=1,
        selector=LabelSelectorArgs(match_labels=app_labels),
        template=PodTemplateSpecArgs(
            metadata=ObjectMetaArgs(labels=app_labels),
            spec=PodSpecArgs(
                containers=[
                    ContainerArgs(
                        name="frontend",
                        image=frontend_image_name,
                        ports=[ContainerPortArgs(container_port=80)],
                        image_pull_policy="Never",
                    )
                ]
            ),
        ),
    ),
    opts=pulumi.ResourceOptions(provider=k8s_provider),
)


# Services
backend = Service(
    backend_app_name,
    metadata=ObjectMetaArgs(
        name="backend-service",
        labels=backend_app_labels,
    ),
    spec=ServiceSpecArgs(
        type="ClusterIP",
        selector=app_labels,
        ports=[ServicePortArgs(port=8000)],
    ),
    opts=pulumi.ResourceOptions(provider=k8s_provider),
)

frontend = Service(
    frontend_app_name,
    metadata=ObjectMetaArgs(
        name="frontend-service",
        labels=frontend_app_labels,
    ),
    spec=ServiceSpecArgs(
        type="ClusterIP" if is_minikube else "LoadBalancer",
        selector=app_labels,
        ports=[ServicePortArgs(port=80)],
    ),
    opts=pulumi.ResourceOptions(provider=k8s_provider),
)

# Ingress
ingress = Ingress(
    "app-ingress",
    metadata=ObjectMetaArgs(
        annotations={
            "<http://nginx.ingress.kubernetes.io/rewrite-target|nginx.ingress.kubernetes.io/rewrite-target>": "/",
        },
        name="app-ingress",
    ),
    spec=IngressSpecArgs(
        rules=[
            IngressRuleArgs(
                http=HTTPIngressRuleValueArgs(
                    paths=[
                        HTTPIngressPathArgs(
                            path="/api/",
                            path_type="Prefix",
                            backend=IngressBackendArgs(
                                service=IngressServiceBackendArgs(
                                    name="backend-service",
                                    port=ServiceBackendPortArgs(number=8000),
                                )
                            ),
                        ),
                        HTTPIngressPathArgs(
                            path="/",
                            path_type="Prefix",
                            backend=IngressBackendArgs(
                                service=IngressServiceBackendArgs(
                                    name="frontend-service",
                                    port=ServiceBackendPortArgs(number=80),
                                )
                            ),
                        ),
                    ]
                )
            )
        ],
    ),
    opts=pulumi.ResourceOptions(provider=k8s_provider),
)