Explanation:
Below are the exact steps/commands you can run.
1) Locate the Deployment and its namespace
kubectl get deploy -A | grep nginx-resources
You should see output like:
<namespace> nginx-resources ...
Set a variable (replace <NS> with what you see):
NS=<NS>
Confirm replicas:
kubectl -n $NS get deploy nginx-resources -o jsonpath='{.spec.replicas}{'\n'}'
2) Check if there is a ResourceQuota in that namespace
kubectl -n $NS get resourcequota
kubectl -n $NS describe resourcequota
If there is a quota, note these fields (common ones):
requests.cpu
requests.memory
limits.cpu
limits.memory
3) Decide requests/limits for the Deployment (example values)
If the question (in your environment) provides specific values, use those.
If it doesn't, a typical safe pair is:
requests: cpu: 100m, memory: 128Mi
limits: cpu: 200m, memory: 256Mi
I'll proceed with these example values. If your lab specifies different numbers, just swap them in.
4) Update the existing Deployment (DO NOT DELETE)
Option A (fastest): kubectl set resources
Assuming the container name is the first container (we'll detect it):
kubectl -n $NS get deploy nginx-resources -o jsonpath='{.spec.template.spec.containers[*].name}{'\n'}'
If it prints a single container name, set it like this:
kubectl -n $NS set resources deploy nginx-resources \
--requests=cpu=100m,memory=128Mi \
--limits=cpu=200m,memory=256Mi
Verify the Deployment now has resources
kubectl -n $NS get deploy nginx-resources -o jsonpath='{.spec.template.spec.containers[0].resources}{'\n'}'
5) Compute the total resources requested by the Deployment
Get replicas:
REPLICAS=$(kubectl -n $NS get deploy nginx-resources -o jsonpath='{.spec.replicas}')
echo $REPLICAS
6) Ensure the namespace quota matches (or exceeds) those totals
This is the part the question warns about: if the quota is too low, the Deployment update will fail to roll out.
6.1 If a ResourceQuota already exists
Patch it to allow at least the totals you calculated.
First, identify quota name:
RQ=$(kubectl -n $NS get resourcequota -o jsonpath='{.items[0].metadata.name}')
echo $RQ
Then patch (example: replicas=2 requests.cpu=200m, requests.memory=256Mi):
kubectl -n $NS patch resourcequota $RQ --type='merge' -p '{
'spec': {
'hard': {
'requests.cpu': '200m',
'requests.memory': '256Mi',
'limits.cpu': '400m',
'limits.memory': '512Mi'
}
}
}'
Adjust those numbers to your replicas request, and replicas limit (if your quota also enforces limits).
6.2 If there is NO ResourceQuota
Create one that matches the Deployment max request totals.
Example for replicas=2 with our sample requests/limits:
cat <<EOF | kubectl apply -n $NS -f -
apiVersion: v1
kind: ResourceQuota
metadata:
name: nginx-resources-quota
spec:
hard:
requests.cpu: '200m'
requests.memory: '256Mi'
limits.cpu: '400m'
limits.memory: '512Mi'
EOF
7) Verify rollout succeeds
kubectl -n $NS rollout status deploy nginx-resources
kubectl -n $NS get pods
Verify the running pods actually have the requests/limits:
kubectl -n $NS get pod -l app=nginx-resources -o jsonpath='{range .items[*]}{.metadata.name}{' '}{.spec.containers[0].resources}{'\n'}{end}'
(If the label selector app=nginx-resources doesn't exist, just pick a pod name from kubectl get pods and run:)
kubectl -n $NS describe pod | sed -n '/Limits:/,/Requests:/p'
Common reasons this fails (and the fix)
Rollout stuck / pods pending with ''exceeded quota''
Check:
kubectl -n $NS describe pod
kubectl -n $NS describe resourcequota
Fix: increase ResourceQuota hard values to match required totals.
You set requests higher than quota allows
Fix: either reduce requests or raise quota.
kubectl get deploy -A | grep nginx-resources
kubectl -n <NS> get deploy nginx-resources -o jsonpath='{.spec.replicas}{'\n'}{.spec.template.spec.containers[0].name}{'\n'}'
kubectl -n <NS> describe resourcequota