首页 > 解决方案 > 当我在地址中使用端口时,Kubernetes 日志不打印请求输出

问题描述

我用minikube创建了一个集群

minikube start 

应用了这个 yaml 清单:

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

而我在容器中的 GO 应用程序docker_gateway只是一个带有一条路由的 gin http 服务器

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()
}

在 Postman 中,我向 192.168.252.130:4001/hello 发出请求并得到回复

但是 Kubernetes Pod 在 Kubernetes 中的日志不会打印这些请求。我希望得到这个:

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

但有趣的是当我添加 Ingress

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

我可以向 192.168.252.130/hello 和 192.168.252.130:4001/hello 发出请求并且没有端口 Pod 的日志打印请求,但使用端口 - 他们没有。

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

标签: gokuberneteskubernetes-ingress

解决方案


这是因为您无法ClusterIP从集群外部(在您的情况下为 minikube 外部)访问类型的 kubernetes 服务。

在此处了解有关服务类型的更多信息

要从外部访问您的服务,请将您的服务更改为NodePort类型。

就像是:

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

然后你就可以访问它了http://<minikube-ip>:30036/


推荐阅读