Manage Milvus with KubeBlocks
The popularity of generative AI (Generative AI) has aroused widespread attention and completely ignited the vector database (Vector Database) market.
Milvus is a highly flexible, reliable, and blazing-fast cloud-native, open-source vector database. It powers embedding similarity search and AI applications and strives to make vector databases accessible to every organization. Milvus can store, index, and manage a billion+ embedding vectors generated by deep neural networks and other machine learning (ML) models.
This tutorial illustrates how to create and manage a Milvus cluster by kbcli
, kubectl
or a YAML file. You can find the YAML examples and guides in the GitHub repository.
Before you start
-
Install kbcli if you want to manage the Milvus cluster with
kbcli
. -
To keep things isolated, create a separate namespace called
demo
throughout this tutorial.kubectl create namespace demo
Create a cluster
KubeBlocks implements a Cluster
CRD to define a cluster. Here is an example of creating a Milvus cluster. If you only have one node for deploying a cluster with multiple replicas, configure the cluster affinity by setting spec.schedulingPolicy
or spec.componentSpecs.schedulingPolicy
. For details, you can refer to the API docs. But for a production environment, it is not recommended to deploy all replicas on one node, which may decrease the cluster availability.
cat <<EOF | kubectl apply -f -
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
namespace: demo
name: mycluster
spec:
terminationPolicy: Delete
clusterDef: milvus
topology: cluster
componentSpecs:
- name: proxy
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
serviceRefs:
- name: milvus-meta-storage
namespace: demo
clusterServiceSelector:
cluster: etcdm-cluster
service:
component: etcd
service: headless
port: client
- name: milvus-log-storage
namespace: demo
clusterServiceSelector:
cluster: pulsarm-cluster
service:
component: broker
service: headless
port: pulsar
- name: milvus-object-storage
namespace: demo
clusterServiceSelector:
cluster: miniom-cluster
service:
component: minio
service: headless
port: http
credential:
component: minio
name: admin
disableExporter: true
- name: mixcoord
replicas: 1
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
serviceRefs:
- name: milvus-meta-storage
namespace: demo
clusterServiceSelector:
cluster: etcdm-cluster
service:
component: etcd
service: headless
port: client
- name: milvus-log-storage
namespace: demo
clusterServiceSelector:
cluster: pulsarm-cluster
service:
component: broker
service: headless
port: pulsar
- name: milvus-object-storage
namespace: demo
clusterServiceSelector:
cluster: miniom-cluster
service:
component: minio
service: headless
port: http
credential:
component: minio
name: admin
disableExporter: true
- name: datanode
replicas: 1
disableExporter: true
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
serviceRefs:
- name: milvus-meta-storage
namespace: demo
clusterServiceSelector:
cluster: etcdm-cluster
service:
component: etcd
service: headless
port: client
- name: milvus-log-storage
namespace: demo
clusterServiceSelector:
cluster: pulsarm-cluster
service:
component: broker
service: headless
port: pulsar
- name: milvus-object-storage
namespace: demo
clusterServiceSelector:
cluster: miniom-cluster
service:
component: minio
service: headless
port: http
credential:
component: minio
name: admin
disableExporter: true
- name: indexnode
replicas: 1
disableExporter: true
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
serviceRefs:
- name: milvus-meta-storage
namespace: demo
clusterServiceSelector:
cluster: etcdm-cluster
service:
component: etcd
service: headless
port: client
- name: milvus-log-storage
namespace: demo
clusterServiceSelector:
cluster: pulsarm-cluster
service:
component: broker
service: headless
port: pulsar
- name: milvus-object-storage
namespace: demo
clusterServiceSelector:
cluster: miniom-cluster
service:
component: minio
service: headless
port: http
credential:
component: minio
name: admin
disableExporter: true
- name: querynode
replicas: 1
disableExporter: true
resources:
limits:
cpu: "0.5"
memory: "0.5Gi"
requests:
cpu: "0.5"
memory: "0.5Gi"
serviceRefs:
- name: milvus-meta-storage
namespace: demo
clusterServiceSelector:
cluster: etcdm-cluster
service:
component: etcd
service: headless
port: client
- name: milvus-log-storage
namespace: demo
clusterServiceSelector:
cluster: pulsarm-cluster
service:
component: broker
service: headless
port: pulsar
- name: milvus-object-storage
namespace: demo
clusterServiceSelector:
cluster: miniom-cluster
service:
component: minio
service: headless
port: http
credential:
component: minio
name: admin
disableExporter: true
EOF
Field | Definition |
---|---|
spec.terminationPolicy | It is the policy of cluster termination. Valid values are DoNotTerminate , Delete , WipeOut . For the detailed definition, you can refer to Termination Policy. |
spec.clusterDef | It specifies the name of the ClusterDefinition to use when creating a Cluster. Note: DO NOT UPDATE THIS FIELD. The value must be milvus to create a Milvus Cluster. |
spec.topology | It specifies the name of the ClusterTopology to be used when creating the Cluster. Valid options are: [standalone,cluster]. |
spec.componentSpecs | It is the list of ClusterComponentSpec objects that define the individual Components that make up a Cluster. This field allows customized configuration of each component within a cluster. |
spec.componentSpecs.serviceRefs | It defines a list of ServiceRef for a Component. |
spec.componentSpecs.serviceRefs.name | It specifies the identifier of the service reference declaration, defined in componentDefinition.spec.serviceRefDeclarations[*].name . |
spec.componentSpecs.serviceRefs.clusterServiceSelector | It references a service provided by another KubeBlocks Cluster. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.cluster | It defines the cluster name. You can name it on demand. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.service.component | It defines the component name. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.service.service | It refers to the default headless Service. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.service.port | It refers to port name. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.credential | It specifies the SystemAccount to authenticate and establish a connection with the referenced Cluster. |
spec.componentSpecs.serviceRefs.clusterServiceSelector.credential.name | It specifies to the name of the credential (SystemAccount) to reference, using account 'admin' in this case. |
spec.componentSpecs.disableExporter | It determines whether metrics exporter information is annotated on the Component's headless Service. Valid options are [true, false]. |
spec.componentSpecs.replicas | It specifies the number of replicas of the component. |
spec.componentSpecs.resources | It specifies the resources required by the Component. |
For more API fields and descriptions, refer to the API Reference.
KubeBlocks operator watches for the Cluster
CRD and creates the cluster and all dependent resources. You can get all the resources created by the cluster with kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=mycluster -n demo
.
kubectl get all,secret,rolebinding,serviceaccount -l app.kubernetes.io/instance=mycluster -n demo
Run the following command to see the created Milvus cluster object:
kubectl get cluster mycluster -n demo -o yaml
Scale
Currently, KubeBlocks supports vertically scaling a Milvus cluster.
Before you start
Check whether the cluster status is Running
. Otherwise, the following operations may fail.
- kubectl
- kbcli
kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster milvus-2.3.2 Delete Running 4m29s
kbcli cluster list mycluster -n demo
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
mycluster demo milvus-2.3.2 Delete Running Jul 05,2024 17:35 UTC+0800
Steps
- OpsRequest
- Edit cluster YAML file
- kbcli
-
Apply an OpsRequest to the specified cluster. Configure the parameters according to your needs.
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: ops-vertical-scaling
namespace: demo
spec:
clusterName: mycluster
type: VerticalScaling
verticalScaling:
- componentName: milvus
requests:
memory: "2Gi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "2"
EOF -
Check the operation status to validate the vertical scaling.
kubectl get ops -n demo
>
NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
demo ops-vertical-scaling VerticalScaling mycluster Succeed 3/3 6mIf an error occurs, you can troubleshoot with
kubectl describe ops -n demo
command to view the events of this operation. -
Check whether the corresponding resources change.
kubectl describe cluster mycluster -n demo
-
Change the configuration of
spec.componentSpecs.resources
in the YAML file.spec.componentSpecs.resources
controls the requirement and limit of resources and changing them triggers a vertical scaling.apiVersion: apps.kubeblocks.io/v1alpha1
kind: Cluster
metadata:
name: mycluster
namespace: demo
spec:
clusterDefinitionRef: milvus
clusterVersionRef: milvus-2.3.2
componentSpecs:
- name: milvus
componentDefRef: milvus
replicas: 1
resources: # Change the values of resources.
requests:
memory: "2Gi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "2"
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
terminationPolicy: Delete -
Check whether the corresponding resources change.
kubectl describe cluster mycluster -n demo
-
Set the
--cpu
and--memory
values according to your needs and run the following command to perform vertical scaling.kbcli cluster vscale mycluster -n demo --cpu=1 --memory=1Gi --components=milvus
Please wait a few seconds until the scaling process is over.
-
Validate the vertical scaling operation.
-
View the OpsRequest progress.
KubeBlocks outputs a command automatically for you to view the OpsRequest progress. The output includes the status of this OpsRequest and Pods. When the status is
Succeed
, this OpsRequest is completed.kbcli cluster describe-ops mycluster-verticalscaling-rpw2l -n demo
-
Check the cluster status.
kbcli cluster list mycluster -n demo
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
mycluster demo Delete Updating Jul 05,2024 17:35 UTC+0800- STATUS=Updating: it means the vertical scaling is in progress.
- STATUS=Running: it means the vertical scaling operation has been applied.
- STATUS=Abnormal: it means the vertical scaling is abnormal. The reason may be that the number of the normal instances is less than that of the total instance or the leader instance is running properly while others are abnormal.
To solve the problem, you can manually check whether this error is caused by insufficient resources. Then if AutoScaling is supported by the Kubernetes cluster, the system recovers when there are enough resources. Otherwise, you can create enough resources and troubleshoot with
kubectl describe
command.
-
-
After the OpsRequest status is
Succeed
or the cluster status isRunning
again, check whether the corresponding resources change.kbcli cluster describe mycluster -n demo
Volume Expansion
Before you start
Check whether the cluster status is Running
. Otherwise, the following operations may fail.
- kubectl
- kbcli
kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster milvus-2.3.2 Delete Running 4m29s
kbcli cluster list mycluster -n demo
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
mycluster demo milvus-2.3.2 Delete Running Jul 05,2024 17:35 UTC+0800
Steps
- OpsRequest
- Edit cluster YAML file
- kbcli
-
Change the value of storage according to your need and run the command below to expand the volume of a cluster.
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: ops-volume-expansion
namespace: demo
spec:
clusterName: mycluster
type: VolumeExpansion
volumeExpansion:
- componentName: milvus
volumeClaimTemplates:
- name: data
storage: "40Gi" # Set the volume storage size.
EOF -
Validate the volume expansion operation.
kubectl get ops -n demo
>
NAMESPACE NAME TYPE CLUSTER STATUS PROGRESS AGE
demo ops-volume-expansion VolumeExpansion mycluster Succeed 3/3 6mIf an error occurs to the vertical scaling operation, you can troubleshoot with
kubectl describe ops -n demo
command to view the events of this operation. -
Check whether the corresponding cluster resources change.
kubectl describe cluster mycluster -n demo
-
Change the value of
spec.componentSpecs.volumeClaimTemplates.spec.resources
in the cluster YAML file.spec.componentSpecs.volumeClaimTemplates.spec.resources
is the storage resource information of the pod and changing this value triggers the volume expansion of a cluster.kubectl edit cluster mycluster -n demo
Edit the value of
spec.componentSpecs.volumeClaimTemplates.spec.resources
....
spec:
clusterDefinitionRef: milvus
clusterVersionRef: milvus-2.3.2
componentSpecs:
- name: milvus
componentDefRef: milvus
replicas: 2
volumeClaimTemplates:
- name: data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 40Gi # Change the volume storage size
... -
Check whether the corresponding cluster resources change.
kubectl describe cluster mycluster -n demo
-
Set the
--storage
value according to your need and run the command to expand the volume.kbcli cluster volume-expand mycluster -n demo --storage=40Gi --components=milvus -t data
The volume expansion may take a few minutes.
-
Validate the volume expansion operation.
-
View the OpsRequest progress.
KubeBlocks outputs a command automatically for you to view the details of the OpsRequest progress. The output includes the status of this OpsRequest and PVC. When the status is
Succeed
, this OpsRequest is completed.kbcli cluster describe-ops mycluster-volumeexpansion-5pbd2 -n demo
-
View the cluster status.
kbcli cluster list mycluster
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
mycluster demo milvus-2.3.2 Delete Running Jul 05,2024 17:35 UTC+0800- STATUS=Updating: it means the volume expansion is in progress.
- STATUS=Running: it means the volume expansion operation has been applied.
-
-
After the OpsRequest status is
Succeed
or the cluster status isRunning
again, check whether the corresponding resources change.kbcli cluster describe mycluster -n demo
Restart
- OpsRequest
- kbcli
-
Restart a cluster.
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: ops-restart
namespace: demo
spec:
clusterName: mycluster
type: Restart
restart:
- componentName: milvus
EOF -
Check the pod and operation status to validate the restarting.
kubectl get pod -n demo
kubectl get ops ops-restart -n demoDuring the restarting process, there are two status types for pods.
- STATUS=Terminating: it means the cluster restart is in progress.
- STATUS=Running: it means the cluster has been restarted.
-
Configure the values of
components
andttlSecondsAfterSucceed
and run the command below to restart a specified cluster.kbcli cluster restart mycluster -n demo --components="milvus" --ttlSecondsAfterSucceed=30
components
describes the component name that needs to be restarted.ttlSecondsAfterSucceed
describes the time to live of an OpsRequest job after the restarting succeeds.
-
Validate the restarting.
Run the command below to check the cluster status to check the restarting status.
kbcli cluster list mycluster -n demo
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
milvus demo milvus-2.3.2 Delete Running Jul 05,2024 18:35 UTC+0800- STATUS=Updating: it means the cluster restart is in progress.
- STATUS=Running: it means the cluster has been restarted.
Stop/Start a cluster
You can stop/start a cluster to save computing resources. When a cluster is stopped, the computing resources of this cluster are released, which means the pods of Kubernetes are released, but the storage resources are reserved. You can start this cluster again to restore it to the state it was in before it was stopped.
Stop a cluster
-
Configure the name of your cluster and run the command below to stop this cluster.
- OpsRequest
- Edit Cluster YAML File
- kbcli
Apply an OpsRequest to stop a cluster.
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: ops-stop
namespace: demo
spec:
clusterName: mycluster
type: Stop
EOFkubectl edit cluster mycluster -n demo
Configure replicas as 0 to delete pods.
...
spec:
clusterDefinitionRef: milvus
clusterVersionRef: milvus-2.3.2
terminationPolicy: Delete
componentSpecs:
- name: milvus
componentDefRef: milvus
disableExporter: true
replicas: 0 # Change this value
...kbcli cluster stop mycluster -n demo
-
Check the status of the cluster to see whether it is stopped.
- kubectl
- kbcli
kubectl get cluster mycluster -n demo
kbcli cluster list mycluster -n demo
Start a cluster
-
Configure the name of your cluster and run the command below to start this cluster.
- OpsRequest
- Edit Cluster YAML File
- kbcli
Apply an OpsRequest to start a cluster.
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1alpha1
kind: OpsRequest
metadata:
name: ops-start
namespace: demo
spec:
clusterName: mycluster
type: Start
EOFkubectl edit cluster mycluster -n demo
Change replicas back to the original amount to start this cluster again.
...
spec:
clusterDefinitionRef: milvus
clusterVersionRef: milvus-2.3.2
terminationPolicy: Delete
componentSpecs:
- name: milvus
componentDefRef: milvus
disableExporter: true
replicas: 1 # Change this value
...kbcli cluster start mycluster -n demo
-
Check the status of the cluster to see whether it is running again.
- kubectl
- kbcli
kubectl get cluster mycluster -n demo
kbcli cluster list mycluster -n demo
Delete a cluster
Termination policy
The termination policy determines how a cluster is deleted.
terminationPolicy | Deleting Operation |
---|---|
DoNotTerminate | DoNotTerminate prevents deletion of the Cluster. This policy ensures that all resources remain intact. |
Delete | Delete deletes Cluster resources like Pods, Services, and Persistent Volume Claims (PVCs), leading to a thorough cleanup while removing all persistent data. |
WipeOut | WipeOut is an aggressive policy that deletes all Cluster resources, including volume snapshots and backups in external storage. This results in complete data removal and should be used cautiously, primarily in non-production environments to avoid irreversible data loss. |
To check the termination policy, execute the following command.
- kubectl
- kbcli
kubectl get cluster mycluster -n demo
>
NAME CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS AGE
mycluster milvus-2.3.2 Delete Running 14m
kbcli cluster list mycluster -n demo
>
NAME NAMESPACE CLUSTER-DEFINITION VERSION TERMINATION-POLICY STATUS CREATED-TIME
mycluster demo milvus-2.3.2 Delete Running Jul 05,2024 17:35 UTC+0800
Steps
Run the command below to delete a specified cluster.
- kubectl
- kbcli
If you want to delete a cluster and its all related resources, you can modify the termination policy to WipeOut
, then delete the cluster.
kubectl patch -n demo cluster mycluster -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo cluster mycluster
kbcli cluster delete mycluster -n demo