@citymallservices/k8s-grpc-resolver

中文文档
1.1.12发布于 2026/09/29ISCTypeScriptCommonJS近 30 天下载 1776

A custom gRPC resolver for Kubernetes services that enables service discovery in Kubernetes clusters. This resolver allows gRPC clients to automatically discover and connect to services running in Kubernetes without hardcoding IP addresses.

原始使用文档
文档目录17 节
  1. Overview
  2. Features
  3. Installation
  4. Golang
  5. Node.js
  6. Usage
  7. Golang
  8. Node.js
  9. How It Works
  10. Golang Implementation
  11. Node.js Implementation
  12. Requirements
  13. Golang
  14. Node.js
  15. Publishing
  16. License
  17. Author

k8s-grpc-resolver

A custom gRPC resolver for Kubernetes services that enables service discovery in Kubernetes clusters. This resolver allows gRPC clients to automatically discover and connect to services running in Kubernetes without hardcoding IP addresses.

Overview

When running microservices in Kubernetes, service discovery can be challenging, especially for gRPC services. This library provides a custom resolver that:

Watches Kubernetes Endpoints resources to track service IP addresses Automatically updates gRPC client connections when endpoints change Handles failover and load balancing across multiple service instances Works seamlessly within Kubernetes clusters

The resolver is available in two implementations:

  • Golang implementation using the official gRPC resolver interface
  • Node.js implementation using @grpc/grpc-js

Features

  • Dynamic Service Discovery: Automatically discovers service endpoints in Kubernetes
  • Live Updates: Watches for changes in service endpoints and updates connections in real-time
  • Fallback Mechanism: Node.js implementation falls back to DNS resolution if Kubernetes API is unavailable
  • Error Handling: Implements backoff strategies for reconnection attempts
  • Simple Integration: Easy to integrate with existing gRPC clients

Installation

Golang

go get github.com/city-mall/k8s-grpc-resolver/golang

Node.js

npm install @citymallservices/k8s-grpc-resolver

Usage

Golang

import (
    "google.golang.org/grpc"
    k8sresolver "github.com/city-mall/k8s-grpc-resolver/golang"
)

func main() {
    // Register the resolver
    resolver := k8sresolver.Builder()

    // Use the resolver in your gRPC client
    // Format: k8s://<namespace>/<service-name>:<port>
    conn, err := grpc.Dial(
        "k8s://default/my-service:8080",
        grpc.WithResolvers(resolver),
        grpc.WithInsecure(),
    )
    if err != nil {
        // Handle error
    }
    defer conn.Close()

    // Use the connection with your gRPC client
    client := pb.NewMyServiceClient(conn)
    // ...
}

For services with the standard Kubernetes DNS format (service.namespace.svc.cluster.local), you can use the Try helper function:

import (
    k8sresolver "github.com/city-mall/k8s-grpc-resolver/golang"
)

// Convert standard Kubernetes DNS to resolver format
address := k8sresolver.Try("my-service.default.svc.cluster.local:8080")
// address will be "k8s://default/my-service:8080"

Node.js

import { setup } from "@citymallservices/k8s-grpc-resolver";
import { credentials, createClient } from "@grpc/grpc-js";

// Register the resolver and get the formatted address
const address = setup("my-service.default:8080");

// Create a client with the address
const client = createClient(address, credentials.createInsecure());

// Use the client as normal

How It Works

Golang Implementation

The Golang implementation:

  1. Registers a custom resolver with the k8s:// scheme
  2. Uses the Kubernetes API to watch Endpoints resources for the specified service
  3. Extracts IP addresses from the Endpoints resource
  4. Updates the gRPC client connection with the new addresses

Node.js Implementation

The Node.js implementation:

  1. Registers a custom resolver with the k8s:// scheme
  2. Initially uses DNS resolution while attempting to connect to the Kubernetes API
  3. Once connected, watches Endpoints resources for the specified service
  4. Updates the gRPC client connection with the new addresses
  5. Implements backoff strategies for reconnection attempts

Requirements

Golang

  • Kubernetes cluster with API access
  • Proper RBAC permissions to read Endpoints resources
  • Go 1.15 or later

Node.js

  • Kubernetes cluster with API access
  • Proper RBAC permissions to read Endpoints resources
  • Node.js 14 or later
  • @grpc/grpc-js 1.13.4 or later
  • @kubernetes/client-node 1.3.0 or later

Publishing

To publish a new version of the Node.js package:

  1. Update the version in package.json
  2. Run the following command:
npm publish

License

ISC

Author

Arkadev Banerjee


This project is maintained by CityMall Services.

版本与发布记录

@citymallservices/k8s-grpc-resolver 共发布 21 个版本,首次发布于 2023/06/29;过去 12 个月发布了 5 个版本。

版本发布日期距今
1.1.12latest2026/09/291 个月前
1.1.112026/09/151 个月前
1.1.102026/09/131 个月前
1.1.92026/09/022 个月前
1.1.82026/04/037 个月前
1.1.72025/06/301.4 年前

关于 @citymallservices/k8s-grpc-resolver 的常见问题

@citymallservices/k8s-grpc-resolver 是什么?

A custom gRPC resolver for Kubernetes services that enables service discovery in Kubernetes clusters. This resolver allows gRPC clients to automatically discover and connect to services running in Kubernetes without hardcoding IP addresses.

如何安装 @citymallservices/k8s-grpc-resolver?

在项目目录运行 npm install @citymallservices/k8s-grpc-resolver,或使用 pnpm add / yarn add / bun add。

@citymallservices/k8s-grpc-resolver 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@citymallservices/k8s-grpc-resolver 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@citymallservices/k8s-grpc-resolver 可以商用吗?

@citymallservices/k8s-grpc-resolver 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@citymallservices/k8s-grpc-resolver 的下载量有多少?

根据 npm 官方下载统计,@citymallservices/k8s-grpc-resolver 在 2026/10/10 – 2026/11/08 这 30 天内被下载 1,776 次,日均约 59 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@citymallservices/k8s-grpc-resolver 还在维护吗?

最新版本 1.1.12 发布于 2026/09/29(1 个月前);至今共发布 21 个版本,其中过去 12 个月发布了 5 个;首次发布于 2023/06/29。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。