run.sh 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. function launchmaster() {
  16. if [[ ! -e /redis-master-data ]]; then
  17. echo "Redis master data doesn't exist, data won't be persistent!"
  18. mkdir /redis-master-data
  19. fi
  20. redis-server /redis-master/redis.conf
  21. }
  22. function launchsentinel() {
  23. while true; do
  24. 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)
  25. if [[ -n ${master} ]]; then
  26. master="${master//\"}"
  27. else
  28. master="${REDIS_MASTER_SERVICE_HOST}"
  29. fi
  30. redis-cli -h ${master} INFO
  31. if [[ "$?" == "0" ]]; then
  32. break
  33. fi
  34. echo "Connecting to master failed. Waiting..."
  35. sleep 10
  36. done
  37. sentinel_conf=/redis-sentinel/sentinel.conf
  38. echo "sentinel monitor mymaster ${master} 6379 2" > ${sentinel_conf}
  39. echo "sentinel down-after-milliseconds mymaster 60000" >> ${sentinel_conf}
  40. echo "sentinel failover-timeout mymaster 180000" >> ${sentinel_conf}
  41. echo "sentinel parallel-syncs mymaster 1" >> ${sentinel_conf}
  42. redis-sentinel ${sentinel_conf}
  43. }
  44. function launchslave() {
  45. while true; do
  46. 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)
  47. if [[ -n ${master} ]]; then
  48. master="${master//\"}"
  49. else
  50. echo "Failed to find master."
  51. sleep 60
  52. exit 1
  53. fi
  54. redis-cli -h ${master} INFO
  55. if [[ "$?" == "0" ]]; then
  56. break
  57. fi
  58. echo "Connecting to master failed. Waiting..."
  59. sleep 10
  60. done
  61. sed -i "s/%master-ip%/${master}/" /redis-slave/redis.conf
  62. sed -i "s/%master-port%/6379/" /redis-slave/redis.conf
  63. redis-server /redis-slave/redis.conf
  64. }
  65. if [[ "${MASTER}" == "true" ]]; then
  66. launchmaster
  67. exit 0
  68. fi
  69. if [[ "${SENTINEL}" == "true" ]]; then
  70. launchsentinel
  71. exit 0
  72. fi
  73. launchslave