How To Use Kubernetes? Step By Step Guide For Beginners

SSupported by cloud service provider DigitalOcean – Try DigitalOcean now and receive a $200 when you create a new account!

Kubernetes is a portable, extensible, open source platform for managing containerized workloads and services. It facilitates declarative configuration and automation, with a large ecosystem of tools and support. Originating from Google in 2014, it builds on over 15 years of experience in running production workloads. It evolved from traditional server deployments to virtualization and then containers, offering benefits like agile development, portability across clouds, resource isolation, and high efficiency.

Key reasons to use Kubernetes include container orchestration to manage deployments without downtime, automatic scaling, self healing (restarting failed containers), load balancing, storage orchestration, and secret management. It’s extensible, supporting diverse workloads if they run in containers.

Before diving in, ensure you have a basic understanding of containers (e.g., Docker). You’ll need a computer with virtualization support (for Minikube). Download Kubernetes tools like kubectl, the command line interface for interacting with clusters. Choose an installation based on your needs: local for learning, managed services for production.

Quick Start Setup:

  1. Install Minikube: Follow platform specific instructions (Linux, macOS, Windows) from the official site.
  2. Start a cluster: Run minikube start to create a single node cluster.
  3. Verify: Use kubectl cluster-info to check the cluster is running.

This sets up a local environment to experiment safely.

Basic Workflow Overview:

  • Create a Cluster: Use Minikube for a simple setup.
  • Deploy an App: Use kubectl to create a Deployment.
  • Explore: Inspect Pods, view logs.
  • Expose: Create a Service to access the app.
  • Scale: Adjust replicas for load handling.
  • Update: Perform rolling updates without downtime.

Kubernetes, often referred to as K8s, is an open source platform that automates the deployment, scaling, and management of containerized applications. It originated from Google’s internal systems and was open sourced in 2014, combining best practices from production environments with community contributions.

Understanding Kubernetes Fundamentals

Before hands-on steps, grasp these core ideas:

  • Containers vs. Kubernetes: Containers (e.g., via Docker) package apps with dependencies. Kubernetes orchestrates them across clusters, handling scheduling, scaling, and failover.
  • Cluster: A set of nodes (machines) working together. Includes a control plane (manages the cluster) and worker nodes (run apps).
  • Pod: The smallest unit, often containing one or more containers sharing storage and network. Pods are ephemeral, Kubernetes recreates them if they fail.
  • Deployment: Manages Pods, ensuring a specified number of replicas run, with support for updates and rollbacks.
  • Service: Abstracts access to Pods, providing load balancing and discovery. Types include ClusterIP (internal), NodePort (external via node ports), and LoadBalancer (cloud provided external IP).
  • Scaling and Updates: Horizontal scaling adds/removes replicas; rolling updates replace instances gradually for zero downtime.
  • Benefits: Includes self healing, automated rollouts, resource optimization, and portability. However, it introduces complexity, so start small.
Key Concept Description Example Use Case
Pod Group of containers sharing resources Running a web app and its sidecar logger
Deployment Manages replica Pods and updates Deploying a stateless API with 3 replicas
Service Exposes Pods to network Load balancing traffic to a microservice
Node Worker machine in the cluster Hosting multiple Pods with kubelet agent
Namespace Virtual cluster for isolation Separating dev and prod environments

These concepts build on container evolution from VMs, offering lighter isolation and better efficiency.

Step 1: Setting Up Your Environment

For beginners, use Minikube to run a local cluster. This avoids cloud costs and complexity.

Prerequisites:

Installation Steps:

  1. Download and install Minikube for your OS.
  2. Verify: Run minikube version.
  3. Start the cluster: minikube start. This downloads images and sets up a VM-based cluster. Use –driver=virtualbox if needed.
  4. Verify setup: kubectl cluster-info (shows master and services running). kubectl get nodes (lists the Minikube node).
  5. Enable addons if desired: minikube addons enable dashboard for a UI, then minikube dashboard to open it in a browser.

Troubleshooting: If issues arise, check logs with minikube logs. Ensure Docker is not conflicting if installed separately.

Step 2: Creating a Kubernetes Cluster

A cluster coordinates nodes to run apps. With Minikube, it’s single node for learning.

  1. As above, minikube start.
  2. Explore: kubectl get all (lists resources). The cluster uses kubelet for node management and a container runtime like Docker.
  3. For multi node (advanced): Use kubeadm on VMs, but stick to Minikube initially.

