What is Kubernetes?
Kubernetes Architecture
Master Node:
- API Server: The central management point of the cluster, exposing the Kubernetes API for user and component interaction.
- etcd: A distributed key-value store that maintains the cluster's configuration data and overall state.
- Controller Manager: This includes various controllers that monitor the cluster state through the API server and make adjustments to maintain the desired state, such as ensuring the correct number of pod replicas are running.
- Scheduler: Assigns new pods to nodes based on resource requirements and availability, distributing the workload across the worker nodes.
Worker Nodes
- Kubelet: TAn agent that runs on each Worker Node, communicating with the Master Node and ensuring the specified containers are running and healthy.
- Container Runtime: Supports multiple runtimes, such as Docker or containerd, responsible for pulling container images and running containers.
- Kube Proxy: Manages network communication within the cluster, handling network routing for services and load balancing.
How they Interact?:
Main K8S Components
Nodes and Pods
Nodes:
Example:
- Node A,
- Node B,
- Node C These nodes host and run containerized applications.
Pods:
Example:
Yml
Explanation:
- The pod is named webapp-with-db
- The pod has two containers: webapp and database.
- The webapp container uses the nginx:latest image, which is a popular web server and reverse proxy. We expose port 80 to access the web application.
- The database container uses the mongo:latest image, which is widely used NoSQL database.
- Both containers share teh same network namespace and can communicate with each other over localhost.
Why Pods and Not Just containers?
- Grouping Containers: Simplifies scheduling, scaling, and management by grouping related containers.
- Shared Resources: Containers within a pod share the same network namespace and can share volumes for easier communication and data sharing.
- Atomic Unit: Pods are an atomic unit of deployment, facilitating easier scaling and management.
- Scheduling and Affinity: Kubernetes schedules pods, not individual containers, ensuring related containers are co-located on the same node.
Kubernetes concepts
Service
Yml
Explanation:
- We've added a new YAML block to create a Service named webapp-service.
- The selector field in the Service ensures t hat it selects pods with the label app:my-webapp. Since our pod has this label, the service will target it.
- The service expose port 80, which matches the port exposed by the webapp container in the pod.
- The targetPort specifies the port on the pod that teh service forwards traffic to. In this case, it is set to 80 to match the containerPort of the webapp container.
Ingress:
Yml
Explanation:
- We've added a new YAML block to create an Ingress name webapp-ingress.
- The host field specifies the domain name under which the web application will be accessible from outside the cluster. Replace mywebapp.alidev.net with you actual domain name or IP address.
- The paths section defines the routing rules. In this example, any incoming requests with a path prefix of / will be forwarded to the webapp-service.
- The backend specifies the target service to which the traffic should be forwarded. In our case, it's the webapp-service we defined earlier