Run Containers as a Non-root User
In some Kubernetes environments, containers cannot be run as the root user. In this case, you can set securityContext to run containers as a non-root user.
Configure TiDB Operator containers
For TiDB Operator containers, you can configure security context in the Helm values.yaml file. All TiDB Operator components (at <controllerManager/scheduler/advancedStatefulset/admissionWebhook>.securityContext) support this configuration.
The following is an example configuration:
controllerManager:
securityContext:
runAsUser: 1000
runAsGroup: 2000
fsGroup: 2000
Configure containers controlled by CR
For containers controlled by Custom Resources (CRs), you can configure the security context at the Pod or container level. The supported fields vary by CR.
Configure the Pod-level security context
podSecurityContext defines Pod-level security settings, such as volume ownership, and provides default security settings for all containers in the generated Pod.
For TidbCluster and DMCluster, you can configure podSecurityContext at the following levels:
Cluster level: configure
spec.podSecurityContextto apply the settings to all components:spec: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000Component level: configure
spec.<component>.podSecurityContextto apply the settings to a specific component. For example, usespec.tidb.podSecurityContextfor the TiDB component of aTidbCluster, orspec.master.podSecurityContextfor the master component of aDMCluster:spec: pd: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000 tidb: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000
If both levels are configured, the component-level podSecurityContext replaces the cluster-level podSecurityContext for that component.
Other CRs, including TidbDashboard, TidbNGMonitoring, TidbInitializer, TidbMonitor, Backup, BackupSchedule, and Restore, also support Pod-level security context settings. For the supported fields and paths, refer to the API documentation.
Configure the container-level security context
Starting from TiDB Operator v1.6.6, the following CRs support container-level security context:
- For
TidbClusterandDMCluster, configurespec.<component>.securityContext, such asspec.pd.securityContext,spec.tidb.securityContext,spec.master.securityContext, orspec.worker.securityContext. These CRs do not support cluster-levelspec.securityContext. - For
TidbDashboard, configurespec.securityContext. - For
TidbNGMonitoring, configurespec.ngMonitoring.securityContext. Configuring top-levelspec.securityContexthas no effect on the generated container. - For
TidbMonitor, configuresecurityContextfor each generated container. The supported paths includespec.initializer.securityContext,spec.prometheus.securityContext,spec.grafana.securityContext,spec.reloader.securityContext,spec.prometheusReloader.securityContext,spec.thanos.securityContext, andspec.dm.initializer.securityContext.
TidbInitializer, Backup, BackupSchedule, and Restore do not support container-level securityContext.
For the complete ComponentSpec.securityContext schema, refer to the ComponentSpec API documentation.
For example, the following configuration runs the PD container as a non-root user, sets the group ownership of its volumes, and prevents container processes from gaining more privileges than their parent processes:
spec:
pd:
podSecurityContext:
fsGroup: 2000
securityContext:
runAsNonRoot: true
runAsUser: 1000
runAsGroup: 2000
allowPrivilegeEscalation: false