vitess-up.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #!/bin/bash
  2. # Copyright 2015 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. # This is an example script that creates a fully functional vitess cluster.
  16. # It performs the following steps:
  17. # - Create etcd clusters
  18. # - Create vtctld pod
  19. # - Create vttablet pods
  20. # - Perform vtctl initialization:
  21. # SetKeyspaceShardingInfo, Rebuild Keyspace, Reparent Shard, Apply Schema
  22. # - Create vtgate pods
  23. script_root=`dirname "${BASH_SOURCE}"`
  24. source $script_root/env.sh
  25. cells=`echo $CELLS | tr ',' ' '`
  26. num_cells=`echo $cells | wc -w`
  27. function update_spinner_value () {
  28. spinner='-\|/'
  29. cur_spinner=${spinner:$(($1%${#spinner})):1}
  30. }
  31. function wait_for_running_tasks () {
  32. # This function waits for pods to be in the "Running" state
  33. # 1. task_name: Name that the desired task begins with
  34. # 2. num_tasks: Number of tasks to wait for
  35. # Returns:
  36. # 0 if successful, -1 if timed out
  37. task_name=$1
  38. num_tasks=$2
  39. counter=0
  40. echo "Waiting for ${num_tasks}x $task_name to enter state Running"
  41. while [ $counter -lt $MAX_TASK_WAIT_RETRIES ]; do
  42. # Get status column of pods with name starting with $task_name,
  43. # count how many are in state Running
  44. num_running=`$KUBECTL get pods | grep ^$task_name | grep Running | wc -l`
  45. echo -en "\r$task_name: $num_running out of $num_tasks in state Running..."
  46. if [ $num_running -eq $num_tasks ]
  47. then
  48. echo Complete
  49. return 0
  50. fi
  51. update_spinner_value $counter
  52. echo -n $cur_spinner
  53. let counter=counter+1
  54. sleep 1
  55. done
  56. echo Timed out
  57. return -1
  58. }
  59. if [ -z "$GOPATH" ]; then
  60. echo "ERROR: GOPATH undefined, can't obtain vtctlclient"
  61. exit -1
  62. fi
  63. export KUBECTL='kubectl'
  64. echo "Downloading and installing vtctlclient..."
  65. go get -u github.com/youtube/vitess/go/cmd/vtctlclient
  66. num_shards=`echo $SHARDS | tr "," " " | wc -w`
  67. total_tablet_count=$(($num_shards*$TABLETS_PER_SHARD*$num_cells))
  68. vtgate_count=$VTGATE_COUNT
  69. if [ $vtgate_count -eq 0 ]; then
  70. vtgate_count=$(($total_tablet_count/4>3?$total_tablet_count/4:3))
  71. fi
  72. echo "****************************"
  73. echo "*Creating vitess cluster:"
  74. echo "* Shards: $SHARDS"
  75. echo "* Tablets per shard: $TABLETS_PER_SHARD"
  76. echo "* Rdonly per shard: $RDONLY_COUNT"
  77. echo "* VTGate count: $vtgate_count"
  78. echo "* Cells: $cells"
  79. echo "****************************"
  80. echo 'Running etcd-up.sh' && CELLS=$CELLS ./etcd-up.sh
  81. wait_for_running_tasks etcd-global 3
  82. for cell in $cells; do
  83. wait_for_running_tasks etcd-$cell 3
  84. done
  85. echo 'Running vtctld-up.sh' && ./vtctld-up.sh
  86. echo 'Running vttablet-up.sh' && CELLS=$CELLS ./vttablet-up.sh
  87. echo 'Running vtgate-up.sh' && ./vtgate-up.sh
  88. wait_for_running_tasks vtctld 1
  89. wait_for_running_tasks vttablet $total_tablet_count
  90. wait_for_running_tasks vtgate $vtgate_count
  91. vtctld_port=30001
  92. vtctld_ip=`kubectl get -o yaml nodes | grep 'type: ExternalIP' -B 1 | head -1 | awk '{print $NF}'`
  93. vtctl_server="$vtctld_ip:$vtctld_port"
  94. kvtctl="$GOPATH/bin/vtctlclient -server $vtctl_server"
  95. echo Waiting for tablets to be visible in the topology
  96. counter=0
  97. while [ $counter -lt $MAX_VTTABLET_TOPO_WAIT_RETRIES ]; do
  98. num_tablets=0
  99. for cell in $cells; do
  100. num_tablets=$(($num_tablets+`$kvtctl ListAllTablets $cell | wc -l`))
  101. done
  102. echo -en "\r$num_tablets out of $total_tablet_count in topology..."
  103. if [ $num_tablets -eq $total_tablet_count ]
  104. then
  105. echo Complete
  106. break
  107. fi
  108. update_spinner_value $counter
  109. echo -n $cur_spinner
  110. let counter=counter+1
  111. sleep 1
  112. if [ $counter -eq $MAX_VTTABLET_TOPO_WAIT_RETRIES ]
  113. then
  114. echo Timed out
  115. fi
  116. done
  117. # split_shard_count = num_shards for sharded keyspace, 0 for unsharded
  118. split_shard_count=$num_shards
  119. if [ $split_shard_count -eq 1 ]; then
  120. split_shard_count=0
  121. fi
  122. echo -n Setting Keyspace Sharding Info...
  123. $kvtctl SetKeyspaceShardingInfo -force -split_shard_count $split_shard_count test_keyspace keyspace_id uint64
  124. echo Done
  125. echo -n Rebuilding Keyspace Graph...
  126. $kvtctl RebuildKeyspaceGraph test_keyspace
  127. echo Done
  128. echo -n Reparenting...
  129. shard_num=1
  130. for shard in $(echo $SHARDS | tr "," " "); do
  131. $kvtctl InitShardMaster -force test_keyspace/$shard `echo $cells | awk '{print $1}'`-0000000${shard_num}00
  132. let shard_num=shard_num+1
  133. done
  134. echo Done
  135. echo -n Applying Schema...
  136. $kvtctl ApplySchema -sql "$(cat create_test_table.sql)" test_keyspace
  137. echo Done
  138. echo "****************************"
  139. echo "* Complete!"
  140. echo "* Use the following line to make an alias to kvtctl:"
  141. echo "* alias kvtctl='\$GOPATH/bin/vtctlclient -server $vtctl_server'"
  142. echo "* See the vtctld UI at: http://${vtctld_ip}:30000"
  143. echo "****************************"