当我在地址中使用端口时,Kubernetes 日志不打印请求输出
Kubernetes logs don't print requests output when I use a port in an address
我用 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
这是因为您无法从集群外部(在您的情况下,在 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/
访问它
我用 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
这是因为您无法从集群外部(在您的情况下,在 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/