
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>...

[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>...
[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
[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 web前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case1)

[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>...

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 web前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case2)

[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
[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>...

[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>...
[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 web前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case3)

[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
[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
[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>...
[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>...


[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>...
[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 secondary前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case4)

[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
[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 ~]#
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>...
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 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
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}前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case5)
[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
[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>

[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 web1前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case6)

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.

[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 web前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case7
[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


[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.70地址 : 中国 广东 深圳运营商 : 电信数据二 : 广东省深圳市 | 腾讯云数据三 : 中国广东省深圳市 | 电信URL : 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.176地址 : 中国 中国数据二 : 广东省广州市 | 腾讯云数据三 : 中国福建省厦门市 | 腾讯URL : 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>...

~/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.176地址 : 中国 中国数据二 : 广东省广州市 | 腾讯云数据三 : 中国福建省厦门市 | 腾讯URL : 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 web前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case8)
[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">...

[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>...
[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 db1前往控制台停用网络策略//(效果等同于kubectl delete networkpolicy case9)
文档反馈