In the following example, an nginx ingress controller is used with specific permissions:
apiVersion: v1
kind: Pod
metadata:
name: nginx-ingress-security-context-demo
namespace: ingress-nginx
spec:
securityContext:
fsGroup: 33
serviceAccountName: nginx-ingress-serviceaccount
containers:
- name: nginx-ingress-controller
image: nginx-ingress-controller:0.26.1
args:
- /nginx-ingress-controller
- --ingress-class=dos-nginx-controller-3
- --annotations-prefix=nginx.ingress.kubernetes.io
securityContext:
allowPrivilegeEscalation: true
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
runAsUser: 33
ports:
- name: http
containerPort: 80
protocol: TCP
- name: https
containerPort: 443
protocol: TCP
Here is a full example: https://raw.githubusercontent.com/kubernetes/ingress-nginx/nginx-0.26.1/deploy/static/mandatory.yaml
If you see the highlighted/bold part in the above template, you will see that the container is adding the Linux capability `
CAP_NET_BIND_SERVICE` via the
securityContext. At the same time, the template is also defining to run this container as user 33 (
www-data) via
runAsUser:33.
However, after persistent disk resizes and cluster upgrades, when we check the capabilities on the running process in the container, we see the permitted and effective capabilities are removed from the file and the process.
The container fails to start with:
Error: exit status 1
nginx: the configuration file /tmp/nginx-cfg488150005 syntax is ok
2019/07/31 11:48:34 [emerg] 81#81: bind() to 0.0.0.0:80 failed (13: Permission denied)