This is unreleased documentation for SUSE® Virtual Clusters v1.3.0 (Dev).

Advanced Usage

This document provides advanced usage information for k3k, including detailed use cases and explanations of the Cluster resource fields for customization.

Customizing the Cluster Resource

The Cluster resource provides a variety of fields for customizing the behavior of your virtual clusters. See CRD documentation for the full specifications.

Most of these customization options can also be configured using the k3kcli tool. Refer to k3kcli documentation for more details.

This example creates a "shared" mode K3k cluster with:

  • 3 servers

  • K3s version v1.31.3-k3s1

  • Custom network configuration

  • Deployment on specific nodes with the nodeSelector

  • kube-api exposed using an ingress

  • Custom K3s serverArgs

  • ETCD data persisted using a PVC

apiVersion: k3k.io/v1beta1
kind: Cluster
metadata:
  name: my-virtual-cluster
  namespace: my-namespace
spec:
  mode: shared
  version: v1.31.3-k3s1
  servers: 3
  tlsSANs:
    - my-cluster.example.com
  nodeSelector:
    disktype: ssd
  expose:
    ingress:
      ingressClassName: nginx
      annotations:
        nginx.ingress.kubernetes.io/ssl-passthrough: "true"
        nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
        nginx.ingress.kubernetes.io/ssl-redirect: "HTTPS"
  clusterCIDR: 10.42.0.0/16
  serviceCIDR: 10.43.0.0/16
  clusterDNS: 10.43.0.10
  serverArgs:
  - --tls-san=my-cluster.example.com
  persistence:
    type: dynamic
    storageClassName: local-path

mode

The mode field specifies the cluster provisioning mode, which can be either shared or virtual. The default mode is shared.

  • shared mode: In this mode, the virtual cluster shares the host cluster’s resources and networking. This mode is suitable for lightweight workloads and development environments where isolation is not a primary concern.

  • virtual mode: In this mode, the virtual cluster runs as a separate K3s cluster within the host cluster. This mode provides stronger isolation and is suitable for production workloads or when dedicated resources are required.

version

The version field specifies the Kubernetes version to be used by the virtual nodes. If not specified, K3k will use the same K3s version as the host cluster. For example, if the host cluster is running Kubernetes v1.31.3, K3k will use the corresponding K3s version (e.g., v1.31.3-k3s1).

servers

The servers field specifies the number of K3s server nodes to deploy for the virtual cluster. The default value is 1.

agents

The agents field specifies the number of K3s agent nodes to deploy for the virtual cluster. The default value is 0.

In shared mode, this field is ignored, as the Virtual Kubelet acts as the agent, and there are no K3s worker nodes.

nodeSelector

The nodeSelector field allows you to specify a node selector that will be applied to all server/agent pods. In shared mode, the node selector will also be applied to the workloads.

expose

The expose field contains options for exposing the API server of the virtual cluster. By default, the API server is only exposed as a ClusterIP, which is relatively secure but difficult to access from outside the cluster.

You can use the expose field to enable exposure via NodePort, LoadBalancer, or Ingress.

TLS passthrough is required

The K3s API server authenticates clients with mTLS certificates, so the TLS connection must reach the API server untouched. An ingress controller that terminates TLS will break both kubectl and agent authentication. Any controller used with expose.ingress must therefore support TLS passthrough.

expose.ingress also requires at least one DNS name in spec.tlsSANs: the SANs are used as the Ingress hosts, and the Ingress API does not accept IP addresses there. IPs in tlsSANs are ignored when building the Ingress.

Nginx

Supported directly through expose.ingress. The ingress controller has to be started with the --enable-ssl-passthrough flag, and the annotations enabling passthrough must be set on the Ingress:

spec:
  tlsSANs:
    - my-cluster.example.com
  expose:
    ingress:
      ingressClassName: nginx
      annotations:
        nginx.ingress.kubernetes.io/ssl-passthrough: "true"
        nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
        nginx.ingress.kubernetes.io/ssl-redirect: "HTTPS"

Traefik

Traefik cannot perform layer 4 TLS passthrough with a standard Ingress, so expose.ingress does not work with it.

Use expose.loadBalancer or expose.nodePort instead, or leave expose unset and create a Traefik IngressRouteTCP pointing at the cluster’s ClusterIP service:

apiVersion: traefik.io/v1alpha1
kind: IngressRouteTCP
metadata:
  name: my-virtual-cluster
  namespace: my-namespace
spec:
  entryPoints:
    - websecure
  routes:
    - match: HostSNI(`my-cluster.example.com`)
      services:
        - name: k3k-my-virtual-cluster-service # k3k-<cluster-name>-service
          port: 443
  tls:
    passthrough: true

