测试 Service 从公网访问:
$ curl -I $ipHTTP/1.1 200 OKServer: nginx/1.21.1Date: Wed, 11 Aug 2021 14:25:06 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Tue, 06 Jul 2021 14:59:17 GMTConnection: keep-aliveETag: "60e46fc5-264"Accept-Ranges: bytesC 测试从其它子网访问 Pod
现在集群上的 Pod 除了可以同一子网内互相访问 , 也可以让 VPC 下其它子网(同一区域或者不同区域都可以)以及通过专线或 VPN 与本 VPC 互通的线下机房网络里的虚机来访问 。下面在不同区域创建一个新子网 , 以及该子网下的虚机 , 来访问集群上的 Pod 。
--network=k8s-vpc
--range=10.150.16.0/21
--region=us-central1
gcloud compute instances create remote-test-1 --project=youzhi-lab --zone=us-central1-b --machine-type=n2-standard-4 --network-interface=network-tier=PREMIUM,subnet=us-central1-sub-1 --can-ip-forward --maintenance-policy=MIGRATE --image=centos-7-v20210721 --image-project=centos-cloud --boot-disk-size=100GB --no-boot-disk-auto-delete --boot-disk-type=pd-balanced --boot-disk-device-name=remote-test-1 --no-shielded-secure-boot --shielded-vtpm --shielded-integrity-monitoring --reservation-affinity=any
登录 remote-test1 虚机 , 并运行以下命令访问 Pod 。
HTTP/1.1 200 OK
Server: nginx/1.21.1
Date: Fri, 13 Aug 2021 05:35:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 06 Jul 2021 14:59:17 GMT
Connection: keep-alive
ETag: "60e46fc5-264"
Accept-Ranges: bytes
$ curl -I 192.168.17.14
HTTP/1.1 200 OK
Server: nginx/1.21.1
Date: Fri, 13 Aug 2021 05:36:00 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 06 Jul 2021 14:59:17 GMT
Connection: keep-alive
ETag: "60e46fc5-264"
Accept-Ranges: bytes
可以确认访问成功 。
D 确认 Node 上关闭 Pod 访问 SNAT
如果检测结果与下面的结果有差异 , 确认执行 Master 节点配置的“安装 ip-masq-agent”一节 。
NAME DESIRED CURRENT READY UP-TO-DATE AVAILABLE NODE SELECTOR AGE
ip-masq-agent 2 2 2 2 2 <none> 19m
kube-proxy 3 3 3 3 3 kubernetes.io/os=linux 29h
在每台 Node 上查看 NAT, IP-MASQ-AGENT Chain 默认会针对目的地址为 RFC1918 地址段不做地址翻译 。
Chain POSTROUTING (policy ACCEPT)
num target prot opt source destination
1 KUBE-POSTROUTING all -- 0.0.0.0/0 0.0.0.0/0 /* kubernetes postrouting rules */
2 IP-MASQ-AGENT all -- 0.0.0.0/0 0.0.0.0/0 /* ip-masq-agent: ensure nat POSTROUTING directs all non-LOCAL destination traffic t
o our custom IP-MASQ-AGENT chain */ ADDRTYPE match dst-type !LOCAL
Chain IP-MASQ-AGENT (1 references)
num target prot opt source destination
1 RETURN all -- 0.0.0.0/0 169.254.0.0/16 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE
match dst-type !LOCAL
2 RETURN all -- 0.0.0.0/0 10.0.0.0/8 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE
match dst-type !LOCAL
3 RETURN all -- 0.0.0.0/0 172.16.0.0/12 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE
match dst-type !LOCAL
4 RETURN all -- 0.0.0.0/0 192.168.0.0/16 /* ip-masq-agent: cluster-local traffic should not be subject to MASQUERADE */ ADDRTYPE
match dst-type !LOCAL
5 MASQUERADE all -- 0.0.0.0/0 0.0.0.0/0 /* ip-masq-agent: outbound traffic should be subject to MASQUERADE (this match must com
e after cluster-local CIDR matches) */ ADDRTYPE match dst-type !LOCAL
四 总结
在 GCE 上通过 kubeadm 可以非常方便的安装 kubernetes 集群 , 通过 GCP VPC 的各种网络功能 , 配合 Kubernetes 本身 cloud-provider 的功能 , 可以非常方便的实现 Kubernetes 网络的各种功能 。
Appendix
1 kubeadm 创建 token
kubeadm join 10.128.1.96:6443
--token xxxx
--discovery-token-ca-cert-hash sha256:xxxx
2 alias IP 模式下的配置
确认删除 Alias IP 的本地路由:
https://cloud.google.com/vpc/docs/configure-alias-ip-ranges#enabling_ip_alias_on_images_disables_cbr0_bridge_on_self-managed_kubernetes_clusters
通过 GCP Alias IP 实现跨 Node 的 Pod 互通
3 K8s 网络针对 GCP 的配置注意事项
推荐阅读
- 电脑在使用过程中经常自动重启如何判断和解决
- 网站建设如何平衡SEO与用户转化
- 口红在哪个平台买是正品 去哪里买口红是正品
- 露地宿根花卉,宿根花卉品种繁多
- 炒股|飞来横祸?宁波男子去年关掉工厂在家职业炒股,如今已亏损150万!
- 命宫在丙寅详解 命宫壬寅是什么意思
- 清朝在西藏设置的机构 清朝在徽汉合流的基础上形成的新剧种是
- 孩子总困是怎么回事
- 懒妈妈培养勤快孩子
- 冬天宝宝“长胡子”是怎么回事?
