Q: What is Kubernetes and why is it used?
Kubernetes (K8s) is an open-source container orchestration platform originally developed by Google. It automates the deployment, scaling, scheduling, and management of containerized applications across a cluster of machines.
**Why it is used:**
- **Automated scaling:** Kubernetes can automatically scale applications up or down based on CPU utilization, memory, or custom metrics using Horizontal Pod Autoscaler (HPA).
- **Self-healing:** If a container crashes, Kubernetes automatically restarts it. If a node fails, workloads are rescheduled onto healthy nodes.
- **Service discovery and load balancing:** Kubernetes provides built-in DNS for service discovery and load balances traffic across pod replicas.
- **Rolling deployments and rollbacks:** Kubernetes enables zero-downtime deployments and can roll back to a previous version if a deployment fails.
- **Declarative configuration:** You describe the desired state (via YAML manifests), and Kubernetes continuously works to achieve and maintain that state.
In production environments, Kubernetes is the de facto standard for container orchestration at scale.
Q: What is a Pod in Kubernetes?
A Pod is the smallest deployable unit in Kubernetes. A Pod represents one or more containers that share the same network namespace, storage volumes, and lifecycle.
**Key characteristics:**
- Containers within a Pod share the same IP address and port space — they communicate via localhost.
- Pods are ephemeral — when a Pod dies, it is not restarted in-place; a new Pod with a new IP is created.
- Pods can have multiple containers (sidecar pattern), but usually contain one main container.
- Init containers run and complete before the main containers start, used for setup tasks.
**Pod lifecycle states:** Pending → Running → Succeeded / Failed / Unknown.
In practice, you almost never create Pods directly — you create Deployments, StatefulSets, or DaemonSets that manage Pods for you.
Q: What is the difference between a Deployment and a StatefulSet?
**Deployment:**
- Manages stateless applications.
- Pods are interchangeable — they have random names (e.g., `nginx-7d4b9c-xyz`).
- Pods can be restarted, replaced, or scaled without concern for identity.
- Ideal for web servers, API services, and any application that does not need persistent storage tied to a specific instance.
**StatefulSet:**
- Manages stateful applications (databases, distributed systems like Kafka, Zookeeper, Elasticsearch).
- Pods have stable, unique identities with predictable names (e.g., `postgres-0`, `postgres-1`).
- Pods are deployed and terminated in order (0, 1, 2...) by default.
- Each Pod gets its own PersistentVolumeClaim (PVC), which persists across Pod restarts.
- Uses a headless Service for stable DNS names (`postgres-0.postgres.namespace.svc.cluster.local`).
**When to use each:** Use Deployments for web applications, microservices, and APIs. Use StatefulSets for databases (PostgreSQL, MySQL), message brokers (Kafka, RabbitMQ), and any system where Pod identity and persistent storage matter.
Q: What is a Kubernetes Service and what types exist?
A Kubernetes Service provides a stable network endpoint (IP address and DNS name) for accessing a set of Pods. Since Pods are ephemeral and their IPs change, Services provide consistent addressing through label selectors.
**Service Types:**
1. **ClusterIP (default):** Exposes the service on a cluster-internal IP. Only accessible within the cluster. Used for internal microservice communication.
2. **NodePort:** Exposes the service on a static port on every node's IP (range: 30000–32767). Accessible from outside the cluster via `NodeIP:NodePort`. Useful for development but not recommended for production.
3. **LoadBalancer:** Creates an external cloud load balancer (AWS ELB, GCP LB) that routes traffic to the service. The standard approach for exposing services externally in cloud environments.
4. **ExternalName:** Maps a service to a DNS name (e.g., an external database). Returns a CNAME record. Useful for integrating external services into the cluster DNS namespace.
**Headless Service:** A ClusterIP service with `clusterIP: None`. Instead of a virtual IP, DNS returns the IPs of individual Pods directly. Required for StatefulSets.
Q: What is a ConfigMap and a Secret in Kubernetes?
Both are Kubernetes objects for injecting configuration data into Pods, but they serve different purposes.
**ConfigMap:**
- Stores non-sensitive configuration data as key-value pairs.
- Examples: database host, application log level, feature flags, configuration files.
- Stored in plain text in etcd.
- Can be mounted as environment variables or as files in a volume.
**Secret:**
- Stores sensitive data: passwords, API keys, TLS certificates, tokens.
- Stored base64-encoded in etcd (not truly encrypted by default — encryption at rest requires additional configuration with KMS providers).
- Should be accessed via environment variables or volume mounts, never hardcoded in container images.
- Can be of type `Opaque`, `kubernetes.io/tls`, `kubernetes.io/dockerconfigjson`, etc.
**Best practice:** Use Secrets for sensitive data, ConfigMaps for non-sensitive configuration. For production systems, integrate with external secret managers (HashiCorp Vault, AWS Secrets Manager) using solutions like External Secrets Operator.