centos7 同步集群多节点hosts文件

脚本使用

  1. 修改prefix(ip前三位)
  2. 修改master,node1, node2 各节点ip号
  3. 修改gateway地址
  4. 修改各节点登录账号(本人使用的统一root账号)

执行

注意:执行时会重启节点网络

#!/bin/bash

#prefix="192.168.31"
#prefix="192.168.1"
prefix="192.168.244"
master="$prefix.100"
node1="$prefix.101"
node2="$prefix.102"
gateway="$prefix.92"

# replace ipaddr
sed -i "s/^IPADDR.*$/IPADDR=$master/g" /etc/sysconfig/network-scripts/ifcfg-ens33
echo "replace network[ipaddr] $master"

# replace gateway $prefix.1
# for example 192.168.31.1
sed -i "s/^GATEWAY.*$/GATEWAY=$gateway/g" /etc/sysconfig/network-scripts/ifcfg-ens33
echo "replace network[gateway] $gateway"

# replace hosts
sed -i "s/^192.*k8s-master$/$master k8s-master/g" /etc/hosts
sed -i "s/^192.*k8s-node1$/$node1 k8s-node1/g" /etc/hosts
sed -i "s/^192.*k8s-node2$/$node2 k8s-node2/g" /etc/hosts
echo "replace hosts file"

# restart network service
systemctl restart network
echo "restart network service"

# distribute hosts to cluster
`scp /etc/hosts root@$node2:/etc/hosts`
`scp /etc/hosts root@$node1:/etc/hosts`
echo "distribute hosts to cluster"
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。