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

我有一个在端口9000上运行,在端口9080上运行。我可以使用以下链接向postman发出请求:''http:// cluster1.example.com/api/v1/namespaces/default/services/my-service:9080/proxygRPC servergRPC-gateway


如何使用 ?gRPC clientgrpc.Dial()


例:


conn, err := grpc.Dial(...?, grpc.WithInsecure())

if err != nil {

    panic(err)

}


婷婷同学_
浏览 209回答 2
2回答

凤凰求蛊

您应该能够通过端口转发从本地连接到 k8s 集群中的服务:kubectl port-forward --context <mycontext> -n <mynamespace> svc/my-service 9000:9000然后,您只需使用localhost将gRPC目标传递到没有方案:Dialconn, err := grpc.Dial("localhost:9000", grpc.WithInsecure())if err != nil {&nbsp; &nbsp; panic(err)}我可能会说显而易见的,但当然服务器也必须在不安全的模式下启动(没有凭据),否则您可能会得到响应代码。Unavailable

慕后森

简短的回答:这基本上不是一个Golang问题,而是一个Kubernetes问题。你必须设置 Kubernetes 部分,并像以前一样在 Golang 中使用它。您可以参考@blackgreen的答案,以获得简单而临时的方法。详Kubernetes使用覆盖网络,在大多数情况下是Flannel,默认情况下,集群内部的通信是设置的,并且它与外部隔离。因为有一些像Calico这样的项目可以连接内部和外部网络,但这是另一回事。如果我们想从外部访问 Pod,有几种解决方案。kubectlhttps://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/Kubectl 用于创建隧道并将一个或多个本地端口转发到 Pod。socat当您停止命令时,端口转发将结束,但如果要临时访问 Pod 进行调试,这是一个不错的选择。kubectl port-forward redis-master-765d459796-258hz 7000:6379服务https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-typesService是一种抽象的方式,用于将一组 Pod 上运行的应用程序公开为网络服务。当从外部访问时,有多种使用方式,NodePort在大多数情况下可能是一个不错的选择。Service它使用 或 在所有节点中创建将网络转发到目标端口。iptablesipvsPort ForwardapiVersion: v1kind: Servicemetadata:&nbsp; name: my-servicespec:&nbsp; type: NodePort&nbsp; selector:&nbsp; &nbsp; app: MyApp&nbsp; ports:&nbsp; &nbsp; &nbsp; # By default and for convenience, the `targetPort` is set to the same value as the `port` field.&nbsp; &nbsp; - port: 80&nbsp; &nbsp; &nbsp; targetPort: 80&nbsp; &nbsp; &nbsp; # Optional field&nbsp; &nbsp; &nbsp; # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)&nbsp; &nbsp; &nbsp; nodePort: 30007入口https://kubernetes.io/docs/concepts/services-networking/ingress/Ingress是一个管理外部网络访问的第7层代理,也是建立在之上的,Ingress工作完美。ServicegRPCHTTP/2如果要公开生产应用程序,则入口应该是选择。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go