on-start.sh 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env bash
  2. # Copyright 2016 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. set -e
  16. CFG=/opt/redis/redis.conf
  17. HOSTNAME=$(hostname)
  18. DATADIR="/data"
  19. # Port on which redis listens for connections.
  20. PORT=6379
  21. # Ping everyone but ourself to see if there's a master. Only one pet starts at
  22. # a time, so if we don't see a master we can assume the position is ours.
  23. while read -ra LINE; do
  24. if [[ "${LINE[0]}" == *"${HOSTNAME}"* ]]; then
  25. sed -i -e "s|^bind.*$|bind ${LINE[0]}|" ${CFG}
  26. elif [ "$(/opt/redis/redis-cli -h "${LINE[0]}" info | grep role | sed 's,\r$,,')" = "role:master" ]; then
  27. # TODO: More restrictive regex?
  28. sed -i -e "s|^# slaveof.*$|slaveof ${LINE[0]} ${PORT}|" ${CFG}
  29. fi
  30. done
  31. # Set the data directory for append only log and snapshot files. This should
  32. # be a persistent volume for consistency.
  33. sed -i -e "s|^.*dir .*$|dir ${DATADIR}|" ${CFG}
  34. # The append only log is written for every SET operation. Without this setting,
  35. # redis just snapshots periodically which is only safe for a cache. This will
  36. # produce an appendonly.aof file in the configured data dir.
  37. sed -i -e "s|^appendonly .*$|appendonly yes|" ${CFG}
  38. # Every write triggers an fsync. Recommended default is "everysec", which
  39. # is only safe for AP applications.
  40. sed -i -e "s|^appendfsync .*$|appendfsync always|" ${CFG}