In this RX-M Cloud Native Short Take, instructor and industry-expert, Jerry Lozano discusses the Kubernetes pod basics module. Jerry explains what a pod is and how it is created and controlled on Kubernetes. If you’re new to Kubernetes, this module is a must-watch, as it covers the basics of pod creation using declarative and imperative methods with the Kubernetes CLI: kubectl.
Video Transcript
Welcome everyone, my name is Jerry Lozano. I’m an instructor for RX-M and in this short take we’re going to look at the module called Kubernetes Pod Basics. In this module we talk about exactly what a pod is as well as how pods are created and controlled on Kubernetes. In this chapter we do several demos and we show you a couple of them in the video.
In this first demonstration we explain that a pod in Kubernetes consists of one or more containers. Kubernetes schedules and launches pods; in general one Kubernetes pod contains one container. We do our demonstration by launching our first pod and we do so declaratively; we just issue the command from the command line. We use the “kubectl” command to run a new pod based on the image httpd (aka the apache image). We give it an appropriate pod name like “apache”.
When the pod is created we can use the “kubectl” command to get information about all the pods running in our Kubernetes namespace by just typing “kubectl get pods”. In this case we see that the pod called “apache” is up and running. If we do the same command but this time use the “-o wide” switch we get more information about each pod running on the system including each pod’s ip address.
At this point is apache actually running? We can find out by using “curl” on the pod’s IP address, in this case: 10.32.0.4 Indeed it works! The apache pod replies with “it works!” in standard html tag format.
How would I stop the apache pod? We use “kubectl delete pod apache” and it stops. At this point if i go back and list the pods that are running in my namespace I get none. Creating pods is a simple task in Kubernetes. Again this was done imperatively; I typed the command in on the console. This is probably not going to work well for a production environment where we literally can have hundreds of pods running at one time–starting up dynamically being shut down dynamically as we scale up and down a large application.
The other way to create a pod is declaratively; to write a specification file (“spec” file) that describes exactly what the pod is, what image it’s based on, plus any other important information such as tagging it with labels or specifying information like persistent volumes that it wants to attach to. The easy way to create a spec file for a resource like a like a pod is to use the “kubectl” command with the “–dry-run” switch. This flag allows us to “pretend” that we are creating a pod. Using the same command that we did before but this time we say that we want to output our information in yaml format (-o yaml). That is the typical format that we use to write a spec file. Then we use the “–dry-run” switch and we specify that we want to create the file for a client by redirecting the output of the command to some file like “myapachepod.yaml”.
Looking at the file that was created reveals a typical yaml specification for a Kubernetes resource. What kind of resource would get created with this file? A pod! It has labels like run=apache. The name of the pod will be “apache”. The specification for the pod contains a list of containers that will be launched with this pod. Our containers list has only one container in the yaml specification, the image is httpd, the name of the container is “apache” just like the pod.
If we check how many pods are running right now as a result of using that command? Issuing “kubectl get pods” shows none. We have successfully used dry-run to create the spec only!
To use the specification we use the “kubectl” command with the “apply” subcommand and we specify the name of the file that we wish to use for our spec file with the “-f” flag. Now the pod apache gets created and now “get pods” shows that that pod is up and running!
Pod creation is just one of the things you’ll learn in the “Pod Basics” module when you attend one of our Kubernetes courses! You can go to the RX-M custom courseware builder, find the “Pod Basics” module under the “Kubernetes” category and add it to a custom course along with a number of other Kubernetes modules.