Master Kubernetes fundamentals with our comprehensive tutorial. Learn container orchestration, deployment strategies, and cluster management through practical examples.
Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It groups containers into logical units for easy management and discovery.
kubectl versionCheck your Kubernetes client and server version
kubectl cluster-infoDisplay cluster information
kubectl get nodesList all nodes in the cluster
Pods are the smallest deployable units in Kubernetes. A Pod represents a single instance of a running process in your cluster and can contain one or more containers.
kubectl run nginx --image=nginxCreate a simple pod running nginx
kubectl get podsList all pods in the current namespace
kubectl get pods -o wideList pods with additional information (IP, Node, etc.)
kubectl describe pod nginxGet detailed information about a specific pod
kubectl logs nginxView logs from a pod
kubectl exec -it nginx -- /bin/bashExecute interactive shell inside a pod
kubectl delete pod nginxDelete a pod
Deployments provide declarative updates for Pods and ReplicaSets. They manage the rollout of new versions and can automatically roll back if something goes wrong.
kubectl create deployment nginx --image=nginx --replicas=3Create a deployment with 3 replicas
kubectl get deploymentsList all deployments
kubectl scale deployment nginx --replicas=5Scale a deployment to 5 replicas
kubectl set image deployment/nginx nginx=nginx:1.21Update the image of a deployment
kubectl rollout status deployment/nginxCheck the rollout status of a deployment
kubectl rollout undo deployment/nginxRollback to the previous deployment version
Services provide a stable network endpoint to access a set of Pods. They enable load balancing and service discovery within the cluster.
kubectl expose deployment nginx --port=80 --type=ClusterIPExpose a deployment as a ClusterIP service (internal only)
kubectl expose deployment nginx --port=80 --type=NodePortExpose a deployment as a NodePort service (external access)
kubectl expose deployment nginx --port=80 --type=LoadBalancerExpose a deployment as a LoadBalancer service (cloud provider)
kubectl get servicesList all services
kubectl describe service nginxGet detailed information about a service
ConfigMaps store non-confidential configuration data, while Secrets store sensitive information like passwords and API keys.
kubectl create configmap app-config --from-literal=APP_ENV=productionCreate a ConfigMap from literal values
kubectl create configmap app-config --from-file=config.propertiesCreate a ConfigMap from a file
kubectl get configmapsList all ConfigMaps
kubectl create secret generic db-password --from-literal=password=mypasswordCreate a Secret from literal values
kubectl get secretsList all Secrets
Namespaces provide a way to divide cluster resources between multiple users or teams. They're ideal for environments with many users spread across multiple teams or projects.
kubectl get namespacesList all namespaces
kubectl create namespace developmentCreate a new namespace
kubectl get pods --namespace=developmentList pods in a specific namespace
kubectl config set-context --current --namespace=developmentSet default namespace for current context
kubectl delete namespace developmentDelete a namespace (and all resources in it)
Kubernetes resources are typically defined using YAML files. This allows for version control and reproducible deployments.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.21
ports:
- containerPort: 80A basic Deployment manifest apiVersion: API version kind: Resource type metadata: Resource metadata spec: Desired state specification
kubectl apply -f deployment.yamlApply a YAML manifest to create/update resources
kubectl delete -f deployment.yamlDelete resources defined in a YAML file
kubectl get deployment nginx-deployment -o yamlExport a resource as YAML
Kubernetes provides various commands to monitor and debug your applications running in the cluster.
kubectl top nodesDisplay resource usage (CPU/Memory) of nodes
kubectl top podsDisplay resource usage of pods
kubectl get eventsList cluster events
kubectl logs -f nginxStream logs from a pod in real-time -f: Follow log output
kubectl port-forward pod/nginx 8080:80Forward local port 8080 to pod port 80 for testing
kubectl describe pod nginxGet detailed information and events for a pod
kubectl get pods - List podskubectl apply -f - Apply configkubectl logs - View logskubectl describe - Get details