Configuring Pod Specifications

1. Setting Container Images

FieldDescription
imageregistry/repo:tag@sha256:digest
imagePullPolicyAlways, IfNotPresent (default), Never
imagePullSecretsList of Secret names for private registries
Note: Pin by digest (@sha256:...) for immutable, reproducible deployments. Avoid :latest.

2. Defining Resource Requests

UnitMeaning
CPU1 = 1 core; 500m = 0.5 core; 100m = 0.1 core
MemoryKi, Mi, Gi (binary) or K, M, G (decimal)
requests.cpuGuaranteed CPU; influences scheduling
requests.memoryGuaranteed memory
resources:
  requests: { cpu: 250m, memory: 256Mi }

3. Defining Resource Limits

LimitBehavior on Exceed
limits.cpuThrottled (CFS quotas)
limits.memoryOOMKilled by kernel
limits.ephemeral-storagePod evicted
Warning: Without memory limit a runaway pod can starve the node. Always set limits in production.

4. Configuring Environment Variables

SourceExample
Literalenv: [{name: LOG_LEVEL, value: "info"}]
ConfigMap keyvalueFrom: { configMapKeyRef: {name, key} }
Secret keyvalueFrom: { secretKeyRef: {name, key} }
Downward APIvalueFrom: { fieldRef: {fieldPath: status.podIP} }
envFromImport all keys from ConfigMap/Secret

Example: Combining env sources

env:
- name: NODE_NAME
  valueFrom: { fieldRef: { fieldPath: spec.nodeName } }
- name: DB_PASS
  valueFrom: { secretKeyRef: { name: db, key: password } }
envFrom:
- configMapRef: { name: app-config }
- secretRef:    { name: app-secrets }

5. Setting Container Commands

FieldMaps to Docker
commandENTRYPOINT (overrides)
argsCMD (overrides)
command: ["sh", "-c"]
args: ["echo hello && sleep 3600"]

6. Configuring Working Directory

FieldEffect
workingDirOverride image WORKDIR
workingDir: /app

7. Setting Security Context

FieldScope
runAsUser / runAsGroupUID/GID
runAsNonRootRefuse to start as UID 0
readOnlyRootFilesystemContainer only
allowPrivilegeEscalationContainer only
capabilitiesadd/drop Linux capabilities
fsGroupVolume ownership GID (Pod)
seccompProfileRuntimeDefault, Localhost, Unconfined

Example: Hardened security context

securityContext:
  runAsNonRoot: true
  runAsUser: 10001
  fsGroup: 10001
  seccompProfile: { type: RuntimeDefault }
containers:
- name: app
  securityContext:
    readOnlyRootFilesystem: true
    allowPrivilegeEscalation: false
    capabilities: { drop: ["ALL"] }

8. Configuring Pod Priority

ObjectField
PriorityClassvalue: 1000000, globalDefault, preemptionPolicy
Podspec.priorityClassName: high
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata: { name: high }
value: 1000000
preemptionPolicy: PreemptLowerPriority

9. Using Pod Preemption Policy

preemptionPolicyEffect
PreemptLowerPriorityDefault; evicts lower-priority pods to fit
NeverPod waits in Pending instead of preempting

10. Setting Pod Restart Policy

restartPolicyUse
AlwaysDefault for Deployments; restart on any exit
OnFailureRestart only on non-zero exit (Jobs)
NeverNever restart (Jobs, one-shot)

11. Configuring Pod DNS Policy

dnsPolicyBehavior
ClusterFirstDefault; uses CoreDNS for cluster domains
ClusterFirstWithHostNetFor hostNetwork pods
DefaultInherit node resolver
NoneUse dnsConfig only
dnsPolicy: None
dnsConfig:
  nameservers: ["1.1.1.1"]
  searches: ["svc.cluster.local"]
  options: [{ name: ndots, value: "2" }]

12. Using Service Account

FieldEffect
serviceAccountNameSA whose token is mounted
automountServiceAccountTokenSet false to skip token mount
spec:
  serviceAccountName: app-sa
  automountServiceAccountToken: false