GKE中的VPC本机群集无法在GKE 1.14中通信

时间:2020-06-10 11:44:24

标签: kubernetes google-kubernetes-engine gke-networking

我在K8s 1.14.10上创建了两个单独的GKE集群。

VPN access to in-house network not working after GKE cluster upgrade to 1.14.6

我已遵循此说明和IP伪装代理程序文档。 我已经尝试使用客户端Pod和服务器Pod交换消息来进行测试。 我正在使用内部节点IP发送消息,并创建了一个ClusterIP来暴露容器。

在防火墙规则中,我已允许每个实例的入出请求,即 0.0.0.0/0 Pic:This is the description of the cluster which I have created IP伪装代理的配置图与文档中的相同。 我可以从Pod内ping另一个节点,但是curl请求说连接被拒绝,并且tcpdump不显示任何数据。

问题: 我需要在gke 1.14中将群集A与群集B通信,并将ipmasquerading设置为true。我被拒绝连接或I / O超时。我尝试使用内部和外部节点IP以及负载均衡器。

1 个答案:

答案 0 :(得分:0)

您提供了非常笼统的信息,没有详细信息,我无法提供特定方案的答案。这可能与您如何创建群集或其他防火墙设置有关。因此,我将提供使用防火墙和masquerade创建和配置2个群集的正确步骤。也许您将能够找到错过或配置错误的步骤。

集群配置(node,pods,svc)位于答案的底部。

1。创建VPC和2个群集

在文档中它说了大约2个不同的项目,但是您可以在一个项目中完成。 在GKE文档中可以找到创建VPC和2个群集的良好示例。 Create VPCCrate 2 clusters。在群集Tier1中,您可以立即启用NetworkPolicy,而不必稍后启用。 之后,您将需要创建Firewall Rules。您还需要向防火墙规则中添加ICMP协议。

此时,您应该能够在2个群集的节点之间ping通。

有关其他防火墙规则(允许Pod,svc等之间的连接),请检查this docs

2。启用IP masquerade agent

如文档中所述,要运行IPMasquerade

如果满足以下一个或多个条件,则ip-masq-agent DaemonSet将作为带有--nomasq-all-reserved-ranges参数的附件自动安装到GKE集群中:

集群具有网络策略。

OR

Pod的CIDR范围不在10.0.0.0/8之内。

