Kubernetes for beginners

Kubernetes simplifies running containerized applications by automating tasks like deployment, scaling, and monitoring. It’s an open-source platform that works across environments – on-premises, cloud, or hybrid. Here’s a quick breakdown of what you’ll learn:

  • Why Kubernetes?
    Automates operations, optimizes resource use, ensures high availability, and supports flexible deployments.
  • Core Components:
    Control Plane (e.g., kube-apiserver, etcd) and Worker Nodes (e.g., kubelet, kube-proxy).
  • Getting Started:
    Use kubectl to manage clusters and Minikube to create a local test cluster.
  • Key Concepts:
    Understand Pods, Deployments for app lifecycles, and Services for networking.
  • Basic Commands:
    Learn to deploy, update, troubleshoot, and monitor applications.

This guide is perfect for beginners looking to set up their first Kubernetes cluster and manage applications effectively.

Kubernetes Tutorial for Beginners [FULL COURSE in 4 Hours]

Starting Your First Cluster

Follow these steps to set up your first Kubernetes cluster.

Setting Up kubectl

kubectl

The kubectl command-line tool is your main way to interact with Kubernetes clusters. Here’s how to get started:

  • Install kubectl based on your operating system:
    • macOS: Run brew install kubectl
    • Linux: Use the following commands:
      curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl" chmod +x kubectl sudo mv kubectl /usr/local/bin/ 
    • Windows: Run choco install kubernetes-cli
  • Verify the installation by running:
    kubectl version --client 
  • Configure kubectl: Ensure you have a configuration file in the $HOME/.kube directory. This file includes cluster connection details and authentication information [2].

Once kubectl is installed and set up, you’re ready to create your local test cluster.

Building a Test Cluster

Minikube is a tool that lets you create a local Kubernetes environment for testing and learning. Here’s how to set it up:

1. System Requirements

Make sure your system meets these requirements:

  • Multi-core processor
  • 8GB RAM
  • 20GB free disk space
  • Virtualization support enabled in BIOS/UEFI

2. Install Minikube

Choose the installation method for your operating system:

# macOS brew install minikube  # Linux curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 chmod +x minikube sudo mv minikube /usr/local/bin/  # Windows choco install minikube 

3. Start Your Cluster

Run the following command to start Minikube:

minikube start 

Once your cluster is running, you can check its status using the commands below.

Testing Your Cluster

Confirm that your cluster is working by performing these steps:

  • Check the status:
    minikube status kubectl cluster-info 
  • Deploy a test application:
    kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10 kubectl expose deployment hello-minikube --type=NodePort --port=8080 
  • Access the application:
    minikube service hello-minikube --url 

"kubectl is a command-line tool for communicating with a Kubernetes cluster’s control plane via the Kubernetes API" [2].

If you can access the test application, you’ve successfully set up your first Kubernetes cluster. This setup is now ready for you to explore Kubernetes features and experiment with deploying applications.

Basic Kubernetes Concepts

Understanding Pods

Pods are the smallest units in Kubernetes that can be deployed. They host one or more containers, all of which share the same network and storage resources. Each Pod is assigned a unique IP address, allowing containers within the Pod to communicate using localhost. While Pods can hold multiple containers, it’s more common to deploy a single container per Pod. Think of Pods as a logical host for your application components – they share the same lifecycle, network, and storage. However, Pods are temporary by nature and aren’t designed to last forever.

Let’s move on to how Kubernetes Deployments handle application lifecycles.

Managing Application Lifecycles

Deployments in Kubernetes are responsible for scaling applications, handling updates, and rolling back changes when needed. During updates, Deployments perform rolling updates to ensure your applications remain available. This process gradually replaces old Pods with new ones, avoiding downtime.

Next, let’s look at how Services provide reliable networking and handle data.

Working with Services and Data

Services in Kubernetes act as stable networking endpoints for Pods. They ensure that applications can consistently communicate, even as Pods are replaced or scaled. For managing application configurations and sensitive data, Kubernetes uses ConfigMaps and Secrets.

Resource Type Purpose Common Uses Size Limit
ConfigMaps Store non-sensitive configuration Environment variables, config files 1 MiB [3]
Secrets Store sensitive information API keys, passwords, certificates 1 MiB

"A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable." – Kubernetes Documentation [3]

You can use ConfigMaps and Secrets in two ways: mount them as files or expose them as environment variables. For sensitive data, Secrets add an extra layer of security by encoding the information in base64.

sbb-itb-5c61b71

Running Your First Application

Creating YAML Files

A YAML file tells Kubernetes how to manage and run your application.

Here’s an example Deployment YAML:

