Also known as K8s, its an open source system for automating deployment, scaling, and management of containerized applications.
It supports a wide variety of applications like stateful, stateless and data intensive apps.
Important concepts related to kubernetes
A deployment of kubernetes is called a kubernetes cluster. Its a cluster of nodes that run a containerized application. Each cluster has one master node, the kubernetes control plane, and one or more worker nodes.
Nodes are not created by kubernetes itself, but by the cloud provider.
The API is exposed by the kubernetes-api-server. It serves as the front end for the control plane.
etcd
is highly available, distributed key-value store that contains all the cluster data and configurations. It stores deployment configuration data, the desired state and the metadata in a way that its accessible in a common location.
kube-scheduler
assigns newly created Pods to nodes, and decides where the workload should run within the cluster. It selects the optimal node based on kubernetes scheduling algorithms and available resources.
Nodes are the worker machines in a kubernetes cluster, and maybe virtual or physical. They are managed by the control plane. They contain the necessary services needed to run applications. Nodes include Pods, which are the smallest deployment units of a kubernetes cluster. Each node can run multiple containers. Containers share all the resouces of a node.
Communicates with the API server to ensure that Pods and their containers are running. It reports the health status of the Pods to the control plane.
container runtime
is configurable, with docker being a popular choice.
Namespaces provide a mechanism for isolating groups of resources within a single cluster. Namespaces segregate cluster by team, project etc.
Namespace provides a scope for object names, each object having a name. Names are unique for a resource type within a namespace.
Pods are the simplest unit in kubernetes and can run one or more containers. Its specs are defined using a YAML format. See an example Pod spec below:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
A Pod spec must contain at least one container.
A replicaset is a set of identical running replicas of a Pod for horizontal scaling. The yaml specs for a replicaset are slightly different from that of a Pod.
apiVersion: v1
kind: ReplicaSet # is specified as a Pod in Pods yaml
metadata:
name: nginx-replicaset
labels:
app: nginx
spec:
replicas: 3 # number of replicas at any given time
# if a Pod dies, a new is created to match this number
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx # matches the selector label
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Generally encapsulated by a deployment.
A deployment is a higher level object that provides updates for Pods and replicasets. Deployments run multiple replicas of an application and are suitable for stateless applications.
apiVersion: v1
kind: Deployment # is specified as a Pod in Pods yaml
metadata:
name: nginx-deployment
labels:
app: nginx
# rest is same as a replicaset
spec:
replicas: 3 # number of replicas at any given time
# if a Pod dies, a new is created to match this number
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx # matches the selector label
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
Rolling updates are applied by scaling up the Pods of the new version to the appropriate number, and scaling down the Pods of old version to zero. This is not a feature of replicaset.
Service is a REST object like Pods, and acts a logical abstraction for a set of Pods in a cluster. It acts as a load balancer across the Pods. A service is assigned a unique IP addresss for accessing applications deployed on Pods.
Client -> Service -> Pod(selects a Pod from the set to direct the client request to)
Service eliminates the need for a separate discovery process. A service is needed because
There are four types of services
spec.externalName
parameterIt is an API object (combined with a controller) that provides routing rules to manage external users’ access to multiple services in a Kubernetes cluster. In production, Ingress exposes applications to the Internet via port 80 (HTTP) or port 443 (HTTPS). An ELB is expensive and managed outside the cluster, while the cluster monitors Ingress.
Its an object that manages stateful applications, by managing the deployment and scaling of the Pods. It also provides guarantees about the ordering and uniqueness of the Pods. A sticky identity is maintained for each of the Pods. It also provides persistent storage volumes for the workloads.
Its an object that created Pods and tracks its completion process. Jobs are retried until they are completed, and deleting a Job removes the created Pods. Suspending a Job will also delete its active Pods until the Job resumes. A Job can run several Pods in parallel. A CronJob is regularly used to create Jobs on an iterative schedule.
kubectl
Is the Kubernetes CLI (Kube Command Tool Line). Helps to deploy applications, inspect and manage cluster resources, view logs etc. Key command types are
kubectl [command] [type] [name] [flags]
where
kubectl create -f nginx.yaml
Some common command examples
# get commands
kubectl get services
kubectl get pods -all-namespaces
kubectl get deployment my-deployment
kubectl get pods
# apply commands to create resources
kubectl apply -f file.yaml
kubectl apply -f folder/
# scale commands for replicas
kubectl scale --replicas=3 rs/foo
kubectl scale --replicas=3 -f file.yaml