The nodeSelector or nodeAffinity is used to limit the range of nodes to which applications can be scheduled, preventing the entire cluster from being threatened due to the exceptions of a single application.
When using a containerized application, comply with the minimum privilege principle and properly set securityContext of Deployments or StatefulSets.
Example YAML for a Deployment:
apiVersion: apps/v1 kind: Deployment metadata: name: security-context-example namespace: security-example spec: replicas: 1 selector: matchLabels: app: security-context-example label: security-context-example strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: annotations: seccomp.security.alpha.kubernetes.io/pod: runtime/default labels: app: security-context-example label: security-context-example spec: containers: - image: ... imagePullPolicy: Always name: security-context-example securityContext: allowPrivilegeEscalation: false readOnlyRootFilesystem: true runAsUser: 1000 capabilities: add: - NET_BIND_SERVICE drop: - all volumeMounts: - mountPath: /etc/localtime name: localtime readOnly: true - mountPath: /opt/write-file-dir name: tmpfs-example-001 securityContext: seccompProfile: type: RuntimeDefault volumes: - hostPath: path: /etc/localtime type: "" name: localtime - emptyDir: {} name: tmpfs-example-001
If application containers on a node do not need to access Kubernetes, you can perform the following operations to disable containers from accessing kube-apiserver:
On the Clusters page of the CCE console, click the name of the cluster to find the information on the details page.
iptables -I OUTPUT -s {container_cidr} -d {Private API server IP} -j REJECT
iptables -I FORWARD -s {container_cidr} -d {Private API server IP} -j REJECT
{container_cidr} indicates the container CIDR of the cluster, for example, 10.0.0.0/16.
To ensure configuration persistence, you are advised to write the command to the /etc/rc.local script.
curl -k https://{Private API server IP}:5443