K8 Interview Q
K8 Interview Q
To scale a deployment in Kubernetes, you can use the kubectl scale command and specify
the desired number of replicas for the deployment. For example, to scale a deployment
named “my-deployment” to 5 replicas, you can use the command: “kubectl scale deployment
my-deployment — replicas=5”.
To troubleshoot a pod that is not starting in Kubernetes, you can use the kubectl describe
pod command to get more information about the pod’s status and events. You can also
check the pod’s logs using the kubectl logs command. Additionally, you can use the kubectl
To deploy a new version of an application in Kubernetes, you can update the deployment’s
image tag to the new version and use the kubectl apply command to apply the changes.
Kubernetes will then automatically create new pods with the updated image and gradually
To implement a rolling update strategy in Kubernetes, you can update the deployment’s
image tag to the new version and use the kubectl apply command with the — record flag to
create a new revision. Then, you can use the kubectl rollout command to perform the rolling
update, specifying the max unavailable and max surge parameters to control the number of
pods that can be unavailable and the number of new pods that can be created at a time.
5# How would you secure a Kubernetes cluster?
To secure a Kubernetes cluster, you can implement various security measures such as using
role-based access control (RBAC) to restrict access to the cluster resources, enabling
network policies to control traffic flow, encrypting sensitive data using secrets, and using
To scale a stateful application in Kubernetes, you need to take into account the stateful
nature of the application and the data it stores. You can use statefulsets to manage stateful
applications and specify the desired number of replicas. However, you also need to ensure
that the data is replicated and distributed properly across the replicas using persistent
To perform a backup and restore of a Kubernetes cluster, you can use various tools and
strategies such as Velero (formerly Heptio Ark), kubeadm-dumper, etcdctl, and other
backup and recovery solutions. You need to ensure that you have a consistent backup of the
cluster state, including the etcd database, and that you have tested the restore process in a
separate environmen
1. What is the difference between config map and secret? (Differentiate the answers as with
examples)
Config maps ideally stores application configuration in a plain text format whereas Secrets store
sensitive data like password in an encrypted format. Both config maps and secrets can be used as
volume and mounted inside a pod through a pod definition file.
Config map:
kubectl create configmap myconfigmap
--from-literal=env=dev
Secret:
echo -n ‘admin’ > ./username.txt
echo -n ‘abcd1234’ ./password.txt
kubectl create secret generic mysecret --from-file=./username.txt --from-
file=./password.txt
2. If a node is tainted, is there a way to still schedule the pods to that node?
When a node is tainted, the pods don’t get scheduled by default, however, if we have to still
schedule a pod to a tainted node we can start applying tolerations to the pod spec.
apiVersion: v1
kind: PersistentVolume
metadata:
name: mypv
spec:
capacity:
storage: 5Gi
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
4. What kind of object do you create, when your dashboard like application, queries the
Kubernetes API to get some data?
You should be creating serviceAccount. A service account creates a token and tokens are stored
inside a secret object. By default Kubernetes automatically mounts the default service account.
However, we can disable this property by setting automountServiceAccountToken: false in our spec.
Also, note each namespace will have a service account
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-sa
automountServiceAccountToken: false
5. What is the difference between a Pod and a Job? Differentiate the answers as with
examples)
A Pod always ensure that a container is running whereas the Job ensures that the pods run to its
completion. Job is to do a finite task.
Examples:
$ kubectl run mypod1 --image=nginx --restart=Never
$ kubectl run mypod2 --image=nginx --restart=onFailure
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
mypod1 1/1 Running 0 59s
$ kubectl get job
NAME DESIRED SUCCESSFUL AGE
mypod1 1 0 19s
6. How does Kubernetes handle service discovery and load balancing?
Kubernetes uses two primary components for service discovery and load balancing:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my_deployment
spec:
selector:
matchLabels:
type: example
color: red
replicas: 3
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 25%
maxUnavailable: 10%
template:
metadata:
labels:
type: example
color: red
spec:
containers:
- name: echocolor
image: reselbob/echocolor:v0.1
ports:
- containerPort: 3000
env:
- name: COLOR_ECHO_COLOR
value: RED
- name: COLOR_ECHO_VERSION
value: V1
---
apiVersion: v1
kind: Service
metadata:
name: my_service
spec:
selector:
type: example
color: blue
ports:
protocol: TCP
port: 3000
targetPort: 3000
type: NodePort
The reason that the service cannot bind to the pods in the deployment is
because the labels in the deployment’s pods do not match the service’s selector
field values.
A service binds to a pod through a label match between a service selector and
pod. When a service starts up, it “looks for” pods in the cluster with labels that
are declared in the service’s selector field.
Figure 1 below illustrates the problem that exists in the manifest file above.
Notice that the service in the figure below shows a Kubernetes service that has
the selector values type=example and color=blue. Yet, the labels field of each of
the pods in the Kubernetes deployment have the
values type=example and color=red. The values in the service’s selector field
and the pods’ labels fields do not match up. Hence, no binding.
Figure 1: For a Kubernetes service to bind to a pod, the values in the
service’s selector field must match the values in the pods’ labels fields.
The values in the service’s selector field and the pods’ labels fields do NOT
match up. Hence, no binding.
An init container is a container that Kubernetes runs before any other containers
in the pod are created. You can use an init container to implement initialization
behavior that other pods will use.
The example below shows how to create an init container that waits for a
database service to come online before it creates the container that will use the
database.
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
spec:
containers:
- name: myapp-container
image: busybox:1.31
initContainers:
- name: init-myservice
image: busybox:1.31
kind: ConfigMap
apiVersion: v1
metadata:
name: db-configmap
data:
database: mongodb
database_uri: mongodb://localhost:27017
keys: |
maximum.connections=5
timeout=10000
A Kubernetes secret is a Kubernetes object that stores information in the cluster
in an encrypted format. Typically a secret is used to store confidential
information. The example below is a manifest file that describes a Kubernetes
secret for a username/password pair. Notice the Base64 encrypted values.
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
username: YWRtaW4=
password: MWYyZDFlMmU2N2Rm
For example, an operator creates a Kubernetes service, the backing pods, the
storage volumes as well as the Roles, and RoleBindngs, and all the
configurations that are part of an application that runs on a Kubernetes cluster.
There are Kubernetes operators for MySQL, Oracle and Redis to name a few
Kubernetes comes with an internal DNS server that automatically assigns DNS
names to the services it creates. These DNS names are accessible from pods
within the cluster.
<service>.<namepace>
However, if both the service and the pod calling the service are in the same
namespace, the calling container within the pod can use just the service name as
the DNS name (see Figure 2 below).
Figure 2: Kubernetes has a DNS server that creates DNS names accessible
within a cluster.
For example, imagine you have a service, called myservice, in the namespace
called mynamespace. The service myservice represents an HTTP web server.
To call the service myservice from a container in the pod yourpod, using curl for
example, you would use the URL http://myservice.mynamespace. Why?
Because yourpod is in a different namespace from myservice.
However, to call myservice from mypod using curl, the DNS name to use
is http://myservice because mypod and myservice are in the same
namespace, mynamespace.
When Kubernetes needs to create a pod(s), it identifies the node where the
pod(s) can be created using Scheduler. A message then gets sent to the kubelet
instance on the identified node. Kubelet in turn works with the container runtime
interface (CRI) to create the container(s) for the pod(s). Figure 3 below outlines
how these processes work together.
Figure 3: How kubelet realizes containers on a Kubernetes node
The manifest file shown below describes a Kubernetes deployment in which all
the pods created for the deployment are assigned to any node that has a label
with key-value pair nodelocation=usa.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 5
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
# pair, nodelocation=usa
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: nodelocation
operator: In
values:
- usa
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
-ADS BY GOOGLE