Development with DevSpace
DevSpace allows you to develop applications directly inside a Kubernetes cluster.
The biggest advantages of developing directly inside Kubernetes is that your dev environment will be very similar to your production environment and you can have much greater confidence that everything will work in production when shipping new features.
#
Kube-Context & NamespaceTo develop and deploy your project with DevSpace, you need a valid kube-context because DevSpace uses the kube-config file just like kubectl or helm.
- Local Cluster
- Remote Cluster
Local Clusters
Local clusters run on your local dev machine and include: minikube, kind, k3s, mikrok8s etc.
If you want to deploy to a local Kubernetes cluster, make sure your current kube-context points to this cluster and tell DevSpace which namespace to use:
Kube-Context Namespace
Running the command above will change the default namespace of your kube-context, i.e. instead of using the default
namespace, kubectl
and other tools will now use a different namespace when working with this kube-context. That also means, you will not always need to use the --namespace / -n
flag.
Remote Clusters
Remote clusters run in a cloud or private data center and include: GKE, EKS, AKS, bare metal etc.
- Use cluster alone
- Share cluster with others
Kube-Context Namespace
Running the command above will change the default namespace of your kube-context, i.e. instead of using the default
namespace, kubectl
and other tools will now use a different namespace when working with this kube-context. That also means, you will not always need to use the --namespace / -n
flag.
#
Kubernetes multi-tenancyOne of the options for setting up a multi-tenant Kubernetes environment would be to use Loft. It allows the developers to create isolated Kubernetes environments in a self-service fashion.
Loft offers a wide range of features to facilitate multi-tenancy in Kubernetes. One of such features is the ability to create Virtual Clusters - a lightweight, fully functional and certified Kubernetes distribution that runs fully within the host cluster.
Loft also adds a resource called "Space" into your Kubernetes cluster. A "Space" is a virtual representation of a Kubernetes namespace, and we will be creating one in the steps below.
#
How to use Loft STEP 1#
Setup Loft and connect your clusterSee the Loft getting started guide for details.
STEP 2
#
Install the Loft plugin for DevSpaceSTEP 3
#
Login to Loft via DevSpaceSTEP 4
#
Create isolated namespaces (= Spaces)Kube-Context
DevSpace automatically sets up a kube-context for this Space, so you can also access your namespace using kubectl
, helm
or any other Kubernetes tool. Try it:
STEP 5
#
Add cluster users and allow them to create SpacesLearn more about how to do this in the Loft documentation.
#
Start Development ModeRun the following command to start the development mode:
Running devspace dev
will do the following:
- Deploy your application according to your
deployments
section - Replace pods according to your
dev.replacePods
section - Forward ports specified in the
dev.ports
section - Sync file changes between your local project directory and the k8s pods according to the
dev.sync
section - Open a terminal according to the
dev.terminal
section, so you start your application and work inside your container
Once the terminal session starts, run the command to start your application:
- Node.js
- Python
- Java
- Ruby
- Golang
- PHP
- ASP.NET
- Your Own Project
#
Workflows#
1. File SyncWhile devspace dev
is running, your source code files will be synchronized between your local project folder and your containers running inside Kubernetes. This allows you to code with your favorite IDE or text editor without having to rebuild your images or redeploy your containers.
Try it and just edit a file!
#
2. DevSpace UIWhen running devspace dev
, DevSpace starts a client-only UI for Kubernetes. You can see that in the output of devspace dev
which should contain a log line similar to this one:
By default, DevSpace starts the development UI on port 8090
but if the port is already in use, it will use a different port. If devspace dev
is running, you can open the link shown in the devspace dev
logs, e.g. http://localhost:8090
You can also start the UI without running devspace dev
using this command:
Runs on Localhost
The UI of DevSpace is open-source just like the rest of the CLI and it runs entirely on localhost.
#
Noteswarning
It is highly discouraged to run devspace dev
multiple times in parallel because multiple instances of port forwarding and file synchronization will disturb each other. Instead:
- Run
devspace enter
to open additional terminal sessions without port forwarding and file sync - Run
devspace logs
to start log streaming without port forwarding and file sync - Run
devspace sync
to sync files on-demand without starting port forwarding etc. - Run
devspace ui
to open the localhost development UI in the browser