Azure Kubernetes - single node, two replicas: port issue?

235 views Asked by At

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
1

There are 1 answers

6
Charles Xu On BEST ANSWER

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.