k8s.io/kubernetes@v1.29.3/test/images/pets/zookeeper-installer/on-start.sh (about)

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