14 KiB
Understood. I'll create comprehensive artifacts for each of the seven main topics you've outlined. This will provide you with a thorough reference for your interview preparation.
- Kubernetes (k8s) Fundamentals:
Kubernetes Fundamentals
Core Components
Pods
- Smallest deployable units in Kubernetes
- Can contain one or more containers
- Share network namespace and storage
- Ephemeral by nature, can be replaced at any time
Deployments
- Manage ReplicaSets
- Provide declarative updates for Pods
- Support rolling updates and rollbacks
- Ensure desired number of Pod replicas are running
Services
- Expose Pods as network services
- Types:
- ClusterIP: Internal cluster communication
- NodePort: Exposes the service on each Node's IP at a static port
- LoadBalancer: Exposes the service externally using a cloud provider's load balancer
- ExternalName: Maps the service to a DNS name
- Service discovery and load balancing
ConfigMaps and Secrets
- Store configuration data and sensitive information
- Can be mounted as volumes or environment variables
- ConfigMaps for non-sensitive configuration data
- Secrets for sensitive data (base64 encoded)
Namespace Management
- Logical partitions within a cluster
- Resource isolation and access control
- Quota management per namespace
- Default namespaces: default, kube-system, kube-public
Resource Management
- Requests: Minimum resources guaranteed
- Limits: Maximum resources allowed
- CPU (in cores or millicores) and memory (in bytes)
- Quality of Service (QoS) classes: Guaranteed, Burstable, BestEffort
Persistent Volumes and Claims
- PersistentVolumes (PV): Cluster-wide storage resources
- PersistentVolumeClaims (PVC): Storage requests by Pods
- StorageClasses: Dynamic provisioning of PVs
- Reclaim policies: Retain, Delete, Recycle
Advanced Concepts
- StatefulSets: For stateful applications
- DaemonSets: Ensure all (or some) nodes run a copy of a Pod
- Jobs and CronJobs: For batch and scheduled tasks
- Ingress: HTTP(S) routing to services
- Deployment Types in Kubernetes:
Kubernetes Deployment Types
Rolling Updates
- Default deployment strategy in Kubernetes
- Gradually replace old Pods with new ones
- Control with
maxSurgeandmaxUnavailableparameters - Pros: Zero downtime, controlled rollout
- Cons: Both versions coexist during update, may require compatibility
Implementation:
spec:
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
Blue/Green Deployments
- Two identical environments: blue (current) and green (new)
- Switch traffic at once by updating a Service
- Easy rollback by switching back to the old version
- Pros: Instant cutover, easy rollback
- Cons: Resource-intensive, requires double the resources
Implementation:
- Deploy new version with a different label
- Test the new deployment
- Update the Service selector to point to the new version
- Remove old deployment if new version is stable
Canary Deployments
- Release to a subset of users or servers
- Gradually increase traffic to the new version
- Ideal for real-world testing and risk mitigation
- Pros: Controlled exposure, real user testing
- Cons: Complexity in managing traffic split
Implementation:
- Deploy both versions
- Create a Service that targets both versions
- Control traffic split using Ingress or Service Mesh (e.g., Istio)
Example using Istio:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-service
spec:
hosts:
- my-service
http:
- route:
- destination:
host: my-service-v1
weight: 90
- destination:
host: my-service-v2
weight: 10
Choosing a Deployment Strategy
- Rolling Update: For most applications, especially stateless
- Blue/Green: When you need atomic updates or easy rollbacks
- Canary: For high-risk changes or when you need gradual user exposure
Best Practices:
- Use readiness probes to ensure only healthy Pods receive traffic
- Implement proper monitoring and logging for all deployment types
- Automate deployments using CI/CD pipelines
- Practice rollbacks to ensure they work as expected
- NATS in Kubernetes:
NATS in Kubernetes
NATS Architecture
- Distributed messaging system
- Publish-subscribe and request-reply messaging patterns
- Core NATS: At-most-once delivery, no persistence
- NATS Streaming (STAN): At-least-once delivery, persistence
- Key components:
- Subjects: Hierarchical naming for messages
- Queues: Load balancing for subscribers
- Clusters: Group of NATS servers
Deploying NATS on Kubernetes
- Core NATS Deployment:
- Use StatefulSet for ordered, unique network identifiers
- Headless Service for peer discovery
- ConfigMap for NATS server configuration
Example NATS StatefulSet:
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nats
spec:
serviceName: "nats"
replicas: 3
selector:
matchLabels:
app: nats
template:
metadata:
labels:
app: nats
spec:
containers:
- name: nats
image: nats:2.1.9-alpine3.12
ports:
- containerPort: 4222
name: client
- containerPort: 6222
name: cluster
- containerPort: 8222
name: monitor
command:
- "nats-server"
- "--config"
- "/etc/nats-config/nats.conf"
- NATS Streaming Deployment:
- Similar to core NATS, but with additional configuration
- Use persistent volumes for message storage
NATS Streaming (Persistence)
- Provides message persistence and guaranteed delivery
- Features:
- Durable subscriptions
- Message replay
- Last value semantics
- Configuration options:
- File-based storage
- SQL database storage (MySQL, PostgreSQL)
NATS Streaming configuration example:
streaming {
id: "nats-streaming-cluster"
store: "file"
dir: "/data/nats-streaming"
channels {
max_inactivity: "24h"
}
}
Best Practices for NATS in Kubernetes
- Use anti-affinity rules to spread NATS servers across nodes
- Implement proper monitoring (e.g., Prometheus metrics)
- Use NetworkPolicies to control access to NATS servers
- Regularly backup NATS Streaming data
- Implement proper authentication and authorization
Scaling NATS in Kubernetes
- Horizontal scaling: Increase the number of NATS server instances
- Vertical scaling: Increase resources for NATS server Pods
- Use NATS supercluster for multi-cluster setups
Troubleshooting NATS in Kubernetes
-
Check Pod logs:
kubectl logs <pod-name> -
Verify network connectivity between Pods
-
Use NATS CLI tools for debugging (nats-top, nats-bench)
-
Monitor NATS server metrics through the monitoring endpoint
-
Event-Driven Architecture (EDA) Concepts:
Event-Driven Architecture (EDA) Concepts
Event Producers and Consumers
Event Producers
- Components that generate events
- Examples: User actions, system state changes, IoT devices
- Responsibilities:
- Create events in a standardized format
- Publish events to appropriate channels or topics
Event Consumers
- Components that react to events
- Examples: Microservices, analytics systems, notification services
- Responsibilities:
- Subscribe to relevant event channels or topics
- Process events and perform actions
Publish-Subscribe Pattern
- Core communication pattern in EDA
- Characteristics:
- Decoupled communication between producers and consumers
- One-to-many distribution of events
- Asynchronous processing
Topics or Channels
- Categorize events for efficient routing
- Allow fine-grained subscription by consumers
- Hierarchical naming often used (e.g., "orders.created", "users.updated")
Benefits
- Scalability: Easy to add new producers or consumers
- Flexibility: Systems can evolve independently
- Resilience: Failure in one component doesn't affect others
Message Queues vs. Streaming
Message Queues
- Characteristics:
- Often used for task distribution
- Competing consumers model
- Messages typically removed after processing
- Use cases:
- Work queue processing
- Load balancing tasks across workers
Streaming
- Characteristics:
- Maintains an ordered log of events
- Allows multiple consumers to read the same events
- Supports event replay and time-based access
- Use cases:
- Event sourcing
- Audit trails
- Real-time analytics
Key EDA Patterns
- Event Notification: Simple event publishing without payload
- Event-Carried State Transfer: Events contain relevant state data
- Event Sourcing: Store state changes as a sequence of events
- CQRS (Command Query Responsibility Segregation): Separate read and write models
Challenges in EDA
- Eventual Consistency: Managing data consistency across services
- Event Schema Evolution: Handling changes in event structure over time
- Error Handling and Dead Letter Queues: Managing failed event processing
- Monitoring and Tracing: Tracking event flow across distributed systems
EDA in Kubernetes
- Use message brokers like Kafka, NATS, or RabbitMQ
- Leverage Kubernetes features:
- StatefulSets for stateful messaging systems
- Services for service discovery
- ConfigMaps and Secrets for configuration
Best Practices
-
Design clear and well-documented event schemas
-
Implement idempotent event consumers
-
Use correlation IDs for tracing events across services
-
Implement proper error handling and retry mechanisms
-
Monitor system health and performance metrics
-
Kafka Basics:
Kafka Basics
Core Concepts
Topics
- Categorize streams of records
- Similar to tables in a database
- Can have multiple partitions for parallelism
- Retention policies define how long data is kept
Partitions
- Ordered, immutable sequence of records
- Each record assigned a sequential offset
- Enable parallel processing and scalability
- Distributed across Kafka brokers
Offsets
- Unique identifier for each record within a partition
- Consumers track their position using offsets
- Allows consumers to replay data from a specific point
Producers and Consumers
Producers
- Publish records to topics
- Can choose the partition for each record
- Support for asynchronous and synchronous sending
- Configurable acknowledgment levels (acks)
Consumers
- Read records from topics
- Organized into consumer groups for scalability
- Each partition consumed by only one consumer in a group
- Offset management: automatic or manual commit
Kafka Streams
- Library for building streaming applications
- Stateful and stateless processing
- Exactly-once semantics
- Supports windowing operations
- Key features:
- KStream: represent streams
- KTable: represent changelog streams
- GlobalKTable: enrichment use cases
Kafka Connect
- Framework for connecting Kafka with external systems
- Source connectors: import data from external systems
- Sink connectors: export data to external systems
- Distributed mode for scalability
Key Features
- High throughput and low latency
- Scalability: can handle trillions of events a day
- Durability: persists messages on disk
- Fault-tolerance: replication across multiple brokers
- Exactly-once semantics (since version 0.11)
Use Cases
- Messaging system
- Activity tracking
- Gather metrics from various sources
- Application logs gathering
- Stream processing
- Event sourcing
- Commit log service
Kafka in Kubernetes
- Use Kafka Operator for easy deployment and management
- StatefulSets for Kafka brokers
- Headless Services for broker discovery
- Configure proper storage class for persistence
Best Practices
- Choose appropriate partition count and replication factor
- Implement proper monitoring and alerting
- Tune producer and consumer configurations for performance
- Implement proper security measures (authentication, authorization, encryption)
- Regular maintenance: log compaction, cluster balancing
Common Challenges
-
Topic management at scale
-
Handling large messages
-
Dealing with rebalancing in consumer groups
-
Ensuring exactly-once processing in stream applications
-
Disaster recovery planning
-
Troubleshooting in Kubernetes:
Troubleshooting in Kubernetes
Logging and Monitoring Tools
Logging
-
Kubectl logs
- Basic logging:
kubectl logs <pod-name> - Follow logs:
kubectl logs -f <pod-name> - Logs from previous instance:
kubectl logs <pod-name> --previous
- Basic logging:
-
Centralized Logging
- ELK Stack (Elasticsearch, Logstash, Kibana)
- Fluentd + Elasticsearch + Kibana
- Loki (part of Grafana stack)
Monitoring
-
Prometheus
- Open-source monitoring and alerting toolkit
- Pull-based metrics collection
- PromQL for querying metrics
-
Grafana
- Visualization platform for metrics
- Supports multiple data sources (including Prometheus)
- Customizable dashboards
-
Kubernetes Dashboard
- Web-based UI for Kubernetes clusters
- Overview of applications running on the cluster
- Create and modify Kubernetes resources
Debugging Pods and Services
Pod Debugging
-
Describe Pod
kubectl describe pod <pod-name>- Check events, status, and configuration
-
Exec into Container
kubectl exec -it <pod-name> -- /bin/sh- Investigate from within the container
-
Port Forwarding
kubectl port-forward <pod-name> 8080:80- Access pod's port locally for debugging
Service Debugging
-
Verify Service Configuration
kubectl get svc <service-name>kubectl describe svc <service-name>
-
Check Endpoints
kubectl get endpoints <service-name>- Ensure pods are correctly registered
-
DNS Troubleshooting
- Deploy a debug pod:
kubectl run -it --rm debug --image=busybox --restart=Never -- sh - Use `nsl
- Deploy a debug pod: