Commit ee54b1d0 authored by Christiaan Hees's avatar Christiaan Hees

Added an example for Meteor with raw documentation

parent 23f2401b
Build a container for your Meteor app
-------------------------------------
To be able to run your Meteor app on Kubernetes you need to build a container for it first. To do that you need to install [Docker](https://www.docker.com) and get an account on [Docker Hub](https://hub.docker.com/). Once you have that you need to add 2 files to your Meteor project "Dockerfile" and ".dockerignore".
"Dockerfile" should contain this:
FROM chees/meteor-kubernetes
ENV ROOT_URL http://myawesomeapp.com
You should replace the ROOT_URL with the actual hostname of your app.
The .dockerignore file should contain this:
.meteor/local
packages/*/.build*
This tells Docker to ignore the files on those directories when it's building your container.
You can see an example of a Dockerfile in our [meteor-gke-example](https://github.com/Q42/meteor-gke-example) project.
Now you can build your container by running something like this in your Meteor project directory:
docker build -t chees/meteor-gke-example:1 .
Here you should replace "chees" with your own username on Docker Hub, "meteor-gke-example" with the name of your project and "1" with the version name of your build.
Push the container to your Docker hub account (replace the username and project with your own again):
docker push chees/meteor-gke-example
Running
-------
Now that you have containerized your Meteor app it's time to set up your cluster. Edit "meteor-controller.json" and make sure the "image" points to the container you just pushed to the Docker Hub.
For Mongo we use a Persistent Disk to store the data. If you're using gcloud you can create it once by running:
gcloud compute disks create --size=200GB mongo-disk
You also need to format the disk before you can use it:
gcloud compute instances attach-disk --disk=mongo-disk --device-name temp-data k8s-meteor-master
gcloud compute ssh k8s-meteor-master --command "sudo mkdir /mnt/tmp && sudo /usr/share/google/safe_format_and_mount /dev/disk/by-id/google-temp-data /mnt/tmp"
gcloud compute instances detach-disk --disk mongo-disk k8s-meteor-master
Now you can start Mongo using that disk:
kubectl create -f mongo-pod.json
kubectl create -f mongo-service.json
Wait until Mongo is started completely and then set up Meteor:
kubectl create -f meteor-controller.json
kubectl create -f meteor-service.json
Note that meteor-service.json creates an external load balancer, so your app should be available through the IP of that load balancer once the Meteor pods are started. On gcloud you can find the IP of your load balancer by running:
gcloud compute forwarding-rules list k8s-meteor-default-meteor | grep k8s-meteor-default-meteor | awk '{print $3}'
You might have to open up port 80 if it's not open yet in your project. For example:
gcloud compute firewall-rules create meteor-80 --allow=tcp:80 --target-tags k8s-meteor-node
TODO replace the mongo image with the official mongo? https://registry.hub.docker.com/_/mongo/
TODO use Kubernetes v1beta3 syntax?
FROM node:0.10
MAINTAINER Christiaan Hees <christiaan@q42.nl>
ONBUILD WORKDIR /appsrc
ONBUILD COPY . /appsrc
ONBUILD RUN curl https://install.meteor.com/ | sh && \
meteor build ../app --directory --architecture os.linux.x86_64 && \
rm -rf /appsrc
# TODO rm meteor so it doesn't take space in the image?
ONBUILD WORKDIR /app/bundle
ONBUILD RUN (cd programs/server && npm install)
EXPOSE 8080
CMD []
ENV PORT 8080
ENTRYPOINT MONGO_URL=mongodb://$MONGO_SERVICE_HOST:$MONGO_SERVICE_PORT /usr/local/bin/node main.js
Building the meteor-kubernetes base image
-----------------------------------------
As a normal user you don't need to do this since the image is already built and pushed to Docker Hub. You can just use it as a base image. See [this example](https://github.com/Q42/meteor-gke-example/blob/master/Dockerfile).
To build and push the base meteor-kubernetes image:
docker build -t chees/meteor-kubernetes .
docker push chees/meteor-kubernetes
{
"id": "meteor-controller",
"kind": "ReplicationController",
"apiVersion": "v1beta1",
"desiredState": {
"replicas": 2,
"replicaSelector": {"name": "meteor"},
"podTemplate": {
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "meteor-controller",
"containers": [{
"name": "meteor",
"image": "chees/meteor-gke-example:latest",
"cpu": 1000,
"memory": 500000000,
"ports": [{"name": "http-server", "containerPort": 8080, "hostPort": 80}]
}]
}
},
"labels": { "name": "meteor" }
}
},
"labels": {"name": "meteor"}
}
{
"apiVersion": "v1beta1",
"kind": "Service",
"id": "meteor",
"port": 80,
"containerPort": "http-server",
"selector": { "name": "meteor" },
"createExternalLoadBalancer": true,
"sessionAffinity": "ClientIP"
}
{
"id": "mongo",
"kind": "Pod",
"apiVersion": "v1beta1",
"desiredState": {
"manifest": {
"version": "v1beta1",
"id": "mongo",
"containers": [{
"name": "mongo",
"image": "mongo",
"cpu": 1000,
"ports": [{ "name": "mongo", "containerPort": 27017 }],
"volumeMounts": [{
"mountPath": "/data/db",
"name": "mongo-disk"
}]
}],
"volumes": [{
"name": "mongo-disk",
"source": {
"persistentDisk": {
"pdName": "mongo-disk",
"fsType": "ext4"
}
}
}]
}
},
"labels": {
"name": "mongo", "role": "mongo"
}
}
{
"id": "mongo",
"kind": "Service",
"apiVersion": "v1beta1",
"port": 27017,
"containerPort": "mongo",
"selector": {
"name": "mongo", "role": "mongo"
},
"labels": {
"name": "mongo"
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment