Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.
Merged
15 changes: 15 additions & 0 deletions .env-kubernetes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export KONFIG_PROJECTROOT=/opt/koding

export KONFIG_MONGO=$BACKEND_POD_IP:27017/koding

export KONFIG_MQ_LOGIN=test
export KONFIG_MQ_PASSWORD=test
export KONFIG_MQ_APIPORT=15672

export KONFIG_REDIS_HOST=$BACKEND_POD_IP
export KONFIG_REDIS_PORT=6379

export KONFIG_POSTGRES_HOST=$BACKEND_POD_IP
export KONFIG_POSTGRES_PORT=5432
export KONFIG_POSTGRES_DBNAME=social
export KONFIG_POSTGRES_USERNAME=socialapplication
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
selenium-debug.log
nginx.conf
nginx.pid
containers.yaml
client-containers.yaml
*.test
*.dump
*.orig
Expand Down
138 changes: 138 additions & 0 deletions config/generateRunFile.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,140 @@ generateDev = (KONFIG, options) ->
fi
}

function k8s () {
params=(start stop)
param=$1
case "${params[@]}" in *"$param"*)
;;
*)
echo "Error: Command not found: $param"
echo "Usage: ./run k8s COMMAND"
echo ""
echo "Commands: "
echo " start : install the services in Kubernetes pods"
echo " stop : stop the currently running Kubernetes cluster"

echo ""
exit 1
;;
esac

if [ "$param" == "start" ]; then

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very big block. Let's split it into a function.

k8s_start

else
minikube stop
fi
}

function k8s_start () {
command -v docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed. Aborting."; exit 1; }
command -v minikube >/dev/null 2>&1 || { echo >&2 "I require a Kubernetes cluster. To install minikube: \
(curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.20.0/minikube-$(uname | awk '{print tolower($0)}')-amd64 && chmod +x minikube && mv minikube /usr/local/bin/)"; exit 1;}
command -v kubectl >/dev/null 2>&1 || { echo >&2 "I require kubectl. To install kubectl: \
(curl -L -O https://storage.googleapis.com/kubernetes-release/release/v1.6.4/bin/$(uname | awk '{print tolower($0)}')/amd64/kubectl && chmod +x kubectl && mv kubectl /usr/local/bin/)"; exit 1;}

# for local development, minikube must be deleted and recreated if a disk resize is needed
export CHANGE_MINIKUBE_NONE_USER=true
sudo -E minikube start --vm-driver=none

sleep 60

export NAMESPACE_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/namespace.yaml"
export BACKEND_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/backend-pod/containers.yaml"
export FRONTEND_DIR="${KONFIG_PROJECTROOT}/deployment/kubernetes/frontend-pod/client-containers.yaml"

envsubst < deployment/kubernetes/backend-pod/containers.yaml.template > deployment/kubernetes/backend-pod/containers.yaml
envsubst < deployment/kubernetes/frontend-pod/client-containers.yaml.template > deployment/kubernetes/frontend-pod/client-containers.yaml

cp $BACKEND_DIR ${KONFIG_PROJECTROOT}/deployment/generated_files
cp $FRONTEND_DIR ${KONFIG_PROJECTROOT}/deployment/generated_files

kubectl apply -f $NAMESPACE_DIR || kubectl create -f $NAMESPACE_DIR || exit 1
kubectl apply -f $BACKEND_DIR || kubectl create -f $BACKEND_DIR || exit 1
kubectl apply -f $FRONTEND_DIR || kubectl create -f $FRONTEND_DIR || exit 1

sleep 5
echo "checking if backend pod is ready"
check_pod_ready
export BACKEND_POD_NAME="$(kubectl get pods --namespace koding -o jsonpath="{.items[0].metadata.name}" || exit 1)"
export FRONTEND_POD_NAME="$(kubectl get pods --namespace koding -o jsonpath="{.items[1].metadata.name}" || exit 1)"
sleep 10

k8s_health_check $BACKEND_POD_NAME koding 5 120 backend
k8s_health_check $FRONTEND_POD_NAME koding 5 120 landing

# guest user can only connect via localhost, a test user equivalent to guest will be created to check connectivity via POD IP.
# more info on guest user update: https://www.rabbitmq.com/blog/2014/04/02/breaking-things-with-rabbitmq-3-3/
k8s_health_check $BACKEND_POD_NAME koding 5 120 rabbitmq
sleep 25
kubectl exec -n koding $BACKEND_POD_NAME -c rabbitmq -- bash -c "rabbitmqctl add_user test test && rabbitmqctl set_user_tags test administrator && rabbitmqctl set_permissions -p / test '.*' '.*' '.*'" || exit 1

k8s_connectivity_check $BACKEND_POD_NAME koding backend

echo "all services are ready"
}

function k8s_connectivity_check () {
# args: $1: POD NAME, $2: NAMESPACE, $3: container name
kubectl exec -n $2 $1 -c $3 -- bash -c "./run is_ready" || exit 1
}

