The core of KubeBlocks is a K8s operator built on KubeBuilder. Before you start, please make sure you have basic understanding of:
- Basic usage of KubeBlocks
- Please install
kbcliand attempt to create a MySQL cluster and establish a connection follow the guidelines.
- Please install
- CRD(Custom Resource Definitions)
- Operator
To run KubeBlocks, you needs Docker and a Kubernetes 1.22+ cluster for development.
Install Docker
For Linux, you'll have to configure docker to run without
sudofor the KubeBlocks build scripts to work. Follow the instructions to manage Docker as a non-root user.
- Kubernetes cluster
You can use cloud Kubernetes service, such as
EKSGKEAKS, or use local Kubernetes cluster, such asMinikubek3dKIND. - For development purposes, you will also want to follow the optional steps to install Helm 3.x.
There are two options for getting an environment up and running for KubeBlocks development:1. Bring your own toolbox; 2. Use development container. Using dev container is optional.
To build KubeBlocks on your own host, needs to install the following tools:
- Go (Golang)
- Make
Download and install Go 1.21 or later.
KubeBlocks uses make for a variety of build and test actions, and needs to be installed as appropriate for your platform:
- Linux
- Install the
build-essentialpackage:sudo apt-get install build-essential
- Install the
- macOS
- Ensure that build tools are installed:
xcode-select --install
- When completed, you should see
makeand other command line developer tools in/usr/bin.
- Ensure that build tools are installed:
When go and make are installed, you can clone the KubeBlocks repository, and build KubeBlocks binaries with the make tool.
- To build for your current local environment:
make all
- To cross-compile for a different platform, use the
GOOSandGOARCHenvironmental variables:make all GOOS=windows GOARCH=amd64
If you are using Visual Studio Code, you can connect to a development container configured for KubeBlocks development. With development container, you don't need to manually install all of the tools and frameworks needed.
- Install VSCode Dev Containers extension
- Open the
KubeBlocksproject folder in VSCode- VSCode will detect the presence of a dev container definition in the repo and will prompt you to reopen the project in a container:

- Alternatively, you can open the command palette and use the Remote-Containers: Reopen in Container command:

- VSCode will pull image and start dev container automatically, once the container is loaded, open an integrated terminal in VS Code and you're ready to develop
KubeBlocksin a containerized environment.
- VSCode will detect the presence of a dev container definition in the repo and will prompt you to reopen the project in a container:
- And you can run
make allto buildKubeBlocksin the dev container.
The devcontainer.json uses the latest image from ApeCloud Docker hub, you can customize image to suit your need.
- Edit the docker/Dockerfile-dev dev container image definition, you can change the
[Option]configuration or install additional tools.# Copy library scripts to execute COPY library-scripts/*.sh library-scripts/*.env /tmp/library-scripts/ # [Option] Install zsh ARG INSTALL_ZSH="true" # [Optional] Uncomment the next line to use go get to install anything else you need # RUN go get -x <your-dependency-or-tool> # [Optional] Uncomment this section to install additional OS packages. # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ # && apt-get -y install --no-install-recommends <your-package-list-here> # [Optional] Uncomment this line to install global node packages. # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&
- Build a new image using updated
Dockerfile, and replace the"image"property in devcontainer.json.{ // Update to your dev container image "image": "docker.io/apecloud/kubeblocks-dev:latest", } - Rebuild and reopen the workspace in the dev container via the command palette and the
Remote-Containers: Rebuild and Reopenin Container command. - When you are satisfied with your changes, you can optionally publish your container image to your own registry to speed up rebuilding the container when you only want to make changes to the
devcontainer.jsonconfiguration in the future. For a docker image nameddocker.io/xxxx/kubeblocks-dev:make build-dev-image DEV_CONTAINER_IMAGE_NAME=docker.io/xxxx/kubeblocks-dev
If you want to reuse an existing Kubernetes config, such as your EKS cluster or local Minikube cluster, you can configure the devcontainer.json copy those settings into the dev container. This requires:
- Enabling the
SYNC_LOCALHOST_KUBECONFIGenvironment variable - Bind mounting the locations of your Kubernetes and Minikube config paths to
/home/kubeblocks/.kube-localhostand/home/kubeblocks/.minikube-localhostrespectively.- You don't need to bind the Minikube path if you're not using it.
"containerEnv": { // Uncomment to overwrite devcontainer .kube/config and .minikube certs with the localhost versions // each time the devcontainer starts, if the respective .kube-localhost/config and .minikube-localhost // folders respectively are bind mounted to the devcontainer. "SYNC_LOCALHOST_KUBECONFIG": "true", }, ... "mounts": [ // Uncomment to clone local .kube/config into devcontainer "type=bind,source=${env:HOME}${env:USERPROFILE}/.kube,target=/home/kubeblocks/.kube-localhost", // Uncomment to additionally clone minikube certs into devcontainer for use with .kube/config "type=bind,source=${env:HOME}${env:USERPROFILE}/.minikube,target=/home/kubeblocks/.minikube-localhost" ]
⚠ The
SYNC_LOCALHOST_KUBECONFIGoption only supports providing the dev container with the snapshot configuration from the host and does not support updating the host Kubernetes configuration from the dev container directly.
Here we present an example of deploying released version of KubeBlocks locally and utilizing it to create an mysql cluster.
- Install
kbcli - Install
KubeBlocks:kbcli kubeblocks install. This will installKubeBlocksand start running its manager.
Databases can be added as addons in KubeBlocks. To test with mysql, you need to install and enable corresponding addon. You can install addons through Helm. Here is a simpler example using kbcli:
# make sure there is an index
kbcli addon index list
# search supported addon
kbcli addon search mysql
# install addon
kbcli addon install mysql --index kubeblocks --version [YOUR_VERSION]
# enable addon
kbcli addon enable mysql
# list the addons again to check whether it is enabled.
kbcli addon listHere, we present a simple example of creating an mysql cluster.
kbcli cluster create mysql mycluster
- You can use
kbcli cluster listto see information of clusters.
Here we present an example of deploying your development version(e.g., Debugging) of the controller locally and utilizing it to create an mysql cluster.
- Follow instructions above to install
mysqladdon. - Stop the manager of
KubeBlocksyou just install:
kubectl scale deployment kubeblocks --replicas=0 -n kb-system- Generate CRD:
make manifests. This will create CRD of yourKubeBlocks. - Deploy CRD:
make install. This will register CR to cluster. - Start your operator:
make run. This will start yourKubeBlockscontroller and output logs.
Now you can test with creating mysql cluster following instructions above.
To observe the behavior of KubeBlocks, it is recommended to debug and trace its execution. The central function responsible for controlling the execution is Reconcile. In the mentioned example, creating a cluster will activate the Reconcile function located in controllers/apps/cluster_controller.go. To trace the execution process, you can:
- Set a breakpoint on
Reconcilefunction incontrollers/apps/cluster_controller.go - And then start manager in
cmd/manager/main.go
More information about debugging can be found in Debug.