Unable to access service in Kubernetes

10/3/2019

I've got this webserver config:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: webserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: webserver
    spec:
      containers:
      - name: webserver
        image: nginx:alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: hostvol
          mountPath: /usr/share/nginx/html
      volumes:
      - name: hostvol
        hostPath:
          path: /home/docker/vol

and this web service config:

apiVersion: v1
kind: Service
metadata:
  name: web-service
  labels:
    run: web-service
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: webserver

I was expecting to be able to connect to the webserver via http://192.168.99.100:80 with this config but Chrome gives me a ERR_CONNECTION_REFUSED.

I tried minikube service --url web-service which gives http://192.168.99.100:30276 however this also has a ERR_CONNECTION_REFUSED.

Any further suggestions?

UPDATE

I updated the port / targetPort to 80.

However, I now get:

ERR_CONNECTION_REFUSED for http://192.168.99.100:80/

and

an nginx 403 for http://192.168.99.100:31540/

-- Snowcrash
kubernetes

4 Answers

10/4/2019

Please check permissions of mounted volume /home/docker/vol

To fix this you have to make the mounted directory and its contents publicly readable:

chmod -R o+rX /home/docker/vol

-- Vishal
Source: StackOverflow

10/3/2019

In your service, you did not specify a targetPort, so the service is using the port value as targetPort, however your container is listening on 80. Add a targetPort: 80 to the service.

-- Burak Serdar
Source: StackOverflow

10/3/2019

NodePort port range varies from 30000-32767(default). When you expose a service without specifying a port, kubernetes picks up a random port from the above range and provide you.

You can check the port by typing the below command

kubectl get svc

In your case - the application is port forwarded to 31540. Your issues seems to be the niginx configuration. Check for the nginx logs.

-- Gowtham
Source: StackOverflow

10/4/2019

In your service, you can define a nodePort

apiVersion: v1
kind: Service
metadata:
  name: web-service
  labels:
    run: web-service
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 32700
    protocol: TCP
  selector:
    app: webserver

Now, you will be able to access it on http://:32700

Be careful with port 80. Ideally, you would have an nginx ingress controller running on port 80 and all traffic will be routed through it. Using port 80 as nodePort will mess up your deployment.

-- Palash Goel
Source: StackOverflow