DEV Community

Cover image for Master Kubernetes: Top 20 DevOps Interview Q&A with Examples
Avesh
Avesh

Posted on • Edited on

Master Kubernetes: Top 20 DevOps Interview Q&A with Examples

Basic Questions

1. What is Kubernetes, and why is it used?

Answer:

Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It provides features such as fault tolerance, load balancing, and self-healing.

Example Use Case:

Imagine an e-commerce website hosted in containers. With Kubernetes, you can automatically scale the number of containers during high traffic (e.g., Black Friday) and ensure containers restart if they fail.


2. What are the key components of Kubernetes architecture?

Answer:

Kubernetes consists of the following core components:

  • Master Node:
    • API Server: Central management point for all REST commands.
    • Scheduler: Allocates pods to nodes based on resource requirements.
    • Controller Manager: Ensures the desired state of the cluster is maintained.
    • ETCD: A key-value store for cluster configuration and state.
  • Worker Node:
    • Kubelet: Communicates with the master node and ensures pods are running.
    • Kube Proxy: Handles networking and traffic forwarding.
    • Container Runtime: Runs containers (e.g., Docker, containerd).

Example:

In a cluster with three nodes, the master schedules workloads while worker nodes host the containers.


3. What are Pods in Kubernetes?

Answer:

A pod is the smallest deployable unit in Kubernetes. It represents a single instance of a running process in a cluster and can contain one or more containers that share the same network namespace and storage.

Example YAML File:

apiVersion: v1
kind: Pod
metadata:
  name: example-pod
spec:
  containers:
  - name: nginx-container
    image: nginx
    ports:
    - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

This pod runs an NGINX container on port 80.


4. How does Kubernetes handle scaling?

Answer:

Kubernetes supports three types of scaling:

  1. Manual Scaling: Adjusting replicas manually.
   kubectl scale deployment my-app --replicas=5
Enter fullscreen mode Exit fullscreen mode
  1. Horizontal Pod Autoscaler (HPA): Automatically scales pods based on CPU or custom metrics.
  2. Vertical Scaling: Adjusts resource requests/limits of containers in a pod.

Example HPA:

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: my-app-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-app
  minReplicas: 1
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 80
Enter fullscreen mode Exit fullscreen mode

This configuration scales the deployment my-app when CPU usage exceeds 80%.


5. What is a Deployment in Kubernetes?

Answer:

A Deployment is a higher-level abstraction that manages ReplicaSets and ensures a desired number of pods are running. It also supports rolling updates and rollbacks.

Example Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17
        ports:
        - containerPort: 80
Enter fullscreen mode Exit fullscreen mode

Intermediate Questions

6. Explain Kubernetes Service Types.

Answer:

Kubernetes services expose applications to enable communication.

  • ClusterIP: Internal access within the cluster (default).
  • NodePort: Exposes the service on a static port of the node.
  • LoadBalancer: Creates an external load balancer (cloud environments).
  • ExternalName: Maps a service to an external DNS name.

Example NodePort Service:

apiVersion: v1
kind: Service
metadata:
  name: nodeport-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30007
Enter fullscreen mode Exit fullscreen mode

This exposes the service on NodeIP:30007.


7. What is a ConfigMap?

Answer:

A ConfigMap decouples application configuration from the container image, allowing easy updates without rebuilding images.

Example ConfigMap:

apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  APP_ENV: production
Enter fullscreen mode Exit fullscreen mode

Using it in a Pod:

env:
- name: APP_ENV
  valueFrom:
    configMapKeyRef:
      name: app-config
      key: APP_ENV
Enter fullscreen mode Exit fullscreen mode

8. What is the purpose of PVs and PVCs?

Answer:

  • PersistentVolume (PV): Represents storage in the cluster.
  • PersistentVolumeClaim (PVC): A request for storage by a pod.

Example PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-example
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data
Enter fullscreen mode Exit fullscreen mode

Example PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-example
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi
Enter fullscreen mode Exit fullscreen mode

9. What are DaemonSets, and why are they used?

Answer:

DaemonSets ensure that a copy of a pod runs on all or specific nodes.

Example Use Case: Deploying monitoring agents like Fluentd or log collectors.

Example DaemonSet:

apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: log-agent
spec:
  selector:
    matchLabels:
      app: log-agent
  template:
    metadata:
      labels:
        app: log-agent
    spec:
      containers:
      - name: log-agent
        image: fluentd
Enter fullscreen mode Exit fullscreen mode

10. How does Kubernetes handle secrets?

Answer:

Secrets securely store sensitive data like passwords, API tokens, and SSH keys.

Example Secret:

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  password: cGFzc3dvcmQ=  # base64-encoded "password"
Enter fullscreen mode Exit fullscreen mode

Using it in a Pod:

env:
- name: PASSWORD
  valueFrom:
    secretKeyRef:
      name: my-secret
      key: password
Enter fullscreen mode Exit fullscreen mode

Advanced Questions

11. What are Taints and Tolerations?

Answer:

  • Taints: Prevent pods from being scheduled on specific nodes.
  • Tolerations: Allow pods to bypass taints.

