编程实验
Kubernetes的一个重要特性就是要把不同node节点的pod(container)连接起来,无视物理节点的限制。但是在某些应用环境中,比如公有云,不同租户的pod不应该互通,这个时候就需要网络隔离。幸好,Kubernetes提供了NetworkPolicy,支持按Namespace级别的网络隔离。
使用NetworkPolicy需要kubernetes1.8以及calico2.6.2。
实验步骤 1.创建两个namespace: ns-calico1、ns-calico2 2.分别在两个namespace下创建应用: ns-calico1下面创建三个应用
ns-calico1下的calico1-busybox应用:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: calico1-busybox namespace: ns-calico1 spec: replicas: 1 template: metadata: labels: user: calico1 app: calico1-busybox spec: containers: -name:calico1-busyboximage: busybox imagePullPolicy: IfNotPresent command: -sleep-"3600"
ns-calico1下的calico1-nginx应用:
apiVersion: extensions/v1beta1 kind: Deployment metadata:name: calico1-nginxnamespace: ns-calico1spec:replicas: 1template:metadata:labels:user: calico1app: calico1-nginxspec:containers:- name: calico1-nginximage: nginxports:- containerPort: 80 ---apiVersion: v1 kind: Service metadata:name: calico1-nginxnamespace: ns-calico1labels:user: calico1spec:selector:app: calico1-nginxports:- port: 80
ns-calico1下的calico1-nginx2应用:
apiVersion: extensions/v1beta1 kind: Deployment metadata:name: calico1-nginx2namespace: ns-calico1spec:replicas: 1template:metadata:labels:user: calico1app: calico1-nginx2spec:containers:- name: calico1-nginx2image: nginxports:- containerPort: 80 ---apiVersion: v1 kind: Service metadata:name: calico1-nginx2namespace: ns-calico1labels:user: calico1spec:selector:app: calico1-nginx2ports:- port: 80ns-
ns-calico2下的calico2-busybox应用:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: calico2-busybox namespace: ns-calico2 spec: replicas: 1 template: metadata: labels: user: calico2 app: calico2-busybox spec: containers: -name:calico2-busyboximage: busybox imagePullPolicy: IfNotPresent command: -sleep-"3600"
测试结果
kubectl exec-itcalico1-busybox-5f4d7d5f4-n7qjg-nns-calico1--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) 通 kubectl exec-itcalico2-busybox-6dd875d4f-8747j-nns-calico2--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) 通
结论:加策略前,同namespace以及跨namespace的应用之间都是互通的
策略文件:
kind:NetworkPolicyapiVersion:networking.k8s.io/v1metadata:name: access-nginx namespace: ns-calico1spec:podSelector: matchLabels: app: calico1-nginx ingress: - from: - podSelector: matchLabels: access:"true"
说明:该策略只允许有access: “true”标签的应用访问ns-calico1下面有app: calico1-nginx标签的应用,即我们的测试应用
测试结果
kubectl exec-itcalico1-busybox-5f4d7d5f4-n7qjg-nns-calico1--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) wget: download timed out 不通 kubectl exec-itcalico2-busybox-6dd875d4f-8747j-nns-calico2--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) wget: download timed out 不通 kubectl exec-itcalico1-busybox-5f4d7d5f4-n7qjg-nns-calico1--wget--spider--timeout=1calico1-nginx2.ns-calico1Connectingtocalico1-nginx2.ns-calico1(10.233.23.44:80) 通 kubectl exec-itcalico2-busybox-6dd875d4f-8747j-nns-calico2--wget--spider--timeout=1calico1-nginx2.ns-calico1Connectingtocalico1-nginx2.ns-calico1(10.233.23.44:80) 通
结论:
1.加上访问策略后,同namespace及跨namespace的应用都无法访问目标应用
2.对某个应用加上访问策略,该策略并不会影响对同namespace下的其他应用的访问
给两个namespace下的busybox应用都添加access: “true”的label
测试结果:
kubectl exec-itcalico1-busybox-5f4d7d5f4-n7qjg-nns-calico1--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) 通 kubectl exec-itcalico2-busybox-6dd875d4f-8747j-nns-calico2--wget--spider--timeout=1calico1-nginx.ns-calico1Connectingtocalico1-nginx.ns-calico1(10.233.30.47:80) wget: download timed out 不通
结论:只有同namespace下的应用打上对应标签才通,跨namespace的应用打上标签也无法访问,说明通策略只在同一个namespace下起作用。
全部0条评论
快来发表一下你的评论吧 !