How to get started with DevSpace and quickly develop Kubernetes apps – CloudSavvy IT

An illustration showing the DevSpace logo, a cloud with a terminal symbol inside

DevSpace is an open source tool that helps developers quickly implement cloud-native applications for Kubernetes clusters. It abstracts a lot of the traditional Kubernetes complexity so you can get back to writing code.

DevSpace is platform diagnostic and works with clusters ranging from your local machine to popular managed public services. The tool is a CLI that automates operations using Kubernetes APIs. You do not need to install anything in your cluster to use DevSpace.

Modern development practices for Kubernetes

DevSpace brings many aspects of the modern app programming experience to Kubernetes-based implementations. It lets you run your development environment on your cluster with automatic hot reload support. When a local file changes, DevSpace automatically updates your containers with the new content.

Built-in port forwarding means you can use localhost to access your implementation. You do not have to open ports or configure HTTP inputs on development clusters, providing an enhanced developer experience and greater security.

A chart showing the DevSpace workflow
DevSpace

DevSpace also helps with troubleshooting. It supports breaking point setting using a Remote Debugger tool that offers live inspection of containerized applications in the cloud. It’s also easy to view a container’s log files or start a terminal session without running unwieldy Kubectl commands.

A web interface is running localhost lets you monitor and interact with your running application. You can escape your terminal completely if you prefer a more visual experience.

Installing DevSpace

DevSpace is distributed as a standalone binary operating on Windows, Mac and Linux systems. Several installation options are available. Here is the recommended command to download DevSpace for Linux and add CLI to your path:

dev
sudo install devspace /usr/local/bin

Alternatively, you can use npm, the Node Package Manager, to install and update DevSpace:

npm install -g devspace

This command will give you a function devspace on your way, provided you already have npm installed.

DevSpace uses your active “KUBE_CONTEXT”In the same way as other ecosystem tools such as Kubectl and Helm. Set KUBECONFIG to a Kubectl-compliant configuration file that defines your cluster connection:

export KUBECONFIG=/path/to/kubeconfig.yaml

# Uses $KUBECONFIG
devspace deploy

Creating a project

The first step in building with DevSpace is to initialize your workbook. This will create one devspace.yaml file that contains the DevSpace configuration for your repository.

devspace init

You will be prompted to select the installation routine you plan to use. DevSpace can work with Kubectl, Helm or Kustomize when you launch your app in your cluster.

Screenshot of "devspace init" command

The proposed QUICKSTART option is a special chart that lets you define components of your application, such as your front-end web server, backend API server and database, instead of providing manually created Helm charts or Kubectl manifests. Use this if you are building a system with a conventional structure and do not want to spend time creating your own Kubernetes resources.

Follow the instructions to provide DevSpace with the path to your Docker file. You need to provide registry authentication and some basic information about your image, such as the port it is listening on. DevSpace will automatically produce a ready-to-use configuration that lets you run devspace deploy to get your app live on Kubernetes.

If you do not use the quick start option, specify the path to your Helm charts or Kubernetes manifests. Follow the instructions to build an initial configuration. Once you get out of the setup wizard, you can continue to add additional images and implementations by manually editing your devspace.yaml file.

Adding an image

DevSpace projects are built from one or more container images. To register an image in your project, add it below images enter your devspace.yamlfile:

images:
  app:
    image: example.com/example/image:latest
    dockerfile: ./Dockerfile
    build:
      disabled: true

That image field defines the name of the image mark. That dockerfile field is optional; once set, it should reference the path to the image’s Docker file in your project. Images with Docker files will be built by automatically devspace build and devspace deploy commands, unless build.disabled is true in the image configuration.

Images are built in parallel to accelerate performance. That --build-sequential flags force images to be built individually in the order they are specified instead. DevSpace will automatically push each image to its respective registry after a build is complete.

Images are not automatically rebuilt unless DevSpace detects a change in either their Docker file or the files in their build context. You can force a rebuild of all configured images with --force-build flag.

Adding an implementation

The second piece of one devspace.yaml is yours deployments. These define the resources that will be created in your cluster.

deployments:
  - name: "helm chart"
    helm:
      chart:
        name: example/example-app
      values:
        VARIABLE_OVERRIDE: "new-value"
  - name: "kubectl manifests"
    kubectl:
      manifests:
        - .kube/manifests

This file defines two separate installations, one using Helm and the other Kubectl. When you run devspace deploy, both the Helm diagram and your Kubectl manifests will be installed in your cluster.

Repetition of deploy command will update your implementations. DevSpace will only reinstall the modified components unless --force-deploy flag is used.

As DevSpace works across clusters, promoting your implementation from development to production is a case of changing your KUBECONFIG environmental variable and recycling devspace deploy. This gives you a single consistent mechanism for managing multiple independent implementations of your system.

Use of profiles

