Most content in this document comes from "Learn Kubernetes in a Month of Lunches".
Kubernetes is a platform for running containers. The two core concepts in Kubernetes are …
A cluster is a single logical unit composed of many individual servers, called nodes in Kubernetes. Some nodes run the Kubernetes API, whereas others run application workloads, all in containers.
The way to interact with the Kubernetes cluster is through YAML files. You write your app specs in YAML files and send them to the Kubernetes API. Kubernetes compare them to the current app state/spec running in the cluster, and makes any changes to get to the desired state.
Important concepts in Kubernetes.
application manifests
: YAML filesResources
: components that go into shipping the app Services
: Kubernetes objects for managing network accessPods
: a unit of compute that runs one or more containers, has its own virtual IP addressDeployments
: manage podsReplicaSets
ConfigMaps
Secrets
Kubernetes does not run containers. Pods are allocated to one node, and it’s that node’s responsibility to manage the Pod and its containers. It does that by working with the container runtime using a known API called the Container Runtime Interface(CRI).
If a node goes offline, the Pod is lost, and Kubernetes does not replace it. In this case, the Deployment
, one of many controllers
in Kubernetes, works with the Kubernetes API to watch the current state of the system, compares that to the desired state of its resources, and makes any changes necessary.
You can create Deployment resources with kubectl, specifying the container image you want to run and any other configuration for the Pod. Kubernetes creates the Deployment, and the Deployment creates the Pod.
# create a Deployment called "hello-kiamol-2", running the same web app:
kubectl create deployment hello-kiamol-2 --image=kiamol/ch02-hello-kiamol
# list all the Pods:
kubectl get pods
The Deployment specification described the Pod you wanted, and the Deployment created that Pod by interacting with the Kubernetes API.
The Deployment adds labels to Pods it manages. In other words, a Deployment and related Pods share the same key-value label(e.g. app=x).
Below YAML script is the simplest app manifest you can write.
pod.yaml
Kubectl doesn’t even need a local copy of a manifest file. It can read the contents from any public URL. Deploy the same Pod definition direct from the file on GitHub.
The Deployment definition is a composite that includes the Pod spec.
You can run commands inside containers with kubectl and connect a terminal session, so you can connect into a Pod’s container as though you were connecting to a remote machine.
For troubleshooting, the simple option is to read the application logs by accessing to the container runtime with kubectl logs
.
You can run commands in Pods that are managed by a Deployment without knowing the Pod name, and you can view the logs of all Pods that match a label selector.
Kubectl lets you copy files between your local machine and containers in Pods. Create a temporary directory on your machine, and copy a file into it from the Pod container.
If you try to delete all Pods by using kubectl delete pods --all
, you can see that Pods created by Deployment controllers are deleted and replaced with new ones.
If you want to delete a resource that is managed by a controller, you need to delete the controller instead. Controllers clean up their resources when they are deleted, so removing a Deployment is like a cascading delete that removes all the Deployment’s Pods, too.