Skip to content

Commit

Permalink
Add Kubernetes topo implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Carson Anderson <ca@carsonoid.net>
  • Loading branch information
carsonoid committed Feb 5, 2020
1 parent 157f952 commit c238e28
Show file tree
Hide file tree
Showing 28 changed files with 3,659 additions and 5 deletions.
27 changes: 27 additions & 0 deletions bootstrap.sh
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,33 @@ function install_etcd() {
command -v etcd && echo "etcd already installed" || install_dep "etcd" "v3.3.10" "$VTROOT/dist/etcd" install_etcd


# Download and install k3s, link k3s binary into our root.
function install_k3s() {
local version="$1"
local dist="$2"

case $(uname) in
Linux) local platform=linux;;
*) echo "ERROR: unsupported platform. K3s only supports running on Linux"; exit 1;;
esac

case $(get_arch) in
aarch64) local target="-arm64";;
x86_64) local target="";;
*) echo "ERROR: unsupported architecture"; exit 1;;
esac

download_url=https://github.com/rancher/k3s/releases/download
file="k3s${target}"

local dest="$dist/k3s${target}-${version}-${platform}"
wget -O $dest "$download_url/$version/$file"
chmod +x $dest
ln -snf $dest "$VTROOT/bin/k3s"
}
command -v k3s || install_dep "k3s" "v1.0.0" "$VTROOT/dist/k3s" install_k3s


# Download and install consul, link consul binary into our root.
function install_consul() {
local version="$1"
Expand Down
2 changes: 2 additions & 0 deletions examples/local/101_initial_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ source "${script_root}/env.sh"
# start topo server
if [ "${TOPO}" = "zk2" ]; then
CELL=zone1 "$script_root/zk-up.sh"
elif [ "${TOPO}" = "k8s" ]; then
CELL=zone1 "$script_root/k3s-up.sh"
else
CELL=zone1 "$script_root/etcd-up.sh"
fi
Expand Down
2 changes: 2 additions & 0 deletions examples/local/401_teardown.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ done;

if [ "${TOPO}" = "zk2" ]; then
CELL=zone1 "$script_root/zk-down.sh"
elif [ "${TOPO}" = "k8s" ]; then
CELL=zone1 "$script_root/k3s-down.sh"
else
CELL=zone1 "$script_root/etcd-down.sh"
fi
Expand Down
15 changes: 11 additions & 4 deletions examples/local/env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fi

# mysqld might be in /usr/sbin which will not be in the default PATH
PATH="/usr/sbin:$PATH"
for binary in mysqld etcd etcdctl curl vtctlclient vttablet vtgate vtctld mysqlctl; do
for binary in mysqld etcd etcdctl curl vtctlclient vttablet vtgate vtctld vtctl mysqlctl; do
command -v "$binary" > /dev/null || fail "${binary} is not installed in PATH. See https://vitess.io/docs/get-started/local/ for install instructions."
done;

Expand All @@ -52,12 +52,19 @@ if [ "${TOPO}" = "zk2" ]; then
ZK_SERVER="localhost:21811,localhost:21812,localhost:21813"
# shellcheck disable=SC2034
TOPOLOGY_FLAGS="-topo_implementation zk2 -topo_global_server_address ${ZK_SERVER} -topo_global_root /vitess/global"

mkdir -p $VTDATAROOT/tmp
elif [ "${TOPO}" = "k8s" ]; then
# Set topology environment parameters.
K8S_ADDR="localhost"
K8S_PORT="8443"
K8S_KUBECONFIG=$VTDATAROOT/tmp/k8s.kubeconfig
# shellcheck disable=SC2034
TOPOLOGY_FLAGS="-topo_implementation k8s -topo_k8s_kubeconfig ${K8S_KUBECONFIG} -topo_global_server_address ${K8S_ADDR}:${K8S_PORT} -topo_global_root /vitess/global"
else
ETCD_SERVER="localhost:2379"
TOPOLOGY_FLAGS="-topo_implementation etcd2 -topo_global_server_address $ETCD_SERVER -topo_global_root /vitess/global"

mkdir -p "${VTDATAROOT}/tmp"
mkdir -p "${VTDATAROOT}/etcd"
fi

# Create a tmp dir
mkdir -p "${VTDATAROOT}/tmp"
29 changes: 29 additions & 0 deletions examples/local/k3s-down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is an example script that stops the k3s server started by k3s-up.sh.

set -e

script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh

# Stop K3s server.
echo "Stopping k3s server..."

pid=`cat $VTDATAROOT/tmp/k3s.pid`
echo "Stopping k3s..."
kill -9 $pid
46 changes: 46 additions & 0 deletions examples/local/k3s-up.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is an example script that creates a Kubernetes api for topo use by running k3s

set -e
cell=${CELL:-'test'}

script_root=$(dirname "${BASH_SOURCE[0]}")

# shellcheck source=./env.sh
# shellcheck disable=SC1091
source "${script_root}/env.sh"

k3s server --disable-agent --data-dir "${VTDATAROOT}/k3s/" --https-listen-port "${K8S_PORT}" --write-kubeconfig "${K8S_KUBECONFIG}" > "${VTDATAROOT}"/tmp/k3s.out 2>&1 &
PID=$!
echo $PID > "${VTDATAROOT}/tmp/k3s.pid"
disown -a
echo "Waiting for k3s server to start"
sleep 15

# Use k3s built-in kubectl with custom config
KUBECTL="k3s kubectl --kubeconfig=${K8S_KUBECONFIG}"

# Add the CellInfo description for the cell
set +e
echo "add $cell CellInfo"
vtctl $TOPOLOGY_FLAGS AddCellInfo \
-root /vitess/$cell \
$cell
set -e

echo "k3s start done..."
31 changes: 31 additions & 0 deletions examples/local/k8s-down.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is an example script that stops the k3s server started by k3s-up.sh.

set -e

script_root=`dirname "${BASH_SOURCE}"`
source $script_root/env.sh

# Stop K3s server.
echo "Stopping k3s server..."

pid=`cat $VTDATAROOT/tmp/k3s.pid`
echo "Stopping k3s..."
set +e
kill -9 $pid
rm ${K8S_KUBECONFIG}
21 changes: 21 additions & 0 deletions examples/local/topo-k8s.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

# Copyright 2019 The Vitess Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is an example script that creates a single shard vttablet deployment.

export TOPO='k8s'


2 changes: 1 addition & 1 deletion examples/local/vttablet-up.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash

# Copyright 2019 The Vitess Authors.
#
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ require (
gopkg.in/asn1-ber.v1 v1.0.0-20150924051756-4e86f4367175 // indirect
gopkg.in/ldap.v2 v2.5.0
honnef.co/go/tools v0.0.1-2019.2.3
k8s.io/api v0.0.0-20190819141258-3544db3b9e44
k8s.io/apimachinery v0.0.0-20190817020851-f2f3a405f61d
k8s.io/client-go v0.0.0-20190819141724-e14f31a72a77
mvdan.cc/unparam v0.0.0-20191111180625-960b1ec0f2c2 // indirect
sourcegraph.com/sqs/pbtypes v1.0.0 // indirect
)
Loading

0 comments on commit c238e28

Please sign in to comment.