gpt4 book ai didi

kubernetes - Kubernetes网络策略与外部服务

转载 作者:行者123 更新时间:2023-12-02 12:34:42 27 4
gpt4 key购买 nike

问题:

我正在尝试使我的API能够进入我的外部托管数据库,同时要求将其他所有内容都列入白名单。

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: server-allow-external
spec:
podSelector:
matchLabels:
component: api
ingress:
- from:
- namespaceSelector: {}
- podSelector:
matchLabels: frontend
- ipBlock:
cidr: 172.17.0.0/16

我在这里尝试了各种组合。网络插件策略,将外部数据库映射到基于集群的服务等,但我还没有走运。

注意:是的,我允许所有 namespace 通信。

上面的代码是我从逻辑上解决问题的地方……但是仍然无法正常工作。如果这可能会产生影响,我还可以在此之外安装一个负载均衡器?

代码:

我有一个依赖于后端API的前端(均在默认 namespace 内)。然后是一个外部数据库。我已经实施了默认的拒绝政策:
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: default-deny-all
namespace: default
spec:
podSelector: {}
ingress: []

然后,我允许从Web访问前端:
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: allow-external
spec:
podSelector:
matchLabels:
component: frontend
ingress:
- from: []

最佳答案

为外部服务器配置网络策略。
您必须修复定义入口的部分。

只需将小节添加到matchLabels-
component: frontend:

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: server-allow-external
spec:
podSelector:
matchLabels:
component: api
ingress:
- from:
- namespaceSelector: {}
- podSelector:
matchLabels:
component: frontend
- ipBlock:
cidr: 172.17.0.0/16

希望对您有所帮助。

关于kubernetes - Kubernetes网络策略与外部服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56608052/

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