#!/bin/bash # 主机名格式为 k8s.node.1-1-1-1.xxx.com,即主机名中包含IP地址 # 如果主机名不是这种方式,需要执行kubectl describe node nodeName来获取IP地址 function dns() { nodes=`kubectl get node |awk 'NR>1{print $1}'` for node in $nodes;do ip=`echo $node |cut -f3 -d'.' |sed 's/-/./g'` echo "address=/$node/$ip" done } dns >/tmp/k8snode.conf diff -q /tmp/k8snode.conf /etc/dnsmasq.d/k8snode.conf if [ $? -ne 0 ];then echo "update dnsmasq..." dns > /etc/dnsmasq.d/k8snode.conf systemctl restart dnsmasq.service fi