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

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