本节内容:pod的网络、内核功能、运行时用户配置
kubectl exec pod-with-host-network -- ifconfig
apiVersion: v1
kind: Pod
metadata:
name: pod-with-host-network
spec:
hostNetwork: true # 使用主机的网络
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
kubectl exec pod-with-host-pid-and-ipc -- ps aux
apiVersion: v1
kind: Pod
metadata:
name: pod-with-host-pid-and-ipc
spec:
hostPID: true
hostIPC: true
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
这种情况不安全,所以在Dockerfile中使用USER来指定用户
kubectl run pod-with-defaults --image alpine --restart Never -- /bin/sleep 999999 pod/pod-with-defaults created
kubectl exec pod-with-defaults -- id
kubectl exec pod-as-user-guest -- id
apiVersion: v1
kind: Pod
metadata:
name: pod-as-user-guest
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
runAsUser: 405 #用户的id,不是用户名,405对应guest
apiVersion: v1
kind: Pod
metadata:
name: pod-run-as-non-root
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
runAsNonRoot: true #
/dev 目录是设备目录,关联硬件设备的
apiVersion: v1
kind: Pod
metadata:
name: pod-privileged
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
privileged: true # 特权模式
linux 内核功能默认以 CAP_开头,但是在pod中声明时可以不加前缀
kubectl exec -it pod-add-settime-capability -- date +%T -s "12:00:00"
apiVersion: v1
kind: Pod
metadata:
name: pod-add-settime-capability
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
capabilities:
add:
- SYS_TIME #
默认情况下容器拥有CAP_CHOWN权限, 允许进程修改⽂件系统中⽂件的所有者。
kubectl exec pod-with-defaults chown guest /tmp
kubectl exec pod-with-defaults -- ls -la / | grep tmp
kubectl exec pod-drop-chown-capability -- chown guest /tmp
# 权限被禁用了apiVersion: v1
kind: Pod
metadata:
name: pod-drop-chown-capability
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
capabilities:
drop:
- CHOWN
kubectl exec pod-with-readonly-filesystem -- touch /new-file
# 失败kubectl exec pod-with-readonly-filesystem -- touch /volume/newfile
kubectl exec pod-with-readonly-filesystem -- ls -la /volume
apiVersion: v1
kind: Pod
metadata:
name: pod-with-readonly-filesystem
spec:
containers:
- name: main
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
readOnlyRootFilesystem: true # 不允许对根文件系统写入
volumeMounts:
- name: my-volume
mountPath: /volume
readOnly: false # 允许挂载的存储卷可以被读写
volumes:
- name: my-volume
emptyDir:
kubectl exec -it pod-with-shared-volume-fsgroup -c first -- id
kubectl exec -it pod-with-shared-volume-fsgroup -c second -- id
apiVersion: v1
kind: Pod
metadata:
name: pod-with-shared-volume-fsgroup
spec:
securityContext:
fsGroup: 555
supplementalGroups: [666, 777]
containers:
- name: first
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
runAsUser: 1111
volumeMounts:
- name: shared-volume
mountPath: /volume
readOnly: false
- name: second
image: alpine
command: ["/bin/sleep", "999999"]
securityContext:
runAsUser: 2222
volumeMounts:
- name: shared-volume
mountPath: /volume
readOnly: false
volumes:
- name: shared-volume
emptyDir:
从 Kubernetes v1.21开始,Pod Security Policy 将被弃用,并将在 v1.25 中删除,Kubernetes 在 1.22 版本引入了 Pod Security Admission 作为其替代者。
如果要使用,需要api-server开启PodSecurityPolicy
默认PodSecurityPolicy 是不生效的,需要手动配置
kubectl create clusterrole psp-default --verb=use --resource=podsecuritypolicies --resource-name=default
kubectl create clusterrolebinding psp-all-users --clusterrole=psp-default --group=system:authenticated --serviceaccount=foo:default
apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
name: default
spec:
privileged: true
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
seLinux:
rule: RunAsAny
volumes:
- '*'
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: default
spec:
hostIPC: false # 不能使用宿主机的IPC
hostPID: false # 不能使用宿主机的PID
hostNetwork: false # 不能使用宿主机的网络空间
hostPorts: # 只能使用宿主机中固定的端口
- min: 10000
max: 11000
- min: 13000
max: 14000
privileged: false # 不能特权模式运行
readOnlyRootFilesystem: true # 容器根目录只读
runAsUser: # 可以用任意用户运行容器
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
seLinux: # 可以使用任意的SELinux选项, SELinux 安全选项
rule: RunAsAny
volumes: # 可以用所有类型的存储卷
- '*'
kubectl get psp
# psp -> PodSecurityPolicy
如果是Dockerfile 中指定的USER,在运行yaml时候会被覆写成2.
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: default
spec:
hostIPC: false
hostPID: false
hostNetwork: false
hostPorts:
- min: 10000
max: 11000
- min: 13000
max: 14000
privileged: false
readOnlyRootFilesystem: true
runAsUser: # 指定用户ID
rule: MustRunAs
ranges:
- min: 2
max: 2
fsGroup:
rule: MustRunAs
ranges:
- min: 2
max: 10
- min: 20
max: 30
supplementalGroups:
rule: MustRunAs
ranges:
- min: 2
max: 10
- min: 20
max: 30
seLinux:
rule: RunAsAny
volumes:
- '*'
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: default
spec:
allowedCapabilities: # 允许添加的
- SYS_TIME
defaultAddCapabilities: # 默认添加的
- CHOWN
requiredDropCapabilities: # 不允许的操作
- SYS_ADMIN
- SYS_MODULE
hostIPC: false
hostPID: false
hostNetwork: false
hostPorts:
- min: 10000
max: 11000
- min: 13000
max: 14000
privileged: false
readOnlyRootFilesystem: true
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
seLinux:
rule: RunAsAny
volumes:
- '*'
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
name: default
spec:
runAsUser:
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
seLinux:
rule: RunAsAny
volumes: # 允许的存储卷类型
- emptyDir
- configMap
- secret
- downwardAPI
- persistentVolumeClaim
kind: NetworkPolicy
metadata:
name: default-deny
spec:
podSelector: # 空的则是匹配所有
NetworkPolicy允许具有app=webserver标签的pod访问具有app=database的pod的访问,并且仅限访问5432端⼜
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: postgres-netpolicy
spec:
podSelector:
matchLabels:
app: database
ingress:
- from:
- podSelector:
matchLabels:
app: webserver
ports:
- port: 5432
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: ipblock-netpolicy
spec:
podSelector:
matchLabels:
app: shopping-cart
ingress:
- from:
- ipBlock:
cidr: 192.168.1.0/24
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: egress-net-policy
spec:
podSelector:
matchLabels:
app: webserver
egress:
- to:
- podSelector:
matchLabels:
app: database
ports:
- port: 5432