0% found this document useful (0 votes)
3 views

lecture2

///////////////////////////

Uploaded by

dim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lecture2

///////////////////////////

Uploaded by

dim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

Agenda

● Commands
● Resources Limits
● NameSpaces
● Taint & Tolerations
● Node Selectors
● Node Affinity
Demo: Specify a memory request and a memory limit

apiVersion: v1
kind: Pod
metadata:
name: memory-demo
namespace: mem-example
spec:
containers:
- name: memory-demo-ctr
image: polinux/stress
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
Demo: Exceed a Container's memory limit

apiVersion: v1
kind: Pod
metadata:
name: memory-demo-2
namespace: mem-example
spec:
containers:
- name: memory-demo-2-ctr
image: polinux/stress
resources:
requests:
memory: "50Mi"
limits:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]
Demo: Specify a memory request that is too big for your Nodes

apiVersion: v1
kind: Pod
metadata:
name: memory-demo-3
namespace: mem-example
spec:
containers:
- name: memory-demo-3-ctr
image: polinux/stress
resources:
limits:
memory: "1000Gi"
requests:
memory: "1000Gi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
Demo: Specify a CPU request and a CPU limit

apiVersion: v1
kind: Pod
metadata:
name: cpu-demo
namespace: cpu-example
spec:
containers:
- name: cpu-demo-ctr
image: vish/stress
resources:
limits:
cpu: "1"
requests:
cpu: "0.5"
args:
- -cpus
- "2"
Note: you could also limit the total number of
other kinds of object. For example, you might
decide to limit how many Deployments that can
live in a single namespace.
Configure Memory and CPU Quotas for a Namespace

kubectl create namespace quota-mem-cpu-example

apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-demo
spec:
hard:
requests.cpu: "1"
requests.memory: 1Gi
limits.cpu: "2"
limits.memory: 2Gi

kubectl apply -f quota-mem-cpu.yaml --namespace=quota-mem-cpu-example


kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml
apiVersion: v1
kind: Pod
metadata: status:
name: quota-mem-cpu-demo hard:
limits.cpu: "2"
spec:
limits.memory: 2Gi
containers: requests.cpu: "1"
- name: quota-mem-cpu-demo-ctr requests.memory: 1Gi
image: nginx used:
resources: limits.cpu: 800m
limits: limits.memory: 800Mi
memory: "800Mi" requests.cpu: 400m
requests.memory: 600Mi
cpu: "800m"
requests:
memory: "600Mi"
cpu: "400m"

kubectl apply -f quota-mem-cpu-pod.yaml --namespace=quota-mem-cpu-example

kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml


Attempt to create a second Pod

apiVersion: v1
kind: Pod
metadata:
name: quota-mem-cpu-demo-2
spec:
containers:
- name: quota-mem-cpu-demo-2-ctr
image: redis
resources:
limits:
memory: "1Gi"
cpu: "800m"
requests:
memory: "700Mi"
cpu: "400m"

kubectl apply -f quota-mem-cpu-pod-2.yaml --namespace=quota-mem-cpu-example

Error from server (Forbidden): error when creating "quota-mem-cpu-pod-2.yaml":


pods "quota-mem-cpu-demo-2" is forbidden: exceeded quota: mem-cpu-demo,
requested: requests.memory=700Mi,used: requests.memory=600Mi, limited:
requests.memory=1Gi
Configure a Pod Quota for a Namespace
kubectl create namespace quota-pod-example

apiVersion: v1 status:
kind: ResourceQuota hard:
metadata: pods: "2"
name: pod-demo used:
pods: "0"
spec:
hard:
pods: "2"

kubectl apply -f quota-pod.yaml --namespace=quota-pod-example

kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml


Create a deployment

apiVersion: apps/v1 kubectl apply -f quota-pod-deployment.yaml


kind: Deployment --namespace=quota-pod-example
metadata:
name: pod-quota-demo
spec: kubectl get deployment pod-quota-demo
selector: --namespace=quota-pod-example --output=yaml
matchLabels:
purpose: quota-demo
replicas: 3
template: spec:
metadata: ...
labels: replicas: 3
purpose: quota-demo ...
spec: status:
containers: availableReplicas: 2
- name: pod-quota-demo ...
image: nginx lastUpdateTime: 2021-04-02T20:57:05Z
message: 'unable to create pods: pods
"pod-quota-demo-1650323038-" is forbidden:
exceeded quota: pod-demo, requested: pods=1,
used: pods=2, limited: pods=2'
Limit Range (Memory):
Provides constraints to limit resource consumption per
Containers or Pods in a namespace.

kubectl create namespace default-mem-example

apiVersion: v1
kind: LimitRange
metadata:
name: mem-limit-range
spec:
limits:
- default:
memory: 512Mi
defaultRequest:
memory: 256Mi
type: Container

