Kubernetes for Development - Install

Tip

If you have the Requirements already installed, you can jump to Create a Cluster

Requirements

Scripts

Clone our https://gitlab.com/kb/k3d GIT repository e.g:

cd ~/dev/module/
git clone git@gitlab.com:kb/k3d.git

Docker

apt install docker.io
usermod -aG docker patrick

Tip

Replace patrick with your own Linux user name.

Tip

Log out of your workstation (and back in again)…

k3d

k3d is https://k3s.io/ running in a Docker container…

wget -q -O - https://raw.githubusercontent.com/rancher/k3d/master/install.sh | TAG=v1.3.4 bash

kubectl

Install kubectl:

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.16.3/bin/linux/amd64/kubectl
mv kubectl ~/bin/
chmod +x ~/bin/kubectl

helm

From Initialize Helm (Install Tiller) (and GitHub issue, tiller won’t install #489)

Download helm from https://github.com/helm/helm/releases/tag/v2.16.1

Note

These instructions work for helm version 2.x.x, but not 3.x.x.

For example:

  • Download helm-v2.16.1-linux-amd64.tar.gz.

  • Extract helm and move it to your /usr/local/bin/helm folder.

Check:

helm version

Create a Cluster

k3d create

Set the KUBECONFIG environment variable:

# bash
export KUBECONFIG=$(k3d get-kubeconfig)
# fish
set -x KUBECONFIG (k3d get-kubeconfig)

Check:

kubectl cluster-info

Configure:

kubectl -n kube-system create serviceaccount tiller
kubectl create clusterrolebinding tiller --clusterrole=cluster-admin --serviceaccount=kube-system:tiller
helm init --service-account tiller

# test (wait for deployment)
kubectl -n kube-system  rollout status deploy/tiller-deploy

Change into our Scripts folder and run the Rancher local storage script e.g:

cd ~/dev/module/k3d/
kubectl apply -f rancher-local-path-storage.yaml
# check
kubectl get storageclass

Note

Not sure if this is needed, but I don’t think Postgres will initilise without it (rancher-local-path-storage.yaml is a copy of the file from https://github.com/rancher/local-path-provisioner).

Postgres

Install k3d and Helm

Change into our Scripts folder and run the init-postgres.sh script e.g:

cd ~/dev/module/k3d/
./init-postgres.sh

# to monitor progress
kubectl get pods -w

Connect to psql

Tip

You may need to wait a minute or two to get a connection…

# bash
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services kb-dev-db-postgresql)
PGPASSWORD="postgres" psql --host $NODE_IP --port $NODE_PORT -U postgres -d postgres

# fish
set -x NODE_IP (kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
set -x NODE_PORT (kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services kb-dev-db-postgresql)
export PGPASSWORD="postgres"; psql --host $NODE_IP --port $NODE_PORT -U postgres -d postgres

Flowable

Install k3d, Helm and Postgres

Add the flowable repo:

helm repo add flowable https://flowable.org/helm/

Connect to psql and create the flowable table:

CREATE DATABASE flowable TEMPLATE=template0 ENCODING='utf-8';

Change into our Scripts folder and run the init-flowable.sh script e.g:

cd ~/dev/module/k3d/
./init-flowable.sh

# to monitor progress
kubectl get pods -w

Create the Ingres:

cd ~/dev/module/k3d/
kubectl apply -f flowable-ingres.yaml

Find the IP address for flowable-rest:

kubectl get ing
# NAME                  HOSTS   ADDRESS      PORTS   AGE
# flowable-rest         *       172.20.0.2   80      2m6s

Browse to:

http GET http://rest-admin:test@172.20.0.2/service/repository/deployments/

Microsoft SQL Server

Change into our Scripts folder and run the init-postgres.sh script e.g:

cd ~/dev/module/k3d/
./init-microsoft-sql-server.sh

# to monitor progress
kubectl get pods -w

To Install tools on Ubuntu 16.04:

curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
apt update
apt install mssql-tools unixodbc-dev

To connect, you first need to initialise your environment:

# fish shell e.g. '.env.fish'
set -x MICROSOFT_SQL_SERVER_HOST (kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
set -x MICROSOFT_SQL_SERVER_PASS (kubectl get secret --namespace default kb-microsoft-mssql-linux-secret -o jsonpath="{.data.sapassword}" | base64 --decode)
set -x MICROSOFT_SQL_SERVER_PORT (kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services kb-microsoft-mssql-linux)

# then connect
export SQLCMDPASSWORD=$MICROSOFT_SQL_SERVER_PASS; sqlcmd -S $MICROSOFT_SQL_SERVER_HOST,$MICROSOFT_SQL_SERVER_PORT -U sa

Restore

# find the name of the pod e.g. 'kb-microsoft-mssql-linux-76c8b6967d-p86lw'
# get access to the file system using the name of the pod e.g.
kubectl exec -it kb-microsoft-mssql-linux-76c8b6967d-p86lw -- /bin/bash
# create a folder for your backup files:
mkdir /var/opt/mssql/backup/

# list the namespaces in your cluster
kubectl get pods --all-namespaces
# copy your backup file to the pod e.g. 'Record.bak'
# kubectl cp /local/path namespace/podname:path/to/directory
# e.g.
kubectl cp ~/repo/backup/Record.bak default/kb-microsoft-mssql-linux-76c8b6967d-p86lw:/var/opt/mssql/backup/

List the logical file names and paths inside the backup. This is done with the RESTORE FILELISTONLY Transact-SQL statement:

export SQLCMDPASSWORD=$MICROSOFT_SQL_SERVER_PASS; \
  sqlcmd -S $MICROSOFT_SQL_SERVER_HOST,$MICROSOFT_SQL_SERVER_PORT -U sa \
  -Q "RESTORE FILELISTONLY FROM DISK = '/var/opt/mssql/backup/Record.bak'"

If you need to, drop the existing database:

export SQLCMDPASSWORD=$MICROSOFT_SQL_SERVER_PASS; \
  sqlcmd -S $MICROSOFT_SQL_SERVER_HOST,$MICROSOFT_SQL_SERVER_PORT -U sa \
  -Q "DROP DATABASE Record"

Restore the backup:

export SQLCMDPASSWORD=$MICROSOFT_SQL_SERVER_PASS; \
  sqlcmd -S $MICROSOFT_SQL_SERVER_HOST,$MICROSOFT_SQL_SERVER_PORT -U sa \
  -Q "RESTORE DATABASE Record FROM DISK = '/var/opt/mssql/backup/Record.bak' WITH MOVE 'Record' TO '/var/opt/mssql/data/Record.mdf', MOVE 'Record_log' TO '/var/opt/mssql/data/Record_log.ldf'"

Commands

Run some SQL:

export SQLCMDPASSWORD=$MICROSOFT_SQL_SERVER_PASS; \
  sqlcmd -S $MICROSOFT_SQL_SERVER_HOST,$MICROSOFT_SQL_SERVER_PORT -U sa

1> SELECT Name FROM sys.Databases
2> GO

1> Use Record
2> GO
Changed database context to 'Record'.

1> SELECT name FROM SYSOBJECTS WHERE xtype='U';
2> GO

Delete Microsoft SQL Server

Warning

This deletes Microsoft SQL Server and data from your cluster.

helm delete kb-microsoft
helm del --purge kb-microsoft;

redis

Install k3d and Helm

Change into our Scripts folder and run the init-redis.sh script e.g:

cd ~/dev/module/k3d/
./init-redis.sh

Connect:

# bash
export NODE_IP=$(kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
export NODE_PORT=$(kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services kb-redis-master)
redis-cli -h $NODE_IP -p $NODE_PORT

# fish
set -x NODE_IP (kubectl get nodes --namespace default -o jsonpath="{.items[0].status.addresses[0].address}")
set -x NODE_PORT (kubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services kb-redis-master)
redis-cli -h $NODE_IP -p $NODE_PORT