如何自定义由使用 AWS NLB for TCP 服务的 Kubernetes LoadBalancer 类型服务创建的安全组入口规则

Seg*_*ult 5 amazon-web-services kubernetes amazon-eks

我有一个 TCP 服务,该服务通过 AWS EKS 集群上的 Kubernetes 部署运行,并通过使用以下定义的 LoadBalancer 类型的服务公开到互联网

\n\n
apiVersion: v1\nkind: Service\nmetadata:\n  annotations:\n    service.beta.kubernetes.io/aws-load-balancer-type: nlb\n    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp\n  name: tcpservice\nspec:\n  selector:\n    app: tcpapp\n  type: LoadBalancer\n  ports:\n  - port: 4453\n    targetPort: 4453\n    name: tcpport\n
Run Code Online (Sandbox Code Playgroud)\n\n

由于负载均衡器类型是 NLB,因此必须在应用于节点本身的安全组上显式允许入口流量。安全组是这样创建的:

\n\n
\xe2\x9c\x94 ~$ aws ec2 describe-security-groups --group-ids sg-2645567125762c6e2 | jq \'.SecurityGroups[0].IpPermissions[0]\'\n{\n  "FromPort": 32163,\n  "IpProtocol": "tcp",\n  "IpRanges": [\n    {\n      "CidrIp": "10.20.0.0/20",\n      "Description": "kubernetes.io/rule/nlb/health=afd5427b6058811ea989512627425a2e"\n    },\n    {\n      "CidrIp": "0.0.0.0/0",\n      "Description": "kubernetes.io/rule/nlb/client=afd5427b6058811ea989512627425a2e"\n    }\n  ],\n  "Ipv6Ranges": [],\n  "PrefixListIds": [],\n  "ToPort": 32163,\n  "UserIdGroupPairs": []\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

所以现在我需要将“0.0.0.0/0”中的CidrIp更改为不同的块。如何使用 kubernetes 清单执行此操作?我查看了 NetworkPolicy 和 Calico 文档,但这控制的是 pod 的流量,而不是服务的流量。我可以使用 AWS API 或手动更改它,但在重新部署服务时这些更改会丢失。

\n

ili*_*efa 5

您需要在服务清单中添加 loadBalancerSourceRanges 参数。

\n\n

来自文档:

\n\n

为了限制哪些客户端 IP\xe2\x80\x99s 可以访问网络负载均衡器,请指定 loadBalancerSourceRanges。

\n\n
spec:\n  loadBalancerSourceRanges:\n  - "143.231.0.0/16"\n
Run Code Online (Sandbox Code Playgroud)\n\n

https://v1-13.docs.kubernetes.io/docs/concepts/services-networking/service/

\n\n

代码的实现方式可以在这里找到:

\n\n

https://github.com/kubernetes/kubernetes/blob/9d6ebf6c78f406d8639aae189901e47562418071/pkg/api/service/util.go

\n