Skip to content

Instantly share code, notes, and snippets.

@mhausenblas
Created April 4, 2018 14:02
Show Gist options
  • Save mhausenblas/c7ff88a7f08505962f3a7194f8d7556e to your computer and use it in GitHub Desktop.
Save mhausenblas/c7ff88a7f08505962f3a7194f8d7556e to your computer and use it in GitHub Desktop.
Developing apps on Kubernetes

Developing apps on Kubernetes

2018-04-04 at CNCF Paris

How do you develop a Kubernetes app, that is, how do you write and test an app that is supposed to run on Kubernetes? In this talk Michael will discuss the challenges as well as review potential tools (incl. Telepresence, ksync, and kubed-sh) you want to be aware of to successfully write Kubernetes apps.

The demo app used throughout here is kubernauts/dok-example-us.

Using ksync with Minikube

Preparation

Install ksync. Then:

kubectl create namespace dok
cd ~/Desktop/ParisCNCF
mkdir -p $(pwd)/ksync
ksync init -n dok
ksync create -n dok --selector=app=stock-con $(pwd)/ksync /app

Live

# deploy app:
kubectl -n=dok apply -f https://raw.githubusercontent.com/kubernauts/dok-example-us/master/stock-gen/app.yaml 
kubectl -n=dok apply -f https://raw.githubusercontent.com/kubernauts/dok-example-us/master/stock-con/app.yaml

# start watching changes:
ksync watch -n dok 

ksync get -n dok

# forward port of stock-con service for local consumption:
kubectl get -n dok po --selector=app=stock-con \
                      -o=custom-columns=:metadata.name --no-headers | \
        xargs -IPOD kubectl -n dok port-forward POD 9898:9898

# check current state of response in another terminal:
watch http localhost:9898/healthz

###############################################################################
# dev iteration
# change stuff in `ksync/` folder, for example update /healthz code in service.js

# clean up:
kubectl delete ns dok

Using Minikube in local dev mode

Based on this local dev setup by Abhishek Tiwari which configures via minikube docker-env the Minikube-internal Docker daemon for building images locally.

Preparation

kubectl create namespace dok
cd ~/Desktop/ParisCNCF
git clone https://github.com/kubernauts/dok-example-us.git && cd dok-example-us
eval $(minikube docker-env)

Live

# deploy app:
kubectl -n=dok apply -f stock-gen/app.yaml 
kubectl -n=dok apply -f stock-con/app.yaml

# forward port of stock-con service for local consumption:
kubectl -n dok get po --selector=app=stock-con \
                      -o=custom-columns=:metadata.name --no-headers | \
        xargs -IPOD kubectl -n dok port-forward POD 9898:9898

###############################################################################
# dev iteration
cd stock-con
docker build -t stock-con:dev -f Dockerfile .
kubectl -n dok set image deployment/stock-con *=stock-con:dev

# check current state of response in another terminal:
watch http localhost:9898/healthz

# clean up:
kubectl delete ns dok

Note: See also how to mount host directories into Minikube.

Using S2I with OSO Pro

Recording in dok-openshift-s2i.mov.

oc new-project dok-test

# stock-gen app:
oc new-app --docker-image=quay.io/mhausenblas/stock-gen:0.3 \
           --name=stock-gen \
           --env=DOK_STOCKGEN_PORT=9999 \
           --env=DOK_STOCKGEN_CRASHEPOCH=100

oc expose dc/stock-gen --port=9999

# stock-con app:
oc new-app https://github.com/kubernauts/dok-example-us \
           --strategy=source \
           --name=stock-con \
           --context-dir=stock-con \
           --env=DOK_STOCKGEN_HOSTNAME=stock-gen \
           --env=DOK_STOCKGEN_PORT=9999

oc get svc

oc patch svc stock-con \
   --type=merge \
   --patch='{"spec": {"ports": [ { "name": "http", "port": 9898, "protocol": "TCP", "targetPort": 9898 } ] } }'

oc expose svc stock-con --port=9898

http http://stock-con-dok-test.b9ad.pro-us-east-1.openshiftapps.com/average/NYSE:RHT

# in stock-con pod do:
curl localhost:9898/healthz

###############################################################################
# dev iteration

# create a Webhook in GitHub for the stock-con build. 
# alternatively manually with `oc start-build stock-con` to trigger build.

# check current state of response in another terminal:
watch http localhost:9898/healthz

# change stuff in the code, for example update /healthz code in service.js

# in stock-con pod do:
curl localhost:9898/healthz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment