> For the complete documentation index, see [llms.txt](https://docs.octoxlabs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.octoxlabs.com/adapters/k/kubernetes.md).

# Kubernetes

Kubernetes is a container orchestration platform. OctoXLabs reads the cluster API with a service account token. It adds nodes to the device inventory and, when you turn the options on, cluster resources to the service inventory, network and exposure rules to the policy inventory, and ingress hosts to the web application inventory.

## Use cases

* **List cluster nodes.** Review the nodes that Kubernetes reports.

```
Adapters = kubernetes
```

* **Find Linux nodes by kernel version.** Check kernels across the cluster.

```
Adapters = kubernetes AND OS.KernelVersion ~ <version>
```

* **Find Services exposed to the internet.** Turn on **Fetch Service Exposure Policies**. Then review LoadBalancer and node port rules in the policy inventory.

```
Adapters = kubernetes AND Direction = Inbound
```

## Before you begin

### Network

| Source              | Destination           | Port                | Protocol |
| ------------------- | --------------------- | ------------------- | -------- |
| OctoXLabs connector | Kubernetes API server | 6443 (configurable) | HTTPS    |

Clear **Is Secure?** to use HTTP.

### Required permissions

| Scope or role                                      | Permission | Needed for                                                                                              |
| -------------------------------------------------- | ---------- | ------------------------------------------------------------------------------------------------------- |
| nodes (core API group)                             | list       | Devices (Fetch Devices)                                                                                 |
| networkpolicies (networking.k8s.io)                | list       | Fetch Network Policies                                                                                  |
| services (core API group)                          | list       | Fetch Service Exposure Policies and Fetch Web Applications                                              |
| ingresses (networking.k8s.io)                      | list       | Fetch Ingress Policies and Fetch Web Applications                                                       |
| endpointslices (discovery.k8s.io)                  | list       | Fetch Web Applications                                                                                  |
| ingressclasses (networking.k8s.io, cluster scoped) | list       | Fetch Web Applications. Needs an additional ClusterRole. Without it the ingress controller stays empty. |
| Resources of the Fetch Services option             | list       | Fetch Services. See the note below.                                                                     |

The baseline says the built-in **view** ClusterRole already grants list on networkpolicies, services, ingresses and endpointslices. It does not grant ingressclasses. The `/version` endpoint is used for the connection test. Without a permission, the matching data is skipped and the rest still works. NetworkPolicy objects are enforced only when the cluster runs a CNI plugin that supports them (Calico, Cilium, Antrea, OVN-Kubernetes). The inventory reports the declared rules, not whether they are enforced.

Fetch Services lists these resources: namespaces, pods, deployments, services, persistentvolumes, statefulsets, daemonsets, jobs, cronjobs, configmaps, secrets, persistentvolumeclaims, networkpolicies, serviceaccounts and ingresses. A resource that the token cannot list is skipped.

## Create credentials in Kubernetes

{% stepper %}
{% step %}

### Create a service account

Create a Kubernetes **ServiceAccount** for OctoXLabs.
{% endstep %}

{% step %}

### Grant read access

Bind the built-in **view** ClusterRole to it. Add a ClusterRole that allows `list` on `ingressclasses` if you use **Fetch Web Applications**.
{% endstep %}

{% step %}

### Create a token

Create a signed bearer token for the service account. See [Kubernetes: Service Account Tokens](https://kubernetes.io/docs/reference/access-authn-authz/authentication/#service-account-tokens).
{% endstep %}

{% step %}

### Note the API server address

Note the address and the port of the API server (6443 by default).
{% endstep %}
{% endstepper %}

## Connection parameters

| Parameter                       | Required | Type    | Default | Description                                                                                                                                                                                                |
| ------------------------------- | -------- | ------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| IP                              | Yes      | String  | None    | IP address of the Kubernetes API server.                                                                                                                                                                   |
| Port                            | No       | Integer | 6443    | Port number.                                                                                                                                                                                               |
| API Token                       | Yes      | String  | None    | Service account bearer token.                                                                                                                                                                              |
| Is Secure?                      | No       | Boolean | True    | Connect over HTTPS.                                                                                                                                                                                        |
| Fetch Devices                   | No       | Boolean | True    | Fetch the nodes as devices.                                                                                                                                                                                |
| Fetch Services                  | No       | Boolean | False   | Fetch Kubernetes resources into the service inventory.                                                                                                                                                     |
| Fetch Network Policies          | No       | Boolean | False   | Collect NetworkPolicy rules as policy assets.                                                                                                                                                              |
| Fetch Service Exposure Policies | No       | Boolean | False   | Collect the access rules of externally reachable Services as policy assets. It covers `loadBalancerSourceRanges` on LoadBalancer Services and node ports. ClusterIP and ExternalName Services are skipped. |
| Fetch Ingress Policies          | No       | Boolean | False   | Collect the source-range restrictions declared on Ingress resources. They are set through ingress-controller annotations, so they appear only for controllers that support them, such as NGINX or HAProxy. |
| Fetch Web Applications          | No       | Boolean | False   | Collect one web application per Ingress and served hostname: load balancer addresses, TLS secret, WAF annotations of the ingress controller, backend Services and pods, and links to the policy records.   |
| Timeout                         | No       | Integer | 30      | Seconds to wait for an API response.                                                                                                                                                                       |

## Troubleshooting

| Error message                                                  | Cause                                                                                       | Fix                                                                                                   |
| -------------------------------------------------------------- | ------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
| Kubernetes table read failed for {url}: {error}                | A list call used by Fetch Web Applications failed. The table is skipped.                    | Check that the token can list that resource. Missing ingressclasses only leaves the controller empty. |
| Could not retrieved {resource\_type}, URL: {url}               | A resource of the Fetch Services option could not be listed. It is skipped (debug message). | Give the token list permission on that resource, or ignore if you do not need it.                     |
| Status Code: `, followed by URL, Method and the response body` | The API answered with a status other than 200. This is the shared REST connector error.     | Read the status code and body in the message, then check the credentials, permissions and URL.        |

## Change history

{% hint style="info" %}
Available starting from version 5.4.2.
{% endhint %}
