Redis、ZooKeeper、Kafkaの管理スクリプト実装

Redisサーバー制御スクリプト

#!/bin/bash

REDIS_PORT=6379
LOCAL_IP=$(hostname -I | awk '{print $1}')

start_redis() {
  if ! pgrep -f "redis-server" &> /dev/null; then
    redis-server /opt/redis/config/redis.conf >/dev/null 2>&1
    if [ $? -eq 0 ]; then
      echo "Redis起動成功: $(pgrep -f "redis-server")"
    else
      echo "Redis起動失敗 - ログを確認"
    fi
  else
    echo "Redisは実行中: $(pgrep -f "redis-server")"
  fi
}

stop_redis() {
  if pgrep -f "redis-server" &> /dev/null; then
    redis-cli -h $LOCAL_IP -p $REDIS_PORT shutdown >/dev/null 2>&1
    sleep 2
    if ! pgrep -f "redis-server" &> /dev/null; then
      echo "Redis停止成功"
    else
      echo "Redis停止失敗: $(pgrep -f "redis-server")"
    fi
  else
    echo "Redisは停止中"
  fi
}

case "$1" in
  start) start_redis ;;
  stop)  stop_redis  ;;
  restart)
    stop_redis
    sleep 3
    start_redis
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

Redis Sentinel制御スクリプト

#!/bin/bash

SENTINEL_PORT=26379
LOCAL_IP=$(hostname -I | awk '{print $1}')

manage_sentinel() {
  case $1 in
    start)
      if ! pgrep -f "redis-sentinel" &> /dev/null; then
        redis-sentinel /opt/redis/config/sentinel.conf >/dev/null 2>&1
        [ $? -eq 0 ] && echo "Sentinel起動: $(pgrep -f "redis-sentinel")" || echo "起動失敗"
      else
        echo "Sentinel実行中: $(pgrep -f "redis-sentinel")"
      fi
      ;;
    stop)
      if pgrep -f "redis-sentinel" &> /dev/null; then
        redis-cli -h $LOCAL_IP -p $SENTINEL_PORT shutdown >/dev/null 2>&1
        sleep 2
        pgrep -f "redis-sentinel" &> /dev/null && echo "停止失敗" || echo "停止成功"
      else
        echo "Sentinelは停止中"
      fi
      ;;
  esac
}

case "$1" in
  start|stop) manage_sentinel $1 ;;
  restart)
    manage_sentinel stop
    sleep 2
    manage_sentinel start
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

ZooKeeperクラスタ制御スクリプト

単一サーバー向け(複数ノード)

#!/bin/bash

ZK_NODES=("zk-node1" "zk-node2" "zk-node3")

zk_operation() {
  for node in "${ZK_NODES[@]}"; do
    if [ "$1" = "start" ]; then
      /opt/${node}/bin/zkServer.sh start &>/dev/null && \
        echo "${node}起動: $(pgrep -f ${node})" || echo "${node}起動失敗"
    else
      /opt/${node}/bin/zkServer.sh stop &>/dev/null && \
        echo "${node}停止" || echo "${node}停止失敗"
    fi
  done
}

case "$1" in
  start|stop) zk_operation $1 ;;
  restart)
    zk_operation stop
    sleep 3
    zk_operation start
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

マルチサーバー向け

#!/bin/bash

ZK_HOME="/opt/zookeeper"

zk_manage() {
  if [ "$1" = "start" ]; then
    ${ZK_HOME}/bin/zkServer.sh start &>/dev/null && \
      echo "ZooKeeper起動: $(pgrep -f zookeeper)" || echo "起動失敗"
  else
    ${ZK_HOME}/bin/zkServer.sh stop &>/dev/null && \
      echo "ZooKeeper停止" || echo "停止失敗"
  fi
}

case "$1" in
  start|stop) zk_manage $1 ;;
  restart)
    zk_manage stop
    sleep 3
    zk_manage start
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

Kafkaクラスタ制御スクリプト

単一サーバー向け(複数ブローカー)

#!/bin/bash

BROKERS=("kafka-broker1" "kafka-broker2" "kafka-broker3")

kafka_control() {
  for broker in "${BROKERS[@]}"; do
    if [ "$1" = "start" ]; then
      /opt/${broker}/bin/kafka-server-start.sh -daemon /opt/${broker}/config/server.properties &>/dev/null && \
        echo "${broker}起動: $(pgrep -f ${broker})" || echo "${broker}起動失敗"
    fi
  done
  
  if [ "$1" = "stop" ]; then
    /opt/kafka-broker1/bin/kafka-server-stop.sh &>/dev/null
    sleep 5
    pgrep -f kafka &>/dev/null && echo "停止失敗: $(pgrep -f kafka)" || echo "全Kafka停止"
  fi
}

case "$1" in
  start|stop) kafka_control $1 ;;
  restart)
    kafka_control stop
    sleep 5
    kafka_control start
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

マルチサーバー向け

#!/bin/bash

KAFKA_HOME="/opt/kafka"

kafka_manage() {
  if [ "$1" = "start" ]; then
    ${KAFKA_HOME}/bin/kafka-server-start.sh -daemon ${KAFKA_HOME}/config/server.properties &>/dev/null && \
      echo "Kafka起動: $(pgrep -f kafka)" || echo "起動失敗"
  else
    ${KAFKA_HOME}/bin/kafka-server-stop.sh &>/dev/null
    sleep 5
    pgrep -f kafka &>/dev/null && echo "停止失敗: $(pgrep -f kafka)" || echo "Kafka停止"
  fi
}

case "$1" in
  start|stop) kafka_manage $1 ;;
  restart)
    kafka_manage stop
    sleep 5
    kafka_manage start
    ;;
  *)
    echo "使用法: $0 [start|stop|restart]"
    ;;
esac

タグ: redis Sentinel ZooKeeper Kafka Shellスクリプト

8月15日 23:20 投稿