Skip to content

Instantly share code, notes, and snippets.

@kadel
Last active February 18, 2019 13:56
Show Gist options
  • Save kadel/fbb416b3b185f597efc43217ec17b22b to your computer and use it in GitHub Desktop.
Save kadel/fbb416b3b185f597efc43217ec17b22b to your computer and use it in GitHub Desktop.
odo like component on Kubernetes (nodejs)
# Bootstrap odo like component on Kubernetes
---
# PVC where source code is persistented
# This is required to make sure that source is not lost form to container,
# because it is not part of the container image.
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
labels:
app.kubernetes.io/name: myapp
name: nodejs-myapp-s2idata
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs
labels:
app.kubernetes.io/name: myapp
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/name: myapp
strategy:
type: Recreate
template:
metadata:
labels:
app.kubernetes.io/name: myapp
spec:
containers:
- image: centos/nodejs-8-centos7:latest
name: myapp
args:
- /var/lib/supervisord/bin/supervisord
- -c
- /var/lib/supervisord/conf/supervisor.conf
command:
- /var/lib/supervisord/bin/dumb-init
- --
env:
- name: ODO_S2I_SRC_BIN_PATH
value: /tmp
- name: ODO_S2I_DEPLOYMENT_DIR
value: /opt/app-root/src
- name: ODO_S2I_WORKING_DIR
value: /opt/app-root/src
- name: ODO_S2I_BUILDER_IMG
value: rhoar-nodejs/nodejs-10
- name: ODO_DEPLOYMENT_BACKUP_DIR
value: /opt/app-root/deployment-backup
- name: ODO_SRC_BACKUP_DIR
value: /opt/app-root/src-backup
- name: ODO_S2I_SCRIPTS_URL
value: /usr/libexec/s2i
- name: ODO_S2I_SCRIPTS_PROTOCOL
value: image://
ports:
- containerPort: 8080
name: 8080-tcp
protocol: TCP
volumeMounts:
- mountPath: /var/lib/supervisord
name: odo-supervisord-shared-data
- mountPath: /opt/app-root
name: nodejs-myapp-s2idata
subPath: app-root
resources: {}
initContainers:
- image: centos/nodejs-8-centos7:latest
name: copy-files-to-volume
args:
- "\nSRC=/opt/app-root\nDEST=/mnt/app-root\n\nif [ -f $DEST/.delete-volume
]; then\n rm -rf $DEST\nfi\n if [ -d $DEST ]; then\n if [ -f $DEST/.sync-volume
]; then\n if ! [[ \"$JUPYTER_SYNC_VOLUME\" =~ ^(false|no|n|0)$ ]];
then\n JUPYTER_SYNC_VOLUME=yes\n fi\n fi\n if [[
\"$JUPYTER_SYNC_VOLUME\" =~ ^(true|yes|y|1)$ ]]; then\n rsync -ar
--ignore-existing $SRC/. $DEST\n fi\n exit\nfi\n if [ -d $DEST.setup-volume
]; then\n rm -rf $DEST.setup-volume\nfi\n\nmkdir -p $DEST.setup-volume\ntar
-C $SRC -cf - . | tar -C $DEST.setup-volume -xvf -\nmv $DEST.setup-volume
$DEST\n\t\t\t"
command:
- sh
- -c
volumeMounts:
- mountPath: /mnt
name: nodejs-myapp-s2idata
- image: quay.io/openshiftdo/supervisord:0.6.0
name: copy-supervisord
args:
- -r
- /opt/supervisord
- /var/lib/
command:
- /usr/bin/cp
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/lib/supervisord
name: odo-supervisord-shared-data
volumes:
- emptyDir: {}
name: odo-supervisord-shared-data
- name: nodejs-myapp-s2idata
persistentVolumeClaim:
claimName: nodejs-myapp-s2idata
---
apiVersion: v1
kind: Service
metadata:
name: myapp
labels:
app.kubernetes.io/name: myapp
spec:
ports:
- name: "8080-tcp"
port: 8080
targetPort: "8080-tcp"
protocol: TCP
selector:
app.kubernetes.io/name: myapp
type: LoadBalancer
# initialize component (create PersistentVolumeClaim, Service and Deployment)
kubectl create -f component.yaml
# push source code from the current directory the runnig container
# get pod name
export POD=`kubectl get pods -l "app.kubernetes.io/name=myapp" -o jsonpath="{.items[0].metadata.name}"`
# copy source files to pod
# because of https://github.com/kubernetes/kubernetes/issues/66170
for f in `ls -1A`; do kubectl cp $f $POD:/tmp/src/ ; done
# execute assemble-and-restart injected into the volume from initContainer
# This script includes some odo magic to make s2i builder images work with supervisor aproach.
# Than it just executes s2i 'assemble' script and start/restart s2i 'run' script
kubectl exec -it $POD /var/lib/supervisord/bin/assemble-and-restart
# access Service created in component.yaml
# for minikube:
minikube service myapp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment