<button class="button button-with-icon cloud-select-dropdown__button button-primary" aria-haspopup="true">Version 1.12 </button>

- [Latest](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/latest/how-to/hardening-your-cluster)
- [Version 1.16](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.16/how-to/hardening-your-cluster.md.txt)
- [Earlier versions (unsupported)](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.12/getting-support.md.txt#version-support)

<br />

This document describes how to harden the security of your Google Distributed Cloud
clusters.

## Secure your containers using SELinux

You can secure your containers by enabling SELinux, which is supported for
Red Hat Enterprise Linux (RHEL) and CentOS. If your host machines are running
RHEL or CentOS and you want to enable SELinux for your cluster, you must enable
SELinux in all of your host machines. See
[secure your containers using SELinux](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.12/installing/configure-selinux.md.txt)
for details.

## Use `seccomp` to restrict containers

> [!WARNING]
>
> **Preview**
>
>
> This product or feature is
>
> subject to the "Pre-GA Offerings Terms" in the General Service Terms section
> of the [Service Specific
> Terms](https://docs.cloud.google.com/terms/service-terms#1).
>
> Pre-GA products and features are available "as is" and might have limited support.
>
> For more information, see the
> [launch stage descriptions](https://cloud.google.com/products/#product-launch-stages).

Secure computing mode (`seccomp`) is available in version 1.11 of Google Distributed Cloud.
Running containers with a `seccomp` profile improves the security of your
cluster because it restricts the system calls that containers are allowed to
make to the kernel. This reduces the chance of kernel vulnerabilities being
exploited.

The default `seccomp` profile contains a list of system calls that a container
is allowed to make. Any system calls not on the list are disallowed. `seccomp`
is enabled by default in version 1.11 of Google Distributed Cloud. This means that all
system containers and customer workloads are run with the container runtime's
default `seccomp` profile. Even containers and workloads that don't specify a
`seccomp` profile in their configuration files are subject to `seccomp`
restrictions.

### How to disable `seccomp` cluster-wide or on particular workloads

You can disable `seccomp` during cluster creation or cluster upgrade only.
`bmctl update` can't be used to disable this feature. If you want to disable
`seccomp` within a cluster, add the following `clusterSecurity` section to the
cluster's configuration file:

```yaml
apiVersion: baremetal.cluster.gke.io/v1
kind: Cluster
metadata:
  name: example
  namespace: cluster-example
spec:
...clusterSecurity:
enableSeccomp: false
...
```

In the unlikely event that some of your workloads need to execute system
calls that `seccomp` blocks by default, you don't have to disable `seccomp` on
the whole cluster. Instead, you can single out particular workloads to run in
`unconfined mode`. Running a workload in `unconfined mode` frees that workload
from the restrictions that the `seccomp` profile imposes on the rest of the
cluster.

To run a container in `unconfined mode`, add the following `securityContext`
section to the Pod manifest:

```yaml

apiVersion: v1
kind: Pod
....
spec:securityContext:
seccompProfile:
type: Unconfined
....
```

## Don't run containers as `root` user

By default, processes in containers execute as `root`. This poses a potential
security problem, because if a process breaks out of the container, that process
runs as root on the host machine. It's therefore advisable to run all your
workloads as a non-root user.

The following sections describe two ways of running containers as a non-root
user.

### Method #1: add `USER` instruction in `Dockerfile`

This method uses a `Dockerfile` to ensure that containers don't run as a `root`
user. In a `Dockerfile`, you can specify which user the process inside a container
should be run as. The following snippet from a `Dockerfile` shows how to do this:

```yaml
....

#Add a user with userid 8877 and name nonroot
RUN useradd −u 8877 nonroot

#Run Container as nonroot
USER nonroot
....
```

In this example, the Linux command `useradd -u` creates a user called `nonroot`
inside the container. This user has a user ID (UID) of `8877`.

The next line in the `Dockerfile` runs the command `USER nonroot`. This command
specifies that from this point on in the image, commands are run as the user
`nonroot`.

Grant permissions to UID `8877` so that the container processes can execute
properly for `nonroot`.

### Method #2: add securityContext fields in Kubernetes manifest file

This method uses a Kubernetes manifest file to ensure that containers don't run
as a `root` user. Security settings are specified for a Pod, and those security
settings are in turn applied to all containers within the Pod.

The following example shows an excerpt of a manifest file for a given Pod:

```yaml

apiVersion: v1
kind: Pod
metadata:
  name: name-of-pod
spec:
  securityContext:
    runAsUser: 8877
    runAsGroup: 8877
....
```

The `runAsUser` field specifies that for any containers in the Pod, all
processes run with user ID `8877`. The `runAsGroup` field specifies that these
processes have a primary group ID (GID) of `8877`. Remember to grant the
necessary and sufficient permissions to UID `8877` so that the container
processes can execute properly.

This ensures that processes within a container are run as UID `8877`, which has
fewer privileges than root.

System containers in Google Distributed Cloud help install and manage clusters,
and they have UIDs and GIDs in the range 2000--4999. Therefore, use UIDs
and GIDs that aren't in this reserved range when you assign permissions to user
workloads.

### How to disable rootless mode

Starting with Google Distributed Cloud release 1.10, Kubernetes control plane
containers and system containers run as non-root users by default.
Google Distributed Cloud assigns these users UIDs and GIDs in the range
2000--4999. However, this assignment can cause problems if those UIDs and
GIDs have already been allocated to processes running inside your environment.

Starting with Google Distributed Cloud release 1.11, you can disable rootless
mode when you upgrade your cluster. When rootless mode is disabled, Kubernetes
control plane containers and system containers run as the root user.

To disable rootless mode, perform the following steps:

1. Add the following `clusterSecurity` section to the cluster's configuration file:

   ```yaml
   apiVersion: baremetal.cluster.gke.io/v1
   kind: Cluster
   metadata:
     name: example
     namespace: cluster-example
   spec:
   ...clusterSecurity:
   enableRootlessContainers: false
   ...
   ```
2. Upgrade your cluster. For details, see [Upgrade clusters](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.12/how-to/upgrade.md.txt).

## Restrict the ability for workloads to self-modify

Certain Kubernetes workloads, especially system workloads, have permission to
self-modify. For example, some workloads vertically autoscale themselves. While
convenient, this can allow an attacker who has already compromised a node to
escalate further in the cluster. For example, an attacker could have a workload
on the node change itself to run as a more privileged service account that
exists in the same namespace.

Ideally, workloads should not be granted the permission to modify themselves in
the first place. When self-modification is necessary, you can limit permissions
by applying Gatekeeper or Policy Controller constraints, such as
[NoUpdateServiceAccount](https://github.com/open-policy-agent/gatekeeper-library/tree/master/library/general/noupdateserviceaccount)
from the open source Gatekeeper library, which provides several useful security
policies.

When you deploy policies, it is usually necessary to allow the controllers that
manage the cluster lifecycle to bypass the policies. This is necessary so that
the controllers can make changes to the cluster, such as applying cluster
upgrades. For example, if you deploy the `NoUpdateServiceAccount` policy on
Google Distributed Cloud, you must set the following parameters in the `Constraint`:

```yaml
parameters:
  allowedGroups:
  - system:masters
  allowedUsers: []
```

## Maintenance

Monitoring security bulletins and upgrading your clusters are important security
measures to take once your clusters are up and running.

### Monitor security bulletins

The GKE Enterprise security team publishes
[security bulletins](https://docs.cloud.google.com/anthos/clusters/docs/security-bulletins)
for high and critical severity vulnerabilities.

These bulletins follow a common Google Cloud vulnerability numbering
scheme and are linked from the main Google Cloud bulletins page and the
Google Distributed Cloud release notes.


[Use this XML feed to subscribe to security bulletins for Google Distributed Cloud and related products.
![Subscribe](https://docs.cloud.google.com/static/docs/images/feed-icon.png)](https://cloud.google.com/feeds/anthos-gke-security-bulletins.xml)

When customer action is required to address these high and critical
vulnerabilities, Google contacts customers by email. In addition, Google might
also contact customers with support contracts through support channels.

For more information about how Google manages security vulnerabilities and
patches for GKE and GKE Enterprise, see
[Security patching](https://docs.cloud.google.com/kubernetes-engine/docs/resources/security-patching.md.txt).

### Upgrade clusters

Kubernetes regularly introduces new security features and provides security
patches. Google Distributed Cloud releases incorporate Kubernetes security
enhancements that address security vulnerabilities that may affect your clusters.

You are responsible for keeping your GKE clusters up
to date. For each release, review the
[release notes](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.12/release-notes.md.txt). To minimize security risks to your
GKE clusters, plan to update to new patch releases every month and minor
versions every three months.

One of the many advantages of upgrading a cluster is that it automatically
refreshes the cluster's kubeconfig file. The kubeconfig file authenticates a
user to a cluster. The kubeconfig file is added to your cluster directory when
you create a cluster with `bmctl`. The default name and path is
`bmctl-workspace/CLUSTER_NAME/CLUSTER_NAME-kubeconfig`.
When you upgrade a cluster, that cluster's kubeconfig file is automatically
renewed. Otherwise, the kubeconfig file expires one year after it was created.

> [!NOTE]
> **Note:** Only the kubeconfig file for the cluster you are upgrading gets renewed. If, for example, you upgrade an admin cluster that manages three user clusters, only the admin cluster's kubeconfig file is refreshed. You must upgrade each of the user clusters separately to refresh their respective kubeconfig files.

For information about how to upgrade your clusters, see
[upgrade your clusters](https://docs.cloud.google.com/anthos/clusters/docs/bare-metal/1.12/how-to/upgrade.md.txt).