10.4.4 验证iptables是否已修改

为了验证使用Istio CNI插件之后iptables是否得到修改,类似于前面章节,按照以下步骤部署示例应用。首先下载示例代码,切换到cni目录,并执行如下命令:


kubectl apply -f httpbin.yaml
kubectl apply -f sleep.yaml
kubectl apply -f destination-rule.yaml
kubectl apply -f virtual-service.yaml

等待应用容器启动之后,选择其中之一的容器例如httpbin-v1-xxxx,通过执行如下命令获取容器ID:


ns=default
podnm=httpbin-v1-f455f64c4-24pjw
container_id=$(kubectl get pod -n ${ns} ${podnm} -o jsonpath="{.status.container-
Statuses[?(@.name=='istio-proxy')].containerID}" | sed -n 's/docker:\/\/\(.*\)/\1/p')

SSH登录进入运行上述pod的Kubernetes工作节点,并使用nsenter查看iptables的配置,执行如下命令:


$ cpid=$(docker inspect --format '{{ .State.Pid }}' $container_id)
$ nsenter -t $cpid -n iptables -L -t nat -n -v --line-numbers -x

可以看到如下类似的返回结果:


Chain PREROUTING (policy ACCEPT 1 packets, 60 bytes)
num      pkts    bytes target   prot opt in   out   source         destination

Chain INPUT (policy ACCEPT 1 packets, 60 bytes)
num      pkts    bytes target   prot opt in   out   source         destination

Chain OUTPUT (policy ACCEPT 14673 packets, 1371876 bytes)
num      pkts    bytes target   prot opt in   out   source         destination
1          76    4560 ISTIO_OUTPUT  tcp  --  *     *     0.0.0.0/0   0.0.0.0/0

Chain POSTROUTING (policy ACCEPT 14727 packets, 1375116 bytes)
num      pkts    bytes target   prot opt in   out   source         destination

Chain ISTIO_IN_REDIRECT (0 references)
num      pkts    bytes target   prot opt in   out   source         destination
1           0      0 REDIRECT   tcp  --  *    *     0.0.0.0/0        0.0.0.0/0            redir ports 15001

Chain ISTIO_OUTPUT (1 references)
num     pkts    bytes target   prot opt in   out   source          destination
1          0      0 ISTIO_REDIRECT  all  --  *     lo     0.0.0.0/0     !127.0.0.1
2        22   1320 RETURN   all  --  *    *     0.0.0.0/0            0.0.0.0/0            owner UID match 1337
3         0      0 RETURN   all  --  *    *     0.0.0.0/0            0.0.0.0/0            owner GID match 1337
4         0      0 RETURN   all  --  *    *     0.0.0.0/0            127.0.0.1
5        54   3240 ISTIO_REDIRECT  all  --  *    *     0.0.0.0/0     0.0.0.0/0

Chain ISTIO_REDIRECT (2 references)
num     pkts    bytes target   prot opt in   out   source          destination
1         54    3240 REDIRECT   tcp  --  *      *     0.0.0.0/0      0.0.0.0/0            redir ports 15001

此外,在Kubernetes集群各个节点上都运行了kubelet进程,而CNI插件是这些运行中的kubelet进程中的一个线程运行时。CNI插件日志位于该kubelet进程下的syslog中。在具有journalctl的系统上,可以通过如下类似的命令行使用less实用程序查看kubelet日志记录,这里显示了最后1000条日志记录,并以vi样式进行搜索:


$ journalctl -t kubelet -n 1000 | less