Skip to content
Snippets Groups Projects
README.md 3.06 KiB
Newer Older
# CockroachDB configuration preparation

These steps reproduce how to generate Cockroach manifest files used in TeraFlowSDN and apply them to MicroK8s.
For stability reasons, we fix the versions providing the manifest files.
In future releases of TeraFlowSDN, we might consider dynamically downloading and modifying the files.

- Ref: https://www.cockroachlabs.com/docs/stable/configure-cockroachdb-kubernetes.html

## Steps:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

DEPLOY_PATH="manifests/cockroachdb"
OPERATOR_BASE_URL="https://raw.githubusercontent.com/cockroachdb/cockroach-operator/master"

mkdir -p ${DEPLOY_PATH}

# Apply Custom Resource Definition for the CockroachDB Operator
curl -o "${DEPLOY_PATH}/crds.yaml" "${OPERATOR_BASE_URL}/install/crds.yaml"
kubectl apply -f "${DEPLOY_PATH}/crds.yaml"

# Deploy CockroachDB Operator
curl -o "${DEPLOY_PATH}/operator.yaml" "${OPERATOR_BASE_URL}/install/operator.yaml"
nano "${DEPLOY_PATH}/operator.yaml"
# - add env var: WATCH_NAMESPACE=%TFS_CRDB_NAMESPACE%
sed s/%TFS_CRDB_NAMESPACE%/crdb/g ${DEPLOY_PATH}/operator.yaml > ${DEPLOY_PATH}/tfs_crdb_operator.yaml
kubectl apply -f "${DEPLOY_PATH}/tfs_crdb_operator.yaml"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

# Deploy CockroachDB
curl -o "${DEPLOY_PATH}/cluster.yaml" "${OPERATOR_BASE_URL}/examples/example.yaml"
nano "${DEPLOY_PATH}/cluster.yaml"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# - set version
# - set number of replicas
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
kubectl create namespace crdb
kubectl apply --namespace crdb -f "${DEPLOY_PATH}/cluster.yaml"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

# Deploy CockroachDB Client
curl -o "${DEPLOY_PATH}/client-secure-operator.yaml" "${OPERATOR_BASE_URL}/examples/client-secure-operator.yaml"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
kubectl create --namespace crdb -f "${DEPLOY_PATH}/client-secure-operator.yaml"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

# Add tfs user with admin rights
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
kubectl exec -it cockroachdb-client-secure --namespace crdb -- ./cockroach sql --certs-dir=/cockroach/cockroach-certs --host=cockroachdb-public
    CREATE USER tfs WITH PASSWORD 'tfs123';
    GRANT admin TO tfs;
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

# Expose CockroachDB SQL port (26257)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
PORT=$(kubectl --namespace crdb get service cockroachdb-public -o 'jsonpath={.spec.ports[?(@.name=="sql")].port}')
PATCH='{"data": {"'${PORT}'": "crdb/cockroachdb-public:'${PORT}'"}}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
kubectl patch configmap nginx-ingress-tcp-microk8s-conf --namespace ingress --patch "${PATCH}"

PORT_MAP='{"containerPort": '${PORT}', "hostPort": '${PORT}'}'
CONTAINER='{"name": "nginx-ingress-microk8s", "ports": ['${PORT_MAP}']}'
PATCH='{"spec": {"template": {"spec": {"containers": ['${CONTAINER}']}}}}'
kubectl patch daemonset nginx-ingress-microk8s-controller --namespace ingress --patch "${PATCH}"

# Expose CockroachDB Console port (8080)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
PORT=$(kubectl --namespace crdb get service cockroachdb-public -o 'jsonpath={.spec.ports[?(@.name=="http")].port}')
PATCH='{"data": {"'${PORT}'": "crdb/cockroachdb-public:'${PORT}'"}}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
kubectl patch configmap nginx-ingress-tcp-microk8s-conf --namespace ingress --patch "${PATCH}"

PORT_MAP='{"containerPort": '${PORT}', "hostPort": '${PORT}'}'
CONTAINER='{"name": "nginx-ingress-microk8s", "ports": ['${PORT_MAP}']}'
PATCH='{"spec": {"template": {"spec": {"containers": ['${CONTAINER}']}}}}'
kubectl patch daemonset nginx-ingress-microk8s-controller --namespace ingress --patch "${PATCH}"