run.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors.
  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 --protected-mode no
  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=$(hostname -i)
  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=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. echo "bind 0.0.0.0"
  43. redis-sentinel ${sentinel_conf} --protected-mode no
  44. }
  45. function launchslave() {
  46. while true; do
  47. 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)
  48. if [[ -n ${master} ]]; then
  49. master="${master//\"}"
  50. else
  51. echo "Failed to find master."
  52. sleep 60
  53. exit 1
  54. fi
  55. redis-cli -h ${master} INFO
  56. if [[ "$?" == "0" ]]; then
  57. break
  58. fi
  59. echo "Connecting to master failed. Waiting..."
  60. sleep 10
  61. done
  62. sed -i "s/%master-ip%/${master}/" /redis-slave/redis.conf
  63. sed -i "s/%master-port%/6379/" /redis-slave/redis.conf
  64. redis-server /redis-slave/redis.conf --protected-mode no
  65. }
  66. if [[ "${MASTER}" == "true" ]]; then
  67. launchmaster
  68. exit 0
  69. fi
  70. if [[ "${SENTINEL}" == "true" ]]; then
  71. launchsentinel
  72. exit 0
  73. fi
  74. launchslave