Kubernetes abstracts hardware, allowing apps to run without tying to specific machines.

Step 3: Deploying Your First Application

Deployments create and manage Pods. Use a sample like echoserver.

  1. Create Deployment: kubectl create deployment hello-minikube –image=k8s.gcr.io/echoserver:1.10. This specifies the image and defaults to 1 replica.
  2. View: kubectl get deployments (shows ready replicas). kubectl describe deployment hello-minikube (details selectors and events).
  3. Get Pods: kubectl get pods (lists running Pods).

This creates a ReplicaSet to maintain the desired state. For custom apps, build a Docker image first and push to a registry.

Step 4: Exploring Your Application

Inspect to troubleshoot.

  1. List Pods: kubectl get pods.
  2. View logs: kubectl logs <pod-name>. Use -f for streaming, -c <container> if multi-container.
  3. Exec into Pod: kubectl exec -it <pod-name> — /bin/sh (or bash). Run commands inside, e.g., ls.
  4. Describe Pod: kubectl describe pod <pod-name> (shows events, conditions).
  5. Nodes: kubectl get nodes. kubectl describe node <node-name> for resource usage.

Pods share IP and storage; use for tightly coupled containers.

Step 5: Exposing Your Application Publicly

Services make apps accessible.

  1. Create Service: kubectl expose deployment hello-minikube –type=LoadBalancer –port=8080. This targets port 8080 on Pods.
  2. View: kubectl get services.
  3. Access locally: minikube service hello-minikube (opens browser with URL).
  4. For NodePort: Use –type=NodePort, access via minikube IP and assigned port.

Services use labels/selectors for routing. In clouds, LoadBalancer provisions external IPs.

Step 6: Scaling Your Application

Adjust replicas for load.

  1. Scale up: kubectl scale deployment hello-minikube –replicas=4.
  2. Verify: kubectl get deployments (shows 4/4 ready). kubectl get pods (lists 4 Pods).
  3. Scale down: kubectl scale deployment hello-minikube –replicas=2.
  4. Autoscaling (advanced): Use HorizontalPodAutoscaler with kubectl autoscale deployment hello-minikube –cpu-percent=50 –min=1 –max=10.

Scaling is horizontal; Kubernetes distributes Pods across nodes.

Step 7: Updating Your Application

Rolling updates ensure zero downtime.

  1. Update image: kubectl set image deployment/hello-minikube echoserver=k8s.gcr.io/echoserver:1.4 (change version).
  2. Monitor: kubectl rollout status deployment/hello-minikube.
  3. View history: kubectl rollout history deployment/hello-minikube.
  4. Rollback: kubectl rollout undo deployment/hello-minikube.
  5. Pause/Resume: kubectl rollout pause deployment/hello-minikube, make changes, then kubectl rollout resume.

Updates replace Pods incrementally, configurable via maxSurge/maxUnavailable.

Step 8: Monitoring, Logging, and Best Practices

  • Monitoring: Use kubectl top pods (needs metrics-server). Dashboard for visuals.
  • Logging: Cluster-level via tools like Fluentd; app-level with kubectl logs.
  • Best Practices: Use YAML manifests for declarative configs (e.g., kubectl apply -f file.yaml). Namespaces for organization. Liveness/readiness probes for health checks. Resource requests/limits to avoid overcommitment.
  • Cleanup: kubectl delete service hello-minikube, kubectl delete deployment hello-minikube, minikube stop, minikube delete.

Advanced Topics for Progression

Once comfortable, explore:

  • ConfigMaps/Secrets for configs.
  • StatefulSets for stateful apps (e.g., databases).
  • Ingress for advanced routing.
  • Helm for package management.
  • Production setups with kubeadm or managed services like GKE/AKS/EKS.

Kubernetes adoption is widespread, but success depends on understanding its abstractions. Practice iteratively, and refer to docs for specifics.

Common Pitfalls and Troubleshooting

  • Pod crashes: Check logs/events with kubectl describe.
  • Resource issues: Set requests/limits in Deployment YAML.
  • Networking: Ensure Services match Pod ports.
  • Version mismatches: Use compatible kubectl/Minikube versions.
,