• 使用 KubeSpray 安装 Kubernetes 时,报错
fatal: [node0]: FAILED! => {
    "assertion": "ip in ansible_all_ipv4_addresses",
    "changed": false,
    "evaluated_to": false,
    "failed": true
}
  • 查看 inventory.ini 配置
cat inventory.ini
# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all]
node0  ansible_host=139.168.12.4  ip=139.168.12.4
...
  • 查找报错信息来源

kubespray/extra_playbooks/roles/network_plugin/calico/tasks/pre.yml

---
- name: Calico | Get kubelet hostname
  shell: >-
    {{ bin_dir }}/kubectl get node -o custom-columns='NAME:.metadata.name,INTERNAL-IP:.status.addresses[?(@.type=="InternalIP")].address'
    | egrep "{{ ansible_all_ipv4_addresses | join('$|') }}$" | cut -d" " -f1
  register: calico_kubelet_name
  delegate_to: "{{ groups['kube-master'][0] }}"
  when:
    - "cloud_provider is defined"
  • 在 Kubernetes 集群上执行 KubeSpray 的检测命令
kubectl get node -o custom-columns='NAME:.metadata.name,INTERNAL-IP:.status.addresses[?(@.type=="InternalIP")].address'
NAME    INTERNAL-IP
node0   192.168.12.4
...

可以发现,ansible 会检测 Node 的 IP 是否在 INTERNAL-IP 中。而 INTERNAL-IP 在 Kubelet 配置,如果没有配置,Kubelet 会使用默认网卡的内网 IP 地址。

  • 查看机器的默认网卡,通常是 eth0
ip route show

default via 192.168.12.1 dev eth0 proto dhcp metric 100
10.233.90.0/24 via 192.168.12.5 dev tunl0 proto bird onlink
blackhole 10.233.94.0/24 proto bird
10.233.94.1 dev calia2d66a641cc scope link
10.233.94.2 dev calibebb7f7ab3d scope link
10.233.94.3 dev caliae46a1366fa scope link
10.233.94.5 dev calibf915b87dfb scope link
10.233.96.0/24 via 192.168.12.6 dev tunl0 proto bird onlink
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
172.18.0.0/16 dev br-b5a4cd7c2364 proto kernel scope link src 172.18.0.1
172.19.0.0/16 dev br-11f14e31b318 proto kernel scope link src 172.19.0.1
172.20.0.0/16 dev br-915052dd5899 proto kernel scope link src 172.20.0.1
172.21.0.0/16 dev br-0598710a1345 proto kernel scope link src 172.21.0.1
192.168.12.0/24 dev eth0 proto kernel scope link src 192.168.12.4 metric 100
  • 查看默认网卡的 IP 地址,这里是 192.168.12.4。
ip address

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 52:54:22:46:f4:c0 brd ff:ff:ff:ff:ff:ff
    inet 192.168.12.4/24 brd 192.168.12.255 scope global noprefixroute dynamic eth0
       valid_lft 81107sec preferred_lft 81107sec
    inet6 fe80::5054:22ff:fe46:f4c0/64 scope link
       valid_lft forever preferred_lft forever
  • 修改 IP 配置信息
cat inventory.ini
# ## Configure 'ip' variable to bind kubernetes services on a
# ## different ip than the default iface
# ## We should set etcd_member_name for etcd cluster. The node that is not a etcd member do not need to set the value, or can set the empty string value.
[all]
node0  ansible_host=192.168.12.4  ip=192.168.12.4
...

如果是多网卡,也可以通过参数指定 Kubelet 的 Node IP:

cat /var/lib/kubelet/kubeadm-flags.env

KUBELET_KUBEADM_ARGS="--cgroup-driver=cgroupfs --network-plugin=cni --pod-infra-container-image=gcr.io/google-containers/pause:3.1 --node-ip={NODE_IP}"