Objects, manifests & kubectl
You never tell Kubernetes "start this container on that server". You describe objects (a Deployment with 3 replicas, a Service in front of it) in YAML files, hand them to the cluster, and let the control plane work out the rest.
The main objects
- Pod: the smallest unit, one (sometimes a few tightly coupled) containers sharing an IP. Pods are disposable and get replaced, never repaired.
- Deployment: manages identical pods for a stateless app: how many replicas, which image, how to roll out updates. You almost never create pods directly; you create a Deployment.
- StatefulSet: like a Deployment, but for databases and other stateful apps, giving each pod a stable name and its own storage.
- Service: a stable name and IP in front of a changing set of pods, load-balancing between them.
- Ingress: routes outside HTTP/HTTPS traffic to Services by hostname or path.
- ConfigMap and Secret: configuration and sensitive values kept outside the image and passed to pods as environment variables or files. Secrets are only base64-encoded by default, not encrypted, so access to them must be restricted.
- Volume / PersistentVolumeClaim: storage that outlives a pod.
- Namespace: a way to divide one cluster into separate areas (per team or per environment).
Manifests: describing what you want
Objects are written as YAML files with four parts: apiVersion, kind, metadata (name, labels) and spec (the desired state). Labels and selectors connect things: the Service finds its pods because they carry the label it selects.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 3 # keep 3 pods running
selector:
matchLabels: {app: web}
template: # the pod blueprint
metadata:
labels: {app: web}
spec:
containers:
- name: web
image: demee/app:1.0
ports: [{containerPort: 3000}]
envFrom: [{configMapRef: {name: web-config}}]
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector: {app: web} # send traffic to pods labelled app=web
ports: [{port: 80, targetPort: 3000}]
kubectl: talking to the cluster
$ kubectl apply -f web.yaml # create/update to match the file
$ kubectl get pods # also: deployments, services, nodes, all
$ kubectl describe pod web-6d9f... # details and recent events (why it's failing)
$ kubectl logs -f web-6d9f... # container output
$ kubectl exec -it web-6d9f... -- sh # shell inside the pod
$ kubectl scale deployment web --replicas=5
$ kubectl set image deployment/web web=demee/app:1.1 # rolling update
$ kubectl rollout undo deployment/web # roll back
$ kubectl delete -f web.yaml
What happens when you apply a file
kubectl apply sends the manifest to the API server, which checks it and saves it in etcd. The Deployment controller notices the new desired state and creates a ReplicaSet, which creates 3 pod objects. The scheduler sees pods with no node yet and assigns each one to a node with room. On those nodes the kubelet sees its new pods, tells the container runtime to pull the image and start the containers, and reports back. Finally the Service's endpoint list is updated so traffic reaches the new pods. Nothing talks to a node directly: every step goes through the API server.