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 & Namespace
To 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 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:
devspace use context # to select the right k8s cluster
devspace use namespace my-namespace # will be automatically created during deployment
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 run in a cloud or private data center and include: GKE, EKS, AKS, bare metal etc.
- Use cluster alone
- Share cluster with others
devspace use context # to select the right k8s cluster
devspace use namespace my-namespace # will be automatically created during deployment
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-tenancy
One 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 1Setup Loft and connect your cluster
See the Loft getting started guide for details.
STEP 2
Install the Loft plugin for DevSpace
devspace add plugin https://github.com/loft-sh/loft-devspace-plugin
STEP 3
Login to Loft via DevSpace
devspace login https://your-loft-instance.tld
STEP 4
Create isolated namespaces (= Spaces)
devspace create space my-app
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:
kubectl get pods
STEP 5
Add cluster users and allow them to create Spaces
Learn more about how to do this in the Loft documentation.
Start Development Mode
Run the following command to start the development mode:
devspace dev
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
npm start
# or: npm run dev
# or: yarn start
python main.py
bundle exec rails server -p 3000 -b 0.0.0.0
go run main.go
./build.sh run
# Your application may already be running
# Try to open the browser on localhost to access it
# You can also run other commands now:
php ...
composer ...
ps aux
dotnet run
# Whatever command is needed to start your application
Workflows
1. File Sync
While 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 UI
When 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:
#########################################################
[info] DevSpace UI available at: http://localhost:8090
#########################################################
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:
devspace ui
The UI of DevSpace is open-source just like the rest of the CLI and it runs entirely on localhost.
Notes
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