Accessing app outside kubernetes cluster with minikube

3/4/2020

i'm new to kubernetes , i'm trying to learn it using minikube and i'm facing a problem with accessing apps outside the cluster. i created a deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 8080

To access it i need to expose it decoratively or imperatively. In the imperative way it works :

kubectl expose deployment nginx-deployment --port 80 --type NodePort

When i create a service declaratively i always end up with a connection refused error :

apiVersion: v1

kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  type : NodePort
  ports:
  - port : 8080
    nodePort : 30018
    protocol : TCP
  selector:
    app: nginx

curl -k http://NodeIP:NodePort returns :

curl: (7) Failed to connect to Node IP port NodePORT: Connection refused

-- Elias
docker
kubernetes
minikube

2 Answers

3/4/2020

Once you create a service in minikube you can expose the service to the outside of the minikube VM (host machine) using the command

minikube service SERVICE_NAME

Refer: https://minikube.sigs.k8s.io/docs/reference/commands/service/

-- Tummala Dhanvi
Source: StackOverflow

3/5/2020

As @Ansil suggested, your nginx should be configured to listen on port 8080 if you want to refer to this port in your Service definition. By default it listens on port 80.

You cannot make it listen on different port like 8080 simply by specifying different containerPort in your Deployment definition as in your example:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx

  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 8080

You can easily verify it on your own by attaching to such Pod:

kubectl exec -ti <nginx-pod-name> -- /bin/bash

Once you're there, run:

ss -ntlp

And you should see on which port your nginx actually listens on.

Additionally you may:

cat /etc/nginx/conf.d/default.conf

It will also tell you on which port your nginx is configured to listen. That's all. It's really simple. You changed containerPort to 8080 but inside your container nothing actually listens on such port.

You can still expose it as a Service (no matter declaratively or imperatively) but it won't change anything as eventually it points to the wrong port on your container, on which nothing listens and you'll see message similar to this one:

curl: (7) Failed to connect to 10.1.2.3 port 30080: Connection refused
-- mario
Source: StackOverflow