# Use your cluster with kubectl

Download your cluster's kubeconfig once. It keeps working across upgrades and control plane restarts.

## Connect with kubectl

![Download kubeconfig on the cluster page](https://imagedelivery.net/xZXo0QFi-1_4Zimer-T0XQ/3a0ec514-0dcf-42bc-0647-8a301186f900/md2x =2060x540)

1. Go to **Kubernetes** and open your cluster.
2. Click **Download kubeconfig**.
3. Save the file as `~/.kube/my-cluster.yaml`, with your cluster's name in place of `my-cluster`.
4. Point `kubectl` at it:

```bash
[label Connect to your cluster]
chmod 600 ~/.kube/my-cluster.yaml
export KUBECONFIG=~/.kube/my-cluster.yaml
kubectl get nodes
```

Your worker nodes are listed with status `Ready`.

## Secure the Kubernetes API

The kubeconfig holds a cluster-admin token, so anyone with the file has full access to your cluster. Treat it like a private key.

The API endpoint on port 6443 is open to any address by default. Limit it to your office or VPN ranges:

1. On the cluster page, click **Configure**.
2. Under **API access**, replace `0.0.0.0/0` with a range you connect from, for example `203.0.113.0/24`.
3. Remove the `::/0` row.
4. Click **Add CIDR** for each other range you need.
5. Click **Save changes**.

Workers reach the control plane over an internal network, so no value here can break your cluster.

### Rotate a leaked kubeconfig

Click **Rotate kubeconfig** on the cluster page and confirm with **Rotate kubeconfig**. Every previously downloaded kubeconfig stops working within about 10 seconds. Download the new one to keep using `kubectl`.

## Expose a service with a load balancer

A Service of `type: LoadBalancer` creates a Better Stack [load balancer](https://betterstack.com/docs/cloud/load-balancers/#load-balancers-created-by-kubernetes) and publishes its public IP addresses in the Service. Deleting the Service removes the load balancer.

```yaml
[label Expose an app on port 80]
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: LoadBalancer
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
```

`EXTERNAL-IP` shows `<pending>` for about 2 minutes while the load balancer boots.

```bash
[label Check the load balancer's IP addresses]
kubectl get service web
```

- **Its type follows your node count.** Set the type or the algorithm when you create the Service, with the `load-balancer.betterstack.com/type` and `load-balancer.betterstack.com/algorithm` annotations, for example `LB2` and `least_connections`.
- **It's managed by the Service.** It's listed in **Load balancers** and billed as an ordinary load balancer, but you change it through the Service manifest, not the dashboard.
- **Node changes don't drop traffic.** On a scale-down, recycle or upgrade, a node is taken out of its load balancers before it's drained.
- **UDP ports work**, but the client's IP address never reaches your pods over UDP.
- **NodePorts aren't reachable from the internet.** Public traffic always arrives through a load balancer.

No ingress controller is included. Install your own, such as Traefik, behind a `LoadBalancer` Service.

### Get the client's IP address

By default your pods see the load balancer's address, not the caller's. `externalTrafficPolicy: Local` doesn't change that. To receive the client's IP address, turn on PROXY protocol in your backend and annotate the Service with `load-balancer.betterstack.com/proxy-protocol: "v2"`:

[code-tabs]
```yaml
[label Traefik Helm values]
service:
  annotations:
    load-balancer.betterstack.com/proxy-protocol: "v2"
ports:
  web:
    proxyProtocol:
      trustedIPs: ["100.89.94.0/24", "10.42.0.0/16"]
  websecure:
    proxyProtocol:
      trustedIPs: ["100.89.94.0/24", "10.42.0.0/16"]
```
```yaml
[label ingress-nginx Helm values]
controller:
  service:
    annotations:
      load-balancer.betterstack.com/proxy-protocol: "v2"
  config:
    use-proxy-protocol: "true"
```
```nginx
[label Plain nginx]
listen 80 proxy_protocol;
set_real_ip_from 100.89.94.0/24;
set_real_ip_from 10.42.0.0/16;
real_ip_header proxy_protocol;
```
[/code-tabs]

Trust both ranges. `100.89.94.0/24` carries the connection under `externalTrafficPolicy: Local`, and your pod CIDR, `10.42.0.0/16` by default, under `Cluster`.

[warning]
#### Every pod behind the Service must accept PROXY protocol
A backend that doesn't expect the header reads it as a malformed request, while the load balancer's health checks keep passing. Turn PROXY protocol on in the backend first, then add the annotation.
[/warning]

## Store data on persistent volumes

PersistentVolumeClaims get Better Stack [volumes](https://betterstack.com/docs/cloud/volumes/volumes/) from the default StorageClass, `betterstack-volume`. Leave out `storageClassName` and claims land there.

- **ReadWriteOnce only.** A volume is attached to one node at a time.
- **Created where the pod runs.** A volume is created on first use, on the node its pod lands on. If the pod moves, the volume follows it.
- **Grow-only.** Raise the claim's request and the filesystem grows online. Up to 10 volumes per node.
- **Snapshots work out of the box.** A `VolumeSnapshot` uses the default class `betterstack-volume-snapshots`. Restore one by creating a claim with `dataSource` pointing at it.
- **Volumes outlive the cluster.** They're listed in **Volumes** and billed as ordinary volumes. With the default reclaim policy `Delete`, removing the claim removes the volume.

A claim that stays `Pending` always says why in its events.

```bash
[label See why a claim is pending]
kubectl describe pvc
```

For scratch data, the `local-path` StorageClass keeps data on the node itself. That data is lost when the node is replaced, recycled or reclaimed.

## Schedule pods on the right nodes

Every worker carries these labels for node selectors and topology spread constraints:

| Label | Value |
|---|---|
| `betterstack.com/node-pool` | The node pool's name |
| `node.kubernetes.io/instance-type` | The server type |
| `topology.kubernetes.io/region` | The region |
| `topology.kubernetes.io/zone` | The data center |

All nodes of a cluster share one data center.

## What else works in the cluster

- **Network policies are enforced.** A default-deny policy also blocks DNS, so allow egress to CoreDNS in `kube-system` on UDP and TCP port 53 next to it.
- **Pod autoscaling.** metrics-server is installed, so `kubectl top` and the HorizontalPodAutoscaler work out of the box.
- **Admission webhooks and aggregated APIs** such as cert-manager, Istio, Kyverno and KEDA work with their charts' defaults.
- **Pod hardening.** `RuntimeDefault` AppArmor and seccomp profiles load on every worker, so the Pod Security restricted profile needs no node setup. Custom `Localhost` AppArmor profiles aren't supported.

## Let external services verify pod tokens

The cluster publishes its service-account token issuer through standard OIDC discovery, for verifiers such as HashiCorp Vault:

```bash
[label Show the OIDC discovery document]
kubectl get --raw /.well-known/openid-configuration
```

The verifier needs the cluster's CA certificate. It's the base64-decoded `certificate-authority-data` in your kubeconfig. The verifier's address must also be allowed under **API access**, since discovery runs on the same port as `kubectl`.

## Need help?

Please let us know at hello@betterstack.com.  
We're happy to help! 🙏
