ASP.NET Core Web API doesn't run on Kubernetes

7/14/2021

I am able to access my ASP.NET Core Web API when passing the environment variable ASPNETCORE_ENVIRONMENT=DEVELOP to Kubernetes but not if I omit it (i.e. ASP.NET Core defaults to Production environment).

I am using:

  • ASP.NET Core 5
  • Docker
  • Kubernetes
  • Minikube

ASP.NET Web API code

dotnet new webapi

My Docker config

FROM mcr.microsoft.com/dotnet/sdk:5.0-focal AS build
WORKDIR /app
COPY . .
RUN dotnet restore
RUN dotnet publish -c release -o /publish ./WebApi/WebApi.csproj --no-restore
COPY .env /publish/
WORKDIR /publish/

FROM mcr.microsoft.com/dotnet/aspnet:5.0-focal AS base
WORKDIR /app/
COPY --from=build /publish/ .

ENTRYPOINT ["dotnet", "WebApi.dll"]

Kubernetes Service config

kind: Service
apiVersion: v1
metadata:
  name: web-api
spec:
  type: NodePort
  ports:
    - name: http
      port: 5002 #Host port
      protocol: TCP
      targetPort: 5000 #Default ASP.NET Core container port
  selector:
    app: web-api

Kubernetes deployment config

kind: Deployment
apiVersion: apps/v1
metadata:
  name: web-api
  labels:
    app: web-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-api
  template:
    metadata:
      labels:
        app: web-api
    spec:
      containers:
        - name: web-api
          image: web-api
          imagePullPolicy: Never
          env:
            - name: ASPNETCORE_URLS
              value: http://+:5000

Skaffold config

apiVersion: skaffold/v2beta11
kind: Config
metadata:
  name: lychee-services-config
build:
  tagPolicy:
    sha256: {}
  artifacts:
  - &web-api
    image: web-api
    context: web-api
profiles:
  - name: local-development
    build:
      tagPolicy:
        sha256: {}
      artifacts:
        - *web-api
    deploy:
      kubectl:
        manifests:
          - web-api/deployment.yml

Execution

I run Skaffold with: skaffold dev --port-forward -p local-development

Skaffold output:

Port forwarding service/web-api in namespace default, remote port 5002 -> address 127.0.0.1 port 5002

I open my browser to http://localhost:5002/swagger and it doesn't work.

I add the env var ASPNETCORE_ENVIRONMENT=DEVELOP to my Kubernetes deployment and it works.

Why?

-- Exegesis
asp.net-core
docker
kubernetes
skaffold

0 Answers