kubernetes中的内存表示单位Mi和M的区别,一般定义 memory 的单位M,它认为 1M=1024K=1024x1024字节,但在k8s中的M表示的意义是不同的,今天特意看了一下官方文档,并实验了一把,特此记录。

官网解释(Meaning of memory)

Mi表示:1Mi=1024x1024
M表示:1M=1000x1000 (其它单位类推, 如Ki/K Gi/G)

验证

创建两个pod, 一个申请1Mi, 另一个申请1M, 通过log来查看他们的区别。

nginx1.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx1
    image: nginx:test
    ports:
    - containerPort: 80
    resources:
      limits:
        cpu: 200m
        memory: 128Mi
      requests:
        cpu: 0.1
        memory: 1Mi

nginx2.yaml:

apiVersion: v1
kind: Pod
metadata:
  name: nginx2
spec:
  containers:
  - name: nginx2
    image: nginx:test
    ports:
    - containerPort: 80
    resources:
      limits:
        cpu: 200m
        memory: 128Mi
      requests:
        cpu: 0.1
        memory: 1M

nginx1.yaml(Mi)申请资源的信息如下,可以看到Memory=1024*1024

I0716 11:05:43.555791   31331 factory.go:469] About to try and schedule pod nginx
I0716 11:05:43.555804   31331 scheduler.go:165] Attempting to schedule pod: default/nginx
I0716 11:05:43.555866   31331 predicates.go:565] Predicate: MilliCPU=100 Memory=1048576 NvidiaGPU=0 OpaqueIntResources=map[]

nginx2.yaml(M)申请资源的信息如下,Memory=1000*1000

I0716 11:05:58.404826   31331 factory.go:469] About to try and schedule pod nginx2
I0716 11:05:58.404840   31331 scheduler.go:165] Attempting to schedule pod: default/nginx2
I0716 11:05:58.404904   31331 predicates.go:565] Predicate: MilliCPU=100 Memory=1000000 NvidiaGPU=0 OpaqueIntResources=map[]

转载自换联网。

  • https://kubernetes.io/docs/concepts/configuration/manage-compute-resources-container/#meaning-of-memory
  • https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#memory-units

完毕。