github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/src/scripts/kube-up.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2014 Google Inc. All rights reserved.
     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  # Bring up a Kubernetes cluster.
    18  #
    19  # If the full release name (gs://<bucket>/<release>) is passed in then we take
    20  # that directly.  If not then we assume we are doing development stuff and take
    21  # the defaults in the release config.
    22  
    23  # exit on any error
    24  set -e
    25  
    26  source $(dirname $0)/util.sh
    27  
    28  # Make sure that prerequisites are installed.
    29  for x in gcloud gsutil; do
    30    if [ "$(which $x)" == "" ]; then
    31      echo "Can't find $x in PATH, please fix and retry."
    32      exit 1
    33    fi
    34  done
    35  
    36  # Find the release to use.  Generally it will be passed when doing a 'prod'
    37  # install and will default to the release/config.sh version when doing a
    38  # developer up.
    39  find-release $1
    40  
    41  # Detect the project into $PROJECT if it isn't set
    42  detect-project
    43  
    44  # Build up start up script for master
    45  KUBE_TEMP=$(mktemp -d -t kubernetes.XXXXXX)
    46  trap "rm -rf ${KUBE_TEMP}" EXIT
    47  
    48  get-password
    49  echo "Generating password: $user:$passwd"
    50  htpasswd -b -c /tmp/htpasswd $user $passwd
    51  cat << EOF > ~/.kubernetes_auth
    52  {
    53    "User": "$user",
    54    "Password": "$passwd"
    55  }
    56  EOF
    57  chmod 0600 ~/.kubernetes_auth
    58  HTPASSWD=$(cat /tmp/htpasswd)
    59  
    60  (
    61    echo "#! /bin/bash"
    62    echo "MASTER_NAME=${MASTER_NAME}"
    63    echo "MASTER_RELEASE_TAR=${RELEASE_NORMALIZED}/master-release.tgz"
    64    echo "MASTER_HTPASSWD='${HTPASSWD}'"
    65    cat $(dirname $0)/../templates/download-release.sh
    66    cat $(dirname $0)/../templates/salt-master.sh
    67  ) > ${KUBE_TEMP}/master-start.sh
    68  
    69  echo "Starting VMs and configuring firewalls"
    70  gcloud compute firewalls create --quiet ${MASTER_NAME}-https \
    71    --project ${PROJECT} \
    72    --target-tags ${MASTER_TAG} \
    73    --allow tcp:443 &
    74  
    75  gcloud compute instances create ${MASTER_NAME}\
    76    --project ${PROJECT} \
    77    --zone ${ZONE} \
    78    --machine-type ${MASTER_SIZE} \
    79    --image ${IMAGE} \
    80    --tags ${MASTER_TAG} \
    81    --scopes compute-rw storage-full \
    82    --metadata-from-file startup-script=${KUBE_TEMP}/master-start.sh &
    83  
    84  for (( i=0; i<${#MINION_NAMES[@]}; i++)); do
    85    (
    86      echo "#! /bin/bash"
    87      echo "MASTER_NAME=${MASTER_NAME}"
    88      echo "MINION_IP_RANGE=${MINION_IP_RANGES[$i]}"
    89      cat $(dirname $0)/../templates/salt-minion.sh
    90    ) > ${KUBE_TEMP}/minion-start-${i}.sh
    91  
    92    gcloud compute instances create ${MINION_NAMES[$i]} \
    93      --project ${PROJECT} \
    94      --zone ${ZONE} \
    95      --machine-type ${MINION_SIZE} \
    96      --image ${IMAGE} \
    97      --tags ${MINION_TAG} \
    98      --can-ip-forward \
    99      --metadata-from-file startup-script=${KUBE_TEMP}/minion-start-${i}.sh &
   100  
   101    gcloud compute routes create ${MINION_NAMES[$i]} \
   102      --project ${PROJECT} \
   103      --destination-range ${MINION_IP_RANGES[$i]} \
   104      --next-hop-instance ${ZONE}/instances/${MINION_NAMES[$i]} &
   105  done
   106  
   107  FAIL=0
   108  for job in `jobs -p`
   109  do
   110      wait $job || let "FAIL+=1"
   111  done
   112  if (( $FAIL != 0 )); then
   113    echo "${FAIL} commands failed.  Exiting."
   114    exit 2
   115  fi
   116  
   117  
   118  detect-master > /dev/null
   119  
   120  echo "Waiting for cluster initialization."
   121  echo
   122  echo "  This will continually check to see if the API for kubernetes is reachable."
   123  echo "  This might loop forever if there was some uncaught error during start"
   124  echo "  up."
   125  echo
   126  
   127  until $(curl --insecure --user ${user}:${passwd} --max-time 1 \
   128          --fail --output /dev/null --silent https://${KUBE_MASTER_IP}/api/v1beta1/tasks); do
   129      printf "."
   130      sleep 2
   131  done
   132  
   133  echo
   134  echo "Kubernetes cluster is running.  Access the master at:"
   135  
   136  echo
   137  echo "  https://${user}:${passwd}@${KUBE_MASTER_IP}"
   138  echo
   139  
   140