在同一个kubernetes集群中,所有的pod网络默认互相联通,默认情况下 Pod 是可以接收来自任何发送方的请求,也可以向任何接收方发送请求。而如果我们要对这个情况作出限制,就需要启动网络插件的策略功能,因为pod 默认都是开放的 底层网路是放行的,就必须通过 NetworkPolicy 对象来指定放行策略。
Namespaces 达到以下目的:
1 对用户做隔离
2 为用户分配配置
3 为资源定制访问策略
4 部署相似的应用或单个应用的不同版本
跨namespace Pod与Service通信
通过Service的ExternalName类型即可实现跨namespace名称空间与Service通信。
通过DNS来解析,不是通过IP地址访问。
Service域名格式:$(service name).$(namespace).svc.cluster.local,其中 cluster.local 为指定的集群的域名
[root@k8s ~]# kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default busybox 1/1 Running 0 57s
default myweb-544b6f5455-lwl7z 1/1 Running 0 15m
kube-system calico-kube-controllers-5978c5f6b5-7wbkf 1/1 Running 0 23m
kube-system calico-node-gdd9x 1/1 Running 0 23m
kube-system coredns-7ff77c879f-f4r2h 1/1 Running 0 36m
kube-system coredns-7ff77c879f-m29w6 1/1 Running 0 36m
kube-system etcd-k8s 1/1 Running 1 36m
kube-system kube-apiserver-k8s 1/1 Running 1 36m
kube-system kube-controller-manager-k8s 1/1 Running 2 36m
kube-system kube-proxy-xlz6x 1/1 Running 3 36m
kube-system kube-scheduler-k8s 1/1 Running 2 36m
test myapp-687598b8b4-mbxsz 1/1 Running 0 13m
uat myapp-687598b8b4-5rlhc 1/1 Running 0 13m
[root@k8s ~]# kubectl get ssvc -A
error: the server doesn't have a resource type "ssvc"
[root@k8s ~]# kubectl get svc -A
[root@k8s ~]# kubectl get svc -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 37m
default myweb ClusterIP 10.96.171.251 <none> 80/TCP 15m
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 37m
test myapp ClusterIP 10.104.56.195 <none> 80/TCP 13m
uat myapp ClusterIP 10.107.250.146 <none> 80/TCP 13m
[root@k8s ~]# kubectl exec -it busybox sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
/ #
/ # ping 10.107.250.146
PING 10.107.250.146 (10.107.250.146): 56 data bytes
64 bytes from 10.107.250.146: seq=0 ttl=64 time=10.061 ms
64 bytes from 10.107.250.146: seq=1 ttl=64 time=0.077 ms
^C
--- 10.107.250.146 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.077/5.069/10.061 ms
/ # ping
/ # ping 10.104.56.195
PING 10.104.56.195 (10.104.56.195): 56 data bytes
64 bytes from 10.104.56.195: seq=0 ttl=64 time=0.061 ms
64 bytes from 10.104.56.195: seq=1 ttl=64 time=0.071 ms
^C
--- 10.104.56.195 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.061/0.066/0.071 ms
/ # ping 172.16.77.5
PING 172.16.77.5 (172.16.77.5): 56 data bytes
64 bytes from 172.16.77.5: seq=0 ttl=64 time=0.092 ms
64 bytes from 172.16.77.5: seq=1 ttl=64 time=0.076 ms
^C
--- 172.16.77.5 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.076/0.084/0.092 ms
/ # nslookup myapp.uat.svc.cluster.local
/ # nslookup myapp.uat.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: myapp.uat.svc.cluster.local
Address 1: 10.107.250.146 myapp.uat.svc.cluster.local
/ # ping myapp.uat.svc.cluster.local
PING myapp.uat.svc.cluster.local (10.107.250.146): 56 data bytes
64 bytes from 10.107.250.146: seq=0 ttl=64 time=0.034 ms
64 bytes from 10.107.250.146: seq=1 ttl=64 time=0.170 ms
^C
--- myapp.uat.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.034/0.102/0.170 ms
/ #
/ # ping myapp.test.svc.cluster.local
PING myapp.test.svc.cluster.local (10.104.56.195): 56 data bytes
64 bytes from 10.104.56.195: seq=0 ttl=64 time=0.068 ms
64 bytes from 10.104.56.195: seq=1 ttl=64 time=0.068 ms
^C
--- myapp.test.svc.cluster.local ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 0.068/0.068/0.068 ms
/ # nslookup myapp.test.svc.cluster.local
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: myapp.test.svc.cluster.local
Address 1: 10.104.56.195 myapp.test.svc.cluster.local
/ #
bash-5.0# kubectl -n cicd-demo-project get pod |grep nacos
cicd-demo-nacos-v1-685fbf8f57-pqtws 1/1 Running 0 3d19h
nacos-0 1/1 Running 0 6d13h
nacos-1 1/1 Running 0 6d13h
nacos-2 1/1 Running 0 6d13h
bash-5.0# kubectl -n cicd-demo-project get svc |grep nacos
cicd-demo-nacos NodePort 172.26.233.80 <none> 8848:31471/TCP 16d
nacos-headless ClusterIP None <none> 8848/TCP 6d16h
bash-5.0# kubectl -n ur-api-test-project exec -it restcloud-gateway-4.5-5cd5679677-cx4mw sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
/usr/local/apache-tomcat-8.5.55 # ping 172.26.233.80
PING 172.26.233.80 (172.26.233.80): 56 data bytes
64 bytes from 172.26.233.80: seq=0 ttl=64 time=0.066 ms
64 bytes from 172.26.233.80: seq=1 ttl=64 time=0.058 ms
64 bytes from 172.26.233.80: seq=2 ttl=64 time=0.063 ms
64 bytes from 172.26.233.80: seq=3 ttl=64 time=0.072 ms
64 bytes from 172.26.233.80: seq=4 ttl=64 time=0.053 ms
64 bytes from 172.26.233.80: seq=5 ttl=64 time=0.059 ms
^C
--- 172.26.233.80 ping statistics ---
6 packets transmitted, 6 packets received, 0% packet loss
round-trip min/avg/max = 0.053/0.061/0.072 ms
/usr/local/apache-tomcat-8.5.55 #
/usr/local/apache-tomcat-8.5.55 # nslookup cicd-demo-nacos.cicd-demo-project.svc.cluster.local
Server: 169.254.25.10
Address: 169.254.25.10:53
Non-authoritative answer:
Name: cicd-demo-nacos.cicd-demo-project.svc.cluster.local
Address: 120.240.95.35
Non-authoritative answer:
*** Can't find cicd-demo-nacos.cicd-demo-project.svc.cluster.local: No answer
/usr/local/apache-tomcat-8.5.55 #
华为云 CCE
设置命名空间级的网络策略
https://support.huaweicloud.com/usermanual-cce/cce_01_0286.html
kubesphere
https://kubesphere.com.cn/docs/project-administration/project-network-isolation
https://kubesphere.com.cn/docs/pluggable-components/network-policy
kubernetes
https://kubernetes.io/zh/docs/concepts/services-networking/dns-pod-service
https://blog.csdn.net/polywg/article/details/109814803
https://www.kubernetes.org.cn/1909.html
跨命名空间pod 访问service dns名称而不是clusterIP,dns记录不会变,IP地址会变。
[
https://blog.csdn.net/kongliand/article/details/114916990
](https://blog.csdn.net/kongliand/article/details/114916990)