这意味着tier-2-clusterip-masq-agent命名空间中已经有kube-system(因为The Pod's CIDR range is not within 10.0.0.0/8.)。而且,如果您在创建NetworkPolicy时启用了tier-1-cluster,则也应该已经安装了它。如果没有,则需要使用命令启用它:

$ gcloud container clusters update tier-1-cluster --update-addons=NetworkPolicy=ENABLED --zone=us-central1-a

要验证一切正常,您必须检查是否创建了Daemonset ip-masq-agent个广告连播。 (每个节点的Pod)。

$ kubectl get ds ip-masq-agent -n kube-system
NAME            DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR                                 AGE
ip-masq-agent   3         3         3       3            3           beta.kubernetes.io/masq-agent-ds-ready=true   168m

如果您将SSH到任何节点,您将能够看到默认的iptables条目。

$ sudo iptables -t nat -L IP-MASQ
Chain IP-MASQ (1 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             169.254.0.0/16       /* ip-masq: local traffic is not subject to MASQUERADE */
RETURN     all  --  anywhere             10.0.0.0/8           /* ip-masq: RFC 1918 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             172.16.0.0/12        /* ip-masq: RFC 1918 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             192.168.0.0/16       /* ip-masq: RFC 1918 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             240.0.0.0/4          /* ip-masq: RFC 5735 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             192.0.2.0/24         /* ip-masq: RFC 5737 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             198.51.100.0/24      /* ip-masq: RFC 5737 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             203.0.113.0/24       /* ip-masq: RFC 5737 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             100.64.0.0/10        /* ip-masq: RFC 6598 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             198.18.0.0/15        /* ip-masq: RFC 6815 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             192.0.0.0/24         /* ip-masq: RFC 6890 reserved range is not subject to MASQUERADE */
RETURN     all  --  anywhere             192.88.99.0/24       /* ip-masq: RFC 7526 reserved range is not subject to MASQUERADE */
MASQUERADE  all  --  anywhere             anywhere             /* ip-masq: outbound traffic is subject to MASQUERADE (must be last in chain) */

3。部署测试应用程序

我使用了GKE docs中的Hello应用程序,并将其部署在两个集群上。此外,我还部署了ubuntu映像进行测试。

4。为IPMasquerade应用正确的配置 此配置必须位于source群集上。

简而言之,如果目标CIDR在nonMasqueradeCIDRs:中,它将显示其内部IP,否则将显示NodeIP作为源。

保存到文本下方的文件config

nonMasqueradeCIDRs:
  - 10.0.0.0/8
resyncInterval: 2s
masqLinkLocal: true

创建IPMasquarade ConfigMap

$ kubectl create configmap ip-masq-agent --from-file config --namespace kube-system

它将覆盖iptables的配置

$ sudo iptables -t nat -L IP-MASQ
Chain IP-MASQ (2 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             10.0.0.0/8           /* ip-masq-agent: local traffic is not subject to MASQUERADE */
MASQUERADE  all  --  anywhere             anywhere             /* ip-masq-agent: outbound traffic is subject to MASQUERADE (must be last in chain) */

5。测试:

伪造IP时

SSH到节点表单Tier2的群集并运行:

sudo toolbox bash
apt-get update
apt install -y tcpdump

现在您应该使用以下命令收听。端口32502是来自NodePort集群的Tier 2服务

tcpdump -i eth0 -nn -s0 -v port 32502

在集群Tier1中,您需要输入ubuntu pod并卷曲NodeIP:NodePort

$ kubectl exec -ti ubuntu -- bin/bash 

您将需要安装curl apt-get install curl

curl NodeIP:NodePort(正在侦听的节点,来自群集层2的服务中的NodePort)。

CLI:

root@ubuntu:/# curl 172.16.4.3:32502
Hello, world!
Version: 2.0.0
Hostname: hello-world-deployment-7f67f479f5-h4wdm

在“节点”上,您可以看到以下条目:

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
12:53:30.321641 IP (tos 0x0, ttl 63, id 25373, offset 0, flags [DF], proto TCP (6), length 60)
    10.0.4.4.56018 > 172.16.4.3.32502: Flags [S], cksum 0x8648 (correct), seq 3001889856

10.0.4.4是Ubuntu pod所在的NodeIP

未伪装IP时

从集群层1中删除ConfigMap

$ kubectl delete cm ip-masq-agent -n kube-system

将文件config的CIDR更改为172.16.4.0/22的节点Tier 2,并重新应用CM

$ kubectl create configmap ip-masq-agent --from-file config --namespace kube-system

SSH到第1层的任何节点,以检查iptables rules是否已更改。

sudo iptables -t nat -L IP-MASQ
Chain IP-MASQ (2 references)
target     prot opt source               destination         
RETURN     all  --  anywhere             172.16.4.0/22        /* ip-masq-agent: local traffic is not subject to MASQUERADE */
MASQUERADE  all  --  anywhere             anywhere             /* ip-masq-agent: outbound traffic is subject to MASQUERADE (must be last in chain) */

现在进行测试,我再次使用Ubuntu pod并像以前一样卷曲相同的ip。

tcpdump: listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
13:16:50.316234 IP (tos 0x0, ttl 63, id 53160, offset 0, flags [DF], proto TCP (6), length 60)
    10.4.2.8.57876 > 172.16.4.3.32502

10.4.2.8是Ubuntu pod的内部IP。

测试配置:

TIER1

NAME                                          READY   STATUS    RESTARTS   AGE   IP         NODE                                            NOMINATED NODE   READINESS GATES
pod/hello-world-deployment-7f67f479f5-b2qqz   1/1     Running   0          15m   10.4.1.8   gke-tier-1-cluster-default-pool-e006097b-5tnj   <none>           <none>
pod/hello-world-deployment-7f67f479f5-shqrt   1/1     Running   0          15m   10.4.2.5   gke-tier-1-cluster-default-pool-e006097b-lfvh   <none>           <none>
pod/hello-world-deployment-7f67f479f5-x7jvr   1/1     Running   0          15m   10.4.0.8   gke-tier-1-cluster-default-pool-e006097b-1wbf   <none>           <none>
ubuntu                                    1/1     Running   0          91s   10.4.2.8   gke-tier-1-cluster-default-pool-e006097b-lfvh   <none>           <none>

NAME                  TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)           AGE    SELECTOR
service/hello-world   NodePort    10.0.36.46   <none>        60000:31694/TCP   14m    department=world,greeting=hello
service/kubernetes    ClusterIP   10.0.32.1    <none>        443/TCP           115m   <none>

NAME                                                 STATUS   ROLES    AGE    VERSION           INTERNAL-IP   EXTERNAL-IP     OS-IMAGE                             KERNEL-VERSION   CONTAINER-RUNTIME
node/gke-tier-1-cluster-default-pool-e006097b-1wbf   Ready    <none>   115m   v1.14.10-gke.36   10.0.4.2      35.184.38.21    Container-Optimized OS from Google   4.14.138+        docker://18.9.7
node/gke-tier-1-cluster-default-pool-e006097b-5tnj   Ready    <none>   115m   v1.14.10-gke.36   10.0.4.3      35.184.207.20   Container-Optimized OS from Google   4.14.138+        docker://18.9.7
node/gke-tier-1-cluster-default-pool-e006097b-lfvh   Ready    <none>   115m   v1.14.10-gke.36   10.0.4.4      35.226.105.31   Container-Optimized OS from Google   4.14.138+        docker://18.9.7<none>   100m   v1.14.10-gke.36   10.0.4.4      35.226.105.31   Container-Optimized OS from Google   4.14.138+        docker://18.9.7

TIER2

$ kubectl get pods,svc,nodes -o wide
NAME                                          READY   STATUS    RESTARTS   AGE   IP           NODE                                            NOMINATED NODE   READINESS GATES
pod/hello-world-deployment-7f67f479f5-92zvk   1/1     Running   0          12m   172.20.1.5   gke-tier-2-cluster-default-pool-57b1cc66-xqt5   <none>           <none>
pod/hello-world-deployment-7f67f479f5-h4wdm   1/1     Running   0          12m   172.20.1.6   gke-tier-2-cluster-default-pool-57b1cc66-xqt5   <none>           <none>
pod/hello-world-deployment-7f67f479f5-m85jn   1/1     Running   0          12m   172.20.1.7   gke-tier-2-cluster-default-pool-57b1cc66-xqt5   <none>           <none>

NAME                  TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)           AGE    SELECTOR
service/hello-world   NodePort    172.16.24.206   <none>        60000:32502/TCP   12m    department=world,greeting=hello
service/kubernetes    ClusterIP   172.16.16.1     <none>        443/TCP           113m   <none>

NAME                                                 STATUS   ROLES    AGE    VERSION           INTERNAL-IP   EXTERNAL-IP      OS-IMAGE                             KERNEL-VERSION   CONTAINER-RUNTIME
node/gke-tier-2-cluster-default-pool-57b1cc66-84ng   Ready    <none>   112m   v1.14.10-gke.36   172.16.4.2    35.184.118.151   Container-Optimized OS from Google   4.14.138+        docker://18.9.7

node/gke-tier-2-cluster-default-pool-57b1cc66-mlmn   Ready    <none>   112m   v1.14.10-gke.36   172.16.4.3    35.238.231.160   Container-Optimized OS from Google   4.14.138+        docker://18.9.7

node/gke-tier-2-cluster-default-pool-57b1cc66-xqt5   Ready    <none>   112m   v1.14.10-gke.36   172.16.4.4    35.202.94.194    Container-Optimized OS from Google   4.14.138+        docker://18.9.7