PODs in Kubernetes:
A POD is the smallest object, that we can create in kubernetes. It's a single instance of an application.kubernetes helps us to deploy our application in the form of containers on a set of machines that are configured as worker nodes in a cluster. However, kubernetes does not deploy containers directly on the worker nodes. The containers are encapsulated into a Kubernetes object which is known as PODs.
When the users increases we need to scale our application so we need to add additional instance of our application to share the load. We need to create new POD with a new instance of our application. A single POD can have multiple containers, except for the fact that they are usually not multiple containers of the same kind.
PODs usually have a one-to-one relationship with containers running your application. To scale UP we create new PODs and to scale down we delete PODs. With PODs, kubernetes does all of the configuration for us automatically. We just need to define what containers a POD consists of and the containers in a POD by default will have access to the same storage, the same network namespace as in they will be created together and destroyed together.
How to Deploy the PODs in Kubernetes:
# Deploy the PODs in K8s :
kubectl run [podname] --image [imagename]
# Example :
kubectl run nginx --image nginx
How to get the list of running PODs in Kubernetes:
# Deploy the PODs in K8s :
kubectl get pods
How to see the detailed informatin of a POD in Kubernetes:
# detailed informatin of a PODs in K8s :
kubectl describe pod [pod-name]
# Example :
kubectl describe pod myapp-pod
PODs Creation using YAML Configuration file:
Kubernetes uses YAML files as input for the creation of objects such as PODs, Replicas, Deployments, Services etc. All of these follow similar structure.
kubernetes definition file always contains 4 top level fields which is listed Below. These are top level or root level properties and we can think of them as siblings, children of the same parent. These are all REQUIRED fields, so we MUST have them in our configuration file.
- apiVersion : This indicates the kubernetes API version that we are using to create the object. Depending on what we are creating we must use the proper apiVersion. for PODs it is v1 and other possible values of apiVersion are apps/v1beta1, extensions/v1beta1 etc.
- kind : The kind refers to the type of object we are trying to create. Some possible values are Pod, ReplicaSet or Deployment or Service
- metadata : The metadata is data about the object like its name, labels etc. It is in a dictionary (key -value ). namesand labels are the children of metadata field. We cannot add any other property other than kubernetes defined under metadata field. However under labels we can add any key-value pair.
- spec : spec stands for specification. Depending upon the objects we are trying to create, we can provide the additional information to kubernetes such as container or image info. spec is different for different objects so its important to understand or refer to the documentation section to get the right format for each.
# pod-definition.yml :
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
lables:
app: myapp
spec:
containers:
- name: nginx-container
image: nginx
# run the yml configuration file :
kubectl create -f pod-definition.yml
Setup Visual Studio code for Yaml file
go to extensions -> search for YAML by Rad Hat -> click on setting gear icon -> go to YAML: Schema -> click on setting gear icon
# setting.json file for YAML: Schema
{
"workbench.colorTheme": "Default Light+",
"yaml.schemas": {
"kubernetes": "*.yaml"
}
}
restart the visual studio code to make the changes effective.