<pre><code>minikube start
--extra-config=apiserver.enable-admission-plugins=PodSecurityPolicy
--addons=pod-security-policy
</code></pre>
<p>we have a default namespace in which the nginx service account does not have the rights to launch the nginx container</p>
<p>when creating a pod, use the command</p>
<pre><code>kubectl run nginx --image=nginx -n default --as system:serviceaccount:default:nginx-sa
</code></pre>
<p>as a result, we get an error</p>
<pre><code> Error: container has runAsNonRoot and image will run as root (pod: &quot;nginx_default(49e939b0-d238-4e04-a122-43f4cfabea22)&quot;, container: nginx)
</code></pre>
<p>as I understand it, it is necessary to write a psp policy that will allow the nginx-sa service account to run under, but I do not understand how to write it correctly for a specific service account</p>
<pre><code>apiVersion: v1
kind: ServiceAccount
metadata:
  name: nginx-sa
  namespace: default

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nginx-sa-role
  namespace: default
rules:
  - apiGroups: [&quot;extensions&quot;, &quot;apps&quot;,&quot;&quot;]
    resources: [ &quot;deployments&quot;,&quot;pods&quot; ]
    verbs: [&quot;get&quot;, &quot;list&quot;, &quot;watch&quot;, &quot;create&quot;, &quot;update&quot;, &quot;patch&quot;, &quot;delete&quot;]

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: nginx-sa-role-binding
  namespace: default
subjects:
  - kind: ServiceAccount
    name: nginx-sa
    namespace: default
roleRef:
  kind: Role
  name: nginx-sa-role
  apiGroup: rbac.authorization.k8s.io
</code></pre>

Iceforest
<p><code>...but I do not understand how to write it correctly for a specific service account</code></p>
<p>After you get your special psp ready for your nginx, you can grant your nginx-sa to use the special psp like this:</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: role-to-use-special-psp
rules:
- apiGroups:
  - policy
  resourceNames:
  - special-psp-for-nginx
  resources:
  - podsecuritypolicies
  verbs:
  - use
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: bind-to-role
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: role-to-use-special-psp
subjects:
- kind: ServiceAccount
  name: nginx-sa
  namespace: default
</code></pre>
