Kubernetes开机脚本

一、脚本使用情况

1、nfs挂载;

2、检查集群服务;

二、脚本内容

#! /bin/bash
#Description: 宕机后,启动服务脚本。慎用
# init env
umask 022
PATH="/sbin:/usr/sbin:/bin:/usr/bin"
export PATH
# Get a sane screen width
[ -z "${COLUMNS:-}" ] && COLUMNS=80
[ -z "${CONSOLETYPE:-}" ] && CONSOLETYPE="`/sbin/consoletype`"
#if [ -f /etc/sysconfig/i18n -a -z "${NOLOCALE:-}" ] ; then
#  . /etc/profile.d/lang.sh
#fi
# Read in our configuration
if [ -z "${BOOTUP:-}" ]; then
  if [ -f /etc/sysconfig/init ]; then
      . /etc/sysconfig/init
  else
    # This all seem confusing? Look in /etc/sysconfig/init,
    # or in /usr/doc/initscripts-*/sysconfig.txt
    BOOTUP=color
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \\033[0;39m"
    LOGLEVEL=1
  fi
  if [ "$CONSOLETYPE" = "serial" ]; then

      BOOTUP=serial
      MOVE_TO_COL=
      SETCOLOR_SUCCESS=
      SETCOLOR_FAILURE=
      SETCOLOR_WARNING=
      SETCOLOR_NORMAL=
  fi
fi


# status
RETVAL=0


echo_success() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"  OK  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 0
}
echo_failure() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"FAILED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 1
}


#添加启动服务


echo -e '\033[31m mount nfs filesystem \033[0m'
mountnow=`df -hT|grep nfs`

if [[ $mountnow =~ "/data" || $mountnow =~ "/k8s-data" ]]
then
  echo -e '\033[31m nfs has mount \033[0m'
else
  rpm -qa|grep nfs-utils
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then echo -e '\033[31m nfs-utils has install \033[0m'
  else yum install -y nfs-utils
  fi
  rpm -qa|grep rpcbind
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then echo -e '\033[31m rpcbind has install \033[0m'
  else yum install -y rpcbind
  fi
#检查nfs
ps aux|grep 'nfsv4.0-svc'|grep -v 'grep'
RETVAL=$?
if [ $RETVAL -eq 0 ]
then echo -e '\033[31m nfs has start \033[0m'
else
  systemctl start nfs
  ps aux|grep 'nfsv4.0-svc'|grep -v 'grep'
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then echo -e '\033[31m nfs start success \033[0m'
  else echo -e '\033[31m nfs start fail \033[0m'
  fi
fi
#检查rpcbind
ps aux|grep rpcbind|grep -v 'grep'
RETVAL=$?
if [ $RETVAL -eq 0 ]
then echo -e '\033[31m rpcbind has start \033[0m'

else
  ps aux|grep rpcbind|grep -v 'grep'
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then echo -e '\033[31m rpcbind start success \033[0m'
  else echo -e '\033[31m rpcbind start fail \033[0m'
  fi
fi
  if [ -d /data ]
  then
  tar czvf data.tar.gz /data
  rm -rf /data
  mkdir /data
  mount -t nfs IP:/ /data
  else
  mkdir /data
  mount -t nfs ip:/ /data
  fi
  if [ -d /k8s-data ]
  then
  tar czvf k8s-data.tar.gz /k8s-data
  rm -rf /k8s-data
  mkdir /k8s-data
  mount -t nfs IP:/ /k8s-data
  else
  mkdir /k8s-data
  mount -t nfs IP:/ /k8s-data
  fi
#检查挂载
  mountstatus=`df -hT|grep nfs`
  [[ $mountstatus =~ "/data" && $mountstatus =~ "/k8s-data" ]] && echo -e '\033[31m  mount nfs success \033[0m' || echo -e '\033[31m mount nfs fail \033[0m'
fi
echo -e '\033[31m etcd \033[0m'
systemctl status etcd|grep running

RETVAL=$?
if [ $RETVAL -eq 0 ]
then echo -e '\033[31m etcd is running \033[0m' 
else echo -e '\033[31m etcd dead then starting \033[0m' 
systemctl start etcd
ps aux|grep etcd|grep -v grep
RETVAL=$?
[ $RETVAL -eq 0 ] && echo -e '\033[31m etcd start success \033[0m' || echo -e '\033[31m etcd start fail! please check! \033[0m'
fi


echo -e '\033[31m docker \033[0m'
systemctl status docker|grep running
RETVAL=$?
if [ $RETVAL -eq 0 ]
then echo -e '\033[31m docker is running \033[0m' 
else echo -e '\033[31m docker dead then starting \033[0m' 
systemctl start docker
systemctl status docker|grep running
RETVAL=$?
[ $RETVAL -eq 0 ] && echo -e '\033[31m docker start success \033[0m' || echo -e '\033[31m docker start fail! please check! \033[0m'
fi


echo -e '\033[31m kubelet \033[0m'
systemctl status kubelet|grep running
RETVAL=$?
if [ $RETVAL -eq 0 ]
then echo -e '\033[31m kubelet is running \033[0m' 
else echo -e '\033[31m kubelet dead then starting \033[0m' 
systemctl start kubelet
systemctl status kubelet|grep running
RETVAL=$?
[ $RETVAL -eq 0 ] && echo -e '\033[31m kubelet start success \033[0m' || echo -e '\033[31m kubelet start fail! please check! \033[0m'
fi

echo -e '\033[31m check node status \033[0m'
nodecheck=`kubectl get nodes`
if [[ ! -n $nodecheck ]]
then echo -e '\033[31m there is node donot master,please turn to master and check node status \033[0m'
else echo -e '\033[31m there is master,checking node status now \033[0m'
  nodename=`kubectl get nodes|grep -v NAME|grep Ready|awk '{print $1}'`
  for i in $nodename
  do
    echo -e $i '\033[31m is Ready \033[0m'
  done
  nodenameno=`kubectl get nodes|grep -v NAME|grep NotReady|awk '{print $1}'`
  if [[ ! -n "$nodenameno" ]]
  then echo -e '\033[31m All node is Ready \033[0m'
  else
  for i in $nodenameno
  do
     echo -e $i '\033[31m is NotReady,checking now \033[0m'
     podname=`kubectl get pods --all-namespaces -o wide|grep 192.168.83|grep $i|awk '{print $2}'`
     for i in $podname
     do
       podstatus=`kubectl get pods --all-namespaces -o wide|grep $i|awk '{print $4}'`
       if [ "$podstatus" == "Running" ]
         then echo -e $i '\033[31m is Running \033[0m'
         else echo -e $1 '\033[31m is Dead \033[0m'
       fi
     done
  done
  fi
fi



你可能感兴趣的:(脚本)