Example Taint:

kubectl taint nodes node1 key=value:NoSchedule
Enter fullscreen mode Exit fullscreen mode

Pod Toleration:

tolerations:
- key: "key"
  operator: "Equal"
  value: "value"
  effect: "NoSchedule"
Enter fullscreen mode Exit fullscreen mode

12. What is the purpose of an Ingress?

Answer:

Ingress manages HTTP/HTTPS traffic to services within the cluster, providing SSL termination and routing.

Example Ingress:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80
Enter fullscreen mode Exit fullscreen mode

13. How does Kubernetes perform rolling updates?

Answer:

Rolling updates replace old pods with new ones gradually to avoid downtime.

kubectl set image deployment/nginx-deployment nginx=nginx:1.18
Enter fullscreen mode Exit fullscreen mode

Check Status:

kubectl rollout status deployment/nginx-deployment
Enter fullscreen mode Exit fullscreen mode

14. What is the role of Helm?

Answer:

Helm is a package manager for Kubernetes, simplifying application deployment using charts (pre-configured templates).

Install NGINX with Helm:

helm install my-nginx bitnami/nginx
Enter fullscreen mode Exit fullscreen mode

15. How does Kubernetes achieve load balancing?

Answer:

Kubernetes achieves load balancing through the following mechanisms:

  1. Internal Load Balancing:

    • Achieved using Services like ClusterIP and NodePort.
    • Distributes traffic between pods based on IP tables and round-robin.
  2. External Load Balancing:

    • Provided by LoadBalancer type services, which integrate with cloud providers (e.g., AWS ELB, GCP LB).
  3. Ingress Controllers:

    • Advanced HTTP load balancing with path and hostname-based routing.

Example LoadBalancer Service:

apiVersion: v1
kind: Service
metadata:
  name: example-loadbalancer
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
Enter fullscreen mode Exit fullscreen mode

This will create a cloud-based external load balancer, accessible over the internet.


16. What is the difference between ReplicaSet and ReplicationController?

Answer:

  • ReplicationController:

    • Ensures a specified number of pod replicas are running.
    • Supports simple equality-based label selectors.
  • ReplicaSet:

    • A more advanced version of ReplicationController.
    • Supports set-based selectors, allowing complex queries.

Example ReplicaSet:

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: example-replicaset
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.17
Enter fullscreen mode Exit fullscreen mode

ReplicaSet ensures three pods of nginx are always running.


17. What are Kubernetes namespaces, and why are they used?

Answer:

Namespaces are used to create isolated environments within a Kubernetes cluster. They help in organizing resources and enabling multi-tenancy.

Default Namespaces:

  • default: Default workspace for resources.
  • kube-system: For Kubernetes system resources.
  • kube-public: For publicly accessible resources.

Creating a Namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: dev-environment
Enter fullscreen mode Exit fullscreen mode

Using Namespace in Commands:

kubectl get pods -n dev-environment
Enter fullscreen mode Exit fullscreen mode

18. What is the role of Kubernetes monitoring tools?

Answer:

Monitoring tools in Kubernetes provide insights into cluster health, resource usage, and application performance.

Popular Tools:

  • Prometheus: Metrics collection and alerting.
  • Grafana: Visualization and dashboarding.
  • Kubernetes Metrics Server: Collects CPU and memory metrics for HPA.

Example Setup:

  1. Deploy Prometheus to collect cluster metrics.
  2. Use Grafana to visualize CPU usage or failed pod restarts.

19. How does Kubernetes handle faults in a cluster?

Answer:

Kubernetes provides self-healing features to manage faults:

  1. Restarting Failed Pods:

    The Kubelet ensures that pods restart automatically when containers fail.

  2. Re-scheduling Pods:

    If a node fails, Kubernetes reschedules pods to other healthy nodes.

  3. ReplicaSets:

    Ensures the desired number of pods are always running.

Example:

If a pod crashes, Kubernetes automatically restarts it based on the RestartPolicy (e.g., Always, OnFailure).


20. Explain Kubernetes control loops.

Answer:

Kubernetes uses control loops to monitor the cluster's actual state and ensure it matches the desired state defined in YAML manifests. The control loop continuously queries the state and triggers actions to fix deviations.

Example Control Loops:

  • Deployment Controller: Ensures the specified number of pods are running.
  • Node Controller: Detects node failures and evicts pods as needed.

Scenario:

If the desired state specifies three replicas of a pod and one crashes, the control loop creates a new pod to restore the count.


Pro-Tip for Kubernetes Interviews

  • Be ready to demonstrate your knowledge with hands-on tasks. For example:
    • Write and deploy a YAML file for a pod, deployment, or service.
    • Debug common issues like crash loops or networking errors.
  • Understand real-world use cases like blue-green deployments, Canary releases, and autoscaling.
  • Familiarize yourself with troubleshooting tools like kubectl logs, kubectl describe, and kubectl get events.

By understanding these concepts with examples, you'll be well-prepared to ace Kubernetes interviews in DevOps. Let me know if you need help setting up a mock interview or hands-on labs for practice!

Top comments (0)