Kubernetes K8S在IPVS代理模式下Service服务的ClusterIP类型访问失败处理

Kubernetes K8S使用IPVS代理模式,当Service的类型为ClusterIP时,如何处理访问service却不能访问后端pod的情况。

背景现象

Kubernetes K8S使用IPVS代理模式,当Service的类型为ClusterIP时,出现访问service却不能访问后端pod的情况。

主机配置规划

服务器名称(hostname)系统版本配置内网IP外网IP(模拟)
k8s-masterCentOS7.72C/4G/20G172.16.1.11010.0.0.110
k8s-node01CentOS7.72C/4G/20G172.16.1.11110.0.0.111
k8s-node02CentOS7.72C/4G/20G172.16.1.11210.0.0.112

场景复现

Deployment的yaml信息

yaml文件

[root@k8s-master service]# pwd
/root/k8s_practice/service
[root@k8s-master service]# cat myapp-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deploy
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
      release: v1
  template:
    metadata:
      labels:
        app: myapp
        release: v1
        env: test
    spec:
      containers:
      - name: myapp
        image: registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80

启动Deployment并查看状态

[root@k8s-master service]# kubectl apply -f myapp-deploy.yaml
deployment.apps/myapp-deploy created
[root@k8s-master service]#
[root@k8s-master service]# kubectl get deploy -o wide
NAME           READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                                                      SELECTOR
myapp-deploy   3/3     3            3           14s   myapp        registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,release=v1
[root@k8s-master service]# kubectl get rs -o wide
NAME                      DESIRED   CURRENT   READY   AGE   CONTAINERS   IMAGES                                                      SELECTOR
myapp-deploy-5695bb5658   3         3         3       21s   myapp        registry.cn-beijing.aliyuncs.com/google_registry/myapp:v1   app=myapp,pod-template-hash=5695bb5658,release=v1
[root@k8s-master service]#
[root@k8s-master service]# kubectl get pod -o wide --show-labels
NAME                            READY   STATUS    RESTARTS   AGE     IP             NODE         NOMINATED NODE   READINESS GATES   LABELS
myapp-deploy-5695bb5658-7tgfx   1/1     Running   0          39s     10.244.2.111   k8s-node02   <none>           <none>            app=myapp,env=test,pod-template-hash=5695bb5658,release=v1
myapp-deploy-5695bb5658-95zxm   1/1     Running   0          39s     10.244.3.165   k8s-node01   <none>           <none>            app=myapp,env=test,pod-template-hash=5695bb5658,release=v1
myapp-deploy-5695bb5658-xtxbp   1/1     Running   0          39s     10.244.3.164   k8s-node01   <none>           <none>            app=myapp,env=test,pod-template-hash=5695bb5658,release=v1

curl访问

[root@k8s-master service]# curl 10.244.2.111/hostname.html
myapp-deploy-5695bb5658-7tgfx
[root@k8s-master service]#
[root@k8s-master service]# curl 10.244.3.165/hostname.html
myapp-deploy-5695bb5658-95zxm
[root@k8s-master service]#
[root@k8s-master service]# curl 10.244.3.164/hostname.html
myapp-deploy-5695bb5658-xtxbp

Service的ClusterIP类型信息

yaml文件

[root@k8s-master service]# pwd
/root/k8s_practice/service
[root@k8s-master service]# cat myapp-svc-ClusterIP.yaml
apiVersion: v1
kind: Service
metadata:
  name: myapp-clusterip
  namespace: default
spec:
  type: ClusterIP  # 可以不写,为默认类型
  selector:
    app: myapp
    release: v1
  ports:
  - name: http
    port: 8080  # 对外暴露端口
    targetPort: 80  # 转发到后端端口

启动Service并查看状态

[root@k8s-master service]# kubectl apply -f myapp-svc-ClusterIP.yaml
service/myapp-clusterip created
[root@k8s-master service]#
[root@k8s-master service]# kubectl get svc -o wide
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE   SELECTOR
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP    16d   <none>
myapp-clusterip   ClusterIP   10.102.246.104   <none>        8080/TCP   6s    app=myapp,release=v1

查看ipvs信息

[root@k8s-master service]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
………………
TCP  10.102.246.104:8080 rr
  -> 10.244.2.111:80              Masq    1      0          0
  -> 10.244.3.164:80              Masq    1      0          0
  -> 10.244.3.165:80              Masq    1      0          0

由此可见,正常情况下:当我们访问Service时,访问链路是能够传递到后端的Pod并返回信息。

Curl访问结果

直接访问Pod,如下所示是能够正常访问的。

