【问题标题】:Kubernetes logs don't print requests output when I use a port in an address当我在地址中使用端口时,Kubernetes 日志不打印请求输出
【发布时间】:2020-02-21 23:34:38
【问题描述】:

我已经用 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

而我在容器 docker_gateway 中的 GO 应用只是一个带有一条路由的 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

【问题讨论】:

    标签: go kubernetes kubernetes-ingress


    【解决方案1】:

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

    详细了解服务类型here

    要从外部访问您的服务,请将您的服务更改为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/访问它

    【讨论】:

    • 哦,谢谢,现在它可以工作并打印日志了!而且您说无法从外部访问 ClusterIP,但是为什么没有 NodePort 或 Ingress 我能够向 http://:4001 发出请求并获得响应?
    • 更正:“无法从外部访问 ClusterIP 类型的 k8s 服务。”
    • 您可以在blog了解NodePort类型或Ingress的服务如何工作
    猜你喜欢
    • 2019-11-11
    • 2014-11-29
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多