首页 > 系统相关 >ubuntu2204脚本初始化集群

ubuntu2204脚本初始化集群

时间:2023-06-13 16:34:25浏览次数:45  
标签:初始化 00 chrony forever 192.168 lft 集群 ff ubuntu2204

@

目录
用于一键部署集群(多节点)的环境初始化,如修改主机名,主机映射,节点相互免密,时间同步等,可多节点部署
ps:脚本需要下载sshpass,chrony等软件包,确保ubuntu源正常

脚本实现

#!/bin/bash

# 定义节点信息
NODES=("192.168.200.180 controller root" "192.168.200.182 compute root" "192.168.200.184 othernode root")

# 定义当前节点的密码(默认集群统一密码)
HOST_PASS="000000"

# 时间同步的目标节点
TIME_SERVER=controller

# 时间同步的地址段
TIME_SERVER_IP=192.160.200.0/24

# 欢迎界面
cat > /etc/motd <<EOF
 ################################
 #    Welcome  to  openstack    #
 ################################
EOF

# 修改主机名
for node in "${NODES[@]}"; do
  ip=$(echo "$node" | awk '{print $1}')
  hostname=$(echo "$node" | awk '{print $2}')

  # 获取当前节点的主机名和 IP
  current_ip=$(hostname -I | awk '{print $1}')
  current_hostname=$(hostname)

  # 检查当前节点与要修改的节点信息是否匹配
  if [[ "$current_ip" == "$ip" && "$current_hostname" != "$hostname" ]]; then
    echo "Updating hostname to $hostname on $current_ip..."
    hostnamectl set-hostname "$hostname"

    if [ $? -eq 0 ]; then
      echo "Hostname updated successfully."
    else
      echo "Failed to update hostname."
    fi

    break
  fi
done

# 遍历节点信息并添加到 hosts 文件
for node in "${NODES[@]}"; do
  ip=$(echo "$node" | awk '{print $1}')
  hostname=$(echo "$node" | awk '{print $2}')

  # 检查 hosts 文件中是否已存在相应的解析
  if grep -q "$ip $hostname" /etc/hosts; then
    echo "Host entry for $hostname already exists in /etc/hosts."
  else
    # 添加节点的解析条目到 hosts 文件
    sudo sh -c "echo '$ip $hostname' >> /etc/hosts"
    echo "Added host entry for $hostname in /etc/hosts."
  fi
done

if [[ ! -s ~/.ssh/id_rsa.pub ]]; then
    ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q -b 2048
fi

# 检查并安装 sshpass 工具
if ! which sshpass &> /dev/null; then
    echo "sshpass 工具未安装,正在安装 sshpass..."
    sudo apt-get install -y sshpass
fi

# 遍历所有节点进行免密操作
for node in "${NODES[@]}"; do
    ip=$(echo "$node" | awk '{print $1}')
    hostname=$(echo "$node" | awk '{print $2}')
    user=$(echo "$node" | awk '{print $3}')

    # 使用 sshpass 提供密码,并自动确认密钥
    sshpass -p "$HOST_PASS" ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub "$user@$hostname"
done

# 时间同步
apt install -y chrony
if [[ $TIME_SERVER_IP == *$(hostname -I)* ]]; then
    # 配置当前节点为时间同步源
    sed -i '20,23s/^/#/g' /etc/chrony/chrony.conf
    echo "server $TIME_SERVER iburst maxsources 2" >> /etc/chrony/chrony.conf
    echo "allow $TIME_SERVER_IP" >> /etc/chrony/chrony.conf
    echo "local stratum 10" >> /etc/chrony/chrony.conf
else
    # 配置当前节点同步到目标节点
    sed -i '20,23s/^/#/g' /etc/chrony/chrony.conf
    echo "pool $TIME_SERVER iburst maxsources 2" >> /etc/chrony/chrony.conf
fi

# 重启并启用 chrony 服务
systemctl restart chronyd
systemctl enable chronyd

echo "###############################################################"
echo "#################      集群初始化成功     #####################"
echo "###############################################################"

脚本测试

节点 IP
controller 192.168.200.180
compute 192.168.200.182

双节点测试

root@localhost:~# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d8:a3:39 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.200.180/24 brd 192.168.200.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed8:a339/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d8:a3:43 brd ff:ff:ff:ff:ff:ff
    altname enp2s2
    inet 192.168.200.181/24 brd 192.168.200.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed8:a343/64 scope link
       valid_lft forever preferred_lft forever