[root@k8s-master service]# curl 10.244.2.111/hostname.html
myapp-deploy-5695bb5658-7tgfx
[root@k8s-master service]#
[root@k8s-master service]# curl 10.244.3.165/hostname.html
myapp-deploy-5695bb5658-95zxm
[root@k8s-master service]#
[root@k8s-master service]# curl 10.244.3.164/hostname.html
myapp-deploy-5695bb5658-xtxbp

但通过Service访问结果异常,信息如下。

[root@k8s-master service]# curl 10.102.246.104:8080
    curl: (7) Failed connect to 10.102.246.104:8080; Connection timed out

处理过程

抓包核实

使用如下命令进行抓包,并通过Wireshark工具进行分析。

tcpdump -i any -n -nn port 80 -w ./$(date +%Y%m%d%H%M%S).pcap

结果如下图:

可见,已经向Pod发了请求,但是没有得到回复。结果TCP又重传了【TCP Retransmission】。

查看kube-proxy日志

[root@k8s-master service]# kubectl get pod -A | grep 'kube-proxy'
kube-system            kube-proxy-6bfh7                             1/1     Running   1          3h52m
kube-system            kube-proxy-6vfkf                             1/1     Running   1          3h52m
kube-system            kube-proxy-bvl9n                             1/1     Running   1          3h52m
[root@k8s-master service]#
[root@k8s-master service]# kubectl logs -n kube-system kube-proxy-6bfh7
W0601 13:01:13.170506       1 feature_gate.go:235] Setting GA feature gate SupportIPVSProxyMode=true. It will be removed in a future release.
I0601 13:01:13.338922       1 node.go:135] Successfully retrieved node IP: 172.16.1.112
I0601 13:01:13.338960       1 server_others.go:172] Using ipvs Proxier.  ##### 可见使用的是ipvs模式
W0601 13:01:13.339400       1 proxier.go:420] IPVS scheduler not specified, use rr by default
I0601 13:01:13.339638       1 server.go:571] Version: v1.17.4
I0601 13:01:13.340126       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_max' to 131072
I0601 13:01:13.340159       1 conntrack.go:52] Setting nf_conntrack_max to 131072
I0601 13:01:13.340500       1 conntrack.go:83] Setting conntrack hashsize to 32768
I0601 13:01:13.346991       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_established' to 86400
I0601 13:01:13.347035       1 conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_close_wait' to 3600
I0601 13:01:13.347703       1 config.go:313] Starting service config controller
I0601 13:01:13.347718       1 shared_informer.go:197] Waiting for caches to sync for service config
I0601 13:01:13.347736       1 config.go:131] Starting endpoints config controller
I0601 13:01:13.347743       1 shared_informer.go:197] Waiting for caches to sync for endpoints config
I0601 13:01:13.448223       1 shared_informer.go:204] Caches are synced for endpoints config
I0601 13:01:13.448236       1 shared_informer.go:204] Caches are synced for service config
可见kube-proxy日志无异常

网卡设置并修改

备注:在k8s-master节点操作的

之后进一步搜索表明,这可能是由于“Checksum offloading” 造成的。信息如下:

[root@k8s-master service]# ethtool -k flannel.1 | grep checksum
rx-checksumming: on
tx-checksumming: on     ##### 当前为 on
    tx-checksum-ipv4: off [fixed]
    tx-checksum-ip-generic: on    ##### 当前为 on
    tx-checksum-ipv6: off [fixed]
    tx-checksum-fcoe-crc: off [fixed]
    tx-checksum-sctp: off [fixed]

flannel的网络设置将发送端的checksum打开了,而实际应该关闭,从而让物理网卡校验。操作如下:

# 临时关闭操作
[root@k8s-master service]# ethtool -K flannel.1 tx-checksum-ip-generic off
Actual changes:
tx-checksumming: off
    tx-checksum-ip-generic: off
tcp-segmentation-offload: off
    tx-tcp-segmentation: off [requested on]
    tx-tcp-ecn-segmentation: off [requested on]
    tx-tcp6-segmentation: off [requested on]
    tx-tcp-mangleid-segmentation: off [requested on]
udp-fragmentation-offload: off [requested on]
[root@k8s-master service]#
# 再次查询结果
[root@k8s-master service]# ethtool -k flannel.1 | grep checksum
rx-checksumming: on
tx-checksumming: off     ##### 当前为 off
    tx-checksum-ipv4: off [fixed]
    tx-checksum-ip-generic: off     ##### 当前为 off
    tx-checksum-ipv6: off [fixed]
    tx-checksum-fcoe-crc: off [fixed]
    tx-checksum-sctp: off [fixed]

当然上述操作只能临时生效。机器重启后flannel虚拟网卡还会开启Checksum校验。

之后我们再次curl尝试

