Skip to content

Getting started on Kubernetes

What you'll need

  1. Docker
  2. Docker-Compose
  3. qlooctl
  4. glooctl (optional)

Steps

Deploy QLoo and Gloo

qlooctl install docker qloo-docker
cd ./qloo-docker
docker-compose up

or

git clone https://github.com/solo-io/qloo
cd qloo/install/docker-compose
./prepare-config-directories
docker-compose up

Deploy the Pet Store

docker run -d -p 1234:8080 soloio/petstore-example:latest

Create a Gloo upstream for the petstore

  • using glooctl:
cat << EOF | glooctl upstream create -f -
name: petstore
type: service
spec:
  hosts:
  # gateway ip for the docker network
  - addr: $(docker inspect qloo-docker_default -f '{{ (index .IPAM.Config 0).Gateway }}')
    port: 1234
EOF
  • writing directly to disk
cat > ./_gloo_config/upstreams/petstore.yaml << EOF 
name: petstore
type: service
spec:
  hosts:
  # gateway ip for the docker network
  - addr: $(docker inspect qloo-docker_default -f '{{ (index .IPAM.Config 0).Gateway }}')
    port: 1234
EOF

OPTIONAL: View the petstore functions using glooctl:

    glooctl upstream get

    +----------+---------+--------+-------------+
    |   NAME   |  TYPE   | STATUS |  FUNCTION   |
    +----------+---------+--------+-------------+
    | petstore | service |        | addPet      |
    |          |         |        | deletePet   |
    |          |         |        | findPetById |
    |          |         |        | findPets    |
    +----------+---------+--------+-------------+

The upstream we want to see is petstore. The functions addPet, deletePet, findPetById, and findPets will become the resolvers for our GraphQL schema.

Create a GraphQL Schema

Copy and paste the following schema into petstore.graphql (or wherever you like):

# The query type, represents all of the entry points into our object graph
type Query {
    pets: [Pet]
    pet(id: Int!): Pet
}

type Mutation {
    addPet(pet: InputPet!): Pet
}

type Pet{
    id: ID!
    name: String!
    status: Status!
}

input InputPet{
    id: ID!
    name: String!
    tag: String
}

enum Status {
    pending
    available
}

Upload the Schema

Upload the schema to QLoo using qlooctl:

qlooctl schema create petstore -f petstore.graphql

OPTIONAL: View the Generated Resolvers

A QLoo ResolverMap will have been generated for the new schema.

Take a look at its structure:

qlooctl resolvermap get petstore-resolvers -o yaml

metadata:
  resource_version: "1"
name: petstore-resolvers
status:
  state: Accepted
types:
  Mutation:
    fields:
      addPet: {}
  Pet:
    fields:
      id: {}
      name: {}
      status: {}
  Query:
    fields:
      pet: {}
      pets: {}

The empty {}'s are QLoo Resolver objects, waiting to be filled in. QLoo supports a variety of Resolver types (and supports extension to its resolution system). In this tutorial, we will create Gloo resolvers, which allow you to connect schema fields to REST APIs, serverless functions and other Gloo functions.

Register some Resolvers

Let's use qlooctl to register some resolvers.

# register findPetById for Query.pets (specifying no arguments)
qlooctl resolvermap register -u petstore -f findPetById Query pets
# register a resolver for Query.pet
qlooctl resolvermap register -u petstore -f findPetById Query pet
# register a resolver for Mutation.addPet
# the request template tells QLoo to use the Variable "pet" as an argument 
qlooctl resolvermap register -u petstore -f addPet Mutation addPet --request-template '{{ marshal (index .Args "pet") }}'

Note: if you get a permission denied error, run

sudo chown -R $USER _gloo_config
sudo chgrp -R $USER _gloo_config

That's it! Now we should have a functioning GraphQL frontend for our REST service.

Visit the Playground

Visit the QLoo UI from your browser: http://localhost:9090/

You should see a landing page for QLoo which contains a link to the GraphQL Playground for our Pet Store. Visit it and try out some queries!

examples:

{
  pet(id:1 ) {
    name
  }
}

{
  "data": {
    "pet": {
      "name": "Dog"
    }
  }
}
{
  pets {
    name
  }
}

{
  "data": {
    "pets": [
      {
        "name": "Dog"
      },
      {
        "name": "Cat"
      }
    ]
  }
}
mutation($pet: InputPet!) {
  addPet(pet: $pet) {
    id
    name
  }
}

with input variable

{
  "pet":{
    "id":3,
    "name": "monkey"
  }
}

{
  "data": {
    "addPet": {
      "name": "monkey"
    }
  }
}