Kubernetes deployment not publicly accesible

1/3/2020

im trying to access a deloyment on our Kubernetes cluster on Azure. This is a Azure Kubernetes Service (AKS). Here are the configuration files for the deployment and the service that should expose the deployment.

Configurations

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mira-api-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mira-api
  template:
    metadata:
      labels:
        app: mira-api
    spec:
      containers:
        - name: backend
          image: registry.gitlab.com/izit/mira-backend
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
      imagePullSecrets:
        - name: regcred
apiVersion: v1
kind: Service
metadata:
  name: mira-api-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
      name: http
  selector:
    run: mira-api

When I check the cluster after applying these configurations I, I see the pod running correctly. Also the service is created and has public IP assigned.

Services shown after applying configuration

After this deployment I don't see any requests getting handled. I get a error message in my browser saying the site is inaccessible. Any ideas what I could have configured wrong?

-- Riraldy
azure-kubernetes
kubernetes
kubernetes-service

1 Answer

1/3/2020

Your service selector labels and pod labels do not match.

You have app: mira-api label in deployment's pod template but have run: mira-api in service's label selector.

Change your service selector label to match the pod label as follows.

apiVersion: v1
kind: Service
metadata:
  name: mira-api-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
      targetPort: 8080
      protocol: TCP
      name: http
  selector:
    app: mira-api

To make sure your service is selecting the backend pods or not, you can run kubectl describe svc <svc name> command and check if it has any Endpoints listed.

# kubectl describe svc postgres
Name:              postgres
Namespace:         default
Labels:            app=postgres
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"postgres"},"name":"postgres","namespace":"default"},"s...
Selector:          app=postgres
Type:              ClusterIP
IP:                10.106.7.183
Port:              default  5432/TCP
TargetPort:        5432/TCP
Endpoints:         10.244.2.117:5432    <------- This line
Session Affinity:  None
Events:            <none>
-- Shashank V
Source: StackOverflow