You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Kubernetes Metacontroller中将service-per-pod扩展支持多端口

Metacontroller service-per-pod 多端口适配方案

你只需要修改sync-service-per-pod.jsonnet配置即可完成适配,操作步骤如下:

  • 替换原配置中硬编码的单端口定义,改为多端口数组。每个端口必须配置唯一的name属性(K8s Service强制要求),同时明确指定protocol区分TCP/UDP,示例如下:
// 可根据需求自定义端口列表
local ports = [
  {name: "http", port: 80, targetPort: 80, protocol: "TCP"},
  {name: "udp-service", port: 30000, targetPort: 30000, protocol: "UDP"}
];
  • 把Service资源spec.ports字段的值替换为上面定义的多端口数组,替换原来的单端口配置。
  • 如果需要动态适配不同StatefulSet的端口需求,可以增加逻辑从StatefulSet的注解中读取端口配置,不需要每次修改jsonnet文件,示例逻辑如下:
// 优先读取StatefulSet注解中配置的端口,无配置则走默认值
local customPorts = parent.metadata.annotations["service-per-pod/ports"] || "[]";
local ports = if std.length(std.parseJson(customPorts)) > 0 then std.parseJson(customPorts) else [
  {name: "http", port: 80, targetPort: 80, protocol: "TCP"}
];
  • 配置修改完成后重新应用Metacontroller相关资源,触发同步逻辑后,新生成的Pod对应Service会自动携带多端口配置,存量旧Service也会自动更新端口配置。

注意:K8s 1.20及以上版本才完整支持同一个Service同时配置TCP和UDP端口,低于该版本的集群需要单独拆分TCP和UDP端口到不同Service。

内容的提问来源于stack exchange,提问作者Vignesh Palanisamy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 19:15:00