简体   繁体   English

如何从外部 gRPC 客户端连接到 k8s 集群内的 gRPC 服务

[英]How to connect to the gRPC service inside k8s cluster from outside gRPC client

I have a gRPC server running on port 9000 with gRPC-gateway running on port 9080 .我有一个gRPC server在端口9000上运行, gRPC-gateway在端口9080上运行。 I can make request to my service with postman using the following link: ```http:// cluster1.example.com /api/v1/namespaces/default/services/ my-service:9080 /proxy我可以使用以下链接通过 postman 向我的服务提出请求:```http: //cluster1.example.com /api/v1/namespaces/default/services/ my-service:9080 /proxy

How can I connect to my service from gRPC client (on my local machine, which is outside of the cluster) using grpc.Dial() ?如何使用grpc.Dial()gRPC client (在集群外部的本地计算机上)连接到我的服务?

Example:例子:

conn, err := grpc.Dial(...?, grpc.WithInsecure())
if err != nil {
    panic(err)
}

You should be able to connect to services in your k8s cluster from local with port forwarding:您应该能够通过端口转发从本地连接到 k8s 集群中的服务:

kubectl port-forward --context <mycontext> -n <mynamespace> svc/my-service 9000:9000

And then you just pass the gRPC target into Dial with localhost and no scheme:然后你只需将 gRPC 目标传递到Dial与 localhost 并且没有方案:

conn, err := grpc.Dial("localhost:9000", grpc.WithInsecure())
if err != nil {
    panic(err)
}

I might state the obvious, but of course the server also must be started in insecure mode (no credentials), otherwise you might get Unavailable response code.我可能 state 很明显,但当然服务器也必须以不安全模式(无凭据)启动,否则您可能会收到Unavailable的响应代码。

Short answer:简短的回答:

This is mostly not a Golang question, it is a Kubernetes question.这主要不是 Golang 问题,而是 Kubernetes 问题。 You have to set up the Kubernetes part and use it like ever before in Golang.您必须设置 Kubernetes 部件并像以前在 Golang 中一样使用它。

You can refer to @blackgreen's answer for a simple and temporary way.您可以参考@blackgreen 的答案以获取简单而临时的方法。

Details细节

Kubernetes uses an overlay network, Flannel in most cases, the communication inside the cluster is set up by default, and it is isolated from outside. Kubernetes使用overlay网络, Flannel大部分情况下,集群内部的通信默认设置,与外部隔离。

Of cause there are some projects like Calico can connect the inside and outside network, but it another story.当然有一些像Calico这样的项目可以连接内部和外部网络,但它是另一回事。

There are several solutions if we want to access the pods from outside.如果我们想从外部访问 Pod,有几种解决方案。

kubectl kubectl

https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/ https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/

Kubectl uses socat to create a tunnel and forward one or more local ports to a pod. Kubectl 使用socat创建隧道并将一个或多个本地端口转发到 pod。

The port forward will end when you stop the command, but it is a good choice if you want to temporarily access the pod for debugging.停止命令时端口转发将结束,但如果您想临时访问 pod 进行调试,这是一个不错的选择。

kubectl port-forward redis-master-765d459796-258hz 7000:6379

Service服务

https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types

Service is an abstract way to expose an application running on a set of Pods as a network service. Service是一种将运行在一组 Pod 上的应用程序公开为网络服务的抽象方式。

when accessing from outside, there are kinds of Service to use, NodePort may be a good choice in most case.当从外部访问时,可以使用多种Service ,在大多数情况下, NodePort可能是一个不错的选择。

It uses iptables or ipvs to create a Port Forward in all Nodes forwarding network to the target port.它使用iptablesipvs在所有节点转发网络中创建一个Port Forward转发到目标端口。

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app: MyApp
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

Ingress入口

https://kubernetes.io/docs/concepts/services-networking/ingress/ https://kubernetes.io/docs/concepts/services-networking/ingress/

Ingress is a layer 7 proxy managing external network access to Service , as gRPC is also built on top of HTTP/2 , Ingress work perfectly . Ingress 是一个管理外部网络访问Service的第 7 层代理,因为gRPC也是建立在HTTP/2之上的,因此Ingress 可以完美地工作

Ingress should be the choice if you are exposing a Production Application.如果您要公开生产应用程序,则应选择 Ingress。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM