I would like to put number of CPUs into an env, something like this:
spec:
containers:
- name: test-container
env:
- name: NUMBER_OF_CPU
valueFrom:
fieldRef:
fieldPath: spec.node.CPU
with kubectl describe nodes
there is an info about the number of CPU of a node:
Addresses:
InternalIP: 127.0.0.1
Hostname: host1
Capacity:
cpu: 8
ephemeral-storage: 242791844Ki
hugepages-1Gi: 0
hugepages-2Mi: 0
memory: 16271048Ki
pods: 110
So how could I pass this information to a pod? Thanks,
in the fieldRef
only annotations, labels, name and namespace are supported. and only from POD object info, this is called Downward API .
From my tests, this command should work for you:
grep -c ^processor /proc/cpuinfo
It will count the number of lines starting with "processor" in /proc/cpuinfo
which equals to the amount shown in kubeclt describe node
.
Depending on your use case, you can pass it as ENV or let your app run the command:
❯ k describe node gke-cluster-1
Capacity:
cpu: 4
ephemeral-storage: 98868448Ki
memory: 15397584Ki
Yaml:
spec:
containers:
- image: ubuntu
name: shell-demo-cpu
env:
- name: NUMBER_OF_CPU
value: "grep -c ^processor /proc/cpuinfo"
Then call the variable and run it:
kubectl exec -it shell-demo-cpu -- /bin/bash
root@gke-cluster-1:/# env
NUMBER_OF_CPU=grep -c ^processor /proc/cpuinfo
root@gke-cluster-1:/# $NUMBER_OF_CPU
4
Let me know if it that helps.
You need to pass args
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"
The args section of the configuration file provides arguments for the container when it starts. The -cpus "2" argument tells the Container to attempt to use 2 CPUs.