Introduction
In this new article we will start to see Kubernetes concepts. The first one is PODS. We will also see how to create a YAML file. But let’s begin by explaining what is a POD.
What is a POD ?
If you have read my previous article about containers on Dockers, you are already familiar with the definition of containers (here). A POD is the basic execution unit of a Kubernetes application–the smallest and simplest unit in the Kubernetes object model that you create or deploy. A Pod represents processes running on your Cluster.
Even if most of the time, a POD is only one container, you can have several containers in a POD.
- Pods with a single container. It’s the model which is the most common used. It’s basically a wrapper around a single container, and Kubernetes manages the Pods rather than the containers directly.
- Pods with multiple containers. A Pod might encapsulate an application composed of multiple co-located containers that are tightly coupled and need to share resources. These co-located containers might form a single cohesive unit of service–one container serving files from a shared volume to the public, while a separate “sidecar” container refreshes or updates those files. It is then seen by K8s as a single manageable entity.
Networking
Pods have an assigned IP address and share the network namespace. If a Pod has multiple containers, they will be able to discuss using localhost. However, if containers in a Pod need to communicate outside the Pod, network and ports have to be configured.
Storage
A shared storage volume will be used by Pods to allow persistent data in case of a container is restarted.
How to create a POD
Introduction to YAML files
YAML (“YAML Ain’t Markup Language”) is a human-readable data-serialization language. It is commonly used for configuration files with Kubernetes (such as Pods).
It is less complex and ungainly than XML or JSON, but provides similar capabilities. It essentially allows you to provide powerful configuration settings, without having to learn a more complex code type like CSS, JavaScript, and PHP.
Here are the following basic rules YAML has in place to avoid issues related to ambiguity in relation to various languages and editing programs :
- YAML files should end in .yaml whenever possible in Grav.
- YAML is case sensitive.
- YAML does not allow the use of tabs. Spaces are used instead as tabs are not universally supported.
A cheat sheet and full specification are available at the official site : Here
Using kubectl command
Using kubectl commands, it is pretty easy to deploy a POD :
# kubectl run [-i] [--tty] --attach <name> --image=<image>
For example, if you want to deploy a POD called redis, just run the following command :
kubectl run -i redis-pod --image=redis
Or if you want to execute a pod as an interactive shell :
kubectl run -i --tty busybox --image=busybox -- sh
As you can see, it is really closed to Docker commands !
But it’s not the best way to manage your POD. You need YAML configuration files which will help you to create, edit and remove them in a proper way.
Using a configuration YAML file
Here is a first thing you need to add in any kubernetes YAML file (Pods, Services, Deployments etc) before configuring it :
apiVersion: kind: metadata:
spec:
In our example, we will create a POD called myapp-container which will use the image “redis”.
So let’s begin with these 4 sections.
- The api version in our case is “v1”.
- The kind will be Pod, for obvious reasons
- Metadata will contain labels like the name
- Finally, the “spec” is the most important part of our YAML file. We will be able to configure our container with all informations about our POD. It can be its name, the image we want to pull or even the memory
Here is our final YAML file called myapp.yaml :
apiVersion: v1 kind: Pod metadata: name: myapp-pod labels: app: myapp spec: containers: - name: myapp-container image: redis
We can now deploy our POD running the following command :
kubectl create -f myapp.yaml
And that’s all !
You can get more information on your POD running the following command :
kubectl describe pods myapp-pod
Useful Kubectl commands
Here are useful kubectl commands related to Pods you should know :
# Get commands with basic output kubectl get pods --all-namespaces # List all pods in all namespaces kubectl get pods -o wide # List all pods with more details kubectl get pods # List all pods in the namespace kubectl get pod my-pod -o yaml # Get a pod's YAML # Describe commands with verbose output kubectl describe pods my-pod # List pods Sorted by Restart Count kubectl get pods --sort-by='.status.containerStatuses[0].restartCount' # Get all running pods in the namespace kubectl get pods --field-selector=status.phase=Running # Show labels for all pods (or any other Kubernetes object that supports labelling) kubectl get pods --show-labels
Sources
kubernetes.io
learn.getgrav.org
wikipedia.org