Docker run iptables No chain/target/match by that name 错误

full docker animals

我使用Docker运行一个HAProxy到多个Kubernetes API Server作为高可用,但经常性的发现某一个Node处于NotReady状态,通过查看kubelet日志发现连接不到Kubernetes API Server。于是赶紧查看以下HAProxy容器,发现HAProxy容器居然已经挂掉了,虽然我设置了--restart=always,但不知道什么原因还能挂掉。

我打算先把容器启动之后再排查,结果发现启动不了

$ docker run k8s-api-haproxy
Error response from daemon: Cannot restart container k8s-api-haproxy: driver failed programming external connectivity on endpoint k8s-api-haproxy (184fc52e77784f545e963bb1a9381586f82c7ac7ec593ed16714a19e8eaa8bc8):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8443 -j DNAT --to-destination 172.17.0.2:8443 ! -i docker0: iptables: No chain/target/match by that name.
 (exit status 1))

查看daemon日志,发现有这么一段

$ cat /var/log/daemon.log | grep k8s-api-haproxy
May 15 14:57:17 node7 dockerd[670]: time="2020-05-15T14:57:17.368981117+08:00" level=error msg="Handler for POST /v1.40/containers/k8s-api-haproxy/restart returned error: Cannot restart container k8s-api-haproxy: driver failed programming external connectivity on endpoint k8s-api-haproxy (184fc52e77784f545e963bb1a9381586f82c7ac7ec593ed16714a19e8eaa8bc8):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 8443 -j DNAT --to-destination 172.17.0.2:8443 ! -i docker0: iptables: No chain/target/match by that name.\n (exit status 1))

从日志结果来看,好像iptables没有匹配到相应的网络接口,只好退出程序。

好奇怪,为什么会有这个问题呢?我使用的是Debian10,Github有相关issue有提到Kubernetes对Debian10的iptables会有不兼容的情况,导致建网络配置时无法创建或者其它原因已经创建好的网络配置会丢失,解决方法是切换以下iptables的版本

update-alternatives --set iptables /usr/sbin/iptables-legacy

然后,需要重启以下Docker

systemctl daemon-reload
systemctl restart docker.service

再看一下,HAProxy已经up状态了

$ docker ps | grep k8s-api-haproxy
dbd9bcb60796        haproxy:2.0.14-alpine   "/docker-entrypoint.…"   24 hours ago        Up 5 seconds         0.0.0.0:8443->8443/tcp   k8s-api-haproxy

问题解决

分享

TITLE: Docker run iptables No chain/target/match by that name 错误

LINK: https://www.qttc.net/521-docker-err-with-iptables.html

NOTE: 原创内容,转载请注明出自琼台博客