Top Kubernetes Objects with examples


Kubernetes objects represent the desired state and configuration of your workloads and resources in a cluster. Here are the top objects, common uses, brief examples, comparisons, and frequently asked questions for each, to help you prepare for principle engineer interview rounds.

1. Pod

Description: A Pod is the smallest deployable unit in Kubernetes, typically running one or more containers as a tightly coupled group.

Common Uses:

  • Running single-container or closely related multi-container applications.

  • Foundation for higher-level controllers (e.g., Deployments, StatefulSets).

Example YAML:

apiVersion: v1 kind: Pod metadata: name: simple-pod spec: containers: - name: nginx image: nginx:latest

Comparison:

  • Pod vs. Deployment: Pods are ephemeral; once lost, they aren't recreated unless managed by a controller like a Deployment

  • Pod vs. Container: A Pod can have one or more containers, whereas a container is just an application package

Top FAQ:

  • Q: How are Pods recreated if a node fails?

    • A: Pods aren't recreated automatically; controllers like Deployments handle this.

  • Q: Can a Pod run across multiple nodes?

    • A: No, all containers in a Pod run on the same node

2. Deployment

Description: Manages stateless application Pods, handles scaling, self-healing, and rolling updates

Common Uses:

  • Running and updating web servers, APIs, and stateless apps.

  • Enabling zero-downtime deployments via rolling updates.

Example YAML:

apiVersion: apps/v1 kind: Deployment metadata: name: my-web spec: replicas: 3 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx image: nginx:1.16

Comparison:

DeploymentStatefulSet
Use CaseStateless appsStateful apps (databases)
ReplicasInterchangeable (no identity)Each has unique identity
Pod NamesRandomStable, ordinal

Top FAQ:

  • Q: What's the main benefit of Deployments over Pods?

    • A: Deployments provide self-healing, scaling, and declarative updates.

  • Q: How to rollback a Deployment?

    • A: Use kubectl rollout undo deployment/<name> to revert to the previous version.

3. Service

Description: Exposes running Pods as a stable network endpoint and handles load balancing

Common Uses:

  • Internal/external app discovery.

  • Load balancing across replicated Pods.

Example YAML:

apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: web ports: - protocol: TCP port: 80 targetPort: 8080

Comparison:

ClusterIPNodePortLoadBalancer
ScopeInternal onlyExposes on nodeExternal IP
UsageApp-to-appDev/test accessCloud/Public apps

Top FAQ:

  • Q: How does a Service maintain connectivity if Pods restart?

    • A: The Service routes traffic to replacement Pods matching its selector automatically.

  • Q: Can I expose a Service outside the cluster?

    • A: Yes, use NodePort or LoadBalancer types.

4. ConfigMap & Secret

Description: Provide configuration and sensitive data (like passwords) to your workloads

Common Uses:

  • Injecting environment variables, configuration files, or credentials into Pods.

Example YAML (ConfigMap):

apiVersion: v1 kind: ConfigMap metadata: name: my-config data: app.properties: | KEY=value

Comparison:

ConfigMapSecret
Data typePlain configSensitive data
EncodingBase64 not requiredMust use Base64

Top FAQ:

  • Q: How do you mount a Secret in a Pod?

    • A: Reference it directly in the spec under envFrom or as a mounted volume.

5. StatefulSet

Description: Manages stateful applications, provides stable network identities and persistent storage for each Pod.

Common Uses:

  • Databases, distributed systems needing stable disk or identity.

Example YAML:

apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: serviceName: "mysql" replicas: 3 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.6

Comparison:

  • StatefulSet vs. Deployment: StatefulSets maintain unique, stable identities for each Pod; Deployments do not

Top FAQ:

  • Q: When to choose StatefulSet over Deployment?

    • A: For workloads needing stable storage or identity, like databases.

6. DaemonSet

Description: Ensures a specific Pod runs on every (or selected) node, typically for cluster-wide services (monitoring, logging agents).

Example YAML:

apiVersion: apps/v1 kind: DaemonSet metadata: name: node-exporter spec: selector: matchLabels: name: node-exporter template: metadata: labels: name: node-exporter spec: containers: - name: exporter image: prom/node-exporter

Comparison:

  • DaemonSet vs. Deployment: DaemonSet schedules Pods on all nodes; Deployment schedules on any available node

Top FAQ:

  • Q: Can a DaemonSet Pod be evicted from a node?

    • A: Yes, but the DaemonSet will reschedule it as needed.

7. Ingress

Description: Manages external HTTP/S access to services in a cluster, typically via path or host-based routing.

Common Uses:

  • Providing a single entry point for web apps.

  • SSL termination, routing policies.

Example YAML:

apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress spec: rules: - host: myapp.domain.com http: paths: - path: / backend: service: name: my-service port: number: 80

Comparison:

  • Ingress vs. Service: Ingress offers L7 (HTTP/S) routing, much more flexible than Service load balancing1.

Top FAQ:

  • Q: Do I need an Ingress Controller to use Ingress objects?

    • A: Yes, Kubernetes Ingress needs a controller for enforcement.

8. Job & CronJob

Description:

  • Job: Runs tasks to completion (e.g., batch data processing).

  • CronJob: Runs Jobs on a scheduled/recurring basis.

Example YAML (CronJob):

apiVersion: batch/v1 kind: CronJob metadata: name: hello spec: schedule: "*/5 * * * *" jobTemplate: spec: template: spec: containers: - name: hello image: busybox args: - /bin/sh - -c - date; echo Hello from Kubernetes! restartPolicy: OnFailure

Comparison:

JobCronJob
RunsOnce, till completionAt intervals
PurposeAd-hoc batch tasksScheduled tasks

Top FAQ:

  • Q: Can a Job be restarted if it fails?

    • A: Yes, depending on its restartPolicy and backoff settings.

Summary Table: Top Kubernetes Objects

ObjectMain UseExample AppSpecial Feature
PodSingle/multi-container workloadSingle Nginx instanceSmallest deployable unit
DeploymentStateless apps, rollout/rollbackScalable web serverManages ReplicaSets, updates
StatefulSetStateful apps needing identity/storageMySQL, KafkaStable identity/persistent storage
DaemonSetCluster-wide services on all/some nodesnode-exporter, log shipperRuns one Pod per node
ServiceLoad balancing, service discoveryExposing web appsStable, virtual IP
IngressExternal HTTP/S routingCentral web entry pointPath, host, TLS routing
JobBatch, one-off tasksData transformationRuns to completion
CronJobScheduled repetitive tasksDaily backup jobTime-based, recurring jobs
ConfigMapConfiguration injectionApp config filesInjects data as ENV/volumes
SecretSecurely store credentialsAPI keys, passwordsBase64, access controls

Further FAQs (General)

  • Q: How are Kubernetes objects managed?

    • A: Via the Kubernetes API, often using kubectl or declarative YAML files. The system ensures the actual state matches your desired state

  • Q: What happens if you manually delete a Pod managed by a Deployment?

    • A: The Deployment automatically recreates the missing Pod.

  • Q: How do you secure sensitive app data in Kubernetes?

    • A: Store in a Secret, and mount or inject it into your Pods.

  • Q: What is the difference between an object and a resource?

    • A: An object is an instance of a resource type; resources define kinds (like Pod, Service)

Comments

Popular posts from this blog

DevOps Learning Roadmap Beginner to Advanced

What is the Difference Between K3s and K3d

Lightweight Kubernetes Options for local development on an Ubuntu machine