Configure container resources and non-root lifecycle
Configure container resources and non-root lifecycle
Containers package a binary with its runtime environment. Run with an unprivileged user, minimal permissions and explicit CPU and memory budgets. Kubernetes requests influence placement and limits constrain usage. CPU limits can throttle execution; memory pressure can cause termination. The Go heap budget is not the whole process memory budget. Tune GOMEMLIMIT with headroom and measure under the actual limits.
package main
import ( "fmt")
func heapBudget(limit, reserve int64) (int64, bool) { if limit <= 0 || reserve < 0 || reserve >= limit { return 0, false } return limit - reserve, true}func main() { fmt.Println(heapBudget(256, 64)) fmt.Println(heapBudget(64, 64))}The numbers are MiB, and the result is only an illustrative allocation budget, not an actual runtime setting. GOMEMLIMIT is a soft limit on runtime-managed memory, not a hard container cap. Include stacks, native allocations, caches and non-Go memory in measurements. Do not apply a universal percentage to all services.
Expected output:
192 true0 falseBound graceful shutdown
package main
import ( "context" "fmt" "net/http" "net/http/httptest" "time")
func main() { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, "ok") })) defer s.Close() ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() fmt.Println(s.Config.Shutdown(ctx) == nil)}This local idle-server example illustrates Shutdown with a bounded context; it does not simulate Kubernetes signals. Production code should use signal.NotifyContext for SIGTERM, mark readiness false, stop admission and wait for active work within a deadline shorter than the termination grace period. Handle shutdown errors and forced termination. Use exec-form entrypoints and ensure required writable paths are owned by the non-root UID.
Expected output:
trueRead a container configuration
This Kubernetes container fragment is illustrative. Substitute an authenticated digest and use an image with UID 10001 permissions and suitable writable mounts if needed. These are sample resource values, not recommended production sizing. A full workload and cluster policies are required; do not apply this fragment directly.
name: inventoryimage: REPLACE_WITH_VERIFIED_IMAGE_DIGESTports: - containerPort: 8080resources: requests: cpu: 100m memory: 128Mi limits: cpu: 500m memory: 256MisecurityContext: runAsNonRoot: true runAsUser: 10001 allowPrivilegeEscalation: false readOnlyRootFilesystem: true capabilities: drop: ["ALL"] seccompProfile: type: RuntimeDefaultTry it locally
Compute an illustrative memory budget for 512 MiB with 128 MiB reserved. Reject negative reserve or reserve equal to limit. Print 384 true and 0 false for the equal-limit case.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service protocols and cloud 05: Configure container resources and non-root lifecycle
Check the lesson objective before moving on.
Is GOMEMLIMIT a hard process memory cap?
Why bound graceful shutdown?