Kubernetes 容器无法正确设置请求资源(CPU & RAM)
Kubernetes container won't set Request resources (CPU & RAM) properly
我正在尝试为我的 kubernetes container 设置一个 固定 资源值。当我尝试以下操作时:
//cont = v1.Container
log.Println("Before", cont.Resources.Requests.Memory())
cont.Resources.Requests.Memory().SetMilli(512) //new wanted value
log.Println("After", cont.Resources.Requests.Memory())
log.Println("Before", cont.Resources.Requests.Cpu())
cont.Resources.Requests.Cpu().SetScaled(2, resource.Giga) //new wanted value
log.Println("After", cont.Resources.Requests.Cpu())
我明白了:
Before 0
After 0
Before 0
After 0
为什么 Set
不将我的值更新为较新的值?如何正确设置 CPU 和 RAM 资源?
由于 Dim's answer, I managed to find a solution. It seems that when resources 未设置,无法在 Memory() 返回的数据结构中设置新值。正确的做法是:
cont.Resources.Requests = make(map[v1core.ResourceName]resource.Quantity)
cont.Resources.Requests[v1core.ResourceMemory] = *resource.NewQuantity(int64(512), resource.BinarySI)
cont.Resources.Requests[v1core.ResourceCPU] = *resource.NewQuantity(int64(4), resource.BinarySI)
干杯!
我正在尝试为我的 kubernetes container 设置一个 固定 资源值。当我尝试以下操作时:
//cont = v1.Container
log.Println("Before", cont.Resources.Requests.Memory())
cont.Resources.Requests.Memory().SetMilli(512) //new wanted value
log.Println("After", cont.Resources.Requests.Memory())
log.Println("Before", cont.Resources.Requests.Cpu())
cont.Resources.Requests.Cpu().SetScaled(2, resource.Giga) //new wanted value
log.Println("After", cont.Resources.Requests.Cpu())
我明白了:
Before 0
After 0
Before 0
After 0
为什么 Set
不将我的值更新为较新的值?如何正确设置 CPU 和 RAM 资源?
由于 Dim's answer, I managed to find a solution. It seems that when resources 未设置,无法在 Memory() 返回的数据结构中设置新值。正确的做法是:
cont.Resources.Requests = make(map[v1core.ResourceName]resource.Quantity)
cont.Resources.Requests[v1core.ResourceMemory] = *resource.NewQuantity(int64(512), resource.BinarySI)
cont.Resources.Requests[v1core.ResourceCPU] = *resource.NewQuantity(int64(4), resource.BinarySI)
干杯!