#!/bin/bash
#Auther jia.yu
#version v1.0
redEcho(){
echo -en "\033[31;31m"
echo -en "$1\n"
echo -en "\033[m"
}
function stopFirewall()
{
#stop firewall
FIREWALL_state=`systemctl status firewalld | awk 'NR==3 {print $2}'`
if [ $FIREWALL_state = 'active' ];then
systemctl stop firewalld
systemctl disable firewalld
else
redEcho "firewall is already stopped,nothing to do"
fi
}
function disableSelinux()
{
#disable selinux
SELINUX_state=`getenforce`
if [ $SELINUX_state = 'Enforcing' ];then
sed -i "s#SELINUX=enforcing#SELINUX=disabled#g" /etc/selinux/config
read -p "Reboot system now?[y/n]" input
case $input in
[yY]*)
shutdown -Fr now
;;
[nN]*)
exit
;;
*)
echo "please enter y or n."
exit
;;
esac
else
redEcho "selinux is already disabled,nothing to do."
fi
}
function installTools()
{
#yum install tools
yum -y install wget vim net-tools yum-utils lrzsz git ntpdate
}
function ntpUpdate()
{
#ntpdate
echo "*/10 * * * * root /usr/sbin/ntpdate time.windows.com >/dev/null 2&>1" >> /etc/crontab
/usr/sbin/ntpdate time.windows.com
}
function vimSyntax()
{
#vim syntax
echo "alias vi='vim'">> /etc/bashrc
echo "export LS_OPTIONS='--color=auto'">> /etc/bashrc
source /etc/bashrc
echo "TERM=xterm-color">> /etc/profile
source /etc/profile
}
function dataDir()
{
#node节点需要配置docker存储路径使用,master节点不用
#fdisk /dev/sdb; docker root dir=/data/docker and /data/docker mount /dev/sdb1
fdisk /dev/sdb<<EOF
n
p
1
w
EOF
mkfs.xfs /dev/sdb1
sleep 5
DISK_UUID=`ls -l /dev/disk/by-uuid/* | grep sdb1 | awk '{print $9}' | awk -F "/" '{print $5}'`
if [ -d /data ];then
echo "the data floder exists. nothing to do."
exit
else
mkdir /data
echo "UUID=$DISK_UUID /data xfs rw,pquota 0 0">>/etc/fstab
mount -a
fi
}
function dockerInstall()
{
#node install docker
sudo yum-config-manager \
--add-repo \
https://download.docker.com/linux/centos/docker-ce.repo
sudo yum -y install docker-ce docker-ce-cli containerd.io
systemctl start docker
systemctl enable docker
}
function dockerRegistry()
{
sudo mkdir -p /etc/docker
cat >> /etc/docker/daemon.json <<EOF
{
"registry-mirrors": ["https://w97h49tv.mirror.aliyuncs.com"]
}
EOF
}
function chgDockerDir()
{
#sed -i "s#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock#ExecStart=/usr/bin/dockerd -H 0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock#g" \
#/usr/lib/systemd/system/docker.service
#change docker basedir
sed -i "s#ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock#ExecStart=/usr/bin/dockerd --data-root=/data/docker $DOCKER_NETWORK_OPTIONS#g" /usr/lib/systemd/system/docker.service
#reload docker
systemctl daemon-reload
systemctl restart docker
}
function configureKernel()
{
#sysctl
cat >>/etc/sysctl.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.swappiness=0
fs.file-max=52706963
fs.nr_open=52706963
EOF
sysctl -p
}
function chgHosts()
{
#hosts
cat >>/etc/hosts<<EOF
172.16.90.131 master01
172.16.90.132 master02
172.16.90.133 master03
172.16.90.134 node01
172.16.90.135 node02
EOF
}
function menu()
{
cat <<eof
|======================================================================|
| MENU |
|======================================================================|
| 1.关闭系统防火墙(stopfirewall) |
| 2.禁用Selinux(disableSelinux) |
| 3.安装常用工具(yum install wget|vim|net-tools|lrzsz|git|ntpdate) |
| 4.格式化数据盘并挂载/data目录 |
| 5.安装docker(安装并修改docker base-dir,镜像加速) |
| 6.修改内核运行参数(chgKernelPara) |
| 7.修改hosts文件(chgHostsFile) |
| 8.退出exit |
|======================================================================|
eof
}
function usage()
{
read -p "please input your choice: " choice
case $choice in
1)
stopFirewall
;;
2)
disableSelinux
;;
3)
installTools
ntpUpdate
vimSyntax
;;
4)
dataDir
;;
5)
dockerInstall
dockerRegistry
chgDockerDir
;;
6)
configureKernel
;;
7)
chgHosts
;;
8)
exit 0
;;
*)
echo "please input number etc [1|2|3|4|5|6|7|8]."
;;
esac
}
function main()
{
while true
do
menu
usage
done
}
main