dehaze

Deploying Custom Code

Space Cloud lets you leverage all the capabilities of Kubernetes without having to learn the Kubernetes API. This guide shows how you can deploy your code to Space Cloud.

If you want an interactive and step-by-step guide to deploy your code, follow this Space Cloud Basic guide

Preparing your code for deploymentlink

To deploy your code, it should have these two files:

  • Dockerfile - To build the docker image since Space Cloud can only deploy docker containers.
  • service.yaml - To describe the service configuration (example: resources, auto-scaling, ports)

If you already have these two files, you can straight away jump to deploying the service.

space-cli has a built-in command to generate both of these. It automatically generates a Dockerfile for us and a service.yaml as well with suitable defaults by asking us only a few required questions.

Checkout into the root folder of your code and just run the following command:

space-cli deploy --prepare

It asks you the following bunch of questions to auto-generate the config file. Most notable questions are:

  • Project Id: Id of the space cloud project in which you want to deploy this service.
  • Service Id: A unique name to identify your service.
  • Version: A unique name (e.g. v1) to identify the different versions of your service.
  • Port: (Default: 8080) Port of the service that you want to expose.

This will generate a Dockerfile and a service.yaml file if these files did not exist before. Feel free to explore and change both these files before finally deploying the service. The service.yaml file looks something like this:

api: /v1/runner/{project}/services/{id}/{version}
meta:
  id: basic-test
  project: myproject
  version: v1
spec:
  autoScale:
    coolDownInterval: 120
    maxReplicas: 100
    minReplicas: 1
    pollingInterval: 15
    triggers:
    - authRef: null
      metadata:
        target: "50"
      name: Request per second
      type: requests-per-second
  statsInclusionPrefixes: http.inbound,cluster_manager,listener_manager
  tasks:
  - docker:
      cmd: []
      image: dockerhub/myproject-basic-test:v1
      imagePullPolicy: pull-if-not-exists
      secret: ""
    env: {}
    id: basic-test
    ports:
    - name: http
      port: 8080
      protocol: http
    resources:
      cpu: 250
      memory: 512
    runtime: image
    secrets: []
  upstreams:
  - projectId: myproject
    service: '*'
  whitelists:
  - projectId: myproject
    service: '*'
type: service

Great! Your service now has everything that is required to deploy it.

Deploying the servicelink

Before we can deploy our service, we need to make sure that you have configured your Space Cloud project for which container registry to publish the docker images to.

If you are using the Docker setup of Space Cloud, then space-cli has a built-in command to spin up a container registry at localhost:5000 for you and configure Space Cloud to publish images to this registry. Here’s the command to do it:

In a production environment we recommend using a managed container registry (example: Google Container Registry), Checkout the docs for using a custom container registry to do so.

space-cli --project <project-id> add registry

Since we have everything ready now, we are now going to build and publish the Docker image for our service and deploy it to Space Cloud with the config specified in its service.yaml file. We are going to use the deploy command of space-cli to do all of these.

Just run the following command from within your service folder:

space-cli deploy

You may have to run the above command with sudo privileges if your docker is not in the sudoer group.

This will first build the docker image for our service and publish it to the configured container registry. The published docker image will be of the following format:

<docker-registry>/<project-id>-<service-id>:<service-version>

space-cli then automatically instructs the Space Cloud to deploy the published docker image of the service with the config specified in the service.yaml file.

That’s it! You have successfully deployed your code using space-cli.

Next stepslink

Have a technical question?

Improve the docs!