gpt4 book ai didi

node.js - 使用 kubernetes 部署时无法连接 gRPC

转载 作者:行者123 更新时间:2023-12-05 03:47:12 26 4
gpt4 key购买 nike

我正在尝试使用 kubernetes 部署 gRPC 服务器,并在集群外部连接到它。服务器相关部分:

function main() {
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;
var server = new grpc.Server();
server.addService(hello_proto.Greeter.service, {sayHello: sayHello});
const url = '0.0.0.0:50051'
server.bindAsync(url, grpc.ServerCredentials.createInsecure(), () => {
server.start();
console.log("Started server! on " + url);
});
}

function sayHello(call, callback) {
console.log('Hello request');
callback(null, {message: 'Hello ' + call.request.name + ' from ' + require('os').hostname()});
}

这是客户端的相关部分:

function main() {
var target = '0.0.0.0:50051';
let pkg = grpc.loadPackageDefinition(packageDefinition);
let Greeter = pkg.helloworld["Greeter"];
var client = new Greeter(target,grpc.credentials.createInsecure());
var user = "client";

client.sayHello({name: user}, function(err, response) {
console.log('Greeting:', response.message);
});
}

当我使用 nodeJS 手动运行它们时,以及当我在 docker 容器中运行服务器时(客户端仍然使用没有容器的 Node 运行)它工作得很好。

带有命令的 docker 文件:docker run -it -p 50051:50051 helloapp

FROM node:carbon

# Create app directory
WORKDIR /usr/src/appnpm

COPY package.json .
COPY package-lock.json .

RUN npm install

COPY . .

CMD npm start

但是,当我使用 kubernetes 部署服务器时(同样,客户端未在容器中运行)我无法连接。

yaml文件如下:

apiVersion: apps/v1
kind: Deployment
metadata:
name: helloapp
spec:
replicas: 1
selector:
matchLabels:
app: helloapp
strategy: {}
template:
metadata:
labels:
app: helloapp
spec:
containers:
image: isolatedsushi/helloapp
name: helloapp
ports:
- containerPort: 50051
name: helloapp
resources: {}
status: {}
---
apiVersion: v1
kind: Service
metadata:
name: helloservice
spec:
selector:
app: helloapp
ports:
- name: grpc
port: 50051
targetPort: 50051

部署和服务启动正常

kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
helloservice ClusterIP 10.105.11.22 <none> 50051/TCP 17s


kubectl get pods
NAME READY STATUS RESTARTS AGE
helloapp-dbdfffb-brvdn 1/1 Running 0 45s

但是当我运行客户端时,它无法连接到服务器。

知道我做错了什么吗?

最佳答案

如评论所述


服务类型

如果您将服务公开为ClusterIP,它仅在集群内部可见,如果您不想在外部公开您的服务,则必须使用nodePort负载均衡器

Publishing Services (ServiceTypes)

For some parts of your application (for example, frontends) you may want to expose a Service onto an external IP address, that's outside of your cluster.Kubernetes ServiceTypes allow you to specify what kind of Service you want. The default is ClusterIP.

Type values and their behaviors are:

ClusterIP: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the cluster. This is the default ServiceType.

NodePort: Exposes the Service on each Node's IP at a static port (the NodePort). A ClusterIP Service, to which the NodePort Service routes, is automatically created. You'll be able to contact the NodePort Service, from outside the cluster, by requesting :.

LoadBalancer: Exposes the Service externally using a cloud provider's load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.

ExternalName: Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up.

相关documentation关于那个。


迷你库

对于 minikube,您可以通过 minikube service 命令实现。

documentation关于 minikube 服务,有一个 example .


grpc http/https

如前所述here通过@murgatroid99

The gRPC library does not recognize the https:// scheme for addresses, so that target name will cause it to try to resolve the wrong name. You should instead use grpc-server-xxx.com:9090 or dns:grpc-server-xxx.com:9090 or dns:///grpc-server-xxx.com:9090. More detailed information about how gRPC interprets channel target names can be found in this documentation page.

因为它不识别 https,我假设它对 http 是一样的,所以这是不可能的。


kubectl 端口转发

另外正如@IsolatedSushi 提到的

It also works when I portforward with the command kubectl -n hellospace port-forward svc/helloservice 8080:50051

如前所述here

Kubectl port-forward allows you to access and interact with internal Kubernetes cluster processes from your localhost. You can use this method to investigate issues and adjust your services locally without the need to expose them beforehand.

有一个example在文档中。

关于node.js - 使用 kubernetes 部署时无法连接 gRPC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64971822/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com