Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Container ASP.NET Core app in a Kubernetes Pod gives error address already in use

I have an ASP.NET Core Multi-Container docker app which I am now trying to host to Kubernetes cluster on my local PC. But unfortunately one container is starting and other is giving error address already in use.

The Deployment file is given below:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: multi-container-dep
  labels:
    app: aspnet-core-multi-container-app
spec:
  replicas: 1
  selector: 
    matchLabels:
      component: multi-container
  template:
    metadata: 
      labels:
        component: multi-container
    spec:
      containers:
        - name: cmultiapp
          image: multiapp
          imagePullPolicy: Never
          ports:
            - containerPort: 80
        - name: cmultiapi
          image: multiapi
          imagePullPolicy: Never
          ports:
            - containerPort: 81

The full logs of the container which is failing is:

Unable to start Kestrel.
  System.IO.IOException: Failed to bind to address http://[::]:80: address already in use.
   ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use
   ---> System.Net.Sockets.SocketException (98): Address already in use
     at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
     at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
     at System.Net.Sockets.Socket.Bind(EndPoint localEP)
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.<Bind>g__BindSocket|13_0(<>c__DisplayClass13_0& )
     --- End of inner exception stack trace ---
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.<Bind>g__BindSocket|13_0(<>c__DisplayClass13_0& )
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind()
     at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken)
     at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig)

Note that I already tried putting another port to that container in the YAML file

ports:
    - containerPort: 81

But it seems to not working. How to fix it?

like image 830
yogihosting Avatar asked Jan 25 '26 16:01

yogihosting


2 Answers

To quote this answer: https://stackoverflow.com/a/62057548/12201084

containerPort as part of the pod definition is only informational purposes.

This means that setting containerPort does not have any influence on what port application opens. You can even skip it and don't set it at all.

If you want your application to open a specific port you need to tell it to the applciation. It's usually done with flags, envs or configfiles. Setting a port in pod/container yaml definition won't change a thing.

You have to remember that k8s network model is different than docker and docker compose's model.


So why does the containerPort field exist if is doesn't do a thing? - you may ask

Well. Actually is not completely true. It's main puspose is indeed for informational/documenting purposes but it may also be used with services. You can name a port in pod definition and then use this name to reference the port in service definition yaml (this only applies to targetPort field).

like image 165
Matt Avatar answered Jan 27 '26 07:01

Matt


I solved this by using environment variables and assigning aspnet url to port 81.

- name: cmultiapi
  image: multiapi
  imagePullPolicy: Never
  ports:
    - containerPort: 81
  env:
    - name: ASPNETCORE_URLS
      value: http://+:81

I would also like to mention the url where I got the necessary help. Link is here.

like image 27
yogihosting Avatar answered Jan 27 '26 08:01

yogihosting



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!