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 the containers controlled by Custom Resource (CR), you can configure security context in any CRs (TidbCluster/DmCluster/TidbInitializer/TidbMonitor/Backup/BackupSchedule/Restore) to make the containers run as a non-root user.
You can use either of the following two types of configuration. If you configure both the cluster level and the component level for a component, only the configuration of the component level takes effect.
Configure
podSecurityContextat the cluster level (spec.podSecurityContext) for all components. The following is an example configuration:spec: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000Configure at the component level for a specific component. For example, configuring
spec.tidb.podSecurityContextforTidbCluster,spec.master.podSecurityContextforDMCluster. The following is an example configuration:spec: pd: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000 tidb: podSecurityContext: runAsUser: 1000 runAsGroup: 2000 fsGroup: 2000