我有两个命名空间:prod
和default
. 我想禁用对这些命名空间内资源的访问(来自default
NS 的资源无法访问来自 的资源,来自的prod
资源prod
无法访问来自 的资源default
)但允许有机会访问这些资源以获取外部流量(入口)。
# namespaces.yaml
---
kind: Namespace
apiVersion: v1
metadata:
name: prod
labels:
tier: prod
---
kind: Namespace
apiVersion: v1
metadata:
name: default
labels:
tier: infra
# network-policies.yaml
---
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: network
namespace: prod
spec:
podSelector: {}
ingress:
- from:
- podSelector: {}
---
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: network
namespace: default
spec:
podSelector: {}
ingress:
- from:
- namespaceSelector:
matchLabels:
tier: dev
- namespaceSelector:
matchLabels:
tier: rc
# services.yaml
---
apiVersion: v1
kind: Service
metadata:
name: {{ include "conference.appService" . }}
labels:
app: {{ include "conference.name" . }}
release: {{ .Release.Name }}
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: http
protocol: TCP
selector:
app: {{ include "conference.name" . }}
release: {{ .Release.Name }}
role: app
来自prod
的 Pod 可以访问给定命名空间内的其他 Pod。来自default
的 Pod 无法访问内部的 pod prod
。
当我尝试从浏览器访问该服务时,它被阻止了。当我尝试使用端口转发到内部服务时prod
- 一切正常。