为了账号安全,请及时绑定邮箱和手机立即绑定

当我使用地址中的端口时,Kubernetes 日志不会打印请求输出

当我使用地址中的端口时,Kubernetes 日志不会打印请求输出

Go
慕后森 2023-07-31 15:57:39
我已经用minikube创建了一个集群minikube start 应用了这个 yaml 清单:apiVersion: apps/v1kind: Deploymentmetadata:  name: gateway-deploymentspec:  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: v1kind: Servicemetadata:  name: gatewayspec:  selector:    app: gateway  ports:  - protocol: TCP    port: 4001我的 GO 应用程序在容器中docker_gateway只是一个带有一条路由的 gin http 服务器package mainimport "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 中的 Kubernetes Pod 日志不会打印这些请求。我期望得到这个:[GIN] 2019/10/25 - 14:17:20 | 200 |       1.115µs |      192.168.252.1| GET      /hello但有趣的是当我添加 Ingress 时apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata:  name: ingressspec:  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
查看完整描述

1 回答

?
炎炎设计

TA贡献1808条经验 获得超4个赞

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/


查看完整回答
反对 回复 2023-07-31
  • 1 回答
  • 0 关注
  • 74 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信