Kubernetes is a powerful container orchestration platform that manages the deployment, scaling, and operation of application containers. Understanding its architecture and components is crucial for effectively utilizing Kubernetes.
A Kubernetes cluster is a set of node machines (physical or virtual) that run containerized applications. A cluster consists of: - Master Node: Controls and manages the Kubernetes cluster. - Worker Nodes: Run the applications and workloads.
A node is a worker machine in Kubernetes, which can be either a physical or virtual machine. Each node contains the services necessary to run pods and is managed by the master node. Nodes can be categorized as: - Master Node: Manages the Kubernetes cluster and its components. - Worker Node: Executes the application containers.
A pod is the smallest deployable unit in Kubernetes, which can contain one or more containers. Pods share the same network namespace and can communicate with each other using localhost. Here’s a simple example of a pod definition:
apiVersion: v1
kind: Pod
metadata:
name: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
A service is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable communication between different parts of your application. Here’s how to define a service:
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
Controllers are control loops that watch the state of your cluster and make or request a changes where needed. Examples include: - ReplicationController: Ensures that a specified number of pod replicas are running. - Deployment: Provides declarative updates for pods and replica sets.
apiVersion: apps/v1
group: apps
kind: Deployment
metadata:
name: my-app-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
Common Mistake: Not defining resource limits can lead to performance issues and resource exhaustion in the cluster.
+------------------+
| Master Node |
| |
| API Server |
| Scheduler |
| Controller |
+------------------+
|
|
+------------------+ +------------------+
| Worker Node 1 | | Worker Node 2 |
| | | |
| Kubelet | | Kubelet |
| Pods | | Pods |
+------------------+ +------------------+
Understanding these components and how they interact is essential for working with Kubernetes effectively. Each component plays a vital role in the orchestration and management of containerized applications.
pod.yaml with the pod definition provided in the lesson.bash
kubectl apply -f pod.yamlbash
kubectl get podsservice.yaml with the service definition provided in the lesson.bash
kubectl apply -f service.yamlbash
kubectl get servicesdeployment.yaml with the deployment definition provided in the lesson.bash
kubectl apply -f deployment.yamlbash
kubectl get deployments