WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Kubernetes · Advanced · question 60 of 100

Describe the role of Kubernetes Custom Controllers and how they can be used to automate and extend cluster functionality.?

📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

Kubernetes Custom Controllers are software components that extend the Kubernetes control plane by adding new behaviors and capabilities to the cluster. Custom Controllers can be created using the Kubernetes API and programming languages such as Go, Python, and Java.

Custom Controllers work by watching for specific events and changes in the Kubernetes API, such as the creation or deletion of resources, and then responding to those events by performing some action. Custom Controllers can be used to automate tasks, such as scaling applications or managing network policies, and to integrate with external systems and services.

Some common examples of Custom Controllers in Kubernetes include:

StatefulSet Controller: Manages stateful applications by ensuring that each Pod has a unique hostname and persistent storage. DaemonSet Controller: Ensures that a specific Pod is running on each node in the cluster, typically used for system-level tasks such as logging or monitoring. Job Controller: Runs a set of Pods to completion and then exits, typically used for batch processing or one-time jobs. Operator Controller: Provides a higher level of abstraction for managing complex applications, typically by using Custom Resources and Controllers specific to that application.

To create a Custom Controller, you would typically start by defining a Custom Resource Definition (CRD) that defines the new resource and its schema. You would then write a Controller that watches for changes to that resource and takes action based on those changes. The Controller would typically interact with the Kubernetes API server to create, update, or delete other resources as needed.

Here is an example of a simple Custom Controller written in Go:

package main

import (
    "fmt"
    "time"

    "k8s.io/apimachinery/pkg/apis/meta/v1"
    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/rest"
    "k8s.io/client-go/tools/cache"
    "k8s.io/client-go/util/workqueue"
)

type CustomController struct {
    clientset kubernetes.Interface
    queue     workqueue.RateLimitingInterface
    informer  cache.SharedIndexInformer
}

func main() {
    // Create a Kubernetes clientset using the default configuration
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }

    // Create a CustomController
    controller := \&CustomController{
        clientset: clientset,
        queue:     workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "CustomController"),
        informer:  createInformer(clientset),
    }

    // Start the informer and the worker
    stop := make(chan struct{})
    defer close(stop)
    go controller.informer.Run(stop)
    go controller.runWorker(stop)

    // Wait forever
    select {}
}
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Kubernetes interview — then scores it.
📞 Practice Kubernetes — free 15 min
📕 Buy this interview preparation book: 100 Kubernetes questions & answers — PDF + EPUB for $5

All 100 Kubernetes questions · All topics