create-dev-cluster.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env bash
  2. DEFAULT_NS="rook-ceph"
  3. CLUSTER_FILES="common.yaml operator.yaml cluster-test.yaml cluster-on-pvc-minikube.yaml dashboard-external-http.yaml toolbox.yaml"
  4. MONITORING_FILES="monitoring/prometheus.yaml monitoring/service-monitor.yaml monitoring/exporter-service-monitor.yaml monitoring/prometheus-service.yaml monitoring/rbac.yaml"
  5. SCRIPT_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)
  6. ROOK_EXAMPLES_DIR="${SCRIPT_ROOT}/../../deploy/examples/"
  7. init_vars(){
  8. local rook_profile_name=$1
  9. local rook_cluster_namespace=$2
  10. local rook_operator_namespace=$3
  11. ROOK_PROFILE_NAME=$rook_profile_name
  12. MINIKUBE="minikube --profile $rook_profile_name"
  13. KUBECTL="$MINIKUBE kubectl --"
  14. ROOK_CLUSTER_NS=$rook_cluster_namespace
  15. ROOK_OPERATOR_NS=$rook_operator_namespace
  16. echo "Using $ROOK_CLUSTER_NS as cluster namespace.."
  17. echo "Using $ROOK_OPERATOR_NS as operator namespace.."
  18. }
  19. update_namespaces() {
  20. for file in $CLUSTER_FILES $MONITORING_FILES; do
  21. echo "Updating namespace on $file"
  22. sed -i.bak \
  23. -e "s/\(.*\):.*# namespace:operator/\1: $ROOK_OPERATOR_NS # namespace:operator/g" \
  24. -e "s/\(.*\):.*# namespace:cluster/\1: $ROOK_CLUSTER_NS # namespace:cluster/g" \
  25. -e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:operator/\1:$ROOK_OPERATOR_NS:\2 # serviceaccount:namespace:operator/g" \
  26. -e "s/\(.*serviceaccount\):.*:\(.*\) # serviceaccount:namespace:cluster/\1:$ROOK_CLUSTER_NS:\2 # serviceaccount:namespace:cluster/g" \
  27. -e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:operator/\1: $ROOK_OPERATOR_NS.\2 # driver:namespace:operator/g" \
  28. -e "s/\(.*\): [-_A-Za-z0-9]*\.\(.*\) # driver:namespace:cluster/\1: $ROOK_CLUSTER_NS.\2 # driver:namespace:cluster/g" \
  29. "$file"
  30. done
  31. }
  32. wait_for_ceph_cluster() {
  33. echo "Waiting for ceph cluster to enter HEALTH_OK"
  34. WAIT_CEPH_CLUSTER_RUNNING=20
  35. while ! $KUBECTL get cephclusters.ceph.rook.io -n "$ROOK_CLUSTER_NS" -o jsonpath='{.items[?(@.kind == "CephCluster")].status.ceph.health}' | grep -q "HEALTH_OK"; do
  36. echo "Waiting for Ceph cluster to enter HEALTH_OK"
  37. sleep ${WAIT_CEPH_CLUSTER_RUNNING}
  38. done
  39. echo "Ceph cluster installed and running"
  40. }
  41. get_minikube_driver() {
  42. os=$(uname)
  43. architecture=$(uname -m)
  44. if [[ "$os" == "Darwin" ]]; then
  45. if [[ "$architecture" == "x86_64" ]]; then
  46. echo "hyperkit"
  47. elif [[ "$architecture" == "arm64" ]]; then
  48. echo "qemu"
  49. else
  50. echo "Unknown Architecture on Apple OS"
  51. exit 1
  52. fi
  53. elif [[ "$os" == "Linux" ]]; then
  54. echo "kvm2"
  55. else
  56. echo "Unknown/Unsupported OS"
  57. exit 1
  58. fi
  59. }
  60. show_info() {
  61. local monitoring_enabled=$1
  62. DASHBOARD_PASSWORD=$($KUBECTL -n "$ROOK_CLUSTER_NS" get secret rook-ceph-dashboard-password -o jsonpath="{['data']['password']}" | base64 --decode && echo)
  63. DASHBOARD_END_POINT=$($MINIKUBE service rook-ceph-mgr-dashboard-external-http -n rook-ceph --url)
  64. BASE_URL="$DASHBOARD_END_POINT"
  65. echo "==========================="
  66. echo "Ceph Dashboard:"
  67. echo " IP_ADDR : $BASE_URL"
  68. echo " USER : admin"
  69. echo " PASSWORD : $DASHBOARD_PASSWORD"
  70. if [ "$monitoring_enabled" = true ]; then
  71. PROMETHEUS_API_HOST="http://$(kubectl -n "$ROOK_CLUSTER_NS" -o jsonpath='{.status.hostIP}' get pod prometheus-rook-prometheus-0):30900"
  72. echo "Prometheus Dashboard: "
  73. echo " API_HOST: $PROMETHEUS_API_HOST"
  74. fi
  75. echo "==========================="
  76. echo " "
  77. echo " *** To start using your rook cluster please set the following env: "
  78. echo " "
  79. echo " > eval \$($MINIKUBE docker-env)"
  80. echo " > alias kubectl=\"$KUBECTL"\"
  81. echo " "
  82. echo " *** To access the new cluster with k9s: "
  83. echo " "
  84. echo " > k9s --context $ROOK_PROFILE_NAME"
  85. echo " "
  86. }
  87. check_minikube_exists() {
  88. echo "Checking minikube profile '$ROOK_PROFILE_NAME'..."
  89. if minikube profile list -l 2> /dev/null | grep -qE "\s$ROOK_PROFILE_NAME\s"; then
  90. echo "A minikube profile '$ROOK_PROFILE_NAME' already exists, please use -f to force the cluster creation."
  91. exit 1
  92. fi
  93. }
  94. setup_minikube_env() {
  95. minikube_driver="$(get_minikube_driver)"
  96. echo "Setting up minikube env for profile '$ROOK_PROFILE_NAME' (using $minikube_driver driver)"
  97. $MINIKUBE delete
  98. $MINIKUBE start --disk-size=40g --extra-disks=3 --driver "$minikube_driver"
  99. eval "$($MINIKUBE docker-env)"
  100. }
  101. create_rook_cluster() {
  102. echo "Creating cluster"
  103. # create operator namespace if it doesn't exist
  104. if ! kubectl get namespace "$ROOK_OPERATOR_NS" &> /dev/null; then
  105. kubectl create namespace "$ROOK_OPERATOR_NS"
  106. fi
  107. $KUBECTL apply -f crds.yaml -f common.yaml -f operator.yaml
  108. $KUBECTL apply -f cluster-test.yaml -f toolbox.yaml
  109. $KUBECTL apply -f dashboard-external-http.yaml
  110. }
  111. check_examples_dir() {
  112. CRDS_FILE="crds.yaml"
  113. if [ ! -e ${CRDS_FILE} ]; then
  114. echo "File ${ROOK_EXAMPLES_DIR}/${CRDS_FILE} does not exist. Please, provide a valid rook examples directory."
  115. exit 1
  116. fi
  117. }
  118. wait_for_rook_operator() {
  119. echo "Waiting for rook operator..."
  120. $KUBECTL rollout status deployment rook-ceph-operator -n "$ROOK_OPERATOR_NS" --timeout=180s
  121. while ! $KUBECTL get cephclusters.ceph.rook.io -n "$ROOK_CLUSTER_NS" -o jsonpath='{.items[?(@.kind == "CephCluster")].status.phase}' | grep -q "Ready"; do
  122. echo "Waiting for ceph cluster to become ready..."
  123. sleep 20
  124. done
  125. }
  126. enable_rook_orchestrator() {
  127. echo "Enabling rook orchestrator"
  128. $KUBECTL rollout status deployment rook-ceph-tools -n "$ROOK_CLUSTER_NS" --timeout=90s
  129. $KUBECTL -n "$ROOK_CLUSTER_NS" exec -it deploy/rook-ceph-tools -- ceph mgr module enable rook
  130. $KUBECTL -n "$ROOK_CLUSTER_NS" exec -it deploy/rook-ceph-tools -- ceph orch set backend rook
  131. $KUBECTL -n "$ROOK_CLUSTER_NS" exec -it deploy/rook-ceph-tools -- ceph orch status
  132. }
  133. enable_monitoring() {
  134. echo "Enabling monitoring"
  135. $KUBECTL apply -f https://raw.githubusercontent.com/coreos/prometheus-operator/v0.40.0/bundle.yaml
  136. $KUBECTL wait --for=condition=ready pod -l app.kubernetes.io/name=prometheus-operator --timeout=30s
  137. $KUBECTL apply -f monitoring/rbac.yaml
  138. $KUBECTL apply -f monitoring/service-monitor.yaml
  139. $KUBECTL apply -f monitoring/exporter-service-monitor.yaml
  140. $KUBECTL apply -f monitoring/prometheus.yaml
  141. $KUBECTL apply -f monitoring/prometheus-service.yaml
  142. PROMETHEUS_API_HOST="http://$(kubectl -n "$ROOK_CLUSTER_NS" -o jsonpath='{.status.hostIP}' get pod prometheus-rook-prometheus-0):30900"
  143. $KUBECTL -n "$ROOK_CLUSTER_NS" exec -it deploy/rook-ceph-tools -- ceph dashboard set-prometheus-api-host "$PROMETHEUS_API_HOST"
  144. }
  145. show_usage() {
  146. echo ""
  147. echo " Usage: $(basename "$0") [-r] [-m] [-p <profile-name>] [-d /path/to/rook-examples/dir]"
  148. echo " -f Force cluster creation by deleting minikube profile"
  149. echo " -r Enable rook orchestrator"
  150. echo " -m Enable monitoring"
  151. echo " -p <profile-name> Specify the minikube profile name"
  152. echo " -d value Path to Rook examples directory (i.e github.com/rook/rook/deploy/examples)"
  153. echo " -c <cluster-namespace>"
  154. echo " -o <operator-namespace>"
  155. }
  156. invocation_error() {
  157. printf "%s\n" "$*" > /dev/stderr
  158. show_usage
  159. exit 1
  160. }
  161. ####################################################################
  162. ################# MAIN #############################################
  163. while getopts ":hrmfd:p:c:o:" opt; do
  164. case $opt in
  165. h)
  166. show_usage
  167. exit 0
  168. ;;
  169. r)
  170. enable_rook=true
  171. ;;
  172. m)
  173. enable_monitoring=true
  174. ;;
  175. f)
  176. force_minikube=true
  177. ;;
  178. d)
  179. ROOK_EXAMPLES_DIR="$OPTARG"
  180. ;;
  181. p)
  182. minikube_profile_name="$OPTARG"
  183. ;;
  184. c)
  185. rook_cluster_ns="$OPTARG"
  186. ;;
  187. o)
  188. rook_operator_ns="$OPTARG"
  189. ;;
  190. \?)
  191. invocation_error "Invalid option: -$OPTARG"
  192. ;;
  193. :)
  194. invocation_error "Option -$OPTARG requires an argument."
  195. ;;
  196. esac
  197. done
  198. echo "Using '$ROOK_EXAMPLES_DIR' as examples directory.."
  199. cd "$ROOK_EXAMPLES_DIR" || exit
  200. check_examples_dir
  201. init_vars "${minikube_profile_name:-rook}" "${rook_cluster_ns:-$DEFAULT_NS}" "${rook_operator_ns:-$DEFAULT_NS}"
  202. if [ -z "$force_minikube" ]; then
  203. check_minikube_exists
  204. fi
  205. if [ "$rook_cluster_ns" != "$DEFAULT_NS" ] || [ "$rook_operator_ns" != "$DEFAULT_NS" ]; then
  206. update_namespaces
  207. fi
  208. setup_minikube_env
  209. create_rook_cluster
  210. wait_for_rook_operator
  211. wait_for_ceph_cluster
  212. if [ "$enable_rook" = true ]; then
  213. enable_rook_orchestrator
  214. fi
  215. if [ "$enable_monitoring" = true ]; then
  216. enable_monitoring
  217. fi
  218. show_info "$enable_monitoring"
  219. ####################################################################
  220. ####################################################################