Secrets
By default, this is turned off.
vCluster automatically adds the required cluster RBAC permissions for retrieving the Secrets and syncing the resources from the host to the virtual cluster.
Enabling sync from host allows you to sync Secrets from the specified namespaces in the host cluster to the specified namespaces in a virtual cluster.
Here is how the required configuration in vcluster.yaml
looks like:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs all Secrets from "foo" namespace
# to the "bar" namespace in a virtual cluster. Secrets names are unchanged.
"foo/*": "bar/*"
Here are a few things to remember when configuring the sync:
- It is also possible to modify the name of the synced resource in the virtual cluster.
- There is no option to sync from all namespaces in the host.
- Sync is one-directional, from host to virtual. If you modify an object in the host, vCluster syncs the change to virtual object.
- When you delete a virtual object, vCluster re-creates it if the host object still exist.
- When you delete a host object, vCluster deletes the corresponding virtual object.
- It is not possible to sync Secrets that were already synced from virtual to host, they are skipped by vCluster.
Namespaces in the virtual cluster are created automatically during the sync (if they do not exist already).
You can use synced Secrets in your workloads as an environment variables source or a volume.
Prerequisites
-
Administrator access to a Kubernetes cluster: See Accessing Clusters with kubectl for more information. Run the command
kubectl auth can-i create clusterrole -A
to verify that your current kube-context has administrative privileges.infoTo obtain a kube-context with admin access, ensure you have the necessary credentials and permissions for your Kubernetes cluster. This typically involves using
kubectl config
commands or authenticating through your cloud provider's CLI tools. -
helm
: Helm v3.10 is required for deploying the platform. Refer to the Helm Installation Guide if you need to install it. -
kubectl
: Kubernetes command-line tool for interacting with the cluster. See Install and Set Up kubectl for installation instructions.
All the specified namespaces have to exist in the host at the vCluster startup.
Sync all Secrets from host namespace
To sync all Secrets from a given namespace in the host to the given namespace in the virtual cluster, use “namespace/*” wildcard, e.g.:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs all Secrets from "foo" namespace
# to the "bar" namespace in a virtual cluster. Secrets names are unchanged.
"foo/*": "bar/*"
Sync only specific Secrets from host namespace
To sync only specific Secrets from namespaces, you need to provide namespace/name
as the key and value:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs Secret named "cm-name" from "foo" host namespace
# to the "bar" namespace in virtual.
"foo/cm-name": "bar/cm-name"
Sync all Secrets from virtual cluster's host namespace
There is also a handy syntax to sync all Secrets from virtual cluster’s own host namespace to the virtual namespace. As virtual cluster’s namespace is not always known upfront (e.g. when virtual cluster is created by the platform), ""
(empty string) is treated as “virtual cluster’s own host namespace”.
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs all Secrets from virtual cluster's host namespace
# to "my-virtual" namespace in a virtual cluster.
"": "my-virtual"
Sync specific Secret from virtual cluster's host namespace
you can also specify only a few Secrets from virtual cluster’s own host namespace this way:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs Secret named "my-cm" from virtual cluster's host namespace
# to "my-virtual-namespace" in a virtual cluster.
"/my-cm": "my-virtual/my-cm"
Modify synced Secret namespace and name in the virtual cluster
It’s also possible to modify Secret name during the sync:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
# syncs "config" Secret from "cert-manager" namespace in the host
# as "my-config" in "my-virtual" namespace in a virtual cluster.
"cert-manager/config": "my-virtual/my-config"
Patches
This feature is available in the vCluster Pro tier. Contact us for more details and to start a trial.
You can specify reverseExpression
in the sync.fromHost.secrets.patches
.
They are applied on the host object and appear in the virtual object.
expressions
have no effect.
So, for the following vcluster.yaml
:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
"default/my-cm": "barfoo2/cm-my"
patches:
- path: metadata.annotations[*]
# optional reverseExpression to reverse the change from the host cluster
reverseExpression: "value.startsWith('www.') ? value.slice(4) : value"
- Your Secret in the host namespace
default
namedmy-cm
is synced to the namespacebarfoo2
in virtual and namedcm-my
. - If
default/my-cm
host object has annotation which value starts withwww.
, e.g.:my-address: www.loft.sh
then synced object in the virtual clusterbarfoo2/cm-my
has annotationmy-address: loft.sh
.
From host Secret sync example
This guide shows how to sync Kubernetes Secret from host clusters and how you can use them in your workload running inside virtual clusters.
Set up cluster contexts
Setting up the host and virtual 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 host cluster, use foobar
as an example:
kubectl --context="${HOST_CTX}" create namespace foobar
You can find your contexts by running kubectl config get-contexts
Enable from host syncing for Secret
Enable the from host syncing for secrets in your virtual cluster configuration:
sync:
fromHost:
secrets:
enabled: true
mappings:
byName:
"foobar/shared-user-env": "my-namespace/user-env"
This configuration:
- Enables from host syncing of the Secret
shared-user-env
in the namespacefoobar
. - Automatically configures RBAC permissions for vCluster, so it can access this Secret (you need to re-deploy vCluster for it to take place)
- Makes this Secret accessible as
user-env
in themy-namespace
in your vCluster.
Create or update a virtual Cluster
following the vCluster quick start
guide.
Sync Secret to virtual cluster and use it in pod
First, create a Secret which you want to sync in the host cluster:
Create Secret in the hostkubectl --context="${HOST_CTX}" create secret generic shared-user-env \
--namespace=foobar \
--from-literal=MY_ENV_1=foo \
--from-literal=MY_ENV_2=barCheck if the Secret is accessible in the virtual cluster:
Get synced Secretkubectl --context="${VCLUSTER_CTX}" get secrets --namespace my-namespace user-env -o yaml
you should see similar output:
Secret contentsapiVersion: v1
data:
MY_ENV_1: Zm9v
MY_ENV_2: YmFy
kind: Secret
metadata:
creationTimestamp: "2025-02-17T12:12:59Z"
name: user-env
namespace: my-namespace
resourceVersion: "15919"
uid: 26cc03d6-5cde-4090-9f9d-3e6fcbeefd24
type: OpaqueNow, you can create a new pod in the
my-namespace
namespace and specify this Secret as a source for environment variables.Save this pod locally to the file called
pod.yaml
:pod.yamlapiVersion: v1
kind: Pod
metadata:
name: my-pod
namespace: my-namespace
spec:
containers:
- name: busybox
image: busybox
command:
- sleep
- "inf"
env:
# Plain Text ENV
- name: DEMO_GREETING
value: "Hello from the environment"
envFrom:
- secretRef:
name: user-envthen, create it in the virtual cluster:
Create podkubectl --context="${VCLUSTER_CTX}" create -f pod.yaml
Virtual Cluster Wait for pod running
Wait for pod runningkubectl --context="${VCLUSTER_CTX}" wait --for=condition=ready pod my-pod --namespace my-namespace --timeout=300s
once it is running, you can list pod's environment variables:
Check environment variableskubectl --context="${VCLUSTER_CTX}" exec -it --namespace my-namespace my-pod -- printenv | grep "MY_ENV"
you should see environment successfully injected from the Secret:
Environment variables from SecretMY_ENV_1=foo
MY_ENV_2=barFrom host Secret syncing allow you to make specific Secret(s) from host clusters accessible inside your virtual clusters. You can make them accessible from different namespaces and/or with different names in the virtual cluster. They are always synced from host to the virtual, so it is also possible to sync one host Secret to the multiple virtual ones. They can also be used as a volume or env source in your workloads.
Ensure that Secret got synced to the virtual cluster
Your Secret should be now accessible in the virtual cluster. Keep in mind, that any edit made in the virtual object is overwritten by the host object data.
Use it in your workload
Verify pod environment variables
Summary
Config reference
secrets
required object pro
Secrets defines if secrets in the host should get synced to the virtual cluster.
secrets
required object proenabled
required boolean false pro
Enabled defines if this option should be enabled.
enabled
required boolean false propatches
required object[] pro
Patches patch the resource according to the provided specification.
patches
required object[] propath
required string pro
Path is the path within the patch to target. If the path is not found within the patch, the patch is not applied.
path
required string proexpression
required string pro
Expression transforms the value according to the given JavaScript expression.
expression
required string proreverseExpression
required string pro
ReverseExpression transforms the value according to the given JavaScript expression.
reverseExpression
required string proreference
required object pro
Reference treats the path value as a reference to another object and will rewrite it based on the chosen mode
automatically. In single-namespace mode this will translate the name to "vxxxxxxxxx" to avoid conflicts with
other names, in multi-namespace mode this will not translate the name.
reference
required object proapiVersion
required string pro
APIVersion is the apiVersion of the referenced object.
apiVersion
required string proapiVersionPath
required string pro
APIVersionPath is optional relative path to use to determine the kind. If APIVersionPath is not found, will fallback to apiVersion.
apiVersionPath
required string prokind
required string pro
Kind is the kind of the referenced object.
kind
required string prokindPath
required string pro
KindPath is the optional relative path to use to determine the kind. If KindPath is not found, will fallback to kind.
kindPath
required string pronamePath
required string pro
NamePath is the optional relative path to the reference name within the object.
namePath
required string pronamespacePath
required string pro
NamespacePath is the optional relative path to the reference namespace within the object. If omitted or not found, namespacePath equals to the
metadata.namespace path of the object.
namespacePath
required string prolabels
required object pro
Labels treats the path value as a labels selector.
labels
required object proselector
required object pro
Selector for Namespace and Object
selector
required object promappings
required object {} pro
Mappings is a map of host-object-namespace/host-object-name: virtual-object-namespace/virtual-object-name.
There are several wildcards supported:
- To match all objects in host namespace and sync them to different namespace in vCluster:
mappings:
"foo/": "foo-in-virtual/"
- To match specific object in the host namespace and sync it to the same namespace with the same name:
mappings:
"foo/my-object": "foo/my-object"
- To match specific object in the host namespace and sync it to the same namespace with different name:
mappings:
"foo/my-object": "foo/my-virtual-object"
- To match all objects in the vCluster host namespace and sync them to a different namespace in vCluster:
mappings:
"": "my-virtual-namespace/*"
- To match specific objects in the vCluster host namespace and sync them to a different namespace in vCluster:
mappings:
"/my-object": "my-virtual-namespace/my-object"
mappings
required object {} pro