app=web label and reject requests from other Pods. This is commonly used to control the access between resources in a project.

app=web label and start the service.kubectl run --generator=run-pod/v1 apiserver --image=nginx --labels app=web --expose --port 80
[root@VM-0-11-centos ~]# kubectl get pods webNAME READY STATUS RESTARTS AGEweb 1/1 Running 0 4s
[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 16s
[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testwebIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web, use custom inbound rules, configure the source type as the Pod, and specify the Pod with the app=web label as the allowed inbound source. The configuration is the same for outbound rules as shown below:

app=web.app=web label can access the web service.app=web label in the current namespace can send requests to the web service.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb --labels app=web -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web label in the current namespace cannot send requests to the web service.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb --labels app2=web2 -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217wget: can't connect to remote host (172.18.255.217): Connection refused
app=web label in other namespaces can send requests to the web service.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb --labels app=web -n secondary -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
kubectl delete pod webkubectl delete service webDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case1`.)
app=web label. This doesn't affect outbound requests.

app=web label and start the service.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --labels app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get pods webNAME READY STATUS RESTARTS AGEweb 1/1 Running 0 4s[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 16s
[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testwebIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web and set to reject all inbound requests as shown below:

app=web label cannot be accessed from any external sources.kubectl run --rm -i -t --image=alpine testweb -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://webwget: can't connect to remote host (172.18.255.217): Connection refused
kubectl delete pod webkubectl delete service webDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case2`.)
app=web label and allow requests only from the current namespace as shown below:

app=web label and start the service.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --labels app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get pods webNAME READY STATUS RESTARTS AGEweb 1/1 Running 0 5s[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 13s
app=web label by default.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb --labels app=web -n secondary -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web, use custom inbound rules, configure the source type as the Pod, leave the namespace empty, and specify any Pod as the allowed inbound source. The configuration is the same for outbound rules as shown below:

app=web label can be accessed from the current namespace.[root@VM-0-11-centos ~]# kubectl run testweb --namespace=default --rm -it --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web label cannot be accessed from other namespaces.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb --labels app=web -n secondary -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.defaultwget: can't connect to remote host (172.18.255.217): Connection refused
kubectl delete pod webkubectl delete service webDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case3`.)
app=web label in the namespace.

app=web label and another with the app=web1 label and start the services.app=web label.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --namespace default --labels=app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 5s
app=web1 label.[root@VM-0-11-centos ~]# kubectl run web1 --image=nginx --namespace default --labels=app=web1 --expose --port 80service/web1 createdpod/web1 created[root@VM-0-11-centos ~]# kubectl get svc web1NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb1 ClusterIP 172.18.255.39 <none> 80/TCP 7s
app=web and app=web1 labels can be accessed by default.app=web label can be accessed.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.217<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web1 label can be accessed.[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.255.39<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web label, specifically, by specifying the current namespace (default) and the app=web label.

default namespace, only the Pod with the app=web label can be accessed from other namespaces, and other Pods (such as that with the app=web1 label) cannot.app=web label can be accessed from other namespaces.[root@VM-0-11-centos ~]# kubectl create namespace secondary[root@VM-0-11-centos ~]# kubectl run testweb --namespace=secondary --rm -i -t --image=alpine -- sh/ # wget -qO- --timeout=2 http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web1 label cannot be accessed from other namespaces.[root@VM-0-11-centos ~]# kubectl create namespace secondary[root@VM-0-11-centos ~]# kubectl run testweb --namespace=secondary --rm -i -t --image=alpine -- sh/ # wget -qO- --timeout=2 http://web1.defaultwget: can't connect to remote host (172.18.255.39): Connection refused
kubectl delete pod web -n defaultkubectl delete service web -n defaultkubectl delete namespace secondaryDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case4`.)
app=web label only from the specified namespace.

app=web label and start the service.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --namespace default --labels=app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 5s
dev and production and verify that the web application can be accessed from all namespaces by default.[root@VM-0-11-centos ~]# kubectl create namespace devnamespace/dev created[root@VM-0-11-centos ~]# kubectl label namespace/dev env=devnamespace/dev labeled[root@VM-0-11-centos ~]# kubectl create namespace productionnamespace/production created[root@VM-0-11-centos ~]# kubectl label namespace/production env=productionnamespace/production labeled[root@VM-0-11-centos ~]#
dev namespace.kubectl run testweb --namespace=dev --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
production namespace.kubectl run testweb --namespace=production --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web, configure the source type as the namespace, and set to allow requests only from the namespace with the env=production label. The configuration is the same for outbound rules as shown below:

dev namespace.kubectl run testweb --namespace=dev --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.defaultwget: can't connect to remote host (172.18.255.217): Connection refused
production namespace.kubectl run testweb --namespace=production --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- --timeout=2 http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
kubectl delete pod webkubectl delete service webkubectl delete namespace {prod,dev}Disable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case5`.)
app=web label only from the cluster and reject those from outside the cluster.app=web label and another with the app=web1 label and start the services. web1 simulates a service in the cluster.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --labels=app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl run web1 --image=nginx --labels=app=web1 --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 5s[root@VM-0-11-centos ~]# kubectl get svc web1NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb1 ClusterIP 172.18.255.39 <none> 80/TCP 7s
web1 service in the cluster.[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.255.39:80<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 220.181.38.148:80<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>
app=web and allow requests from any namespace in the cluster. The configuration is the same for outbound rules as shown below:

web1 service in the cluster.[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.255.39:80<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 220.181.38.148:80curl: (: not foundo connect to 220.181.38.148 port 80: Connection refused
kubectl delete pod webkubectl delete service webkubectl delete pod web1kubectl delete service web1Disable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case6`.)
app=web label only from TCP port 5000 and reject requests from other ports (this doesn't affect UDP access).

app=web label and open ports 5000 and 8000.kubectl run web --image=ahmet/app-on-two-ports --labels app=webpod/web created[root@VM-0-11-centos ~]# kubectl get pod web -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESweb 1/1 Running 0 117s 172.18.0.42 172.16.0.11 <none> <none>
[root@VM-0-11-centos ~]# kubectl run testweb --namespace=dev --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.0.42:5000/metricshttp.requests=2go.goroutines=5go.cpus=4/ # wget -qO- http://172.18.0.42:8000Hello from HTTP server.
app=web, allow requests only from TCP port 5000 in any namespace in the cluster, and allow requests only from TCP port 5000 at any endpoint outside the cluster as shown below:
[root@VM-0-11-centos ~]# kubectl run testweb --namespace=dev --rm -i -t --image=alpine -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://172.18.0.42:5000/metricshttp.requests=2go.goroutines=5go.cpus=4/ # wget -qO- http://172.18.0.42:8000wget: can't connect to remote host (172.18.0.42): Connection refused
kubectl delete pod webkubectl delete service webDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case7`.)
app=web label only from the specified IP.app=web label and start the service.[root@VM-0-11-centos ~]# kubectl run web --namespace default --image=nginx --labels=app=webpod/web created[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 6s

106.xx.xx.61.

[root@VM-0-11-centos ~]# kubectl run --rm -it --image=alpine testweb -- shIf you don't see a command prompt, try pressing enter./ # wget -qO- http://web.default<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
~/workspace/networkpolicy_test curl cip.ccIP: 113.xx.xx.70Address: Shenzhen, Guangdong Province, ChinaISP: China TelecomData 2: Shenzhen, Guangdong Province | Tencent CloudData 3: Shenzhen, Guangdong Province, China | China TelecomURL: http://www.cip.cc/113.xx.xx.70~/workspace/networkpolicy_test curl 106.xx.xx.61<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...[root@VM-0-11-centos ~]# curl cip.ccIP: 175.xx.xx.176Address: China ChinaData 2: Guangzhou, Guangdong Province | Tencent CloudData 3: Xiamen, Fujian Province, China | TencentURL: http://www.cip.cc/175.xx.xx.176[root@VM-0-11-centos ~]# curl --connect-timeout 5 106.xx.xx.61<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=web and allow requests only from the specified IP outside the cluster as shown below:

~/workspace/networkpolicy_test curl 106.xx.xx.61<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
[root@VM-0-11-centos ~]# curl cip.ccIP: 175.xx.xx.176Address: China ChinaData 2: Guangzhou, Guangdong Province | Tencent CloudData 3: Xiamen, Fujian Province, China | TencentURL: http://www.cip.cc/175.xx.xx.176[root@VM-0-11-centos ~]# curl --connect-timeout 5 106.xx.xx.61curl: (28) Connection timed out after 5001 milliseconds
kubectl delete pod webkubectl delete service webDisable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case8`.)
app=web label to access only port 80 of the Pod with the app=db label and the specified IP.app=web label and another with the app=db label and start the services.[root@VM-0-11-centos ~]# kubectl run web --image=nginx --labels=app=web --expose --port 80service/web createdpod/web created[root@VM-0-11-centos ~]# kubectl get svc webNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb ClusterIP 172.18.255.217 <none> 80/TCP 5s[root@VM-0-11-centos ~]# kubectl run db --image=nginx --port 80 --expose --labels app=dbservice/db createdpod/db created[root@VM-0-11-centos ~]# kubectl get svc dbNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEdb ClusterIP 172.18.254.45 <none> 80/TCP 6s
[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.254.45<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...# curl 220.181.38.148:80<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html># curl 103.41.167.234:80<!DOCTYPE html><html lang="zh">...
app=web, allow outbound requests only from the specified IP outside the cluster, and allow TCP requests only through port 80 of the Pod with the app=db label in any namespace as shown below:
app=db label.[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.254.45:80<!DOCTYPE html><html><head><title>Welcome to nginx!</title>...
app=db label.[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.254.45:81curl:(7)Failed to connect to 172.18.254.45 port 81: Connection refused
[root@VM-0-11-centos ~]# kubectl get svc web1NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEweb1 ClusterIP 172.18.255.39 <none> 80/TCP 55m[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 172.18.255.39:80curl:(7)Failed to connect to 172.18.255.39 port 80: Connection refused
[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 220.181.38.148:80<html><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></html>
[root@VM-0-11-centos ~]# kubectl exec -it web -- sh# curl 103.xx.xx.234curl:(7)Failed to connect to 103.xx.xx.234 port 80: Connection refused
kubectl delete pod webkubectl delete service webkubectl delete pod dbkubectl delete service db1Disable the network policy in the console// (This can also be done by running `kubectl delete networkpolicy case9`.)
피드백