Pods

Creating a POD using YAML based configuration file

YAML in kubernetes

Kubernetes uses YAML files as inputs to create objects such as PODs, replicas, deployments, services etc,.

Kubernetes definition file always contain four top level fields (properties)

  • apiVersion - version of kubernetes API we are using to create the objects.
kind version
POD v1
Service v1
ReplicaSet apps/v1
Deployment apps/v1
  • kind - kind refers to the type of object which we are going to create.
  • metadata - data about the object like it's name, labels etc,. metadata will be in the form a dictionary, name, labels are children of metadata. Name is a string whereas label is a dictinary.
    Under metadata we can only specify name or lables are anything else that kubernetes expects under metadata, we can add any properties as we wish under metadata.
    However, under labels we can have any key value pairs as we see them fit.
  • spec - depending on the object which we are going to create, this is where we would provide additional information to kubernetes pertaining to that object. This will be different for different objects.
    spec is a dictionary.

kubectl create -f <yml filename> or
kubectl apply -f <yml filename>
This command is used to create the object (based on yml file) in kubernetes cluster.

pod-definition.yml

apiVersion: v1
kind: Pod
metadata: 
  name: myapp-pod
  labels: 
      app: myapp
      type: front-end
      # to differentiate the pods later if needed
spec:
  containers: 
  # container is a list/array.
  # PODs can have multiple containers within them.
    - name: nginx-container
      image: nginx

kubectl create -f pod.definition.yml
With this command, kubernetes create the pod.

Commands

kubectl get pods
To see list of pods available.

kubectl describe pod <pod name>
To see detailed information about the pod.
kubectl describe pod myapp-pod

kubectl pod myapp-pod -o wide
To get the details about the pod

kubectl delete pod myapp-pod
To delete the pod named myapp-pod