Do you understand kyverno polices? Can you implement a simple policy to make sure all of…
- User
Do you understand kyverno polices? Can you implement a simple policy to make sure all of the replicas of a deployment run on a different node.
- Assistant
Do you understand kyverno polices? Can you implement a simple policy to make sure all of the replicas of a deployment run on a different node.
Yes, I understand Kyverno policies and can help you create one to ensure that all replicas of a Deployment run on different nodes. This can be achieved by enforcing Pod Anti-Affinity rules, which prevent pods from being scheduled on the same node based on defined criteria.
Below is a simple Kyverno policy that:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: ensure-disparate-nodes
spec:
validationFailureAction: enforce
rules:
- name: check-pod-anti-affinity
match:
resources:
kinds:
- Deployment
validate:
message: "All replicas must be scheduled on different nodes using pod anti-affinity."
pattern:
spec:
template:
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: "?*"
topologyKey: "kubernetes.io/hostname"
- name: add-pod-anti-affinity
match:
resources:
kinds:
- Deployment
mutate:
patchStrategicMerge:
spec:
template:
spec:
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: "{{ request.object.metadata.labels.app }}"
topologyKey: "kubernetes.io/hostname"
Policy Metadata
ClusterPolicy.ensure-disparate-nodes.spec.validationFailureAction
enforce, which means that any Deployment that doesn't comply with the policy will be rejected.Rules
check-pod-anti-affinityDeployment.podAntiAffinity rule.
spec.template.spec.affinity.podAntiAffinity.requiredDuringSchedulingIgnoredDuringExecution: Requires that pods use anti-affinity based on the app label and the node's hostname.add-pod-anti-affinityDeployment.podAntiAffinity rules if they're missing.
app label to ensure that anti-affinity rules apply to pods with the same application label.topologyKey: "kubernetes.io/hostname" ensures that pods are scheduled on different nodes based on hostname.Validation Rule (check-pod-anti-affinity): When a Deployment is created or updated, Kyverno checks if the pod template includes the necessary Pod Anti-Affinity rules. If not, the Deployment is rejected with the specified message.
Mutation Rule (add-pod-anti-affinity): If the Pod Anti-Affinity rules are missing, Kyverno automatically injects them into the Deployment’s pod template. This ensures that even if the user doesn't specify the anti-affinity, it gets enforced.
Save the Policy: Save the above YAML content to a file, e.g., ensure-disparate-nodes.yaml.
Apply the Policy:
kubectl apply -f ensure-disparate-nodes.yaml
Deploy Applications: When deploying your applications using Deployments, Kyverno will automatically enforce that all replicas run on different nodes.
Here's an example Deployment that will comply with the above policy:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchLabels:
app: my-app
topologyKey: "kubernetes.io/hostname"
With the Kyverno policy in place, you can omit the affinity section, and Kyverno will automatically add it:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
Kyverno ensures that the affinity settings are present and correctly configured to distribute replicas across different nodes.
Label Matching: The policy assumes that Deployments have an app label. Ensure that your Deployments are labeled accordingly.
Topology Key: The topologyKey is set to kubernetes.io/hostname, which is standard for node-based scheduling. Adjust if you're using different topology keys (e.g., zones, regions).
Policy Scope: This is a ClusterPolicy, meaning it applies across the entire Kubernetes cluster. If you want to scope it to specific namespaces, consider using a Policy instead.
By implementing this Kyverno policy, you ensure high availability and fault tolerance for your applications by distributing replicas across different nodes, thus avoiding single points of failure.
Not yet included in a published release.