Sunday, September 29, 2024

Bash script tự động cài ceph trên Centos 7

-

CEPH là một sản phẩm nguồn mở để triển khai giải pháp storage hợp nhất, tức là nó có thể cung cấp được các loại storage bao gồm: Object Storage, Block Storage và File Storage trên một hệ thống duy nhất. CEPH có thể đáp ứng quy mô triển khai từ vài máy chủ cho tới vài chục máy để cung cấp hạ tầng lưu trữ cho ứng dụng trong hạ tầng của bạn. Sau đây mình sẽ chia sẻ 1 script tự động cài và quản lý ceph storage trên Centos 7.

#!/bin/bash
password_node="password"
function batdau {
clear
echo -e "\n - Please select an application you want to use.\n"
echo -e " 1- Ssh-key."
echo -e " 2- Basic install."
echo -e " 3- Deploy ceph cluster."
echo -e " 4- Deploy Ceph OSD."
echo -e " 5- Add Metadatas."
echo -e " 6- Manage Pool."
echo -e " 7- Manage FS."
echo -e " 8- Enable dashboard."
echo -e " 9- Mount ceph client."
echo -e " 10- Remove ceph cluster."
echo -e " 11- Exit."
read -p "Pick a letter to run a command for more info: " runCommand
if [[ $runCommand == '' ]];then
    echo -e "\nPlease enter all values above.\n"
    batdau
else
case $runCommand in
  1) printf "\nSsh-key...\n" && sshkey;;
  2) printf "\nBasic install...\n" && basic_install;;
  3) printf "\nDeploy ceph cluster...\n" && ceph_install;;
  4) printf "\nDeploy Ceph OSD...\n" && add_hdd;;
  5) printf "\nAdd Metadatas...\n" && create_mds;;
  6) printf "\nManage Pool...\n" && ManagePool;;
  7) printf "\nManage FS...\n" && ManageFS;;
  8) printf "\nEnable dashboard...\n" && enable_dashboard;;  
  9) printf "\nMount ceph client...\n" && mount_ceph;; 
  10) printf "\nRemove ceph cluster...\n" && ceph_remove;; 
  11) printf "\nExit...\n" && exit;; 
esac
fi
}

