None of our current deployments have any resource limits set. We have very little traffic, so it's not currently an issue, but in a production environment, we would want to set resource limits to ensure that our pods don't consume too many resources.
We wouldn't want a pod to hog all the CPU and RAM on its node, suffocating all of the other pods on the node.
We can set resource limits in our deployment files. Here's an example:
spec:
containers:
- name: <container-name>
image: <image-name>
resources:
limits:
memory: <max-memory>
cpu: <max-cpu>
Memory is measured in bytes, so we can use the suffixes Ki, Mi, and Gi to specify kibibytes, mebibytes, and gibibytes, respectively. For example, 512Mi is 512 mebibytes.
CPU is measured in cores, so we can use the suffix m to specify milli-cores. For example, 500m is 500 milli-cores, or 0.5 cores.
It would be really hard to test resource limits with our SynergyChat web application because we have no production traffic. Instead, I've created a couple of custom applications we can use to test and debug resource limits.
The bootdotdev/synergychat-testcpu:latest image on Docker Hub is an application that simply consumes as much CPU power as it can.
Create a new file called testcpu-deployment.yaml with the following:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: synergychat-testcpu
name: synergychat-testcpu
spec:
replicas: 1
selector:
matchLabels:
app: synergychat-testcpu
template:
metadata:
labels:
app: synergychat-testcpu
spec:
containers:
- image: bootdotdev/synergychat-testcpu:latest
name: synergychat-testcpu
Add a CPU limit of 50m to the deployment.
Apply the deployment, then make sure the pod is running:
kubectl get pod
It might take a minute or so, but soon you should be able to see its metrics with top:
kubectl top pod
Assuming everything is working properly, you should see that the pod is using about 50 milli-cores of CPU. That's because k8s is throttling the pod to ensure that it doesn't use more than 50 milli-cores.
Run:
kubectl proxy
Run and submit the CLI tests.