[root@k8s-master ~]# curl 10.102.246.104:8080
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.102.246.104:8080/hostname.html
myapp-deploy-5695bb5658-7tgfx
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.102.246.104:8080/hostname.html
myapp-deploy-5695bb5658-95zxm
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.102.246.104:8080/hostname.html
myapp-deploy-5695bb5658-xtxbp
[root@k8s-master ~]#
[root@k8s-master ~]# curl 10.102.246.104:8080/hostname.html
myapp-deploy-5695bb5658-7tgfx

由上可见,能够正常访问了。

永久关闭flannel网卡发送校验

备注:所有机器都操作

使用以下代码创建服务

[root@k8s-node02 ~]# cat /etc/systemd/system/k8s-flannel-tx-checksum-off.service
[Unit]
Description=Turn off checksum offload on flannel.1
After=sys-devices-virtual-net-flannel.1.device

[Install]
WantedBy=sys-devices-virtual-net-flannel.1.device

[Service]
Type=oneshot
ExecStart=/sbin/ethtool -K flannel.1 tx-checksum-ip-generic off

开机自启动,并启动服务

1 systemctl enable k8s-flannel-tx-checksum-off
2 systemctl start  k8s-flannel-tx-checksum-off

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: K8sIPVSiptables的区别在于它们是不同的负载均衡技术。IPVS是一种基于内核的负载均衡技术,它可以在内核层面进行负载均衡,提高了负载均衡的效率和性能。而iptables是一种基于用户空间的负载均衡技术,它需要在用户空间进行处理,因此效率和性能相对较低。在K8s中,IPVS通常用于服务的负载均衡,而iptables则用于网络策略的实现。 ### 回答2: Kubernetes(简称为k8s)是一种通用的开源平台,用于自动部署、扩展和管理容器化应用程序。在Kubernetes中,IPVSiptables是两种不同的网络代理,它们之间有很多区别。 首先,IPVS是基于四层网络代理,而iptables是基于五层网络代理。IPVS通过监听网络流量并进行路由,可以对L4层上的传输控制协议(TCP)、用户数据报协议(UDP)和其他协议进行路由和负载均衡。这意味着IPVS可以实现精细的四层网络代理,并更好地处理垂直扩展负载均衡和在k8s集群上运行的数据库等服务。 另一方面,iptables可以处理传输控制协议(TCP)和用户数据报协议(UDP)之外的网络层上的协议(例如,Internet控制报文协议ICMP)。因此,在k8s中使用iptables来代理网络不仅可以实现简单的TCP或UDP流量路由,还可以进行更细粒度的路由(如IP地址或网络端口)。 其次,IPVSiptables的工作方式也有所不同。IPVS使用IP地址和端口号将流量重定向到不同的后端Pod,而iptables使用更细粒度的规则,它通常会检查包中的一些字段(如源和目的地址、端口和协议),并使用这些字段来匹配规则中的条件。当数据包满足匹配条件时,iptables可以更容易地将流量重定向到特定的后端Pod,从而实现负载均衡。 因此,从功能和执行方式上来看,IPVSiptables都有其优劣。在使用k8s网络代理时,应根据特定的使用场景和需求以及负载均衡策略权衡二者之间的区别,选择最适合的选项。需要注意的是,在不同时期,k8s网络代理观念也会有所变化,甚至会出现新的选项。因此,在选择k8s网络代理时,应及时了解k8s的最新情况和趋势,以便为集群带来更好的网络性能和扩展性。 ### 回答3: k8s中的ipvsiptables是两种不同的负载均衡技术,它们的作用都是将请求平均地分配到不同的后端服务中,以提高系统的可用性。它们之间的区别包括以下几个方面: 1. 实现原理:ipvs是基于网络层实现的负载均衡技术,它使用Linux内核提供的网络层IP Virtual Server机制,通过虚拟IP地址实现负载均衡,将来自客户端的请求转发到不同的后端服务。而iptables是基于应用层实现的负载均衡技术,它使用Linux内核提供的iptables规则,针对特定的应用端口对请求进行过滤和转发。 2. 性能:由于ipvs是基于网络层实现的,所以它的性能比iptables更高。ipvs的转发速度比iptables快,同时ipvs可以使用网卡的多队列技术,支持多核CPU,可以更好地利用服务器的硬件资源。 3. 功能:ipvsiptables在功能上也有较大的区别。ipvs不仅可以实现负载均衡,还可以实现网络地址转换(NAT)、流量控制、反向代理等功能。而iptables则更适合实现网络安全相关的功能,如防火墙、入侵检测等。 4. 稳定性:ipvs作为Linux内核的一部分,稳定性更高。而iptables则可能会受到用户规则的影响,导致系统出现异常。 综上所述,ipvsiptables各有优缺点,在k8s中的应用也会因情况而异。如果对性能有较高要求,需要进行流量控制或反向代理,则可以选择ipvs。如果需求更偏向于网络安全,则可以选择iptables。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值