function k8s_health_check () {
# args: $1: POD NAME, $2: NAMESPACE, $3: health check interval (recommended 5 seconds), $4: timeout duration
declare interval=$3
declare timeout=$4
declare duration=0

sleep $interval

declare response_code=$(kubectl exec -n $2 $1 -c $5 -- echo 'i am alive' || exit 1)

echo -n $5 : 'health-check : '

until [[ $response_code != *"error"* ]]; do
if [ $duration -eq $timeout ]; then
echo ' timed out!'
exit 255
fi

echo -n '.'

sleep $interval
duration=$((duration + interval))

response_code=$(kubectl exec -n $2 $1 -c $5 -- echo 'i am alive' || exit 1)
done

echo ' succeeded!'
}

function check_pod_ready () {
remaining_time=600

until eval "check_pod_response"; do
sleep 30
let remaining_time--
if [ $remaining_time == 0 ]; then
echo "time out!"
exit 1
fi
echo -n '.'
done

echo "backend pod is ready and running..."
}

function check_pod_response () {
declare response_code=$(kubectl get pods --namespace koding -o jsonpath="{.items[0].status.phase}" || exit 1)

if [[ $response_code == *"Pending"* ]]; then
return 1
fi

return 0
}

function switch_client_version () {
if [ "$1" == "default" ]; then
rm $KONFIG_PROJECTROOT/CLIENTVERSION
Expand Down Expand Up @@ -659,6 +793,10 @@ generateDev = (KONFIG, options) ->
elif [ "$1" == "switchclient" ]; then
switch_client_version $2

elif [ "$1" == "k8s" ]; then
shift
k8s "$@"

else
echo "Unknown command: $1"
printHelp
Expand Down
93 changes: 93 additions & 0 deletions deployment/kubernetes/backend-pod/containers.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
apiVersion: v1
kind: Pod
metadata:
name: backend
namespace: koding
spec:
restartPolicy: Never
containers:
- name: backend
image: koding/base
workingDir: /opt/koding
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
- name: KONFIG_PROJECTROOT
value: /opt/koding
- name: KONFIG_MONGO
value: $(POD_IP):27017/koding
- name: KONFIG_MQ_LOGIN
value: test
- name: KONFIG_MQ_PASSWORD
value: test
- name: KONFIG_MQ_APIPORT
value: "15672"
- name: KONFIG_MQ_HOST
value: $(POD_IP)
- name: KONFIG_POSTGRES_HOST
value: $(POD_IP)
- name: KONFIG_POSTGRES_PORT
value: "5432"
- name: KONFIG_POSTGRES_DBNAME
value: social
- name: KONFIG_POSTGRES_USERNAME
value: socialapplication
- name: KONFIG_REDIS_HOST
value: $(POD_IP)
- name: KONFIG_REDIS_PORT
value: "6379"
- name: GOPATH
value: $(KONFIG_PROJECTROOT)/go
- name: GOBIN
value: $(GOPATH)/bin
command: [ "scripts/bootstrap-container", "--config", "dev", "--projectRoot", "/opt/koding", "--publicPort", "8090", "--host", "${KONFIG_DOMAINS_BASE}:${KONFIG_PUBLICPORT}", "--hostname", "${KONFIG_DOMAINS_BASE}" ]
ports:
- containerPort: 8090
hostPort: 8090
volumeMounts:
- mountPath: /opt/koding
name: koding-working-tree

- name: mongo
image: mongo:3.2.8
workingDir: /opt/koding
args: [ "--nojournal", "--noprealloc", "--smallfiles" ]
ports:
- containerPort: 27017
hostPort: 27017

- name: postgres
image: koding/postgres
workingDir: /opt/koding
ports:
- containerPort: 5432
hostPort: 5432

# Countly is a TODO for now.
- name: countly
image: koding/countly-server:latest
workingDir: /opt/koding
ports:
- containerPort: 32768
hostPort: 32768

- name: rabbitmq
image: rabbitmq:3-management
workingDir: /opt/koding
ports:
- containerPort: 5672
hostPort: 5672

- name: redis
image: redis
workingDir: /opt/koding
ports:
- containerPort: 6379
hostPort: 6379

volumes:
- name: koding-working-tree
hostPath:
path: ${PWD}
28 changes: 28 additions & 0 deletions deployment/kubernetes/frontend-pod/client-containers.yaml.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
apiVersion: v1
kind: Pod
metadata:
name: frontend
namespace: koding
spec:
restartPolicy: Never
containers:
- name: landing
image: koding/base
workingDir: /opt/koding
command: [ "make", "--directory", "client/landing", "dev" ]
volumeMounts:
- mountPath: /opt/koding
name: koding-working-tree

- name: client
image: koding/base
workingDir: /opt/koding
command: [ "make", "--directory", "client", "development" ]
volumeMounts:
- mountPath: /opt/koding
name: koding-working-tree

volumes:
- name: koding-working-tree
hostPath:
path: ${PWD}
4 changes: 4 additions & 0 deletions deployment/kubernetes/namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: v1
kind: Namespace
metadata:
name: koding