Kubernetes (K8s) is an open-source container orchestration platform designed to automate the deployment, scaling, and management of containerized applications. It provides a robust framework for managing containerized applications across a cluster of machines. This guide will cover the basics of Kubernetes and include hands-on lab exercises to help you practice.
Kubernetes is a container orchestration platform that automates various aspects of application deployment, scaling, and management. It provides tools for managing containers in a distributed environment, ensuring that your applications are highly available and scalable.
Master Node
:
Manages the Kubernetes cluster and its components.Worker Nodes
: Run the application containers.Pods
:
The smallest deployable units in Kubernetes, encapsulating one or more containers.Services
:
Abstracts access to a set of pods, providing load balancing and service discovery.Deployments
: Manages the deployment and scaling of pods.Namespaces
:
Provides a way to divide cluster resources between multiple users.kubectl
:
Command-line tool to interact with the Kubernetes cluster.kube-apiserver
:
API server for communication within the cluster.kube-scheduler
:
Schedules pods to worker nodes.kube-controller-manager
: Manages controllers for ensuring the desired state of the cluster.etcd
: Key-value store for storing cluster configuration data.You can set up a local Kubernetes environment using tools like Minikube, kind (Kubernetes IN Docker), or by using managed Kubernetes services like Google Kubernetes Engine (GKE), Amazon EKS, or Azure Kubernetes Service (AKS).
kubectl version
: Check the Kubernetes version.kubectl cluster-info
:
Display cluster information.kubectl get nodes
:
List all nodes in the cluster.kubectl get pods
:
List all pods in the current namespace.kubectl describe <resource>
: Show detailed information about a resource.kubectl logs <pod>
:
View logs of a pod.kubectl apply -f <file>
: Apply a configuration file to create or update resources.kubectl delete -f <file>
: Delete resources defined in a configuration file.You can use Minikube or Docker for Desktop
Create a YAML file named pod.yaml with the following content:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
Apply the configuration with kubectl apply -f pod.yaml.
kubectl get pods
to list all pods.kubectl describe pod my-pod
to get detailed information about the pod.kubectl logs my-pod
to view the logs of the container.Create a Deployment:
Create a YAML file named deployment.yaml with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
kubectl apply -f deployment.yaml.
kubectl scale deployment my-deployment --replicas=5
.kubectl get deployments
to list deployments.kubectl describe deployment my-deployment
for detailed information.Create a YAML file named service.yaml with the following content:
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
kubectl apply -f service.yaml
.kubectl get services
to list services and find the external IP or URL.Create a YAML file named namespace.yaml with the following content:
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
kubectl apply -f namespace.yaml.
kubectl config set-context --current --namespace=my-namespace
.kubectl get pods -n my-namespace
.By understanding these Kubernetes fundamentals and completing the lab exercises, you’ll gain a solid foundation in managing containerized applications with Kubernetes. Practice these commands and concepts regularly to build your expertise and effectively manage your Kubernetes clusters.