root@localhost:~# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ee:13:d7 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.200.182/24 brd 192.168.200.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feee:13d7/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ee:13:e1 brd ff:ff:ff:ff:ff:ff
    altname enp2s2
    inet 192.168.200.183/24 brd 192.168.200.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feee:13e1/64 scope link
       valid_lft forever preferred_lft forever

双节点上运行

root@localhost:~# cat init.sh
#!/bin/bash

# 定义节点信息
NODES=("192.168.200.180 controller root" "192.168.200.182 compute root")

# 定义当前节点的密码(默认集群统一密码)
HOST_PASS="000000"

# 时间同步的目标节点
TIME_SERVER=controller

# 时间同步的地址段
TIME_SERVER_IP=192.160.200.0/24

# 欢迎界面
cat > /etc/motd <<EOF
 ################################
 #    Welcome  to  openstack    #
 ################################
EOF

# 修改主机名
for node in "${NODES[@]}"; do
  ip=$(echo "$node" | awk '{print $1}')
  hostname=$(echo "$node" | awk '{print $2}')

  # 获取当前节点的主机名和 IP
  current_ip=$(hostname -I | awk '{print $1}')
  current_hostname=$(hostname)

  # 检查当前节点与要修改的节点信息是否匹配
  if [[ "$current_ip" == "$ip" && "$current_hostname" != "$hostname" ]]; then
    echo "Updating hostname to $hostname on $current_ip..."
    hostnamectl set-hostname "$hostname"

    if [ $? -eq 0 ]; then
      echo "Hostname updated successfully."
    else
      echo "Failed to update hostname."
    fi

    break
  fi
done

# 遍历节点信息并添加到 hosts 文件
for node in "${NODES[@]}"; do
  ip=$(echo "$node" | awk '{print $1}')
  hostname=$(echo "$node" | awk '{print $2}')

  # 检查 hosts 文件中是否已存在相应的解析
  if grep -q "$ip $hostname" /etc/hosts; then
    echo "Host entry for $hostname already exists in /etc/hosts."
  else
    # 添加节点的解析条目到 hosts 文件
    sudo sh -c "echo '$ip $hostname' >> /etc/hosts"
    echo "Added host entry for $hostname in /etc/hosts."
  fi
done

if [[ ! -s ~/.ssh/id_rsa.pub ]]; then
    ssh-keygen -t rsa -N '' -f ~/.ssh/id_rsa -q -b 2048
fi

# 检查并安装 sshpass 工具
if ! which sshpass &> /dev/null; then
    echo "sshpass 工具未安装,正在安装 sshpass..."
    sudo apt-get install -y sshpass
fi

# 遍历所有节点进行免密操作
for node in "${NODES[@]}"; do
    ip=$(echo "$node" | awk '{print $1}')
    hostname=$(echo "$node" | awk '{print $2}')
    user=$(echo "$node" | awk '{print $3}')

    # 使用 sshpass 提供密码,并自动确认密钥
    sshpass -p "$HOST_PASS" ssh-copy-id -o StrictHostKeyChecking=no -i /root/.ssh/id_rsa.pub "$user@$hostname"
done

# 时间同步
apt install -y chrony
if [[ $TIME_SERVER_IP == *$(hostname -I)* ]]; then
    # 配置当前节点为时间同步源
    sed -i '20,23s/^/#/g' /etc/chrony/chrony.conf
    echo "server $TIME_SERVER iburst maxsources 2" >> /etc/chrony/chrony.conf
    echo "allow $TIME_SERVER_IP" >> /etc/chrony/chrony.conf
    echo "local stratum 10" >> /etc/chrony/chrony.conf
else
    # 配置当前节点同步到目标节点
    sed -i '20,23s/^/#/g' /etc/chrony/chrony.conf
    echo "pool $TIME_SERVER iburst maxsources 2" >> /etc/chrony/chrony.conf
fi

# 重启并启用 chrony 服务
systemctl restart chronyd
systemctl enable chronyd