DevSpace supports profiles that let you change devspace.yaml sections for each of your environments. Each named profile can replace, merge and change fields in your configuration file.

The profiles themselves are also defined in yours devspace.yaml:

images:
  api-server:
    image: example.com/api-server:latest
  web-server:
    image: example.com/web-server:latest
  debug:
    image: example.com/debug-tool:latest
# ... omitted
profiles:
  - name: production
    patches:
      - op: remove
        path: images.debug

Profiles are activated by passing -p or --profile flags for DevSpace commands. Here’s how to start an implementation using production profile defined above. The implementation does not include debug image as it is removed by one of the profile patches.

devspace deploy --profile production

Using Hot Reload

The hot reload function is activated by configuring file synchronization:

dev:
  sync:
    - imageSelector: example.com/api-server:latest
      localSubPath: ./api/
      containerPath: /var/www/html
      excludePaths:
        - vendor/

add dev key as a field at the top level of your devspace.yaml, and then use sync to configure hot reload on a per-image basis. That imageSelector matches image tags to apply the hot reload rule to. Files from localSubPath in your workbook will be synced to containerPath in rolled-out container deposits.

Start a hot reload session by running devspace dev or devspace sync. The previous command launches all of DevSpace’s developer environment features, including port forwarding and live log streaming.

Hot reload is standard for replacing files in existing containers. This is similar to using Docker volumes and volume mounts when working with local container instances. In some scenarios, you may want to start a new DevSpace deployment when your file system changes. This is achieved via the separate autoReload possibility:

images:
  api:
    image: example.com/api-server:latest
deployments:
  - name: api-deployment
  # ... omitted
dev:
  autoReload:
    paths:
      - ./kernel/*
    images:
      - example.com/api-server:latest
    deployments:
      - api-deployment

This example will relocate api-deployment when files in your room kernel directory change. This is ideal when modifying files that need to be run through a build process to make them useful for your containers.

Port forwarding

Port forwarding is configured via dev.ports field in your devspace.yaml. Forward reverse is also supported, rental localhost addresses in your containers card to ports on your local machine.

dev:
  ports:
    - imageSelector: example.com/api-server:latest
      forward:
        - port: 8080
          remotePort: 80
      reverseForward:
        - port: 9000
          remote: 9000

This example sets one forward from localhost:8080 on your machine for port 80 in running containers example.com/api-server picture. There is also a backward forward, which directs in-container traffic to localhost:9000 back to port 9000 on your machine.

Other features

In addition to the features covered here, DevSpace also offers support for several other feature groups that let you monitor implementations, interact with containers, and configure advanced development workflows:

  • Automatic terminal start lets you start an external shell session every time you run devspace dev.
  • Automatic URL opening launches websites and web apps in your browser when you go into development mode.
  • Configurable logging defines which containers are to be displayed in the log stream to developer mode.
  • Custom commands act as shortcuts to common actions and help new team members interact with your implementations without having to learn lengthy processes in Kubectl.
  • Hooks run commands during the deployment process so you can manually configure containers or register new deployments to a centralized monitoring service.
  • The user interface runs automatically in development mode with devspace dev and can be opened in your browser using devspace ui.

DevSpace also offers plugins that can add even more functionality to the system. The plugin API supports installation from external URLs or local scripts and facilitates new commands, hooks and variables. Developing your own plugin provides a way to standardize your use of DevSpace across multiple independent projects.

Implementations in CI / CD pipelines

DevSpace can handle production implementations as part of your CI / CD pipeline. It provides an official Docker image and supports non-interactive use if you include explicit selectors such as -l to label in your commands.

An implementation in a CI pipeline could look like this:

echo $KUBECONFIG_CI_VARIABLE > /path/to/kubeconfig
export KUBECONFIG=/path/to/kubeconfig
devspace deploy --profile production --timeout 60 --wait --skip-build

This will implement your application using production DevSpace profile. That --skip-build flag instructs DevSpace not to build your images. Buildings should normally be handled as a separate step earlier in your pipeline.

That --wait flag forces DevSpace to wait for the availability of the pod number specified in your devspace.yaml, instead of exiting immediately after running the command. This is more appropriate for a CI environment where you want confirmation that your workload is live. Allowing DevSpace to quit without waiting can mean that your pipeline is marked as successful, even if there is an implementation issue.

Conclusion

DevSpace is emerging as a developer-oriented Kubernetes deployment tool that provides useful abstractions for key operations. It reduces the amount of YAML needed to launch containers into a cluster, replacing it with simple configuration keys and terminal commands that define, build, and launch images.

DevSpace UI
DevSpace

As a platform and environmental diagnostic tool, DevSpace allows developers to worry less about the unique features of individual clusters. Regardless of implementation goals, the overall experience is similar to building a traditional system on your local machine. This helps to soften Kubernete’s learning curve by mapping the concepts back to more universally understood development practices.

Leave a Comment