Sync a namespaced custom resource from the control plane cluster
This guide walks through syncing a namespaced custom resource from the control plane cluster to a tenant clusterTenant clusterA fully isolated Kubernetes environment provisioned for a single tenant. Each tenant cluster has its own API server, controller manager, and resource namespace, backed by a virtualized control plane hosted on a control plane cluster. From the tenant's perspective it behaves exactly like a standard Kubernetes cluster., using an example CRD. For the config reference, see Custom resources from the control plane cluster.
Set up cluster contexts​
Setting up the control plane and tenant cluster contexts makes it easier to switch between them.
export HOST_CTX="your-host-context"
export VCLUSTER_CTX="vcluster-ctx"
Then, create a namespace in your control plane clusterControl plane clusterThe Kubernetes cluster that hosts the virtualized control planes for tenant clusters. The control plane cluster is operated by the platform provider and is completely invisible to tenants. There are no shared control plane nodes, no in-cluster agent pods, and no lateral path between tenant environments. With shared nodes, this cluster also runs tenant workloads alongside the control plane pods — the same node pool is used for both.. This example uses foobar2:
kubectl --context="${HOST_CTX}" create namespace foobar2
You can find your contexts by running kubectl config get-contexts
Create a CustomResourceDefinition in the control plane cluster​
Save the following CustomResourceDefinition:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: examples.demo.loft.sh
spec:
group: demo.loft.sh
versions:
- name: v1
served: true
storage: true
schema:
openAPIV3Schema:
type: object
properties:
spec:
type: object
properties:
image:
type: string
replicas:
type: integer
additionalPrinterColumns:
- name: Image
type: string
description: The image of an example
jsonPath: .spec.image
- name: Replicas
type: integer
description: The number of replicas in example
jsonPath: .spec.replicas
scope: Namespaced
names:
plural: examples
singular: example
kind: Example
Save this file locally and apply it to the control plane cluster:
kubectl --context="${HOST_CTX}" create -f example-crd.yaml
Enable from-host syncing for your custom resource​
Enable from-host syncing for Example custom resources in your tenant cluster configuration:
sync:
fromHost:
customResources:
examples.demo.loft.sh:
enabled: true
scope: Namespaced
mappings:
byName:
"foobar2/*": "default/*"
This configuration:
- Enables from-host syncing of
examples.demo.loft.shfrom thefoobar2namespace you created earlier. - Automatically configures RBAC permissions for vClustervClusterAn open-source software product that creates and manages tenant clusters within Kubernetes infrastructure. vCluster provides tenant isolation capabilities while reducing infrastructure costs., so it can get, watch, and list
Exampleresources in thefoobar2namespace. - Syncs all
Examplesfrom thefoobar2namespace to thedefaultnamespace in the tenant cluster.
Create or update a tenant cluster following the vCluster quick start guide.
Sync a namespaced custom resource to the tenant cluster​
First, create an example resource in the control plane cluster.
Copy this file and save it locally as
example-cr.yaml:apiVersion: demo.loft.sh/v1kind: Examplemetadata:name: my-examplenamespace: foobar2spec:image: "my-image:latest"replicas: 2Then create the example in the control plane cluster:
Create example in the hostkubectl --context="${HOST_CTX}" create -f example-cr.yamlCheck the custom resource in the tenant cluster:
Get synced CustomResourcekubectl --context="${VCLUSTER_CTX}" get examples.demo.loft.sh --namespace defaultYou should see similar output:
examples in vClusterNAME IMAGE REPLICASmy-example my-image:latest 2Now edit the example in the control plane cluster and see that the change syncs to the tenant cluster. To set replicas to 4, run:
Patch Example CRkubectl --context="${HOST_CTX}" patch examples.demo.loft.sh my-example --type='json' -p='[{"op": "replace", "path": "/spec/replicas", "value": 4}]' --namespace foobar2Tenant Cluster Check the number of replicas:
Check examplekubectl --context="${VCLUSTER_CTX}" get --namespace default examples.demo.loft.shYou should see the number of replicas updated from the control plane cluster object:
Example updated in vClusterNAME IMAGE REPLICASmy-example my-image:latest 4
Ensure the custom resource synced to the tenant cluster​
The custom resource should now be accessible in the tenant cluster. If you edit a field on the tenant object, it stays until the control plane cluster changes that same field, which then overwrites it.