#!/bin/bash # Copyright 2014 The Kubernetes Authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. proc launchmaster { if [[ ! -e /redis-master-data ]] { echo "Redis master data doesn't exist, data won't be persistent!" mkdir /redis-master-data } redis-server /redis-master/redis.conf --protected-mode no } proc launchsentinel { while true { setvar master = $(redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1) if [[ -n ${master} ]] { setvar master = "${master//\"}" } else { setvar master = $(hostname -i) } redis-cli -h ${master} INFO if [[ "$?" == "0" ]] { break } echo "Connecting to master failed. Waiting..." sleep 10 } setvar sentinel_conf = 'sentinel.conf' echo "sentinel monitor mymaster ${master} 6379 2" > ${sentinel_conf} echo "sentinel down-after-milliseconds mymaster 60000" >> ${sentinel_conf} echo "sentinel failover-timeout mymaster 180000" >> ${sentinel_conf} echo "sentinel parallel-syncs mymaster 1" >> ${sentinel_conf} echo "bind 0.0.0.0" >> ${sentinel_conf} redis-sentinel ${sentinel_conf} --protected-mode no } proc launchslave { while true { setvar master = $(redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | cut -d' ' -f1) if [[ -n ${master} ]] { setvar master = "${master//\"}" } else { echo "Failed to find master." sleep 60 exit 1 } redis-cli -h ${master} INFO if [[ "$?" == "0" ]] { break } echo "Connecting to master failed. Waiting..." sleep 10 } sed -i "s/%master-ip%/${master}/" /redis-slave/redis.conf sed -i "s/%master-port%/6379/" /redis-slave/redis.conf redis-server /redis-slave/redis.conf --protected-mode no } if [[ "${MASTER}" == "true" ]] { launchmaster exit 0 } if [[ "${SENTINEL}" == "true" ]] { launchsentinel exit 0 } launchslave