How to Speed Up Your Local Kubernetes Development With DevSpace
Tags: devops, kubernetes, k3s, devspace
Published: 2022-04-26 22:02:19

[Skaffold](https://dev.to/otomato_io/local-kubernetes-development-with-skaffold-i0k) is a command line tool from Google that facilitates continuous development for Kubernetes applications. You can _iterate_ on your application source code locally then deploy to local or remote Kubernetes clusters. Skaffold handles the workflow for building, pushing and deploying your application. 

[DevSpace](https://github.com/loft-sh/devspace) is a ~2.8k ⭐ open-source developer tool from Loft Labs 🐦[@loft_sh](https://twitter.com/loft_sh) for Kubernetes that lets you develop and deploy cloud-native software _faster_. It is a client-only CLI tool that runs on your machine and works with any Kubernetes cluster. You can use it to automate image building and deployments, to develop software directly inside Kubernetes and to streamline workflows across your team as well as across dev, staging and production.

DevSpace is platform-agnostic and works with clusters ranging from your local machine to popular managed public services. The tool brings many aspects of the modern app programming experience to Kubernetes-based deployments. 

> It lets you run your development environment on your cluster with support for automatic hot reloading. When a local file changes, DevSpace automatically updates your containers with the new content (see "The hot reload functionality" section below).

Built-in port forwarding means you can use localhost to access your deployment. You don’t need to open ports or setup HTTP Ingresses on development clusters, offering an improved developer experience and greater security.

##With DevSpace developer is capable to:

- Build, test and debug applications directly inside Kubernetes
- Develop with hot reloading: updates your running containers without rebuilding images or restarting containers
- Unify deployment workflows within your team and across dev, staging and production
- Automate repetitive tasks for image building and deployment

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jl5fnya2iut1p81sp8yc.png)

##Installing DevSpace
DevSpace is a client-only binary which works on Windows, Mac, and Linux. It is super lightweight, does not require any server-side component, and it does not have any dependencies. My machine is running Ubuntu Focal, I am a Debian fan.

```bash
$ curl -s -L "https://github.com/loft-sh/devspace/releases/latest" | sed -nE 's!.*"([^"]*devspace-linux-amd64)".*!https://github.com\1!p' | xargs -n 1 curl -L -o devspace && chmod +x devspace;
sudo install devspace /usr/local/bin;
```

###Cluster connection
DevSpace uses your active `KUBE_CONTEXT` in the same way as other ecosystem tools like Kubectl and Helm. Set `KUBECONFIG` to a `kubectl`-compatible config file defining your cluster connection:

```bash
export KUBECONFIG=/path/to/kubeconfig.conf
```
I use [k3s/k3d](https://www.sokube.ch/post/k3s-k3d-k8s-a-new-perfect-match-for-dev-and-test) as my local Kubernetes development stage. Let's 

###Start the cluster
```bash
$k3d cluster start my-cluster
INFO[0000] Using the k3d-tools node to gather environment information 
INFO[0000] Starting new tools node...                   
INFO[0000] Starting Node 'k3d-my-cluster-tools'         
INFO[0001] HostIP: using network gateway 172.19.0.1 address 
INFO[0001] Starting cluster 'my-cluster'                
INFO[0001] Starting servers...                          
INFO[0001] Starting Node 'k3d-my-cluster-server-0'      
INFO[0006] Starting agents...                           
INFO[0006] Starting Node 'k3d-my-cluster-agent-0'       
INFO[0019] Starting helpers...                          
INFO[0019] Starting Node 'k3d-my-cluster-serverlb'      
INFO[0026] Injecting records for hostAliases (incl. host.k3d.internal) and for 3 network members into CoreDNS configmap... 
INFO[0029] Started cluster 'my-cluster'
```

Let's do something impressive on demo project.
```
$ git clone https://github.com/otomato-gh/devspace-quickstart-python.git
$ cd devspace-quickstart-python
```
In DevSpace terms, we're ready to 
##Initialize our project
```
$ devspace init
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h9alm2mcawdzoe6il6e5.png)
Choose `Component Helm Chart` for the first question. In a next step you may choose a Dockerfile (we have one here in our project) and a Docker registry for storing images. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yt4uzgwl6naq9op8corh.png)
After running `devspace init`, you will see a `devspace.yaml` file in your project which should look [like this one](https://github.com/otomato-gh/devspace-quickstart-python/blob/main/devspace.yaml).
##⚙️ DEVELOPment mode
```
$ devspace dev
```
What's up:
```
[info]   Using namespace 'default'               
[info]   Using kube context 'k3d-my-cluster'
[info]   Execute '/home/roman/.devspace/bin/helm upgrade devspace-quickstart-python --namespace default --values /tmp/740048097 --install /home/roman/.devspace/component-chart/component-chart-0.8.4.tgz --kube-context k3d-my-cluster'
[done] √ Deployed helm chart (Release revision: 1)
[done] √ Successfully deployed devspace-quickstart-python with helm
[0:replacePod] Try to find replaced pod...
[0:replacePod] Try to find replaceable pod...
[0:replacePod] Replacing Pod default/devspace-quickstart-python-69787d5b47-rvlf4...
[0:replacePod] Scaled down Deployment default/devspace-quickstart-python
[0:replacePod] Waiting for Pod devspace-quickstart-python-69787d5b47-rvlf4 to get terminated...
[0:replacePod] Successfully replaced pod default/devspace-quickstart-python-69787d5b47-rvlf4
                                             
#########################################################
[info]   DevSpace UI available at: http://localhost:8090
#########################################################

[0:ports] Port-Forwarding: Waiting for containers to start...
[0:sync] Waiting for containers to start...
[0:sync] Starting sync...
[0:ports] Port forwarding started on 8080:8080 (default/devspace-quickstart-python-69787d5b47-rvlf4-742jd)
[0:sync] Sync started on /home/roman/devspace-quickstart-python <-> . (Pod: default/devspace-quickstart-python-69787d5b47-rvlf4-742jd)
[0:sync] Waiting for initial sync to complete
[info]   Opening 'http://localhost:8080' as soon as application will be started (timeout: 4m0s)
[info]   Terminal: Waiting for containers to start...
[info]   Opening shell to pod:container devspace-quickstart-python-69787d5b47-rvlf4-742jd:container-0
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd3pfrh9492v830rho5n.png)
DevSpace will starts the [development UI](https://devspace.sh/cli/docs/getting-started/development) on port `8090`.

###Adding an image
DevSpace projects are built from one or more container images. To register an image in your project, add it under the `images` key in your `devspace.yaml` file:
```yaml
images:
  app:
    image: rbalashevich/app:latest
    dockerfile: ./Dockerfile
    build:
      disabled: true
```
The `image` field defines the image tag name. The `dockerfile` field is optional; when set, it should reference the path to the image’s `Dockerfile` in your project. Images with Dockerfiles will be built automatically by launching `devspace build` and `devspace deploy` commands, unless `build.disabled` is `true` in the image’s [config](https://devspace.sh/cli/docs/configuration/reference).

###Networking
Port-forwarding allows you to access your application on `localhost:[PORT]` by forwarding the network traffic _from_ a localhost port _to_ a specified port of a container.

When starting the development mode, DevSpace starts port-forwarding as configured in the `dev.ports` section of the 
`devspace.yaml`.

```yaml
  # `dev.ports` specifies all ports that should be forwarded while `devspace dev` is running
  # Port-forwarding lets you access your application via localhost on your local machine
  ports:
  - imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    forward:
    - port: 8080
```

##⚙️ DEPLOYment mode

You may learn the details about deployment mode [here](https://devspace.sh/cli/docs/getting-started/deployment).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/weld77w3vt61tx3jjcev.png)

###The hot reload functionality 

This is enabled by setting up file synchronization:
```yaml
  # `dev.sync` configures a file sync between our Pods in k8s and your local project files
  sync:
  - imageSelector: ${IMAGE} # Select the Pod that runs our `${IMAGE}`
    # `excludePaths` option expects an array of strings with paths that should not be synchronized between the
    # local filesystem and the remote container filesystem. It uses the same syntax as `.gitignore`.
    excludePaths:
    - .git/
    uploadExcludePaths:
    - Dockerfile
```
The `imageSelector` matches image tags to apply the hot reload rule to. To establish an on-demand file synchronization between your local computer and the containers running inside Kubernetes, use the following command:
```
$ devspace sync   
```
Common flags are described [here](https://devspace.sh/cli/docs/guides/file-synchronization).

## What's more inside our toolbox?
DevSpace capabilities also include:
- Deployments [via](https://devspace.sh/cli/docs/configuration/deployments/basics) `helm`, `kubectl`.
- `Dockerfile` modification in-memory on execution time.
- Development tools, such as [log aggregation](https://devspace.sh/cli/docs/configuration/development/log-streaming).
- [Custom hooks](https://devspace.sh/cli/docs/configuration/hooks/basics) (actions that are carried out based on events).
- [Custom commands](https://devspace.sh/cli/docs/configuration/commands/basics) (to build complex or lengthy commands into a single sub-command).
- Custom [profiles](https://devspace.sh/cli/docs/configuration/profiles/basics) (to change anything in the `devspace.yaml` and `Dockerfile` using `add`, `patch`, `remove` and `merge` operations.
- The profiles bring the ability to use [different configurations](https://devspace.sh/cli/docs/guides/dev-staging-production) for specific deployment types, such as staging, production or testing.
- [Might be seen](https://devspace.sh/cli/docs/guides/ci-cd-integration) as a replacement or a companion for CI/CD tools for GitOps, such as Flux or ArgoCD, which can allow developers to _only_ have to configure `YAML` files that are then _uploaded_ to a Git repository for deployment.

> Summing up, we at Otomato consider DevSpace makes developers more productive in the Kubernetes environment. It would not be bad to say this amazing tool have been designed to lower the Kubernetes learning curve.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iw9wlggfhf35dq93lj6n.jpg)