apiVersion: apps/v1 kind: Deployment metadata:   name: nginx-deployment spec:   replicas: 2   selector:     matchLabels:       app: nginx   template:     metadata:       labels:         app: nginx     spec:       containers:       - name: nginx         image: nginx:1.14.2         ports:         - containerPort: 80 

Key parts of this file:

  • apiVersion and kind: Define the type of resource.
  • metadata: Contains the name and other identifiers.
  • spec: Specifies the desired state, like the number of replicas and container details.

Once your YAML file is ready, you can deploy your application using Kubernetes commands.

Using kubectl Commands

To deploy your application, use the following command:

kubectl apply -f your-deployment.yaml 

After applying the file, check the status of your deployment and pods:

kubectl get deployments kubectl get pods 

"A Deployment creates and updates instances of your application."

To access your application, you can create a proxy to the cluster’s internal network:

kubectl proxy 

With the application running, updating it is straightforward.

Updating Your Application

Kubernetes allows you to update your application without downtime by performing rolling updates. Here are some common update actions:

Update Action Command Purpose
Update Image kubectl set image deployment/nginx-deployment nginx=nginx:1.15.0 Change the container image.
Scale Pods kubectl scale deployment/nginx-deployment --replicas=4 Adjust the number of pods.
Rollback kubectl rollout undo deployment/nginx-deployment Revert to a previous version.

Kubernetes handles the update process by:

  • Creating new pods with the updated configuration.
  • Ensuring the new pods are healthy.
  • Gradually removing old pods.
  • Keeping the application available throughout the process.

Basic kubectl Commands

Checking System Status

To keep an eye on your cluster’s health, use the kubectl get command:

# List all pods in the current namespace kubectl get pods  # Get details about nodes kubectl get nodes -o wide  # Check services kubectl get services 

For more specific details about a resource, try:

kubectl describe pod <pod-name> 

Need to monitor resource usage? Use:

# View node resource usage kubectl top nodes  # View pod resource usage kubectl top pods 

If something seems off, move on to troubleshooting with the commands below.

Finding and Fixing Problems

Here are some handy commands for troubleshooting:

Command Purpose When to Use
kubectl logs <pod-name> View container logs Investigate application errors
kubectl get events --all-namespaces Show all cluster events Spot system-wide issues
kubectl exec -it <pod-name> -- /bin/bash Access the pod’s shell Debug container problems
kubectl port-forward <pod-name> 8080:8080 Forward a local port to the pod Test services locally

To confirm the API server is healthy:

kubectl get --raw=/healthz 

If the response is "ok", your API server is functioning properly [4].

These commands are essential for diagnosing and fixing common cluster issues.

Quick Command Reference

Here’s a quick cheat sheet for essential kubectl commands:

Category Command What It Does
Creation kubectl create -f <file>.yaml Create resources using a YAML file
Updates kubectl apply -f <file>.yaml Update resources with changes in YAML
Deletion kubectl delete -f <file>.yaml Delete resources from a YAML file
Scaling kubectl scale deployment/<name> --replicas=3 Adjust the number of pods
Editing kubectl edit deployment <name> Modify live resources
Version kubectl version Check the kubectl client and server versions

For pods with multiple containers, you can specify which container’s logs to view:

kubectl logs <pod-name> -c <container-name> 

Conclusion

What You Learned

This guide introduced you to the basics of Kubernetes. You now understand how it simplifies the deployment, scaling, and management of containerized applications[1]. You’re also familiar with key components like pods, nodes, clusters, and services.

Here’s what you’ve accomplished:

  • Set up a local cluster using Minikube and kubectl
  • Managed application deployments effectively
  • Performed cluster operations with kubectl
  • Worked with ConfigMaps and Secrets
  • Optimized resources for better performance

This is just the beginning. Keep practicing and dive into more advanced topics to expand your knowledge.

Moving Forward

Take your Kubernetes skills to the next level with these steps:

  • Explore Advanced Features
    Learn about tools like Kubernetes Operators and Helm charts. These can help you handle more complex tasks and automate workflows.
  • Join the Community
    Participate in the Kubernetes Slack community to stay updated on trends and connect with experienced users who can offer guidance.
  • Practice, Practice, Practice
    Experiment with deployment strategies, service meshes for networking, monitoring and logging setups, and GitOps workflows for continuous delivery.

Make the official Kubernetes documentation your go-to resource for reliable and updated technical details[1]. As you progress, focus on advanced areas like Role-Based Access Control (RBAC) and network policies. These are crucial for building secure, production-ready environments.

Kubernetes is a dynamic system that rewards consistent learning and practice. Each project you tackle will sharpen your container orchestration skills.

Related posts

Categories