kubectl apply -f memory-defaults.yaml --namespace=default-mem-example


Now if you create a Pod in the default-mem-example namespace, and any container
within that Pod does not specify its own values for memory request and memory limit,
then the control plane applies default values: a memory request of 256MiB and a
memory limit of 512MiB.

apiVersion: v1 containers:
- image: nginx
kind: Pod imagePullPolicy: Always
metadata: name: default-mem-demo-ctr
name: default-mem-demo resources:
limits:
spec: memory: 512Mi
containers: requests:
- name: default-mem-demo-ctr memory: 256Mi
image: nginx

kubectl apply -f memory-defaults-pod.yaml --namespace=default-mem-example

kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example


What if you specify a container's limit, but not its request?

apiVersion: v1
kind: Pod
metadata:
name: default-mem-demo-2 resources:
spec: limits:
containers: memory: 1Gi
requests:
- name: default-mem-demo-2-ctr
memory: 1Gi
image: nginx
resources:
limits:
memory: "1Gi"

kubectl apply -f memory-defaults-pod-2.yaml --namespace=default-mem-example

kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example


What if you specify a container's request, but not its limit?

apiVersion: v1
kind: Pod
metadata:
name: default-mem-demo-3
spec: resources:
limits:
containers:
memory: 512Mi
- name: default-mem-demo-3-ctr requests:
image: nginx memory: 128Mi
resources:
requests:
memory: "128Mi"

kubectl apply -f memory-defaults-pod-3.yaml --namespace=default-mem-example

kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example


Limit Range (CPU):

kubectl create namespace default-cpu-example

apiVersion: v1
kind: LimitRange
metadata:
name: cpu-limit-range
spec:
limits:
- default:
cpu: 1
defaultRequest:
cpu: 0.5
type: Container

kubectl apply -f cpu-defaults.yaml --namespace=default-cpu-example


Now if you create a Pod in the default-cpu-example namespace, and any container in
that Pod does not specify its own values for CPU request and CPU limit, then the control
plane applies default values: a CPU request of 0.5 and a default CPU limit of 1.

containers:
apiVersion: v1 - image: nginx
kind: Pod imagePullPolicy: Always
metadata: name: default-cpu-demo-ctr
name: default-cpu-demo resources:
spec: limits:
cpu: "1"
containers:
requests:
- name: default-cpu-demo-ctr cpu: 500m
image: nginx

kubectl apply -f cpu-defaults-pod.yaml --namespace=default-cpu-example

kubectl get pod default-cpu-demo --output=yaml --namespace=default-cpu-example


What if you specify a container's limit, but not its request?

apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-2
spec: resources:
containers: limits:
- name: default-cpu-demo-2-ctr cpu: "1"
requests:
image: nginx
cpu: "1"
resources:
limits:
cpu: "1"

kubectl apply -f cpu-defaults-pod-2.yaml --namespace=default-cpu-example

kubectl get pod default-cpu-demo-2 --output=yaml --namespace=default-cpu-example


What if you specify a container's request, but not its limit?

apiVersion: v1
kind: Pod
metadata:
name: default-cpu-demo-3
spec: resources:
limits:
containers:
cpu: "1"
- name: default-cpu-demo-3-ctr requests:
image: nginx cpu: 750m
resources:
requests:
cpu: "0.75"

kubectl apply -f cpu-defaults-pod-3.yaml --namespace=default-cpu-example

kubectl get pod default-cpu-demo-3 --output=yaml --namespace=default-cpu-example


Configure Minimum and Maximum Memory Constraints for a Namespace

kubectl create namespace constraints-mem-example


limits:
- default:
memory: 1Gi
apiVersion: v1 defaultRequest:
kind: LimitRange memory: 1Gi
metadata: max:
name: mem-min-max-demo-lr memory: 1Gi
spec: min:
limits: memory: 500Mi
- max: type: Container
memory: 1Gi
min:
memory: 500Mi
type: Container

kubectl apply -f memory-constraints.yaml --namespace=constraints-mem-example

kubectl get limitrange mem-min-max-demo-lr --namespace=constraints-mem-example --output=yaml


apiVersion: v1
kind: Pod
metadata:
name: constraints-mem-demo
spec:
containers:
resources:
- name: constraints-mem-demo-ctr limits:
image: nginx memory: 800Mi
resources: requests:
limits: memory: 600Mi
memory: "800Mi"
requests:
memory: "600Mi"

kubectl apply -f memory-constraints-pod.yaml --namespace=constraints-mem-example

kubectl get pod constraints-mem-demo --output=yaml --namespace=constraints-mem-example


Attempt to create a Pod that exceeds the maximum memory constraint

