K8s APIs and How to use Them
- K8s has workloads and workloads help in managing and run the applications in k8s cluster
- K8s workloads will have
- Pods
- Controllers
Pods
- Pod is basic unit of execution in K8s (vms in hypervisor and container in docker)
- Pod has application container, storage resources and network identity (ip address)
- Pod has docker container
- Pods can have single or multiple containers in it.
- One-container-per-pod is the most common k8s usecase
- pods might encapsulate an application of multiple containers that are tightly coupled.
Lets use API Reference to create a Sample Pod
- Navigate to k8s API Reference From here (we will be using API version v1.18)
- Now navigate to pod
- Now fill the yaml file by using apireference
---
apiVersion: v1
kind: Pod
metadata:
name: hello-pod
spec:
containers:
- name: myjenkins
image: jenkins
- Now login into k8s master and execute the following commands
kubectl get nodes
kubectl apply -f hello-pod.yaml
kubectl get pods
kubectl get pods -o wide
kubectl describe pod hello-pod
kubectl get pods -oyaml
kubectl get pods -oyaml | grep uid
kubectl delete -f hello-pod.yaml
- Dont try to remember commands refer here for cheatsheet
How kubectl helps
- kubectl takes commandline args or yaml file as input and it creates json and speaks with k8s apis with the created json
Imperative vs Declarative style in kubectl
- Imperative style is building commandlines for creation of k8s objects.
- Declarative style is building a yaml file with state information
- If we build commandlines (imperative) changes will not be version controlled and difficult to handle
- Repeating the activity would mean rebuilding the command
Durability of Pods
- Pods aren’t created as durable entities
- They wont survive failures (node/pod/other)
- so to ensure pods state we use controllers
Pod lifecycle States
- Pending
- Running
- Failed
- succeded
Container Probes inside POd
- A probe is a diagnostic/check performed by a kublet on a container.
- Kubelet will call Handler (which is implemented by a container)
- ExecAction
- TCPSocketAction
- HTTPGetAction
- Probe can have one of following results
- Success
- Failure
- Unknown
- Kubelet can react to three kinds of probes on running containers
- livenessProbe: indicates wheter the container is running or not
- readinessProbe: indicates whether the container is ready to service requests
- startupProbe: indicates whether application in container is started
Init Containers
- Specialize container inside the pod that run before the application containers in a Pod
- Init containers can contain utilities or setup scripts
Challenges in the Pod
- Pods cannot maintain state (Pod cannot correct itself)
- How Networking is applied to Pods
- How to persis the data from the containers running inside pods
Controllers to the Rescue
- Controllers help in maintaining desired state. Controllers manage pods
- In k8s cluster directly creating pods is not a good idea dealing with controllers and making controllers manage pods is way ahead
- Controllers in K8s
- ReplicatSet
- ReplicationController
- Deployments
- StatefulSets
- DaemonSet
- Jobs
- CronJob
Replication Controller
- A replication controller ensures specified number of pod replicas are running at any time
- To create a Replication Controller create a template/manifest by using API Reference
- Lets create a replication controller with 3 jenkins pods
---
apiVersion: v1
kind: ReplicationController
metadata:
name: hello-rc
spec:
replicas: 3
template:
metadata:
labels:
app: jenkins
spec:
containers:
- name: jenkins
image: jenkins:2.60.3
- Now execute the following commands
kubectl apply -f hello-rc.yaml
kubectl get rc -o wide
kubectl get pods -o wide
kubectl get rc -oyaml| grep uid
- Now lets increase the replications to 5
---
apiVersion: v1
kind: ReplicationController
metadata:
name: hello-rc
spec:
replicas: 5
template:
metadata:
labels:
app: jenkins
spec:
containers:
- name: jenkins
image: jenkins:2.60.3
- Now lets see what happens when one node fails. (I will be stopping 24 node for check)
- Experment with 2 replicas and kill one node