github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/openshift-origin/create.sh (about)

     1  #!/bin/bash
     2  
     3  # Copyright 2014 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  # Creates resources from the example, assumed to be run from Kubernetes repo root
    20  echo
    21  echo "===> Initializing:"
    22  if [ ! $(which python) ]
    23  then
    24  	echo "Python is a prerequisite for running this script. Please install Python and try running again."
    25  	exit 1
    26  fi
    27  
    28  if [ ! $(which gcloud) ]
    29  then
    30  	echo "gcloud is a prerequisite for running this script. Please install gcloud and try running again."
    31  	exit 1
    32  fi
    33  
    34  gcloud_instances=$(gcloud compute instances list | grep "\-master")
    35  if [ -z "$gcloud_instances" ] || [ -z "${KUBE_GCE_INSTANCE_PREFIX}" ]
    36  then
    37  	echo "This script is only able to supply the necessary serviceaccount key if you are running on Google"
    38  	echo "Compute Engine using a cluster/kube-up.sh script with KUBE_GCE_INSTANCE_PREFIX set. If this is not"
    39  	echo "the case, be ready to supply a path to the serviceaccount public key."
    40  	if [ -z "${KUBE_GCE_INSTANCE_PREFIX}" ]
    41  	then
    42  		echo "Please provide your KUBE_GCE_INSTANCE_PREFIX now:"
    43  		read KUBE_GCE_INSTANCE_PREFIX
    44  	fi
    45  fi
    46  
    47  export OPENSHIFT_EXAMPLE=$(pwd)/examples/openshift-origin
    48  echo Set OPENSHIFT_EXAMPLE=${OPENSHIFT_EXAMPLE}
    49  export OPENSHIFT_CONFIG=${OPENSHIFT_EXAMPLE}/config
    50  echo Set OPENSHIFT_CONFIG=${OPENSHIFT_CONFIG}
    51  mkdir ${OPENSHIFT_CONFIG}
    52  echo Made dir ${OPENSHIFT_CONFIG}
    53  echo
    54  
    55  echo "===> Setting up OpenShift-Origin namespace:"
    56  kubectl create -f ${OPENSHIFT_EXAMPLE}/openshift-origin-namespace.yaml
    57  echo
    58  
    59  echo "===> Setting up etcd-discovery:"
    60  # A token etcd uses to generate unique cluster ID and member ID. Conforms to [a-z0-9]{40}
    61  export ETCD_INITIAL_CLUSTER_TOKEN=$(python -c "import string; import random; print(''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(40)))")
    62  
    63  # A unique token used by the discovery service. Conforms to etcd-cluster-[a-z0-9]{5}
    64  export ETCD_DISCOVERY_TOKEN=$(python -c "import string; import random; print(\"etcd-cluster-\" + ''.join(random.SystemRandom().choice(string.ascii_lowercase + string.digits) for _ in range(5)))")
    65  sed -i.bak -e "s/INSERT_ETCD_INITIAL_CLUSTER_TOKEN/\"${ETCD_INITIAL_CLUSTER_TOKEN}\"/g" -e "s/INSERT_ETCD_DISCOVERY_TOKEN/\"${ETCD_DISCOVERY_TOKEN}\"/g" ${OPENSHIFT_EXAMPLE}/etcd-controller.yaml
    66  
    67  kubectl create -f ${OPENSHIFT_EXAMPLE}/etcd-discovery-controller.yaml --namespace='openshift-origin'
    68  kubectl create -f ${OPENSHIFT_EXAMPLE}/etcd-discovery-service.yaml --namespace='openshift-origin'
    69  echo
    70  
    71  echo "===> Setting up etcd:"
    72  kubectl create -f ${OPENSHIFT_EXAMPLE}/etcd-controller.yaml --namespace='openshift-origin'
    73  kubectl create -f ${OPENSHIFT_EXAMPLE}/etcd-service.yaml --namespace='openshift-origin'
    74  echo
    75  
    76  echo "===> Setting up openshift-origin:"
    77  kubectl config view --output=yaml --flatten=true --minify=true > ${OPENSHIFT_CONFIG}/kubeconfig
    78  kubectl create -f ${OPENSHIFT_EXAMPLE}/openshift-service.yaml --namespace='openshift-origin'
    79  echo
    80  
    81  export PUBLIC_OPENSHIFT_IP=""
    82  echo "===> Waiting for public IP to be set for the OpenShift Service."
    83  echo "Mistakes in service setup can cause this to loop infinitely if an"
    84  echo "external IP is never set. Ensure that the OpenShift service"
    85  echo "is set to use an external load balancer. This process may take"
    86  echo "a few minutes. Errors can be found in the log file found at:"
    87  echo ${OPENSHIFT_EXAMPLE}/openshift-startup.log
    88  echo "" > ${OPENSHIFT_EXAMPLE}/openshift-startup.log
    89  while [ ${#PUBLIC_OPENSHIFT_IP} -lt 1 ]; do
    90  	echo -n .
    91  	sleep 1
    92  	{
    93  		export PUBLIC_OPENSHIFT_IP=$(kubectl get services openshift --namespace="openshift-origin" --template="{{ index .status.loadBalancer.ingress 0 \"ip\" }}")
    94  	} >> ${OPENSHIFT_EXAMPLE}/openshift-startup.log 2>&1
    95  	if [[ ! ${PUBLIC_OPENSHIFT_IP} =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
    96  		export PUBLIC_OPENSHIFT_IP=""
    97  	fi
    98  done
    99  echo
   100  echo "Public OpenShift IP set to: ${PUBLIC_OPENSHIFT_IP}"
   101  echo
   102  
   103  echo "===> Configuring OpenShift:"
   104  docker run --privileged -v ${OPENSHIFT_CONFIG}:/config openshift/origin start master --write-config=/config --kubeconfig=/config/kubeconfig --master=https://localhost:8443 --public-master=https://${PUBLIC_OPENSHIFT_IP}:8443 --etcd=http://etcd:2379
   105  sudo -E chown -R ${USER} ${OPENSHIFT_CONFIG}
   106  
   107  # The following assumes GCE and that KUBE_GCE_INSTANCE_PREFIX is set
   108  export ZONE=$(gcloud compute instances list | grep "${KUBE_GCE_INSTANCE_PREFIX}\-master" | awk '{print $2}' | head -1)
   109  echo "sudo cat /srv/kubernetes/server.key; exit;" | gcloud compute ssh ${KUBE_GCE_INSTANCE_PREFIX}-master --zone ${ZONE} | grep -Ex "(^\-.*\-$|^\S+$)" > ${OPENSHIFT_CONFIG}/serviceaccounts.private.key
   110  # The following insertion will fail if indentation changes
   111  sed -i -e 's/publicKeyFiles:.*$/publicKeyFiles:/g' -e '/publicKeyFiles:/a \ \ - serviceaccounts.private.key' ${OPENSHIFT_CONFIG}/master-config.yaml
   112  
   113  docker run -it --privileged -e="KUBECONFIG=/config/admin.kubeconfig" -v ${OPENSHIFT_CONFIG}:/config openshift/origin cli secrets new openshift-config /config -o json &> ${OPENSHIFT_EXAMPLE}/secret.json
   114  kubectl create -f ${OPENSHIFT_EXAMPLE}/secret.json --namespace='openshift-origin'
   115  echo
   116  
   117  echo "===> Running OpenShift Master:"
   118  kubectl create -f ${OPENSHIFT_EXAMPLE}/openshift-controller.yaml --namespace='openshift-origin'
   119  echo
   120  
   121  echo Done.