-1

I'm currently creating a Kubernetes cluster in Azure Kubernetes for a production environment. In my cluster, I will have single node in the node pool - pool1.

Now, I want to deploy an application with 2 replicas as shown below

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-1
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1

on which port the application will listen to? as two pods will be deployed on the same node, how does the port will be allocated?

Update: As suggested, have updated the manifest with service definition.

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetport: 80
  type: LoadBalancer
One Developer
  • 99
  • 5
  • 43
  • 103

1 Answers1

2

Actually, AKS will use two different ports of the node for the two replicas, but you also need to expose the 80 port of the container to outside, because the Nginx listens to port 80. Usually, the AKS uses the service to route the requests outside to all the pods, and the service works like a load balance.

So you just need to focus on exposing the port that the image listens to and the port of the service that you want to expose to the outside.

Charles Xu
  • 29,862
  • 2
  • 22
  • 39
  • have updated the question with the service definition. Now, if 2 pods listen to the same port 80, would that cause an issue? – One Developer Oct 13 '20 at 06:53
  • @KarthikeyanVijayakumar If the port 80 is not in use, then no problem. The service will load balance for you. You can treat the pods as different backends of the service. – Charles Xu Oct 13 '20 at 06:57
  • but in case those the pods listens to port 80, correct? If so, one would fail, correct? Are we saying containerport is virutal and same port can be used by multiple containers as long as they are deployed on a separate pod? – One Developer Oct 13 '20 at 07:08
  • @KarthikeyanVijayakumar In fact, each pod has the 80 port inside it, but outside, the node will allocate one available port for it, maybe 1234, maybe 80, it's the nodes' ports. They are different and do not conflict. – Charles Xu Oct 13 '20 at 07:13
  • Than you, final query - Is there any port should be unique or worried about? – One Developer Oct 13 '20 at 07:21
  • 1
    @KarthikeyanVijayakumar I think there is no special port you need to worry about. The only thing I think is that the port with little number is used by the system. You'd better use big number port like more than 10000. – Charles Xu Oct 13 '20 at 07:52