我是靠谱客的博主 闪闪胡萝卜,最近开发中收集的这篇文章主要介绍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 '33[31m mount nfs filesystem 33[0m'
mountnow=`df -hT|grep nfs`

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

else
  ps aux|grep rpcbind|grep -v 'grep'
  RETVAL=$?
  if [ $RETVAL -eq 0 ]
  then echo -e '33[31m rpcbind start success 33[0m'
  else echo -e '33[31m rpcbind start fail 33[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 '33[31m  mount nfs success 33[0m' || echo -e '33[31m mount nfs fail 33[0m'
fi
echo -e '33[31m etcd 33[0m'
systemctl status etcd|grep running

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


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


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

echo -e '33[31m check node status 33[0m'
nodecheck=`kubectl get nodes`
if [[ ! -n $nodecheck ]]
then echo -e '33[31m there is node donot master,please turn to master and check node status 33[0m'
else echo -e '33[31m there is master,checking node status now 33[0m'
  nodename=`kubectl get nodes|grep -v NAME|grep Ready|awk '{print $1}'`
  for i in $nodename
  do
    echo -e $i '33[31m is Ready 33[0m'
  done
  nodenameno=`kubectl get nodes|grep -v NAME|grep NotReady|awk '{print $1}'`
  if [[ ! -n "$nodenameno" ]]
  then echo -e '33[31m All node is Ready 33[0m'
  else
  for i in $nodenameno
  do
     echo -e $i '33[31m is NotReady,checking now 33[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 '33[31m is Running 33[0m'
         else echo -e $1 '33[31m is Dead 33[0m'
       fi
     done
  done
  fi
fi



最后

以上就是闪闪胡萝卜为你收集整理的Kubernetes开机脚本的全部内容,希望文章能够帮你解决Kubernetes开机脚本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部