Search code examples
kuberneteskubernetes-service

Not able to access kubernetes service from browser


The following are the yaml files for pod and services

apiVersion: v1
kind: Pod
metadata:
  name: webapp
  labels:
    app: "webapp"

spec:
  containers:
  - name: webapp
    image: richardchesterwood/k8s-fleetman-webapp-angular:release0
    ports:
      - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: fleetman-webapp

spec:
  selector:
    app: webapp

  ports:
    - name: http
      protocol: TCP
      port: 80
      nodePort: 30080
  
  type: NodePort

I want to access the webapp from browser and use localhost:30080 or minikube-ip:30080, but was unable to access.

I troubleshooted the webapp image working, by going into the shell for the pod and executing

wget http://localhost:80
-- index.html got downloaded.



kubectl get all
NAME         READY   STATUS    RESTARTS   AGE
pod/webapp   1/1     Running   0          23m

NAME                      TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/fleetman-webapp   NodePort    10.96.240.170   <none>        80:30080/TCP   173m
service/kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP        26h

Where am i going wrong, or whats the next step for debugging?


Solution

  • If you are running locally, you can do this:

    kubectl port-forward -n default service/fleetman-webapp 30080:80

    With NodePort, it won't be listening on loopback address (localhost). Instead, it will be listening on Kubernetes node's IP. So you have to do port forwarding make it work on localhost