See How-to: Expose Virtual Cluster Kubernetes API for more ways to expose the API server.

Custom Service annotations

expose.annotations adds annotations directly to the Service generated for expose.loadBalancer or expose.nodePort. This is useful to configure cloud-provider-specific load balancer behavior, such as requesting an internal (private) load balancer or a static IP.

This is different from expose.ingress.annotations, which annotates the Ingress resource instead (for example, to enable TLS passthrough).

spec:
  expose:
    loadBalancer: {}
    annotations:
      service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
      service.beta.kubernetes.io/aws-load-balancer-internal: "true"

Annotations are reconciled on every update to the Cluster resource: adding, changing, or removing a key in expose.annotations updates the generated Service accordingly, and any annotations set by other controllers are preserved.

LoadBalancer

expose.loadBalancer exposes the API server through a Service of type LoadBalancer. It accepts the following optional fields:

  • serverPort: an additional port on which the K3s server is exposed. The Service always keeps port 443, because the internal components reach the server on it: the bootstrap process, other servers, agents, and pods. A custom serverPort is added as an extra port named k3s-server-lb-port. If you set it to 0 or a negative value, only port 443 is allocated.

  • etcdPort: the port on which etcd is exposed. The default is 2379. If you set it to 0 or a negative value, etcd is not exposed.

  • loadBalancerClass: the class of the load-balancer implementation for the Service. If you do not set it, the default load-balancer implementation of the host cluster is used. This is useful when the host cluster runs more than one load-balancer implementation.

loadBalancerClass cannot be changed after it is set

Kubernetes does not allow changing the loadBalancerClass of a Service while its type remains LoadBalancer. K3k therefore rejects any update that changes or removes loadBalancerClass once it is set.

To avoid this, set loadBalancerClass when you create the cluster.

The following example uses the AWS Load Balancer Controller class to expose the API server through a Network Load Balancer on port 6443. Replace service.k8s.aws/nlb with the class of the load-balancer implementation in your host cluster:

spec:
  expose:
    loadBalancer:
      serverPort: 6443
      loadBalancerClass: service.k8s.aws/nlb

When the cluster is exposed with a LoadBalancer, K3k does the following:

  • Waits for the load balancer to assign an address to the Service before configuring the server pods.

  • Adds the IP addresses and host names assigned by the load balancer to the TLS SANs of the server certificate. You do not need to add them to spec.tlsSANs.

  • Generates a kubeconfig that points to the first load-balancer address, using serverPort if it is set or 443 otherwise.

  • Rolls out the server pods when the load-balancer address changes or when you switch the expose type, because K3s reads its TLS SANs only at startup.

k3kcli cluster create always exposes the cluster with a NodePort and has no flag for expose.loadBalancer. To use a LoadBalancer, create the Cluster resource with a manifest, or select Load Balancer under Direct API Server Exposure in the Rancher UI. The Rancher UI sets the ports but not loadBalancerClass, so set loadBalancerClass with a manifest. A VirtualClusterPolicy cannot set the expose configuration.

clusterCIDR

The clusterCIDR field specifies the CIDR range for the pods of the cluster. The default value is 10.42.0.0/16 in shared mode, and 10.52.0.0/16 in virtual mode.

serviceCIDR

The serviceCIDR field specifies the CIDR range for the services in the cluster. The default value is 10.43.0.0/16 in shared mode, and 10.53.0.0/16 in virtual mode.

In shared mode, the serviceCIDR should match the host cluster’s serviceCIDR to prevent conflicts and in virtual mode both serviceCIDR and clusterCIDR should be different than the host cluster.

clusterDNS

The clusterDNS field specifies the IP address for the CoreDNS service. It needs to be in the range provided by serviceCIDR. The default value is 10.43.0.10.

serverArgs

The serverArgs field allows you to specify additional arguments to be passed to the K3s server pods.

serverAffinity and agentAffinity

The serverAffinity field allows you to specify the node affinity or inter-pod affinity/anti-affinity for the server pods. The accepted value is the same as the k8s version https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity.

The agentAffinity field allows you to specify the node affinity or inter-pod affinity/anti-affinity for the agent pods. The accepted value is the same as the k8s version https://kubernetes.io/docs/concepts/scheduling-eviction/assign-pod-node/#affinity-and-anti-affinity.

Note that if a server or agent affinity is specified in the VirtualClusterPolicy assigned to this cluster, those affinities will override the values set in the cluster CRD.

Using the cli

You can check the k3kcli documentation for the full specs.

