Kubernetes Flashcards
study kubernetes
[k8s] what are the different ways to create k8s cluster
k8s cluster can be setup in various ways
- minikube - single node cluster
- Google Kubernetes engine which allows setting up multi-master k8s cluster on the google cloud.
- kubeadm tool - use this tool to setup k8s cluster on physical or virtual machines. Manual setup of k8s cluster.
- kops tool - This tool is built on top of kubeadm and is available on github. It helps you deploy production-grade, highly available Kubernetes clusters on AWS, GKE, VMware vSphere and so on.
[k8s] what is a pod
A pod is a group of one or more tightly related containers that will always run together on the same worker node and in the same linux namespace(s). Each pod is like separate logical machine with its own IP, hostname, processes and so on, running a single application.
[k8s]how the pod is created
When you run the “kubectl run” command, it creates a new ReplicationController object in the cluster by sending a REST HTTP request to the Kubernetes API server. the ReplicationController then created a new pod, which was then scheduled to one of the worker nodes by the scheduler. The kubelet on that node saw that the pod was scheduled to it and instructed docker to pull the specified image from the registry because the imgae wan not available locally. After downloading the image, Docker created and ran the container
[k8s] what is a service and different types of services avaialble in k8s
Pods are exposed within cluster and to outside world using service. A service gets a static IP address which never changes during the lifetime of the service.
Pods are ephemeral - it can disappear at any time because the node it is running failed, because someone deleted the pod, or because the pod was evicted from an otherwise healthy node. When any of those occurs, a missing pod is replaced by a new one by the ReplicationController. This new pod gets a different IP
address from the pod it’s replacing.
Instead of connecting to pods directly, clients should connect to the service through its constant IP address. The service makes sure one of the pods receives the connection, regardless of where the pod is currently running (and what its IP address is).
Some useful properties of service object
sessionAffinity: clientIP = All requests made by a certain client to be redirected to same pod every time.
Following are the different types of services
- ClusterIP
- ExternalName
- NodePort
- LoadBalancer
[k8s]A pod is group of one or more tightly related containers. So does all the containers in a pod share same filesystem
No.
Because all containers of a pod run under the same Network and UTS namespaces, they all share the same hostname and network interfaces. Similarly, all containers of a pod run under the same IPC namespace
and can communicate through IPC.
But when it comes to the filesystem, things are a little different. Because most of the container’s filesystem comes from the container image, by default, the filesystem of each container is fully isolated from other containers.
[k8s]where do containerized application logs
Containerized applications usually log to the standard output and standard error stream instead of writing to their log files.
$docker logs
$kubectl logs
[k8s] what is a label
A label is an arbitrary key-value pair you
attach to a resource, which is then utilized when selecting resources using label selectors. resources are filtered based on whether they include the label specified in the selector. A resource can have more than one label, as long as the keys of those labels are
unique within that resource.
[k8s] How a pod can be scheduled to specific nodes?
Assign labels to the nodes (for example: gpu=true). Use nodeSelector attribute in pod spec to tell Kubernetes to deploy the pod only to the nodes containing the label “gpu=true”.
[k8s] What is a namespace? whats its use?
Kubernetes namespaces provide a scope for objects names. Instead of having all your resources in one single namespace, you can split them into multiple namespaces, which also allows you to use the same resource names multiple times (across different namespaces).
Using multiple namespaces allows you to split complex systems with numerous components into smaller distinct groups. They can also be used for separating resources
in a multi-tenant environment, splitting up resources into production, development, and QA environments, or in any other way you may need.
$kubectl get ns
Besides isolating resources, namespaces are also used for allowing only certain users access to particular resources and even for limiting the amount of computational resources available to individual users.
[k8s] what is managed pods
Managed pods are the pods created by replication controller / replica set or deployment. Pods created directly (using kubectl run command) are called unmanaged pods. In case of node failure, kubernetes will reschedule managed pods to other nodes. It will never schedule unmanaged pods to other nodes as only kubelet on that given node knows about the pod. Since node no longer there, it cannot be rescheduled on other nodes.
[k8s] what is liveness prob? why is it needed?
kubelet on the node starts running a container as soon as it is scheduled on the node. If containers main process crashes then kubelet will restart the container.
But sometimes application process does not crash but stops responding say because it falls into an infinite loop or a deadlock. To make sure applications are restarted in such cases, an application’s health must be checked from outside. This can be done using liveness probe. The liveness probe can be specified for each container in the pod’s specification. Kubernetes will periodically execute the probe and restart the container if the probe fails.
3 types of liveness probe supported by k8s
1. An HTTP get probe performs GET request on the container’s IP and path you specified. If response is received and response code does not represent an error then prob is successful
2. A TCP socket probe which tries to open a TCP socket to the given port
3. an exec probe that executes arbitrary command
inside the container.
Additional properties can be set for liveness probe, such as
1. delay: start probe after ‘delay; time once container started
2. timeout: time within container must return response
3. period: frequency of the probe
[k8s]What is replication controller
RC is a k8s resource that ensures its pods are always kept running. It constantly monitors the list of running pods and makes sure the number of pods matches desired number. RC has three essential parts
1. label selector - which determines what pods are in RC’s scope.
2. replica count - which specifies desired number of pods that should be running
3. pod template - which is used for creating new pod replicas.
RC enables following powerful features
- Makes sure a pod is always running by starting a new pod when an existing one goes missing
- When a cluster node fails, it creates replacement replicas for all the pods that were running on the failed node.
- Enables horizontal scaling of pod, manual or automatic
[k8s]What is replica set? why it is preferred over RC?
Replica set is new generation of RC and replaces it completely. Replica set behaves exactly like RC, but it has more expressive pod selector. Replica set’s selector allows matching pods that lack a certain label or pods that include certain label key, regardless of its value
[k8s]What is a daemon set
DaemonSets run only a single pod replica on each node in the cluster and each node needs to run exactly one instance of pod. This is useful for infrastructure related pods that perform system level operations. For example, log collector or resource monitor that need to run on every node.
The pods created by Daemonset have a target node specified and skips the scheduler..
Its possible to run the pods on a subset of all the nodes. This is done by specifying the nodeSelector property in pod template.
$kubectl get ds #get all daemon sets
[k8s] What is a job resource
Replica set, replication controller, daemon sets are used when a pod need to be run continuously. Job resource allows you to run a pod whose container isn’t restarted when the process running inside finishes successfully. Once it does, the pod is considered complete.
Pods managed by job are rescheduled in case of node failure.
Pod is not deleted once job is complete so that you can check the job logs. Pod is deleted when job is deleted or when you explicitly delete the pod.
The job may be configured to create more than one pod instance and run them in parallel or sequentially. If you want to run a job more than once, then you set completions to how many times you want the job’s pod to run. The job would be run one after another sequentially. The job can be made run parallel by specifying parallelism property
example
completions: 5
parallelism: 2
means run the job 5 times, with 2 jobs in parallel.
A pod’s time can be limited by setting the activeDeadlineSeconds property in the pod spec. If the pod runs longer than that, the system will try to terminate it and will mark the job as failed.
[k8s] what is cronJob resource
CronJob resource allows to run a job a given time and the interval. Its same as cron daemon on Linux.
[k8s] What is the use of named port
A name can be given to the port exposed by the pod in the pod’s yaml definition, which can be referred in the service spec.
This enables you to change port numbers of pod later without having to change the service spec.
[l8s] What are the different service discoveries supported by k8s?
- Discovering services through environment variables - When a pod is started, Kubernetes initializes a set of environment variables pointing to each service that exists at that moment. If you create the service before creating the client pods, processes in those pods can get the IP address and port of the service by
inspecting their environment variables. - Using DNS - Kubernetes run an internal DNS server in a pod named “kube-dns-xxx” and a service by same name i.e. “kube-dns”. All the pods running in the cluster are automatically configured to use this internal DNS server. This is done by modifying the container’s /etc/resolve.conf file. Any DNS query performed by a process running in a pod will be handled by kubernetes’s own DNS server which knows all the services running in the system. Each service automatically gets a DNS entry in the internal DNS server.
[k8s] How a pod can access a service which is living outside the cluster
Services don’t link to pods directly. an ‘endpoint’ resource sits between pod(s) and service. An Endpoint resource is a list of IP addresses and ports exposing a service.
$kubectl get endpoints
The endpoint resource is automatically created by k8s using the pod selector specified in the service spec. If you create a service with a pod selector then k8s will not even create the Endpoint resource. Then its upto you to create the Endpoint resource to specify the list of endpoint for the service.
This technique is used to create a service which is living outside the cluster.
1. Create service spec without mentioning pod selector.
2. Create Endpoint spec which would list external IP addresses and ports
Note that the endpoint resource has same name as service. That is how an endpoint is associated with a service.
[k8s] What is an External service?
An external service can be referred by using its FQDN instead of exposing it by manually configuring the service’s Endpoints,
apiVersion: v1
kind: Service
metadata:
name: external-service
spec:
type: ExternalName
externalName: someapi.somecompany.com
ports:
- port: 80
After the service is created, pods can connect to the external service through the external-service.default.svc.cluster.local domain name.
ExternalName service are implemented at DNS level using a simple CNAME DNS record. Therefor, clients connecting to the service will connect to the external service directly, bypassing the service proxy completely. For this reason, these types of service don’t even get cluster IP.
[k8s] How service can be exposed to the external clients clients?
Following are the ways to expose a service to external clients
- NodePort service - For a NodePort service, each cluster node opens a port on the node itself and redirects traffic received on that port to the underlying service.
- LoadBalancer service - This makes service accessible through a dedicated load balancer. The load balancer redirects traffic to the node port across all the nodes. Clients connect to the service through the load balancer’s IP.
- Creatign an Ingress resource for exposing multiple services through a single IP address.
[k8s] What is a readyness probe?
The readiness probe is invokes periodically and determines whether the specific pod should receive client requests or not. When a container’s readiness probe returns success, it is signaling that the container is ready to accept requests.
When a container is started, kubernetes can be configured to wait for a configurable amount of time to pass before performing the first readiness check. After that, it invokes the probe periodically and acts based on the result of the readiness probe. If a pod reports that it is not ready, it is removed from the service. If the pod then becomes ready again, it is re-added.
Unlike liveness probe, if a container fails the readiness check, it wont be killed or restarted
Three types of readiness probe exists
1. An Exec probe - A process is executed and containers’s status is determined by the process’s exit code.
2. HTTP Get probe - Sends HTTP Get request to container and the status code of response is used to determine container is ready or not.
3. A TCP socket probe - Opens TCP socket to a specified port and container is ready if connection succeeds.