文章目录
参考地址
网络策略 https://kubernetes.io/zh-cn/docs/concepts/services-networking/network-policies/
网络策略字段 https://kubernetes.io/zh-cn/docs/reference/kubernetes-api/policy-resources/network-policy-v1/
一、TASK
Solve this question on: ssh cks3477
There is a metadata service available at http://192.168.100.21:32000 on which Nodes can reach sensitive data, like cloud credentials for initialisation. By default, all Pods in the cluster also have access to this endpoint. The DevSecOps team has asked you to restrict access to this metadata server.
In Namespace metadata-access:
Create a NetworkPolicy named metadata-deny which prevents egress to 192.168.100.21 for all Pods but still allows access to everything else
Create a NetworkPolicy named metadata-allow which allows Pods having label role: metadata-accessor to access endpoint 192.168.100.21
There are existing Pods in the target Namespace with which you can test your policies, but don't change their labels.
中译
在以下位置解决此问题:ssh cks3477
有一个元数据服务可用http://192.168.100.21:32000, 节点可以通过该服务访问敏感数据,例如用于初始化的云凭证。默认情况下,集群中的所有 Pod 也可以访问此终端节点。DevSecOps 团队已要求您限制对此元数据服务器的访问。
1、在 命名空间metadata-access中 :创建一个 名为metadata-deny 的 NetworkPolicy,该策略可以防止该空间下所有 Pod 访问元数据地址 egress to 192.168.100.21 ,但仍然允许访问其他所有内容
2、创建一个 名为 NetworkPolicy,允许 具有 label role: metadata-accessor 的 Pod 访问 到元数据服务器endpoint 192.168.100.21
目标 Namespace 中存在现有的 Pod,您可以使用这些 Pod 来测试您的策略,但不要更改它们的标签。
二、问题解决过程
1.问题一解题
过程如下(示例):
yaml
#按要求连接对应的集群
candidate@terminal:~$ ssh cks3477
#切换到root用户下,防止普通用户操作写入文件没权限
candidate@cks3477:~$ sudo -i
candidate@cks3477:~# vim netDeny.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: metadata-deny
namespace: metadata-access
spec:
podSelector: {}
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 0.0.0.0/0
except:
- 192.168.100.21/32
candidate@cks3477:~# kubectl apply -f netDeny.yaml
2.读入数据
过程如下(示例):
yaml
#按要求连接对应的集群
candidate@terminal:~$ ssh cks3477
#切换到root用户下,防止普通用户操作写入文件没权限
candidate@cks3477:~$ sudo -i
candidate@cks3477:~# vim netAllow.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: metadata-allow
namespace: metadata-accessor
spec:
podSelector:
matchLabels:
role: metadata-accessor
policyTypes:
- Egress
egress:
- to:
- ipBlock:
cidr: 192.168.100.21/32
candidate@cks3477:~# kubectl apply -f netAllow.yaml