Kubernetes 大小定义: “ Gi”和“ G”的区别是什么?

当我研究 Kubernetes 模板的 yaml 定义时,我偶然发现了大小的不同定义。一开始我以为是关于幻觉的,但它们是一样的。那有什么区别呢?当两者都是一样的时候,哪个才是正确的?

storage: 5Gstorage: 5Gi

volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
resources:
requests:
storage: 2Gi

详见此处: Https://github.com/cvallance/mongo-k8s-sidecar/blob/master/example/statefulset/mongo-statefulset.yaml

还有这个:

 volumeClaimTemplates:
- metadata:
name: mongo-persistent-storage
spec:
resources:
requests:
storage: 5G

详情请看: https://github.com/openebs/openebs/blob/master/k8s/demo/mongodb/mongo-statefulset.yml

69810 次浏览

From Kubernetes source:

Limits and requests for memory are measured in bytes. You can express memory as a plain integer or as a fixed-point integer using one of these suffixes: E, P, T, G, M, K. You can also use the power-of-two equivalents: Ei, Pi, Ti, Gi, Mi, Ki. For example, the following represent roughly the same value:

128974848, 129e6, 129M, 123Mi

So those are the "bibyte" counterparts, like user2864740 commented.

A little info on those orders of magnitude:

The kibibyte was designed to replace the kilobyte in those computer science contexts in which the term kilobyte is used to mean 1024 bytes. The interpretation of kilobyte to denote 1024 bytes, conflicting with the SI definition of the prefix kilo (1000), used to be common.

So, as you can see, 5G means 5 Gigabytes while 5Gi means 5 Gibibytes. They amount to:

  • 5 G = 5000000 KB / 5000 MB
  • 5 Gi = 5368709.12 KB / 5368.70 MB

Therefore, in terms of size, they are not the same.

Exactly, one of them (G) is power of ten, while the other one (Gi) is power of two. So,

  • 10^3 is power of ten. the result is 1000, or 1G
  • 2^10 is power of two. the result is 1024, or 1Gi

These are different units - one of the are in Binary prefix and the other are in Decimal prefix.

Simply said, the units such as M, G, T are based on power of 10 - multiplies of 1000. The units such as Mi, Gi, Ti are based on power of 2 - multiplies of 1024.

A “kibibyte” is equal to 1024, or 2^10, bytes. Simple enough, but isn’t a “kilobyte” also 1024 bytes? Well, it is, sometimes. As defined by the International System of Units, the prefix “kilo” refers to 1000, or 10^3. Most storage manufacturers measure and label capacity in base 10 (1 kilobyte = 1000 bytes; 1 megabyte = 1000 kilobytes; 1 gigabyte = 1000 megabytes; 1 terabyte = 1000 gigabytes). RAM vendors and most operating systems, however, use base 2 (1 kilobyte = 1024 bytes; 1 megabyte = 1024 kilobytes; 1 gigabyte = 1024 megabytes; 1 terabyte = 1024 gigabytes). (A notable exception to this is macOS, which has used base 10 since OS X 10.6.) So in order to know what exactly is meant by “gigabyte”, you need to know the context in which the word is being used. If you are talking about raw hard drive capacity, a gigabyte is 1000000000 bytes. If you are talking about the file system on top of that hard drive, then gigabyte means 1073741824 bytes.

Link to the source.