Kubernetes, NodeJS and gRPC

269 views
Skip to first unread message

Sharan Karthikeyan

unread,
Jan 16, 2023, 12:21:44 AM1/16/23
to grpc.io
I have created a headless service in kubernetes for gRPC server. But, I'm not able to connect that server in NodeJS client the connection url like this "bbl-org-server.default.svc.cluster.local" -- "<serviceName>.<namespace>.svc.cluster.local". Please help me.

Regards,
Sharan

Richard Belleville

unread,
Jan 19, 2023, 5:51:39 PM1/19/23
to grpc.io
Can you please provide more context? Your headless service should create a DNS entry in kubedns as you've described and the kubernetes NodeJS client should be able to resolve that DNS entry. Exactly what is the issue you are experiencing? 

Sharan Karthikeyan

unread,
Jan 30, 2023, 11:37:40 AM1/30/23
to grpc.io
Actually, I found the answer. I have used the localhost socket in the gRPC server, I changed it 0.0.0.0:PORT, and then it worked.
Thank you for the response.

Keyur Patel

unread,
Feb 6, 2023, 4:39:43 AM2/6/23
to grpc.io
Hi @sharan,
Can you share your yaml files example? I am facing the same issue.

Keyur Patel

unread,
Feb 6, 2023, 4:55:26 AM2/6/23
to grpc.io
// Server.yaml

Screenshot 2023-02-06 at 3.21.21 PM.png

// Client.yaml

Screenshot 2023-02-06 at 3.22.59 PM.png

// Service.yaml

Screenshot 2023-02-06 at 3.24.50 PM.png

Sharan Karthikeyan

unread,
Feb 6, 2023, 5:07:06 AM2/6/23
to grpc.io
Can you please share your client listening port and IP address?
If you are using 127.0.0.1 (localhost), then change it to 0.0.0.0:PORT
Message has been deleted

Sharan Karthikeyan

unread,
Feb 6, 2023, 5:11:28 AM2/6/23
to grpc.io
Make sure gRPC server running on 0.0.0.0:PORT, so you have to make the connection to that socket from your client side. If you are using localhost:PORT in client side, it is pointing to the current pod's (client side - local machine) socket, hence you need change 0.0.0.0:PORT in both gRPC side (server) as well as your client side (Node or Apache or Go or etc).

Keyur Patel

unread,
Feb 6, 2023, 5:13:30 AM2/6/23
to grpc.io
I am using this on the client side: "<serviceName>.<namespace>.svc.cluster.local:80". 

Sharan Karthikeyan

unread,
Feb 6, 2023, 5:43:44 AM2/6/23
to grpc.io
I will give a step-by-step approach to solving this problem.
1. You have a gRPC server. Make sure it is running on the "0.0.0.0:PORT" socket. And, make a cluster IP service to this gRPC pod. Assume we named it "grpc_service".
Screenshot 2023-02-06 at 4.06.22 PM.png
2. Assume you have Node JS (REST API - CLIENT) listening on the Kubernetes service of your gRPC. ``config.OG_GRPC_SERVER = "grpc_service:PORT"
Screenshot 2023-02-06 at 4.08.12 PM.png

If you do this, you will not need the "<serviceName>.<namespace>.svc.cluster.local:80" connection URL. I can you are facing an issue with connection in client.

Sharan Karthikeyan

unread,
Feb 6, 2023, 5:55:01 AM2/6/23
to grpc.io
Please share your client connection file and the gRPC server connection file.

Keyur Patel

unread,
Feb 6, 2023, 6:05:03 AM2/6/23
to grpc.io
Hi @sharan,
Thanks for sharing the details. I am using the same Nodejs server & client. Let me try this client configuration. 
Are you creating a headless service and adding a DNS entry in kubedns?


Here is my server & client files:

// Server.ts
...imports
const gRPCPort = config.GRPC_SERVER_PORT;
const options: protoLoader.Options = {
keepCase: true,
longs: String,
enums: String,
defaults: true,
arrays: true,
oneofs: true,
};
const PROTO_FILE = '../proto/xyz.service.proto';
const packageDef = protoLoader.loadSync(
path.resolve(__dirname, PROTO_FILE),
options,
);
const proto = loadPackageDefinition(packageDef) as unknown as ProtoGrpcType;
const server = new Server();
// XYZ Services
server.addService(proto.xyz.xyzService.service, {
GetABC: (req, res) => getABCHandler(req, res),
} as XyzServiceHandlers);
server.bindAsync(
`0.0.0.0:${gRPCPort}`,
ServerCredentials.createInsecure(),
(err, port) => {
if (err) {
throw err;
}
console.log(`gRPC server listening on ${port}`);
server.start();
},
);

// Client.ts
const gRPCPort = config.GRPC_SERVER_PORT;
export const client = new proto.xyz.xyzService(
`0.0.0.0:${gRPCPort}`,
credentials.createInsecure()
);

const deadline = new Date();
deadline.setSeconds(deadline.getSeconds() + 1);
client.waitForReady(deadline, (err) => {
if (err) {
console.error(err);
return;
}
onClientReady();
});

function onClientReady() {
console.log("? gRPC Client is ready");
}

Keyur Patel

unread,
Feb 6, 2023, 6:26:33 AM2/6/23
to grpc.io
Hi @sharan,
Thank you so much man. "service_name:PORT" worked. 

Sharan Karthikeyan

unread,
Feb 6, 2023, 6:49:28 AM2/6/23
to grpc.io
Great!!
Reply all
Reply to author
Forward
0 new messages