go - 当我在地址中使用端口时,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
解决方案
这是因为您无法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/
推荐阅读
- python - 为什么这段代码在 Windows 和 Linux 之间打印出不同的结果?
- ios - 当委托字段不被称为 `delegate` 时实现 `DelegateProxy` (RxSwift/RxCocoa)
- spring-boot - 在 Spring Boot Aspect 中未获取实际参数名称
- ffmpeg - FFMPEG缩放平移多个图像
- vb.net - vb .net基于同一位置的两个列表框重命名文件
- python - flask-pymongo:我把 mongo 放在一个单独的 python 文件中,它变成了 NoneType
- vue.js - 如何扩展 Vuetify 组件并保留 ref?
- azure - 如何将多个 Web 应用程序添加到单个网络中,以便连接到一个 Web 应用程序的 vnet 也可以访问其他 Web 应用程序
- d3.js - 如何将 csv 文件加载到 nuxt vue 组件中
- php - 我怎样才能获得永久IP