'Unable to access the nginx through Kubernetes LoadBalancer service

I'm using k8s provided with docker desktop (windows).

My deployment.yml file is

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx-app
  template:
    metadata:
      labels:
        app: nginx-app
    spec:
      containers:
      - name: nginx-container
        image: nginx:stable-alpine
        ports:
        - containerPort: 80

and my service yml file is

apiVersion: v1
kind: Service
metadata:
  name: my-service
  labels:
    app: nginx-app
spec:
  selector:
    app: nginx-app
  type: NodePort
  ports:
  - nodePort: 31000
    port: 80
    targetPort: 80

all are up and running but I'm unable to access the application.

>curl localhost:31000
curl: (7) Failed to connect to localhost port 31000: Connection refused

>kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-685658ccbf-g84w5   1/1     Running   0          8s

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
service/kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP        14h
service/my-service   LoadBalancer   10.96.210.40   localhost     80:31000/TCP   4s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   1/1     1            1           8s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-685658ccbf   1         1         1       8s

Note: created the Inbound/Outbound rule for this 31000 port in windows firewall to make sure it won't block



Solution 1:[1]

There are some questions you can try to answer in order to Debug Services:

  1. Does the Service exist?: In your case we see that it does.

  2. Does the Service work by DNS name?: One of the most common ways that clients consume a Service is through a DNS name.

  3. Does the Service work by IP?: Assuming you have confirmed that DNS works, the next thing to test is whether your Service works by its IP address.

  4. Is the Service defined correctly?: You should really double and triple check that your Service is correct and matches your Pod's port. Also:

  • Is the Service port you are trying to access listed in spec.ports[]?

  • Is the targetPort correct for your Pods (some Pods use a different port than the Service)?

  • If you meant to use a numeric port, is it a number (9376) or a string "9376"?

  • If you meant to use a named port, do your Pods expose a port with the same name?

  • Is the port's protocol correct for your Pods?

  1. Does the Service have any Endpoints?: Check that the Pods you ran are actually being selected by the Service.

  2. Are the Pods working?: Check again that the Pods are actually working.

  3. Is the kube-proxy working?: Confirm that kube-proxy is running on your Nodes.

Going through the above steps will help you find the cause of this and possible future issues with services.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wytrzymały Wiktor