echo "###############################################################"
echo "#################      集群初始化成功     #####################"
echo "###############################################################"
root@localhost:~# bash init.sh
Added host entry for controller in /etc/hosts.
Added host entry for compute in /etc/hosts.
sshpass 工具未安装,正在安装 sshpass...
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  sshpass
0 upgraded, 1 newly installed, 0 to remove and 12 not upgraded.
Need to get 11.7 kB of archives.
After this operation, 35.8 kB of additional disk space will be used.
Get:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/universe amd64 sshpass amd64 1.09-1 [11.7 kB]
Fetched 11.7 kB in 6s (1,809 B/s)
Selecting previously unselected package sshpass.
(Reading database ... 109470 files and directories currently installed.)
Preparing to unpack .../sshpass_1.09-1_amd64.deb ...
Unpacking sshpass (1.09-1) ...
Setting up sshpass (1.09-1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  systemd-timesyncd
The following NEW packages will be installed:
  chrony
0 upgraded, 1 newly installed, 1 to remove and 11 not upgraded.
Need to get 290 kB of archives.
After this operation, 362 kB of additional disk space will be used.
Get:1 https://mirrors.tuna.tsinghua.edu.cn/ubuntu jammy/main amd64 chrony amd64 4.2-2ubuntu2 [290 kB]
Fetched 290 kB in 7s (43.5 kB/s)
(Reading database ... 109475 files and directories currently installed.)
Removing systemd-timesyncd (249.11-0ubuntu3.7) ...
Selecting previously unselected package chrony.
(Reading database ... 109461 files and directories currently installed.)
Preparing to unpack .../chrony_4.2-2ubuntu2_amd64.deb ...
Unpacking chrony (4.2-2ubuntu2) ...
Setting up chrony (4.2-2ubuntu2) ...

Creating config file /etc/chrony/chrony.conf with new version

Creating config file /etc/chrony/chrony.keys with new version
dpkg-statoverride: warning: --update given but /var/log/chrony does not exist
Created symlink /etc/systemd/system/chronyd.service → /lib/systemd/system/chrony.service.
Created symlink /etc/systemd/system/multi-user.target.wants/chrony.service → /lib/systemd/system/chrony.service.
Processing triggers for dbus (1.12.20-2ubuntu4.1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...

No services need to be restarted.

No containers need to be restarted.

No user sessions are running outdated binaries.

No VM guests are running outdated hypervisor (qemu) binaries on this host.
Failed to enable unit: Refusing to operate on alias name or linked unit file: chronyd.service
###############################################################
#################      集群初始化成功     #####################
###############################################################

验证测试

root@controller:~# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d8:a3:39 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.200.180/24 brd 192.168.200.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed8:a339/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:d8:a3:43 brd ff:ff:ff:ff:ff:ff
    altname enp2s2
    inet 192.168.200.181/24 brd 192.168.200.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fed8:a343/64 scope link
       valid_lft forever preferred_lft forever
root@controller:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 huhy

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.200.180 controller
192.168.200.182 compute
root@controller:~# ssh compute
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-69-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed Jun  7 01:13:53 PM UTC 2023

  System load:  0.0               Processes:              259
  Usage of /:   7.7% of 95.90GB   Users logged in:        1
  Memory usage: 4%                IPv4 address for ens33: 192.168.200.182
  Swap usage:   0%                IPv4 address for ens34: 192.168.200.183


 * Introducing Expanded Security Maintenance for Applications.
   Receive updates to over 25,000 software packages with your
   Ubuntu Pro subscription. Free for personal use.

     https://ubuntu.com/pro

Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update

Last login: Wed Jun  7 13:04:49 2023 from 192.168.200.181
root@localhost:~#
root@controller:~# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? controller                    0   9     0     -     +0ns[   +0ns] +/-    0ns
root@controller:~#
root@localhost:~# bash
root@compute:~# ip a
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ee:13:d7 brd ff:ff:ff:ff:ff:ff
    altname enp2s1
    inet 192.168.200.182/24 brd 192.168.200.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feee:13d7/64 scope link
       valid_lft forever preferred_lft forever
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:0c:29:ee:13:e1 brd ff:ff:ff:ff:ff:ff
    altname enp2s2
    inet 192.168.200.183/24 brd 192.168.200.255 scope global ens34
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:feee:13e1/64 scope link
       valid_lft forever preferred_lft forever
root@compute:~# cat /etc/hosts
127.0.0.1 localhost
127.0.1.1 huhy

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
192.168.200.180 controller
192.168.200.182 compute
root@compute:~# ssh controller
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-60-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Wed Jun  7 01:18:31 PM UTC 2023

  System load:  0.00537109375     Processes:              262
  Usage of /:   7.7% of 95.90GB   Users logged in:        1
  Memory usage: 5%                IPv4 address for ens33: 192.168.200.180
  Swap usage:   0%                IPv4 address for ens34: 192.168.200.181

 * Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
   just raised the bar for easy, resilient and secure K8s cluster deployment.

   https://ubuntu.com/engage/secure-kubernetes-at-the-edge

 * Introducing Expanded Security Maintenance for Applications.
   Receive updates to over 25,000 software packages with your
   Ubuntu Pro subscription. Free for personal use.

     https://ubuntu.com/pro

Expanded Security Maintenance for Applications is not enabled.

11 updates can be applied immediately.
To see these additional updates run: apt list --upgradable

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


*** System restart required ***
 ################################
 #    Welcome  to  openstack    #
 ################################
Last login: Wed Jun  7 12:53:20 2023 from 192.168.200.1
root@controller:~# exit
logout
Connection to controller closed.
root@compute:~#
root@compute:~# chronyc sources
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^? controller                    0   9     0     -     +0ns[   +0ns] +/-    0ns
root@compute:~#

标签:初始化,00,chrony,forever,192.168,lft,集群,ff,ubuntu2204
From: https://www.cnblogs.com/hoyeong/p/17477990.html

相关文章

  • K8S集群中开启firewalld 防火墙,防火墙策略配置
    在所有节点上执行#确保开启防火墙服务systemctlrestartfirewalld#将集群内所有的节点IP配置到防火墙可信区中firewall-cmd--permanent--zone=trusted--add-source=172.17.185.91firewall-cmd--permanent--zone=trusted--add-source=172.17.185.92firewall-cmd-......
  • Elasticsearch8 单机以及集群部署(docker-compose)
    本文以Elasticsearch8.8.0为例,介绍一下单机安装ES,使用docker-compose方式进行便捷管理需要准本docker环境:一键安装脚本 https://www.cnblogs.com/Alay/p/15433473.html需要准备docker-compose环境:一键安装脚本 https://www.cnblogs.com/Alay/p/15433907.html官网阅......
  • rke高可用k8s集群安装和实现手册
    一、环境基础要求软件环境:下表为rke安装k8s集群需要的软件环境软件版本操作系统systemCentos7.9Docker20.10.20k8s1.25.9rke1.4.5DockerComposev2.18.1 主机、ip、角色主机名称ip地址角色master01......
  • 搭建storm集群(apache-storm-0.9.5.tar.gz)
    //搭建storm集群(apache-storm-0.9.5.tar.gz)#bycoco#2015-07-15前期准备:3台服务器:192.168.8.94 192.168.8.95192.168.8.96去storm官网下载响应版本的软件包:http://storm.apache.org/downloads.html在这里下载的是:wg......
  • 了解 Kubernetes (k8s) 概念,搭建一套集群。
    ......
  • 使用containerd从0搭建k8s(kubernetes)集群
    准备环境准备两台服务器节点,如果需要安装虚拟机,可以参考《wmware和centos安装过程》机器名IP角色CPU内存centos01192.168.109.130master4核2Gcentos02192.168.109.131node4核2G设置主机名,所有节点都执行vim/etc/hosts#增加192.168.109.130cento......
  • Java开发 - 让你少走弯路的Redis集群搭建
    前言前文中,我们已经对Redis的单节点哨兵的搭建方式做了演示和测试,相信大家已经了解了怎么操作,虽然是单节点,但基本已经满足了部分公司的日常需要,毕竟Redis集群不是什么项目都适用,用上了Redis,也未必需要使用哨兵,甚至集群。但今天,我们还是要把Redis哨兵集群的搭建方式给大家做个分享,万......
  • 两种比较好的ES集群迁移数据的方法
    https://blog.csdn.net/yumin841931783/article/details/103092091?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-103092091-blog-110650439.235%5Ev28%5Epc_relevant_default_base1&de......
  • redis集群异常修复
    概述分析redis在异常情况在的处理流程,redis集群在正常情况下自动主从切换,前提条件master在线多于一半的情况https://github.com/tair-opensource/RedisShake/wikihttp://www.redis.cn/commands/cluster-failover.html 环境:centos7.xredis-5.xredis-cluster(3master/3slave......
  • Redis集群-哨兵模式搭建(1主2从3哨兵节点)
    Redis集群-哨兵模式搭建(1主2从3哨兵节点)原创 北极星 运维记事 2023-04-2022:47 发表于四川收录于合集#redis8个主机规划类型IP地址端口号主192.168.77.1456379从1192.168.77.1466379从2192.168.77.1476379哨兵1192.168.77.14526379哨兵2......