function import_ceph_node {
rm -rf /etc/hosts
value=('ceph1|10.10.10.101' 'ceph2|10.10.10.102' 'ceph3|10.10.10.103')
echo "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4" >> /etc/hosts
dem=1; for i in ${value[@]}
do
IFS='|' read hostname ipaddr <<< "$i"
echo "$ipaddr $hostname" >> /etc/hosts
ip[$dem]=$ipaddr; name[$dem]=$hostname; total_value=${#value[*]}
((dem++))
done
}

function copy_ceph {
import_ceph_node
dem=1; for ((i=0; i<$total_value; i++)); do
hostname=${name[$dem]}
ipaddr=${ip[$dem]}
for i in $hostname; do scp /etc/hosts $i:/etc/hosts; done
((dem++)); done
}

function sshkey {
import_ceph_node
ssh-keygen
yum -y install sshpass
dem=1
for ((i=0; i<$total_value; i++))
do
hostname=${name[$dem]}
ssh-copy-id $hostname
sshpass -p "$password_node" ssh-copy-id -i ~/.ssh/id_rsa.pub root@$hostname
((dem++))
done
}

function basic_install {
import_ceph_node
copy_ceph
dem=1
for ((i=0; i<$total_value; i++))
do
hostname=${name[$dem]}
ipaddr=${ip[$dem]}
ssh $hostname "$(typeset -f); goto_basic_install"
((dem++))
done
}

function goto_basic_install {
systemctl disable firewalld
systemctl stop firewalld
systemctl disable NetworkManager
systemctl stop NetworkManager
systemctl enable network
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
setenforce 0
check_ntp=$(rpm -qa | grep ntp-)
check_net=$(rpm -qa | grep net-tools)
check_telnet=$(rpm -qa | grep telnet)
check_wget=$(rpm -qa | grep wget)
if [[ $check_ntp == '' || $check_net == '' || $check_telnet == '' || $check_wget == '' ]]; then
yum -y install ntp
yum -y install net-tools
yum -y install telnet 
yum -y install wget
fi
systemctl restart ntpd
systemctl enable ntpd
systemctl status ntpd
ntpdate -q  0.ro.pool.ntp.org  1.ro.pool.ntp.org

add-repos

echo "root ALL = (root) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/root
sudo chmod 0440 /etc/sudoers.d/root
}

function ceph_install {
import_ceph_node
rm -rf ~/.ssh/config; dem=1; for ((i=0; i<$total_value; i++))
do
hostname=${name[$dem]}
#------------- start configure -------------
cat >> ~/.ssh/config << EOF
Host $hostname
   Hostname $hostname
   User root
EOF
#------------- end configure -------------
((dem++))
done
chmod 600 ~/.ssh/config

sudo yum -y install ceph-deploy
sudo yum -y install yum-plugin-priorities python-setuptools

mkdir -p /root/my-cluster
cd /root/my-cluster

ceph-deploy new ${name[1]} --public-network 10.10.10.0/24

dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in $hostname; do ceph-deploy install $i; done
#------------- end configure -------------
((dem++)); done

ceph-deploy mon create-initial

dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------\
if [[ $dem != '1' ]]; then
  for i in $hostname; do ceph-deploy mon add $i; done
fi
#------------- end configure -------------
((dem++)); done

dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in $hostname; do ceph-deploy admin $i; done
#------------- end configure -------------
((dem++)); done

dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in $hostname; do ceph-deploy mgr create $i; done
#------------- end configure -------------
((dem++)); done

chmod 644 /etc/ceph/ceph.client.admin.keyring
}

function add_hdd {
import_ceph_node
cd /root/my-cluster
dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in ${hostname[@]}; do echo $dem")" $i; ip[$dem]=$i; done
#------------- end configure -------------
((dem++)); done

echo 
read -p "- Please select an IP address you want to install the cluster: " dem

function tieptuc {
echo
if [[ $dem != '1' ]]; then
  ssh ${name[$dem]} "lsblk"; else
  lsblk
fi
echo
read -p "- Please select the HDD you want to add: " disk
ceph-deploy osd create --data /dev/$disk ${name[$dem]}
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && tieptuc;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}
tieptuc
}

function create_mds {
import_ceph_node
cd /root/my-cluster
dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in ${hostname[@]}; do echo $dem")" $i; ip[$dem]=$i; done
#------------- end configure -------------
((dem++)); done
echo; read -p "- Please select an hostname you want to install the cluster: " dem
ceph-deploy mds create ${name[$dem]}
cd ~
echo
ceph -s
echo
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && create_mds;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}

function add-repos {
cat > /etc/yum.repos.d/ceph.repo << OEF
[Ceph]
name=Ceph packages for $basearch
baseurl=http://download.ceph.com/rpm-mimic/el7/$basearch
enabled=1
gpgcheck=1
type=rpm-md
gpgkey=https://download.ceph.com/keys/release.asc
priority=1
OEF
sudo sed -i 's|name=.*|name=Ceph packages for $basearch|g' /etc/yum.repos.d/ceph.repo
sudo sed -i 's|baseurl=.*|baseurl=http://download.ceph.com/rpm-mimic/el7/$basearch|g' /etc/yum.repos.d/ceph.repo

cat >> /etc/yum.repos.d/ceph.repo << OEF
[Ceph-noarch]
name=Ceph noarch packages
baseurl=http://download.ceph.com/rpm-mimic/el7/noarch
enabled=1
gpgcheck=1
type=rpm-md
gpgkey=https://download.ceph.com/keys/release.asc
priority=1
[ceph-source]
name=Ceph source packages
baseurl=http://download.ceph.com/rpm-mimic/el7/SRPMS
enabled=1
gpgcheck=1
type=rpm-md
gpgkey=https://download.ceph.com/keys/release.asc
priority=1
OEF
}

function showcluster {
  watch ceph -s
}

function for_value_v1 { j=1; for i in ${value_local[@]}; do echo $j")" $i; ip[$j]=$i; ((j++)); total_value=${#value[*]}; done }

function mount_ceph {
add-repos
import_ceph_node; rm -rf /etc/hosts
value_local=$(ifconfig | grep inet | awk '{print $2}'); for_value_v1; echo; hostname_local=$(hostname)
read -p "- Please select an IP address you want to install: " select_value; ip=${ip[$select_value]}
echo -e "\nYou have selected the ip address: $ip\n"
echo "127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4" >> /etc/hosts; echo "$ip $hostname_local" >> /etc/hosts
dem=1; for i in ${value[@]}; do IFS='|' read hostname ipaddr <<< "$i"; echo "$ipaddr $hostname" >> /etc/hosts; ip[$dem]=$ipaddr; name[$dem]=$hostname; total_value=${#value[*]}; ((dem++)); done
sudo yum install -y yum-utils && sudo yum-config-manager --add-repo https://dl.fedoraproject.org/pub/epel/7/x86_64/ && sudo yum install --nogpgcheck -y epel-release && sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7 && sudo rm /etc/yum.repos.d/dl.fedoraproject.org*
yum -y install python-setuptools python-devel ceph-common ceph-fuse
ssh ${name[1]} "sudo ceph-authtool -p /etc/ceph/ceph.client.admin.keyring" > admin.key 
chmod 600 admin.key
mkdir -p /ceph_storage
mount -t ceph ${name[1]}:6789,${name[1]}:6789,${name[1]}:6789:/ /ceph_storage -o name=admin,secretfile=admin.key
df -hT
}

function create_pool {
echo; ceph osd lspools
echo; read -p "- Pool Name: " pool_name
echo; read -p "- PG Number: " pgnum; echo
ceph osd pool create $pool_name $pgnum; echo
ceph osd lspools
echo; echo -e "\nAdd $pool_name success.................\n"; echo
echo
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && create_pool;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}

function remove_pool {
echo; ceph osd lspools
echo; read -p "- Pool Name you want remove: " pool_name
ceph tell mon.* injectargs --mon-allow-pool-delete=true
ceph osd pool delete $pool_name $pool_name --yes-i-really-really-mean-it; echo; ceph osd lspools
echo; echo -e "\nRemove $pool_name success.................\n"; echo
echo
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && remove_pool;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}

function enable_dashboard {
sleep 2; ceph mgr module ls
sleep 2; ceph mgr module enable dashboard
sleep 2; ceph dashboard create-self-signed-cert
sleep 2; ceph dashboard set-login-credentials hoanghd hoanghd
sleep 2; ceph mgr services
sleep 2; echo; echo -e "\nEnable enable dashboard success, use user and password default [hoanghd].................\n"; echo
}

function ceph_remove {
import_ceph_node
cd /root/my-cluster
dem=1; for ((i=0; i<$total_value; i++)); do hostname=${name[$dem]}
#------------- start configure -------------
for i in ${hostname[@]}; do ceph-deploy purge $i; ceph-deploy purgedata $i; ceph-deploy forgetkeys; ceph-deploy forgetkeys; rm ceph.*; done
#------------- end configure -------------
((dem++)); done
sleep 2; echo; echo -e "\nCeph destroy success.................\n"; batdau
}

function create_fs {
echo; echo -e "Use commanline fs new <fs_name> <metadata> <data> {--force} {--allow-dangerous-metadata-overlay} :  make new filesystem using named pools <metadata> and <data>"
echo; ceph osd lspools
echo; read -p "- FS Name: " fs_name
echo; read -p "- Metadata: " metadata
echo; read -p "- Data Name: " data
ceph fs new $fs_name $metadata $data
echo; echo -e "\nEnable $pool_name success.................\n"; echo
ceph fs ls ; ceph mds stat; echo; ceph fs status; echo
}

function for_value_convert {
j=1
for i in ${value[@]}
    do
        echo $j")" $i
        ip[$j]=$i
        ((j++))
    done
array_name=${value[@]}
array=(${array_name//"|"/ })
set -f
for h in "${!array[@]}"
do
    total_value=${#array[*]}
done
value_end=${array[$total_value - 1 ]}
value_start=${array[ 0 - $total_value]}
value_number=${!array[@]}
value_name=${array[@]}
}

function startandstopfs {
ceph fs status; echo
value=( true false ); for_value_convert; echo 
echo -e "\nChoise False to Start FS or True to Stop FS.\n"
echo; read -p "- FS Name: " fs_name; echo
read -p "- Please select the true or false: " stt; stt=${ip[$stt]}
echo -e "\nChoise False to Start FS or True to Stop FS.\n"
if [[ $stt == 'true' ]]; then
printf_stt="Stop FS"; else printf_stt="Start FS"; fi
echo -e "\nYou are choosing $printf_stt\n"
echo; ceph fs ls; echo
ceph fs set $fs_name down $stt
echo; ceph fs status; echo
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && startandstopfs;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}

function deletefs {
ceph fs ls; echo
value=( true false ); echo 
echo; read -p "- FS Name: " fs_name; echo
ceph fs rm $fs_name --yes-i-really-mean-it
sleep 2; echo; echo -e "\nDelete $fs_name success.................\n"; echo; ceph fs ls; echo
read -n1 -p "Do you want to continue [y = yes, n = no, s = show ceph cluster] " runCommand
echo -e ""
case $runCommand in
    y|Y) printf "\nContinue...\n" && deletefs;;
    n|N) printf "\nGo back start...\n" && batdau;;
    s|S) printf "\nShow cluster...\n" && showcluster;;
esac
}

function ManageFS {
clear
echo -e "\n - Please select an application you want to use.\n"
echo -e " 1- Create Ceph FS."
echo -e " 2- Start and Stop Ceph FS."
echo -e " 3- Delete Ceph FS."
read -p "Pick a letter to run a command for more info: " runCommand

if [[ $runCommand == '' ]];then
    echo -e "\nPlease enter all values above.\n"
    batdau
else
case $runCommand in
  1) printf "\nCreate Ceph FS...\n" && create_fs;;
  2) printf "\nStart and Stop Ceph FS...\n" && startandstopfs;; 
  3) printf "\nDelete Ceph FS...\n" && deletefs;;
esac
fi
}

function ManagePool {
clear
echo -e "\n - Please select an application you want to use.\n"
echo -e " 1- Create pool."
echo -e " 2- Remove pool."
read -p "Pick a letter to run a command for more info: " runCommand

if [[ $runCommand == '' ]];then
    echo -e "\nPlease enter all values above.\n"
    batdau
else
case $runCommand in
  1) printf "\nCreate pool...\n" && create_pool;;
  2) printf "\nRemove pool...\n" && remove_pool;;
esac
fi
}

function remove_osd {
echo; ceph-volume  lvm  list; echo
echo; ceph osd out osd.0; echo
echo; systemctl stop osd.1 ; echo
}

batdau [1-100]

LEAVE A REPLY

Please enter your comment!
Please enter your name here

4,956FansLike
256FollowersFollow
223SubscribersSubscribe
spot_img

Related Stories