No storage provider:

  • Ephemeral Storage:

      k3kcli cluster create --persistence-type ephemeral my-cluster
  • Using --persistence-type ephemeral will result in data loss if the nodes are restarted.

  • It is highly recommended to use --persistence-type dynamic with a configured storage class.

Managing kubeconfig contexts

When a virtual cluster is ready, k3kcli cluster create adds it as a new context to your current kubeconfig. The context is named <namespace>/<name>. For example, a cluster named mycluster created in the default k3k-mycluster namespace gets the k3k-mycluster/mycluster context.

k3kcli writes the context to the same kubeconfig file it reads, using the same precedence as kubectl:

  1. The file passed with the --kubeconfig flag.

  2. The first file listed in the $KUBECONFIG environment variable.

  3. The default $HOME/.kube/config file.

k3kcli adds the cluster, user, and context entries of the virtual cluster to this file. The other entries and the current context remain unchanged. k3kcli does not switch your current context, so kubectl keeps pointing to the host cluster until you select the new context.

The following example shows the end of the k3kcli cluster create output:

INFO[0003] Extracting Kubeconfig for 'myshared' cluster
WARN[0003] Writing a kubeconfig to the current directory is deprecated and will be removed in a future release. The cluster is now added as a context to your kubeconfig. Use --out to keep writing a standalone kubeconfig, or --no-out to silence this warning.
INFO[0003] Wrote a standalone kubeconfig to '/home/user/k3k-myshared-myshared-kubeconfig.yaml'
INFO[0003] Added the 'k3k-myshared/myshared' context in the current kubeconfig (/home/user/.kube/config)

You can start using the cluster with:

	kubectl config use-context k3k-myshared/myshared
	kubectl cluster-info

Switching between clusters

List the available contexts:

$ kubectl config get-contexts
CURRENT   NAME                    CLUSTER                 AUTHINFO                NAMESPACE
*         default                 default                 default
          k3k-hcp/hcp             k3k-hcp/hcp             k3k-hcp/hcp
          k3k-myshared/myshared   k3k-myshared/myshared   k3k-myshared/myshared

Switch to a virtual cluster:

$ kubectl config use-context k3k-hcp/hcp
Switched to context "k3k-hcp/hcp".

To switch back to the host cluster, select its context again, for example kubectl config use-context default.

To run a single command against a virtual cluster without changing the current context, use the --context flag:

kubectl --context k3k-myshared/myshared get nodes

Standalone kubeconfig files

Earlier versions of k3kcli wrote a standalone kubeconfig file named <namespace>-<name>-kubeconfig.yaml to the current directory. k3kcli still writes this file by default and prints a deprecation warning.

Standalone kubeconfig file in the current directory is deprecated

A future release will stop writing a kubeconfig file to the current directory. Scripts that rely on this file will no longer find it.

Use the context added to your kubeconfig, or pass --out <path> to write the file explicitly.

Use one of the following flags with k3kcli cluster create or k3kcli kubeconfig generate to control the standalone file:

--out <path>

Writes a standalone kubeconfig to <path> and does not print the deprecation warning. Use this option when you need a separate file, for example to share access to the virtual cluster.

--no-out

Does not write a standalone kubeconfig file and does not print the deprecation warning. The context is still added to your kubeconfig.

You cannot use --out and --no-out together.

# Add the context only
k3kcli cluster create --no-out mycluster

# Add the context and write a standalone file
k3kcli cluster create --out ./mycluster-kubeconfig.yaml mycluster

The --config-name flag of k3kcli kubeconfig generate is deprecated. Use --out instead.

Regenerating a kubeconfig

k3kcli kubeconfig generate handles kubeconfig files the same way as k3kcli cluster create. It adds the <namespace>/<name> context to your current kubeconfig and accepts the --out and --no-out flags. If the context already exists, its entries are overwritten. Use this command in the following cases:

  • To add the context for a cluster created with a Cluster resource.

  • To get new credentials, for example to rotate the certificates.

k3kcli kubeconfig generate --no-out k3k-mycluster/mycluster

The cluster argument accepts either <name>, which uses the k3k-<name> namespace, or <namespace>/<name>. The --name flag is deprecated in favor of this argument.

Removing contexts

k3kcli cluster delete removes the context, cluster, and user entries of the deleted cluster from your current kubeconfig. If the removed context was the current context, k3kcli clears the current context.

$ k3kcli cluster delete k3k-myshared/myshared
INFO[0001] Deleting 'myshared' cluster in namespace 'k3k-myshared'
INFO[0001] Removed the 'k3k-myshared/myshared' context from the current kubeconfig (/home/user/.kube/config)

k3kcli cluster delete does not remove standalone kubeconfig files. Delete them manually.