Allow multiple k8s instances on the same gke cluster using k8s namespaces

This commit is contained in:
Joshua Thompson 2016-01-19 15:44:57 -08:00
Родитель 587d9caa2e
Коммит 544beec523
16 изменённых файлов: 91 добавлений и 38 удалений

Просмотреть файл

@ -5,13 +5,14 @@
# KUBECTL='gcloud container kubectl' for a while. Now that most of our
# use cases just need KUBECTL=kubectl, we'll make that the default.
KUBECTL=${KUBECTL:-kubectl}
VITESS_NAME=${VITESS_NAME:-'default'}
# This should match the nodePort in vtctld-service.yaml
VTCTLD_PORT=${VTCTLD_PORT:-30001}
# Get the ExternalIP of any node.
get_node_ip() {
$KUBECTL get -o template --template '{{range (index .items 0).status.addresses}}{{if eq .type "ExternalIP" "LegacyHostIP"}}{{.address}}{{end}}{{end}}' nodes
$KUBECTL get -o template --template '{{range (index .items 0).status.addresses}}{{if eq .type "ExternalIP" "LegacyHostIP"}}{{.address}}{{end}}{{end}}' nodes --namespace=$VITESS_NAME
}
# Try to find vtctld address if not provided.
@ -26,7 +27,7 @@ get_vtctld_addr() {
# Find the name of a vtctld pod.
get_vtctld_pod() {
$KUBECTL get -o template --template "{{if ge (len .items) 1 }}{{(index .items 0).metadata.name}}{{end}}" -l 'app=vitess,component=vtctld' pods
$KUBECTL get -o template --template "{{if ge (len .items) 1 }}{{(index .items 0).metadata.name}}{{end}}" -l 'app=vitess,component=vtctld' pods --namespace=$VITESS_NAME
}
start_vtctld_forward() {
@ -37,7 +38,7 @@ start_vtctld_forward() {
fi
tmpfile=`mktemp`
$KUBECTL port-forward -p $pod 0:15999 &> $tmpfile &
$KUBECTL port-forward -p $pod 0:15999 --namespace=$VITESS_NAME &> $tmpfile &
vtctld_forward_pid=$!
until [[ `cat $tmpfile` =~ :([0-9]+)\ -\> ]]; do :; done

Просмотреть файл

@ -8,16 +8,17 @@ set -e
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
VITESS_NAME=${VITESS_NAME=-'default'}
CELLS=${CELLS:-'test'}
cells=`echo $CELLS | tr ',' ' '`
# Delete replication controllers
for cell in 'global' $cells; do
echo "Stopping etcd replicationcontroller for $cell cell..."
$KUBECTL stop replicationcontroller etcd-$cell
$KUBECTL stop replicationcontroller etcd-$cell --namespace=$VITESS_NAME
echo "Deleting etcd service for $cell cell..."
$KUBECTL delete service etcd-$cell
$KUBECTL delete service etcd-$cell-srv
$KUBECTL delete service etcd-$cell --namespace=$VITESS_NAME
$KUBECTL delete service etcd-$cell-srv --namespace=$VITESS_NAME
done

Просмотреть файл

@ -14,7 +14,7 @@ script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
replicas=${ETCD_REPLICAS:-3}
VITESS_NAME=${VITESS_NAME:-'default'}
CELLS=${CELLS:-'test'}
cells=`echo $CELLS | tr ',' ' '`
@ -23,7 +23,7 @@ for cell in 'global' $cells; do
echo "Creating etcd service for $cell cell..."
cat etcd-service-template.yaml | \
sed -e "s/{{cell}}/$cell/g" | \
$KUBECTL create -f -
$KUBECTL create --namespace=$VITESS_NAME -f -
# Expand template variables
sed_script=""
@ -33,6 +33,6 @@ for cell in 'global' $cells; do
# Create the replication controller.
echo "Creating etcd replicationcontroller for $cell cell..."
cat etcd-controller-template.yaml | sed -e "$sed_script" | $KUBECTL create -f -
cat etcd-controller-template.yaml | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
done

Просмотреть файл

@ -4,11 +4,13 @@
set -e
VITESS_NAME=${VITESS_NAME='-default'}
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
echo "Stopping guestbook replicationcontroller..."
$KUBECTL stop replicationcontroller guestbook
$KUBECTL stop replicationcontroller guestbook --namespace=$VITESS_NAME
echo "Deleting guestbook service..."
$KUBECTL delete service guestbook
$KUBECTL delete service guestbook --namespace=$VITESS_NAME

Просмотреть файл

@ -7,8 +7,10 @@ set -e
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
VITESS_NAME=${VITESS_NAME:-'default'}
echo "Creating guestbook service..."
$KUBECTL create -f guestbook-service.yaml
$KUBECTL create --namespace=$VITESS_NAME -f guestbook-service.yaml
echo "Creating guestbook replicationcontroller..."
$KUBECTL create -f guestbook-controller.yaml
$KUBECTL create --namespace=$VITESS_NAME -f guestbook-controller.yaml

Просмотреть файл

@ -0,0 +1,13 @@
#!/bin/bash
# This is an example script that stops vtctld.
set -e
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
namespace=${VITESS_NAME:-'vitess'}
echo "Deleting namespace $namespace..."
$KUBECTL delete namespace $namespace

Просмотреть файл

@ -0,0 +1,6 @@
kind: Namespace
apiVersion: v1
metadata:
name: {{namespace}}
labels:
name: {{namespace}}

Просмотреть файл

@ -0,0 +1,18 @@
#!/bin/bash
# This is an example script that starts vtctld.
set -e
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
namespace=${VITESS_NAME:-'vitess'}
echo "Creating namespace $namespace..."
sed_script=""
for var in namespace; do
sed_script+="s,{{$var}},${!var},g;"
done
cat namespace-template.yaml | sed -e "$sed_script" | $KUBECTL create -f -

Просмотреть файл

@ -1,10 +1,12 @@
#!/bin/bash
GKE_CLUSTER_NAME=${GKE_CLUSTER_NAME:-'example'}
SHARDS=${SHARDS:-'-80,80-'}
TABLETS_PER_SHARD=${TABLETS_PER_SHARD:-3}
CELLS=${CELLS:-'test'}
TEST_MODE=${TEST_MODE:-'0'}
VITESS_NAME=${VITESS_NAME:-'default'}
export VITESS_NAME=$VITESS_NAME
./vtgate-down.sh
SHARDS=$SHARDS CELLS=$CELLS TABLETS_PER_SHARD=$TABLETS_PER_SHARD ./vttablet-down.sh
@ -12,9 +14,11 @@ SHARDS=$SHARDS CELLS=$CELLS TABLETS_PER_SHARD=$TABLETS_PER_SHARD ./vttablet-down
./etcd-down.sh
if [ $TEST_MODE -gt 0 ]; then
gcloud compute firewall-rules delete ${GKE_CLUSTER_NAME}-vtctld -q
gcloud compute firewall-rules delete ${VITESS_NAME}-vtctld -q
fi
for cell in `echo $CELLS | tr ',' ' '`; do
gcloud compute firewall-rules delete ${GKE_CLUSTER_NAME}-vtgate-$cell -q
gcloud compute firewall-rules delete ${VITESS_NAME}-vtgate-$cell -q
done
./namespace-down.sh

Просмотреть файл

@ -12,8 +12,7 @@
# 7. Forward vtgate port
# Customizable parameters
GKE_ZONE=${GKE_ZONE:-'us-central1-b'}
GKE_CLUSTER_NAME=${GKE_CLUSTER_NAME:-'example'}
VITESS_NAME=${VITESS_NAME:-'vitess'}
SHARDS=${SHARDS:-'-80,80-'}
TABLETS_PER_SHARD=${TABLETS_PER_SHARD:-3}
RDONLY_COUNT=${RDONLY_COUNT:-0}
@ -30,9 +29,6 @@ TEST_MODE=${TEST_MODE:-'0'}
cells=`echo $CELLS | tr ',' ' '`
num_cells=`echo $cells | wc -w`
# Get region from zone (everything to last dash)
gke_region=`echo $GKE_ZONE | sed "s/-[^-]*$//"`
num_shards=`echo $SHARDS | tr "," " " | wc -w`
total_tablet_count=$(($num_shards*$TABLETS_PER_SHARD*$num_cells))
vtgate_count=$VTGATE_COUNT
@ -51,6 +47,7 @@ export VTGATE_TEMPLATE=$VTGATE_TEMPLATE
export VTTABLET_TEMPLATE=$VTTABLET_TEMPLATE
export VTGATE_REPLICAS=$vtgate_count
export VTCTLD_SERVICE_TYPE=$VTCTLD_SERVICE_TYPE
export VITESS_NAME=$VITESS_NAME
function update_spinner_value () {
spinner='-\|/'
@ -72,7 +69,7 @@ function wait_for_running_tasks () {
while [ $counter -lt $MAX_TASK_WAIT_RETRIES ]; do
# Get status column of pods with name starting with $task_name,
# count how many are in state Running
num_running=`$KUBECTL get pods | grep ^$task_name | grep Running | wc -l`
num_running=`$KUBECTL get pods --namespace=$VITESS_NAME | grep ^$task_name | grep Running | wc -l`
echo -en "\r$task_name: $num_running out of $num_tasks in state Running..."
if [ $num_running -eq $num_tasks ]
@ -107,6 +104,8 @@ echo "* VTGate count: $vtgate_count"
echo "* Cells: $cells"
echo "****************************"
echo 'Running namespace-up.sh' && ./namespace-up.sh
echo 'Running etcd-up.sh' && CELLS=$CELLS ./etcd-up.sh
wait_for_running_tasks etcd-global 3
for cell in $cells; do
@ -183,10 +182,10 @@ echo Done
if [ $TEST_MODE -gt 0 ]; then
echo Creating firewall rule for vtctld
vtctld_port=15000
gcloud compute firewall-rules create ${GKE_CLUSTER_NAME}-vtctld --allow tcp:$vtctld_port
gcloud compute firewall-rules create ${VITESS_NAME}-vtctld --allow tcp:$vtctld_port
vtctld_ip=''
until [ $vtctld_ip ]; do
vtctld_ip=`kubectl get -o template --template '{{if ge (len .status.loadBalancer) 1}}{{index (index .status.loadBalancer.ingress 0) "ip"}}{{end}}' service vtctld`
vtctld_ip=`$KUBECTL get -o template --template '{{if ge (len .status.loadBalancer) 1}}{{index (index .status.loadBalancer.ingress 0) "ip"}}{{end}}' service vtctld --namespace=$VITESS_NAME`
sleep 1
done
vtctld_server="$vtctld_ip:$vtctld_port"
@ -196,10 +195,10 @@ vtgate_servers=''
for cell in $cells; do
echo Creating firewall rule for vtgate in cell $cell
vtgate_port=15001
gcloud compute firewall-rules create ${GKE_CLUSTER_NAME}-vtgate-$cell --allow tcp:$vtgate_port
gcloud compute firewall-rules create ${VITESS_NAME}-vtgate-$cell --allow tcp:$vtgate_port
vtgate_ip=''
until [ $vtgate_ip ]; do
vtgate_ip=`kubectl get -o template --template '{{if ge (len .status.loadBalancer) 1}}{{index (index .status.loadBalancer.ingress 0) "ip"}}{{end}}' service vtgate-$cell`
vtgate_ip=`$KUBECTL get -o template --template '{{if ge (len .status.loadBalancer) 1}}{{index (index .status.loadBalancer.ingress 0) "ip"}}{{end}}' service vtgate-$cell --namespace=$VITESS_NAME`
sleep 1
done
vtgate_servers+="vtgate-$cell: $vtgate_ip:$vtgate_port,"
@ -208,7 +207,7 @@ done
if [ -n "$NEWRELIC_LICENSE_KEY" ]; then
echo Setting up Newrelic monitoring
i=1
for nodename in `$KUBECTL get nodes --no-headers | awk '{print $1}'`; do
for nodename in `$KUBECTL get nodes --no-headers --namespace=$VITESS_NAME | awk '{print $1}'`; do
gcloud compute copy-files newrelic.sh $nodename:~/
gcloud compute copy-files newrelic_start_agent.sh $nodename:~/
gcloud compute copy-files newrelic_start_mysql_plugin.sh $nodename:~/

Просмотреть файл

@ -7,8 +7,10 @@ set -e
script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
VITESS_NAME=${VITESS_NAME:-'default'}
echo "Stopping vtctld replicationcontroller..."
$KUBECTL stop replicationcontroller vtctld
$KUBECTL stop replicationcontroller vtctld --namespace=$VITESS_NAME
echo "Deleting vtctld service..."
$KUBECTL delete service vtctld
$KUBECTL delete service vtctld --namespace=$VITESS_NAME

Просмотреть файл

@ -8,13 +8,14 @@ script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh
service_type=${VTCTLD_SERVICE_TYPE:-'ClusterIP'}
VITESS_NAME=${VITESS_NAME:-'default'}
echo "Creating vtctld $service_type service..."
sed_script=""
for var in service_type; do
sed_script+="s,{{$var}},${!var},g;"
done
cat vtctld-service-template.yaml | sed -e "$sed_script" | $KUBECTL create -f -
cat vtctld-service-template.yaml | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
echo "Creating vtctld replicationcontroller..."
# Expand template variables
@ -24,10 +25,10 @@ for var in backup_flags; do
done
# Instantiate template and send to kubectl.
cat vtctld-controller-template.yaml | sed -e "$sed_script" | $KUBECTL create -f -
cat vtctld-controller-template.yaml | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
echo
echo "To access vtctld web UI, start kubectl proxy in another terminal:"
echo " kubectl proxy --port=8001"
echo "Then visit http://localhost:8001/api/v1/proxy/namespaces/default/services/vtctld:web/"
echo "Then visit http://localhost:8001/api/v1/proxy/namespaces/$VITESS_NAME/services/vtctld:web/"

Просмотреть файл

@ -4,6 +4,7 @@
set -e
VITESS_NAME=${VITESS_NAME:-'default'}
CELLS=${CELLS:-'test'}
cells=`echo $CELLS | tr ',' ' '`
@ -12,9 +13,9 @@ source $script_root/env.sh
for cell in $cells; do
echo "Stopping vtgate replicationcontroller in cell $cell..."
$KUBECTL stop replicationcontroller vtgate-$cell
$KUBECTL stop replicationcontroller vtgate-$cell --namespace=$VITESS_NAME
echo "Deleting vtgate service in cell $cell..."
$KUBECTL delete service vtgate-$cell
$KUBECTL delete service vtgate-$cell --namespace=$VITESS_NAME
done

Просмотреть файл

@ -11,6 +11,7 @@ VTGATE_REPLICAS=${VTGATE_REPLICAS:-3}
VTDATAROOT_VOLUME=${VTDATAROOT_VOLUME:-''}
VTGATE_TEMPLATE=${VTGATE_TEMPLATE:-'vtgate-controller-template.yaml'}
CELLS=${CELLS:-'test'}
VITESS_NAME=${VITESS_NAME:-'default'}
vtdataroot_volume='emptyDir: {}'
if [ -n "$VTDATAROOT_VOLUME" ]; then
@ -27,7 +28,7 @@ for cell in $cells; do
done
echo "Creating vtgate service in cell $cell..."
cat vtgate-service-template.yaml | sed -e "$sed_script" | $KUBECTL create -f -
cat vtgate-service-template.yaml | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
sed_script=""
for var in replicas vtdataroot_volume cell; do
@ -35,5 +36,5 @@ for cell in $cells; do
done
echo "Creating vtgate replicationcontroller in cell $cell..."
cat $VTGATE_TEMPLATE | sed -e "$sed_script" | $KUBECTL create -f -
cat $VTGATE_TEMPLATE | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
done

Просмотреть файл

@ -19,6 +19,7 @@ keyspace='test_keyspace'
SHARDS=${SHARDS:-'0'}
TABLETS_PER_SHARD=${TABLETS_PER_SHARD:-5}
UID_BASE=${UID_BASE:-100}
VITESS_NAME=${VITESS_NAME:-'default'}
num_shards=`echo $SHARDS | tr "," " " | wc -w`
uid_base=$UID_BASE
@ -40,7 +41,7 @@ for shard in `seq 1 $num_shards`; do
fi
echo "Deleting pod for tablet $alias..."
$KUBECTL delete pod vttablet-$uid
$KUBECTL delete pod vttablet-$uid --namespace=$VITESS_NAME
done
let cell_index=cell_index+100000000
done

Просмотреть файл

@ -18,6 +18,7 @@ UID_BASE=${UID_BASE:-100}
VTTABLET_TEMPLATE=${VTTABLET_TEMPLATE:-'vttablet-pod-template.yaml'}
VTDATAROOT_VOLUME=${VTDATAROOT_VOLUME:-''}
RDONLY_COUNT=${RDONLY_COUNT:-2}
VITESS_NAME=${VITESS_NAME:-'default'}
vtdataroot_volume='emptyDir: {}'
if [ -n "$VTDATAROOT_VOLUME" ]; then
@ -56,7 +57,7 @@ for shard in $(echo $SHARDS | tr "," " "); do
done
# Instantiate template and send to kubectl.
cat $VTTABLET_TEMPLATE | sed -e "$sed_script" | $KUBECTL create -f -
cat $VTTABLET_TEMPLATE | sed -e "$sed_script" | $KUBECTL create --namespace=$VITESS_NAME -f -
done
let cell_index=cell_index+100000000
done