apiVersion: v1
kind: Pod
metadata:
name: constraints-mem-demo-2
spec:
containers:
- name:
constraints-mem-demo-2-ctr
image: nginx
resources:
limits:
memory: "1.5Gi"
requests:
memory: "800Mi”

kubectl apply -f memory-constraints-pod-2.yaml --namespace=constraints-mem-example

Error from server (Forbidden): error when creating "memory-constraints-pod-2.yaml":


pods "constraints-mem-demo-2" is forbidden: maximum memory usage per Container is 1Gi, but limit is
1536Mi.
Attempt to create a Pod that does not meet the minimum memory request

apiVersion: v1
kind: Pod
metadata:
name: constraints-mem-demo-3
spec:
containers:
- name:
constraints-mem-demo-3-ctr
image: nginx
resources:
limits:
memory: "800Mi"
requests:
memory: "100Mi"

kubectl apply -f memory-constraints-pod-3.yaml --namespace=constraints-mem-example

Error from server (Forbidden): error when creating "memory-constraints-pod-3.yaml":


pods "constraints-mem-demo-3" is forbidden: minimum memory usage per Container is 500Mi, but request
is 100Mi.
Create a Pod that does not specify any memory request or limit

apiVersion: v1
kind: Pod
metadata:
name: constraints-mem-demo-4 resources:
spec: limits:
containers: memory: 1Gi
- name: constraints-mem-demo-4-ctr requests:
image: nginx memory: 1Gi

kubectl apply -f memory-constraints-pod-4.yaml --namespace=constraints-mem-example

kubectl get pod constraints-mem-demo-4 --namespace=constraints-mem-example --output=yaml


Configure Minimum and Maximum CPU Constraints for a Namespace

kubectl create namespace constraints-cpu-example


limits:
- default:
cpu: 800m
apiVersion: v1
defaultRequest:
kind: LimitRange
cpu: 800m
metadata:
max:
name: cpu-min-max-demo-lr
cpu: 800m
spec:
min:
limits:
cpu: 200m
- max:
type: Container
cpu: "800m"
min:
cpu: "200m"
type: Container

kubectl apply -f cpu-constraints.yaml --namespace=constraints-cpu-example

kubectl get limitrange cpu-min-max-demo-lr --output=yaml --namespace=constraints-cpu-example


apiVersion: v1
kind: Pod
metadata:
name: constraints-cpu-demo
spec:
containers:
- name: constraints-cpu-demo-ctr
image: nginx resources:
resources: limits:
cpu: 800m
limits:
requests:
cpu: "800m" cpu: 500m
requests:
cpu: "500m"

kubectl apply -f cpu-constraints-pod.yaml --namespace=constraints-cpu-example

kubectl get pod constraints-cpu-demo --output=yaml --namespace=constraints-cpu-example


Attempt to create a Pod that exceeds the maximum CPU constraint

apiVersion: v1
kind: Pod
metadata:
name: constraints-cpu-demo-2
spec:
containers:
- name:
constraints-cpu-demo-2-ctr
image: nginx
resources:
limits:
cpu: "1.5"
requests:
cpu: "500m"

kubectl apply -f cpu-constraints-pod-2.yaml --namespace=constraints-cpu-example

Error from server (Forbidden): error when creating "cpu-constraints-pod-2.yaml":


pods "constraints-cpu-demo-2" is forbidden: maximum cpu usage per Container is 800m, but limit is
1500m.
Attempt to create a Pod that does not meet the minimum CPU request

apiVersion: v1
kind: Pod
metadata:
name: constraints-cpu-demo-3
spec:
containers:
- name:
constraints-cpu-demo-3-ctr
image: nginx
resources:
limits:
cpu: "800m"
requests:
cpu: "100m"

kubectl apply -f cpu-constraints-pod-3.yaml --namespace=constraints-cpu-example

Error from server (Forbidden): error when creating "cpu-constraints-pod-3.yaml":


pods "constraints-cpu-demo-3" is forbidden: minimum cpu usage per Container is 200m, but request is
100m.
Create a Pod that does not specify any CPU request or limit

apiVersion: v1
kind: Pod
metadata:
resources:
name: constraints-cpu-demo-4
limits:
spec: cpu: 800m
containers: requests:
- name: constraints-cpu-demo-4-ctr cpu: 800m
image: vish/stress

kubectl apply -f cpu-constraints-pod-4.yaml --namespace=constraints-cpu-example

kubectl get pod constraints-cpu-demo-4 --namespace=constraints-cpu-example --output=yaml


Manual Scheduling
Manual Scheduling
References:

● https://www.udemy.com/course/certified-kubernetes-administ
rator-with-practice-tests

● https://www.udemy.com/course/certified-kubernetes-applicati
on-developer

● https://kubernetes.io/docs

You might also like