How does a .NET Core console application deploy to Kubernetes?

11/13/2020

I have a console application that acts as a listener from some message broker. I've containerized it and successfully run as a local docker. Now I would like to deploy it to Kubernetes (specifically EKS on AWS).

So I created a yaml file that needed for deployment, however, I was confused about the port needed in the yaml file. From the experience of deploying API, I know the port should be the same as the Docker Image exposed port, however, the Console Application does not expose any port, and no need port to run either.

So I simply put out some ports and try to deploy. And naturally it is not working. I have pulled the image from ECR and runs to confirm it was working. Only the deployment to EKS is not.

May I know is there a correct way to deploy a .NET Core console application to Kubernetes?

Below is my Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Listener/Listener.csproj", "Listener/"]
COPY ["Infra/Infra.csproj", "Infra/"]
RUN dotnet restore "Listener/Listener.csproj"
COPY . .
WORKDIR "/src/Listener"
RUN dotnet build "Listener.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Listener.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Listener.dll"]

Below is my yaml file for this console application:

apiVersion: apps/v1
items:
- apiVersion: v1
  kind: Service
  metadata:
    annotations:
      Process: listener
    creationTimestamp: null
    labels:
      app: listener
    name: listener
  spec:
    type: LoadBalancer
    ports:
    - name: "5999"
      port: 5999
      targetPort: 5999
    selector:
      app: listener
  status:
    loadBalancer: {}
- apiVersion: apps/v1
  kind: Deployment
  metadata:
    annotations:
      Process: listener
    creationTimestamp: null
    labels:
      app: listener
    name: listener
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: listener
    strategy: {}
    template:
      metadata:
        creationTimestamp: null
        labels:
          app: listener
      spec:
        containers:
        - env:
          image: *****.dkr.ecr.<region>.amazonaws.com/listener:latest
          name: listener
          ports:
          - containerPort: 5999
          resources: {}
        restartPolicy: Always
  status: {}
kind: List
metadata: {}

Really appreciates it if anyone could help! Thanks!

-- Corene
.net-core
amazon-eks
c#
docker
kubernetes

1 Answer

11/13/2020

Well, if your project doesn't actually listen on any ports, then you don't need to expose any. You can do away with these lines from your deployment

ports:
- containerPort: 5999

And, since services are defined as "an abstract way to expose an application running on a set of Pods as a network service," you don't actually need the service definition either, meaning that all you need is your deployment.

-- DiplomacyNotWar
Source: StackOverflow