Create a ConfigMap or generic Secret from command-line key-value pairs.
→Use `kubectl create configmap <name> --from-literal=<key>=<value>` or `kubectl create secret generic <name> --from-literal=<key>=<value>`.
Why: `--from-literal` is for direct key-value input. Use the flag multiple times for multiple keys. This is faster than creating a YAML file for simple cases.
Reference↗
Inject all key-value pairs from a ConfigMap or Secret as environment variables into a container.
→In the container spec, use `envFrom` with `configMapRef` or `secretRef`. Example: `envFrom: [{configMapRef: {name: my-config}}]`.
Why: `envFrom` is a bulk operation that maps all keys from the source to environment variables. This avoids manually listing each key.
Reference↗
Inject a single, specific value from a ConfigMap or Secret as an environment variable.
→Use `env.valueFrom`. Example: `env: [{name: LOG_LEVEL, valueFrom: {configMapKeyRef: {name: my-config, key: log_level}}}]`.
Why: `valueFrom` provides selective injection and allows mapping the source key to a different environment variable name.
Mount a ConfigMap or Secret as files into a Pod, allowing for live updates.
→Define a `volume` of type `configMap` or `secret`. Mount it into the container using `volumeMounts`. The files will be named after the keys.
Why: Mounted files from ConfigMaps/Secrets are updated automatically when the source changes. Environment variables are not, requiring a Pod restart.
Reference↗
Enforce security best practices: prevent running as root, make root filesystem read-only, or specify a user ID.
→Use `securityContext` at the Pod or container level. Set `runAsNonRoot: true`, `readOnlyRootFilesystem: true`, and/or `runAsUser: <UID>`.
Why: SecurityContext provides fine-grained, declarative control over container privileges, essential for hardening applications and meeting security policies.
Reference↗
Grant a Pod minimal permissions to access the Kubernetes API.
→1. Create a custom `ServiceAccount`. 2. Create a `Role` with only the necessary API permissions (e.g., list pods). 3. Create a `RoleBinding` to link the ServiceAccount and Role. 4. Assign the ServiceAccount to the Pod via `spec.serviceAccountName`.
Why: Follows the principle of least privilege, minimizing the attack surface if a Pod is compromised.
Prevent the automatic mounting of a ServiceAccount token into a Pod that does not need API access.
→Set `automountServiceAccountToken: false` in the Pod spec or on the ServiceAccount itself.
Why: Reduces the attack surface by not providing API credentials to containers that do not require them.
Create a Secret for use in TLS termination for an Ingress or other secure service.
→Use `kubectl create secret tls <secret-name> --cert=<path/to/cert.pem> --key=<path/to/key.pem>`.
Why: This creates a Secret of the correct type `kubernetes.io/tls` with the standard `tls.crt` and `tls.key` data keys expected by Ingress controllers.
Expose Pod metadata (like name, namespace, labels, or node IP) to a container.
→Use the Downward API to project metadata as environment variables or files in a `downwardAPI` volume. Example: `valueFrom: {fieldRef: {fieldPath: metadata.name}}`.
Why: Allows containers to be self-aware without needing to query the Kubernetes API, simplifying configuration and reducing RBAC requirements.
Reference↗
Set default CPU/memory requests and limits for all Pods in a namespace.
→Create a `LimitRange` object in the namespace. Define `default` and `defaultRequest` values for resources.
Why: Ensures all Pods have resource constraints, improving scheduling and stability, even if developers forget to specify them. Works in concert with ResourceQuota.
Limit the total amount of resources (CPU, memory, object count) that can be consumed in a namespace.
→Create a `ResourceQuota` object. Define hard limits in `spec.hard`, e.g., `requests.cpu: "4"`, `pods: "10"`.
Why: Prevents one namespace or team from consuming all cluster resources, ensuring fair resource allocation.