# AddK3sCluster

- Module: [CommunityToolkit.Aspire.Hosting.K3s](/reference/api/go/communitytoolkit.aspire.hosting.k3s.md)
- Version: `13.6.1`
- Kind: `method`
- Defined on: `DistributedApplicationBuilder`
- Source: [GitHub](https://github.com/CommunityToolkit/Aspire/tree/d9c2addac6f527ef2e33712d290b84574b455a3b)

Adds a k3s Kubernetes cluster to the distributed application.

## Remarks

The cluster runs as a privileged container using the `rancher/k3s` image.
No host-side `kubectl`, `helm`, or `k3s` binaries are required.
Three kubeconfig variants are written to `{AppHostDirectory}/.k3s/{name}/`
when the cluster becomes ready:
-
-
Call `WithReference(cluster)` on a dependent resource builder to inject
these credentials automatically.
All other cluster options are available as fluent builder methods:
`WithK3sVersion`, `WithAgentCount`, `WithPodSubnet`,
`WithServiceSubnet`, `WithDisabledComponent`,
`WithExtraArg`, `WithDataVolume`, `WithHelmImage`,
`WithKubectlImage`, and `WithLifetime`.

## Definition

```go
func (DistributedApplicationBuilder) AddK3sCluster(
    name string,
    options ...*AddK3sClusterOptions,
) K3sClusterResource
```

## Parameters

- `name` (`string`) -- The resource name. Also used as the DNS hostname by which containers in the DCP network reach the cluster's API server (e.g. `https://{name}:6443`).
- `options` (`...*AddK3sClusterOptions`) -- optional settings, see [Options](#options)

## Options

Optional parameters are passed in a trailing `options ...*AddK3sClusterOptions` argument.

- `ApiServerPort` (`*float64`) -- Host port to bind the Kubernetes API server (port 6443) to. When `null` (the default) a random available port is assigned.
- `AgentCount` (`*float64`) -- Number of k3s agent (worker) nodes to add. When `null` (the default) a single-node cluster is created -- the server node acts as both control-plane and worker. Equivalent to calling `WithAgentCount` on the returned builder.

## Returns

[`K3sClusterResource`](/reference/api/go/communitytoolkit.aspire.hosting.k3s/k3sclusterresource.md) -- A builder for the `K3sClusterResource`.

Returns a handle; failures surface later via `Err()`.

## Applies to

- `DistributedApplicationBuilder`
