-
Notifications
You must be signed in to change notification settings - Fork 384
Description
π οΈ General Kubernetes Operator Questions
1οΈβ£ What is a Kubernetes Operator, and why do we use it?
β How to Answer:
"A Kubernetes Operator is a controller that automates the lifecycle of complex applications running in Kubernetes. It extends Kubernetes' capabilities by introducing Custom Resource Definitions (CRDs) that define application-specific configurations.
We use Operators when Helm charts or plain YAML aren't enough, such as when we need:
- Automated self-healing (e.g., restarting failed Laravel instances)
- Scaling logic based on app-specific metrics
- Managing external dependencies (e.g., provisioning databases)
Operators enable day-2 operations, like upgrades, failover, and monitoring, making them ideal for long-running applications."
2οΈβ£ What is the difference between a Controller and an Operator?
β How to Answer:
"A Controller is a core Kubernetes component that ensures a resource's actual state matches its desired state (e.g., ReplicaSet ensures the correct number of pods).
An Operator is a specialized Controller that manages custom application logic via CRDs.
π Key Differences:
| Feature | Controller | Operator |
|---|---|---|
| Works with | Built-in Kubernetes resources | Custom Resources (CRDs) |
| Automates | Basic Kubernetes features | App-specific lifecycle |
| Example | Deployment controller | LaravelApp operator |
In short, all Operators are Controllers, but not all Controllers are Operators."
π Reconciliation & Controller Logic Questions
3οΈβ£ How does the Reconciliation Loop work in a Kubernetes Operator?
β How to Answer:
"The Reconciliation Loop is the heart of an Operator. It continuously:
- Watches for changes in a Custom Resource (CR) (e.g.,
LaravelApp). - Fetches the actual state from the Kubernetes API.
- Compares it with the desired state.
- Takes action if there's a difference (e.g., creates a Deployment, updates an image, or scales pods).
- Updates the status of the CR.
π‘ Example: If a LaravelApp is supposed to have 3 replicas but only 2 are running, the Operator will create another pod to match the desired state."
π Key Code in Reconcile()
func (r *LaravelAppReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
laravelApp := &appv1alpha1.LaravelApp{}
if err := r.Get(ctx, req.NamespacedName, laravelApp); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// Ensure Deployment exists, create/update if needed
return ctrl.Result{}, nil
}4οΈβ£ How do you prevent an Operator from continuously reconciling in an infinite loop?
β How to Answer:
"This can happen when the Operator modifies the resource in Reconcile(), triggering another loop.
To prevent this:
- Check for unnecessary updates before modifying resources.
- Use Status fields instead of Spec fields to store internal state.
- Use proper backoff mechanisms (e.g., requeue with a delay).
π Example: Avoiding Unnecessary Updates
if existingDeployment.Spec.Replicas == laravelApp.Spec.Replicas {
return ctrl.Result{}, nil
}This ensures the Operator only updates when necessary."
π CRD & Webhook Questions
5οΈβ£ What are Custom Resource Definitions (CRDs) in Kubernetes?
β How to Answer:
"A Custom Resource Definition (CRD) extends Kubernetes by adding a new custom resource type, allowing us to manage application-specific logic within Kubernetes.
π‘ Example:
If I need to deploy Laravel applications, I can create a CRD called LaravelApp:
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
name: laravelapps.example.com
spec:
group: example.com
names:
kind: LaravelApp
plural: laravelapps
scope: Namespaced
versions:
- name: v1alpha1
served: true
storage: trueWith this, I can apply LaravelApp resources like:
apiVersion: example.com/v1alpha1
kind: LaravelApp
metadata:
name: my-laravel
spec:
replicas: 3
image: "laravel:latest"This allows the Operator to manage Laravel applications natively within Kubernetes."
6οΈβ£ What are Webhooks in Kubernetes, and how are they used in Operators?
β How to Answer:
"Webhooks allow us to validate or mutate Custom Resources before they are stored in etcd. There are two types:
- Mutating Webhooks β Modify requests before they are persisted (e.g., adding default values).
- Validating Webhooks β Reject invalid requests (e.g., prevent
replicas: -1).
π Example: Webhook to Validate LaravelApp CR
func (r *LaravelApp) ValidateCreate() error {
if r.Spec.Replicas < 1 {
return fmt.Errorf("replicas must be at least 1")
}
return nil
}This ensures users can't deploy Laravel apps with zero or negative replicas."
β‘ Performance & Debugging Questions
7οΈβ£ How do you optimize Operator performance?
β How to Answer:
"To optimize performance, I follow these best practices:
- Rate limiting & backoff β Avoid excessive API calls (
RequeueAfter: time.Second * 30). - Efficient caching β Use Informer cache instead of querying API directly.
- Status Updates β Store internal state in
.statusinstead of.specto prevent infinite loops. - Leader Election β Ensure only one Operator instance is actively reconciling."
π Example: Requeue with Backoff
return ctrl.Result{RequeueAfter: time.Minute}, nilπ Security & RBAC Questions
8οΈβ£ How do you secure a Kubernetes Operator?
β How to Answer:
"Security best practices for Operators include:
- Minimal RBAC permissions β Grant only necessary access.
- Run as non-root user β Avoid running with full privileges.
- Secure secrets β Use Kubernetes Secrets instead of environment variables.
- Enable TLS for webhooks β Prevent MITM attacks on Operator webhooks."
π Example: Restricting Operator Permissions
rules:
- apiGroups: ["laravel.example.com"]
resources: ["laravelapps"]
verbs: ["get", "list", "watch", "create", "update", "delete"]π§© Miscellaneous & Scenario-Based Questions
9οΈβ£ What would you do if an Operator is consuming too many Kubernetes API calls?
β How to Answer:
"I would:
- Enable caching using Informers to reduce direct API calls.
- Batch updates instead of updating individual resources frequently.
- Use exponential backoff for retries.
- Reduce requeue frequency to avoid unnecessary loops."
π How would you design an Operator for a multi-tenant system?
β How to Answer:
"For a multi-tenant system:
- Use Namespace-scoped CRDs instead of Cluster-wide CRDs.
- Leverage NamespaceSelectors to filter resources per tenant.
- Enforce RBAC restrictions to prevent cross-tenant access."
π Example: Restrict CRDs to Tenant Namespaces
namespaceSelector:
matchLabels:
tenant: "customer-a"π― Final Notes
You're now 100% ready for your interview! π
- Mock questions? β Covered.
- Best responses? β Structured & concise.
- Deep dive on topics? β Advanced topics included.