on-start.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #! /bin/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 -eo pipefail
  16. # This script configures zookeeper cluster member ship for version of zookeeper
  17. # >= 3.5.0. It should not be used with the on-change.sh script in this example.
  18. # As of April-2016 is 3.4.8 is the latest stable.
  19. # Both /opt and /tmp/zookeeper are assumed to be volumes shared with the parent.
  20. # The format of each line in the dynamic config file is:
  21. # server.<1 based index>=<server-dns-name>:<peer port>:<election port>[:role];[<client port address>:]<client port>
  22. # <1 based index> is the server index that matches the id in datadir/myid
  23. # <peer port> is the port on which peers communicate to agree on updates
  24. # <election port> is the port used for leader election
  25. # [:role] can be set to observer, participant by default
  26. # <client port address> is optional and defaults to 0.0.0.0
  27. # <client port> is the port on which the server accepts client connections
  28. CFG=/opt/zookeeper/conf/zoo.cfg.dynamic
  29. CFG_BAK=/opt/zookeeper/conf/zoo.cfg.bak
  30. MY_ID_FILE=/tmp/zookeeper/myid
  31. HOSTNAME=$(hostname)
  32. while read -ra LINE; do
  33. PEERS=("${PEERS[@]}" "${LINE[0]}")
  34. done
  35. # Don't add the first member as an observer
  36. if [ ${#PEERS[@]} -eq 1 ]; then
  37. # We need to write our index in this list of servers into MY_ID_FILE.
  38. # Note that this may not always coincide with the hostname id.
  39. echo 1 > "${MY_ID_FILE}"
  40. echo "server.1=${PEERS[0]}:2888:3888;2181" > "${CFG}"
  41. # TODO: zkServer-initialize is the safe way to handle changes to datadir
  42. # because simply starting will create a new datadir, BUT if the user changed
  43. # pod template they might end up with 2 datadirs and brief split brain.
  44. exit
  45. fi
  46. # Every subsequent member is added as an observer and promoted to a participant
  47. echo "" > "${CFG_BAK}"
  48. i=0
  49. LEADER=$HOSTNAME
  50. for peer in "${PEERS[@]}"; do
  51. (( i=i+1 ))
  52. if [[ "${peer}" == *"${HOSTNAME}"* ]]; then
  53. MY_ID=$i
  54. MY_NAME=${peer}
  55. echo $i > "${MY_ID_FILE}"
  56. echo "server.${i}=${peer}:2888:3888:observer;2181" >> "${CFG_BAK}"
  57. else
  58. if [[ $(echo srvr | /opt/nc "${peer}" 2181 | grep Mode) = "Mode: leader" ]]; then
  59. LEADER="${peer}"
  60. fi
  61. echo "server.${i}=${peer}:2888:3888:participant;2181" >> "${CFG_BAK}"
  62. fi
  63. done
  64. # zookeeper won't start without myid anyway.
  65. # This means our hostname wasn't in the peer list.
  66. if [ ! -f "${MY_ID_FILE}" ]; then
  67. exit 1
  68. fi
  69. # Once the dynamic config file is written it shouldn't be modified, so the final
  70. # reconfigure needs to happen through the "reconfig" command.
  71. cp ${CFG_BAK} ${CFG}
  72. # TODO: zkServer-initialize is the safe way to handle changes to datadir
  73. # because simply starting will create a new datadir, BUT if the user changed
  74. # pod template they might end up with 2 datadirs and brief split brain.
  75. /opt/zookeeper/bin/zkServer.sh start
  76. # TODO: We shouldn't need to specify the address of the master as long as
  77. # there's quorum. According to the docs the new server is just not allowed to
  78. # vote, it's still allowed to propose config changes, and it knows the
  79. # existing members of the ensemble from *its* config.
  80. ADD_SERVER="server.$MY_ID=$MY_NAME:2888:3888:participant;0.0.0.0:2181"
  81. /opt/zookeeper/bin/zkCli.sh reconfig -s "${LEADER}":2181 -add "${ADD_SERVER}"
  82. # Prove that we've actually joined the running cluster
  83. ITERATION=0
  84. until echo config | /opt/nc localhost 2181 | grep "${ADD_SERVER}" > /dev/null; do
  85. echo $ITERATION] waiting for updated config to sync back to localhost
  86. sleep 1
  87. (( ITERATION=ITERATION+1 ))
  88. if [ $ITERATION -eq 20 ]; then
  89. exit 1
  90. fi
  91. done
  92. /opt/zookeeper/bin/zkServer.sh stop