From c258c2db92bb30b6649065036e60408688905ee4 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Sun, 23 Apr 2017 17:00:09 +0300 Subject: [PATCH] docs: add example configs for kubernetes; resolves #327 --- README.md | 9 +-- cmd/cayley/command/http.go | 5 +- docs/Container.md | 4 ++ docs/k8s/cayley-mongo.yml | 114 +++++++++++++++++++++++++++++++++++++ docs/k8s/cayley-single.yml | 73 ++++++++++++++++++++++++ docs/k8s/k8s.md | 25 ++++++++ 6 files changed, 223 insertions(+), 7 deletions(-) create mode 100644 docs/k8s/cayley-mongo.yml create mode 100644 docs/k8s/cayley-single.yml create mode 100644 docs/k8s/k8s.md diff --git a/README.md b/README.md index 94d147a2c..3d4889e9f 100644 --- a/README.md +++ b/README.md @@ -25,12 +25,9 @@ Its goal is to be a part of the developer's toolbox where [Linked Data](http://l * [GraphQL](./docs/GraphQL.md)-inspired\* query language. * (simplified) [MQL](./docs/MQL.md), for [Freebase](https://en.wikipedia.org/wiki/Freebase) fans * Plays well with multiple backend stores: - * [LevelDB](https://github.com/google/leveldb) - * [Bolt](https://github.com/boltdb/bolt) - * [PostgreSQL](http://www.postgresql.org) - * [MySQL](https://www.mysql.com) - * [MongoDB](https://www.mongodb.org) for distributed stores - * [CockroachDB](https://www.cockroachlabs.com) + * KVs: [Bolt](https://github.com/boltdb/bolt), [LevelDB](https://github.com/google/leveldb) + * NoSQL: [MongoDB](https://www.mongodb.org) + * SQL: [PostgreSQL](http://www.postgresql.org), [CockroachDB](https://www.cockroachlabs.com), [MySQL](https://www.mysql.com) * In-memory, ephemeral * Modular design; easy to extend with new languages and backends * Good test coverage diff --git a/cmd/cayley/command/http.go b/cmd/cayley/command/http.go index 55f1eeb26..016597373 100644 --- a/cmd/cayley/command/http.go +++ b/cmd/cayley/command/http.go @@ -9,6 +9,7 @@ import ( "github.com/spf13/viper" "github.com/cayleygraph/cayley/clog" + "github.com/cayleygraph/cayley/graph" "github.com/cayleygraph/cayley/internal" chttp "github.com/cayleygraph/cayley/internal/http" "github.com/cayleygraph/cayley/quad" @@ -27,7 +28,9 @@ func NewHttpCmd() *cobra.Command { if init, err := cmd.Flags().GetBool("init"); err != nil { return err } else if init { - if err = initDatabase(); err != nil { + if err = initDatabase(); err == graph.ErrDatabaseExists { + clog.Infof("database already initialized, skipping init") + } else if err != nil { return err } } diff --git a/docs/Container.md b/docs/Container.md index 0c20d3b46..c7f70aa15 100644 --- a/docs/Container.md +++ b/docs/Container.md @@ -1,3 +1,7 @@ +# Running in Kubernetes + +To run Cayley in K8S check [this docs section](./k8s/k8s.md). + # Running in a container A container exposing the HTTP API of Cayley is available. diff --git a/docs/k8s/cayley-mongo.yml b/docs/k8s/cayley-mongo.yml new file mode 100644 index 000000000..21642ea82 --- /dev/null +++ b/docs/k8s/cayley-mongo.yml @@ -0,0 +1,114 @@ +kind: Namespace +apiVersion: v1 +metadata: + name: cayley +--- +apiVersion: v1 +kind: Service +metadata: + name: mongo + namespace: cayley + labels: + name: mongo +spec: + ports: + - name: mgo + port: 27017 + targetPort: mgo + clusterIP: None + selector: + role: mongo +--- +apiVersion: apps/v1beta1 +kind: StatefulSet +metadata: + name: mongo + namespace: cayley +spec: + serviceName: "mongo" + replicas: 3 + template: + metadata: + namespace: cayley + labels: + role: mongo + environment: test + spec: + terminationGracePeriodSeconds: 10 + containers: + - name: mongo + image: mongo:3 + command: + - mongod + - "--replSet" + - rs0 + - "--smallfiles" + - "--noprealloc" + ports: + - name: mgo + containerPort: 27017 + volumeMounts: + - name: mongo-pvc + mountPath: /data/db + - name: mongo-sidecar + image: cvallance/mongo-k8s-sidecar + env: + - name: MONGO_SIDECAR_POD_LABELS + value: "role=mongo,environment=test" + volumeClaimTemplates: + - metadata: + name: mongo-pvc + spec: + accessModes: [ "ReadWriteOnce" ] + storageClassName: standard + resources: + requests: + storage: 20Gi +--- +kind: Service +apiVersion: v1 +metadata: + name: cayley + namespace: cayley +spec: + selector: + app: cayley + ports: + - protocol: TCP + port: 80 + targetPort: http +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: cayley + namespace: cayley +spec: + replicas: 1 + template: + metadata: + namespace: cayley + labels: + app: cayley + spec: + initContainers: + - name: cayley-init + image: quay.io/cayleygraph/cayley:v0.7.0-alpha + command: + - cayley + - init + - -d=mongo + - -a=mongo + containers: + - name: cayley + image: quay.io/cayleygraph/cayley:v0.7.0-alpha + command: + - cayley + - http + - --init # TODO: remove once initContainers works properly + - -d=mongo + - -a=mongo + - --host=:64210 + ports: + - name: http + containerPort: 64210 \ No newline at end of file diff --git a/docs/k8s/cayley-single.yml b/docs/k8s/cayley-single.yml new file mode 100644 index 000000000..3d5e56302 --- /dev/null +++ b/docs/k8s/cayley-single.yml @@ -0,0 +1,73 @@ +kind: Namespace +apiVersion: v1 +metadata: + name: cayley +--- +kind: Service +apiVersion: v1 +metadata: + name: cayley + namespace: cayley +spec: + selector: + app: cayley + ports: + - protocol: TCP + port: 80 + targetPort: http +--- +kind: PersistentVolumeClaim +apiVersion: v1 +metadata: + name: cayley-pvc + namespace: cayley +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 20Gi + storageClassName: standard +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: cayley + namespace: cayley +spec: + replicas: 1 # cannot be really scaled because of local backend + template: + metadata: + namespace: cayley + labels: + app: cayley + spec: + initContainers: + - name: cayley-init + image: quay.io/cayleygraph/cayley:v0.7.0-alpha + command: + - cayley + - init + - -c=/etc/cayley.json + volumeMounts: + - mountPath: /data + name: database + containers: + - name: cayley + image: quay.io/cayleygraph/cayley:v0.7.0-alpha + command: + - cayley + - http + - --init # TODO: remove once initContainers is out of beta + - -c=/etc/cayley.json + - --host=:64210 + ports: + - name: http + containerPort: 64210 + volumeMounts: + - mountPath: /data + name: database + volumes: + - name: database + persistentVolumeClaim: + claimName: cayley-pvc \ No newline at end of file diff --git a/docs/k8s/k8s.md b/docs/k8s/k8s.md new file mode 100644 index 000000000..2c1a48c7c --- /dev/null +++ b/docs/k8s/k8s.md @@ -0,0 +1,25 @@ +# Running in Kubernetes + +Most examples requires Kubernetes 1.5+ and PersistentVolumes are configured. + +After running scripts namespace `cayley` will be created and service with the same name will be available in cluster. Service is of type `ClusterIP` by default. If you want to expose it, consider changing type to `LoadBalancer`. + +## Single instance (Bolt) + +This is a simplest possible configuration: single Cayley instance with persistent storage, using Bolt as a backend. + +```bash +kubectl create -f ./docs/k8s/cayley-single.yml +``` + +## Distributed (MongoDB cluster) + +This example is based on [thesandlord/mongo-k8s-sidecar](https://github.com/thesandlord/mongo-k8s-sidecar) and runs Cayley on top of 3-node Mongo cluster. + +```bash +kubectl create -f ./docs/k8s/cayley-mongo.yml +``` + +## Distributed + +*TODO: PostgreSQL, CockroachDB* \ No newline at end of file