Kubernetes logs don't print requests output when I use a port in an address

10/25/2019

I've created a cluster with minikube

minikube start 

Applied this yaml manifest:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: gateway-deployment
spec:
  selector:
    matchLabels:
      app: gateway
  replicas: 1
  template:
    metadata:
      labels:
        app: gateway
    spec:
      containers:
      - name: gateway
        image: docker_gateway
        imagePullPolicy: Never
        ports:
        - containerPort: 4001
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
  - protocol: TCP
    port: 4001

And my GO app in the container docker_gateway is just a gin http server with one route

package main
import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/hello", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "hello",
        })
    })

    server = &http.Server{
        Addr:    ":4001",
        Handler: r,
    }

    server.ListenAndServe()
}

In Postman I make requests to 192.168.252.130:4001/hello and get a responses

But Kubernetes Pod's logs in Kubernetes don't print those requests. I expect to get this:

[GIN] 2019/10/25 - 14:17:20 | 200 |       1.115µs |      192.168.252.1| GET      /hello

But an interesting thing is when I add Ingress

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  backend:
    serviceName: gateway
    servicePort: 4001

I am able to make requests to 192.168.252.130/hello and 192.168.252.130:4001/hello And without the port Pod's logs print requests, but with the port - they don't.

[GIN] 2019/10/25 - 14:19:13 | 200 |       2.433µs |      192.168.252.1| GET      /hello
-- ligowsky
go
kubernetes
kubernetes-ingress

1 Answer

10/25/2019

It's because you cannot access a kubernetes service of ClusterIP type from outside(in your case, outside of minikube) of the cluster.

Learn more about service types here

To access your service from outside, change your service to NodePort type.

Something like:

apiVersion: v1
kind: Service
metadata:
  name: gateway
spec:
  selector:
    app: gateway
  ports:
  - protocol: TCP
    nodePort: 30036
    port: 4001
  type: NodePort

Then you will be able to access it at http://<minikube-ip>:30036/

-- Kamol Hasan
Source: StackOverflow