create.sh 5.6 KB

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