Deep dive into Kubernetes Deployments

Introduction

Setting Resource Requests / Limits

Requests

Limits

Example

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world-app
template:
metadata:
labels:
app: hello-world-app
spec:
containers:
- name: hello-world
image: tutum/hello-world:latest
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: "1"
memory: "1G"
requests:
cpu: "0.5"
memory: "256M"
ports:
- name: http
containerPort: 80

Setting pod affinity rules

apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world-app
template:
metadata:
labels:
app: hello-world-app
spec:
containers:
- name: hello-world
image: tutum/hello-world:latest
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: "1"
memory: "1G"
requests:
cpu: "0.5"
memory: "256M"
ports:
- name: http
containerPort: 80
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: app
operator: In
values:
- hello-world-app
topologyKey: "kubernetes.io/hostname"
> kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
hello-world-58f885df9-99ts2 1/1 Running 0 17m
hello-world-84fc85967b-gx28g 0/1 Pending 0 9s
> kubectl get event --field-selector involvedObject.name=hello-world-84fc85967b-gx28g
LAST SEEN   TYPE      REASON             OBJECT                             MESSAGE
77s Warning FailedScheduling pod/hello-world-84fc85967b-gx28g 0/1 nodes are available: 1 node(s) didn't match pod affinity/anti-affinity, 1 node(s) didn't match pod anti-affinity rules.
> kubectl get replicasets.apps
NAME                     DESIRED   CURRENT   READY   AGE
hello-world-58f885df9 1 1 1 23m
hello-world-84fc85967b 1 1 0 6m16s
> kubectl delete replicasets.apps hello-world-58f885df9

Creating and setting priority classes

apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: high
value: 6000000
preemptionPolicy: Never
globalDefault: false
description: "This priority class will not cause other pods to be preempted."
---
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: low
value: 300000
preemptionPolicy: Never
globalDefault: false
description: "This priority class will not cause other pods to be preempted."
> kubectl get priorityclasses.scheduling.k8s.io
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
app: hello-world-app
template:
metadata:
labels:
app: hello-world-app
spec:
containers:
- name: hello-world
image: tutum/hello-world:latest
imagePullPolicy: IfNotPresent
resources:
limits:
cpu: "1"
memory: "1G"
requests:
cpu: "0.5"
memory: "256M"
ports:
- name: http
containerPort: 80
priorityClassName: high

That’s it

Experienced Full-stack Developer | PHP | Kubernetes | Go | DevOps | AWS | Google Cloud

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store