Unable to access application on pod on minikube

9/16/2020

I have created a deployment using below simple yaml file using

kubectl apply -f deployment.yaml

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    tier: frontend
    app: myapp
spec:
  selector:
    matchLabels:
      app: myapp
  replicas: 3
  template:
    metadata:
      name: nginx-pod
      labels:
        app: myapp
    spec:
      containers:
        - name: nginx-pod
          image: nginx

and then created a service for it using below yaml file using

kubectl apply -f service.yaml

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  type: NodePort
  ports:
    - port: 89
      targetPort: 89
      nodePort: 30009
  selector:
    app: myapp

Now when i run

minikube service myapp-service

it gives me

$ minikube service myapp-service
|-----------|---------------|-------------|-------------------------|
| NAMESPACE |     NAME      | TARGET PORT |           URL           |
|-----------|---------------|-------------|-------------------------|
| default   | myapp-service |          89 | http://172.17.0.2:30009 |
|-----------|---------------|-------------|-------------------------|
🏃  Starting tunnel for service myapp-service.
|-----------|---------------|-------------|------------------------|
| NAMESPACE |     NAME      | TARGET PORT |          URL           |
|-----------|---------------|-------------|------------------------|
| default   | myapp-service |             | http://127.0.0.1:52289 |
|-----------|---------------|-------------|------------------------|
🎉  Opening service default/myapp-service in default browser...
❗  Because you are using a Docker driver on darwin, the terminal needs to be open to run it.

and when I try to access the given http://127.0.0.1:52289, I get "This site can’t be reached" error.

is there anything wrong in the yaml files? I am using

minikube version: v1.12.1

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:43:34Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

Docker version

$ docker version
Client: Docker Engine - Community
 Version:           19.03.8
 API version:       1.40
 Go version:        go1.12.17
 Git commit:        afacb8b
 Built:             Wed Mar 11 01:21:11 2020
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.8
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.17
  Git commit:       afacb8b
  Built:            Wed Mar 11 01:29:16 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
-- Manoj Bharadwaj
kubectl
kubernetes
minikube
nginx
service

1 Answer

9/17/2020

This is a community wiki answer aimed to sum up the info from the comments with additional details and explanations.

Guys in the comments are right. There is a flaw in your ports configuration. There are few thing that you need to know that would help you better understand this concept. We have several different port configurations for Kubernetes services:

  • Port: exposes the Kubernetes service on the specified port within the cluster. Other pods within the cluster can communicate with this server on the specified port.

  • TargetPort: is the port on which the service will send requests to, that your pod will be listening on. Your application in the container will need to be listening on this port also.

  • NodePort: exposes a service externally to the cluster by means of the target nodes IP address and the NodePort. NodePort is the default setting if the port field is not specified.

From your example above the myapp-service service will be exposed internally to cluster applications on port 89 and externally to the cluster on the node IP address on 30009. It will also forward requests to pods with the label “app: my-app” on port 89.

You can check the details of your service with:

kubectl describe service myapp-service

Adjust your targetPort: to 80 and it should be fine.

-- Wytrzymały Wiktor
Source: StackOverflow