Kubernetes Deep-Dive

Kubernetes Fundamentals for Newbies: All You Need to Work with Kubernetes

Damian Igbe, Phd
Sept. 18, 2024, 12:21 p.m.

Subscribe to Newsletter

Be first to know about new blogs, training offers, and company news.

Introduction to Kubernetes

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.

Key Concepts

1. What is Kubernetes?

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.

2. Kubernetes Architecture

  • 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.

3. Kubernetes Components

  • 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.

Basic Commands

1. Setting Up Kubernetes

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).

2. Basic kubectl Commands

  • 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.

Lab Exercises

Exercise 1: Setting Up Kubernetes

You can use  Minikube or Docker for Desktop

  1. Install Minikube:
  2. Start Minikube:
    • Run minikube start to initialize a local Kubernetes cluster.
  3. Verify Installation:
    • Run kubectl cluster-info to ensure your cluster is running.

Exercise 2: Managing Pods

  1. Create a Pod:

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
  1. Apply the configuration with kubectl apply -f pod.yaml.
  2. List and Describe Pods:
    • Run kubectl get pods to list all pods.
    • Use kubectl describe pod my-pod to get detailed information about the pod.
  3. View Logs:
    • Run kubectl logs my-pod to view the logs of the container.

Exercise 3: Creating and Managing Deployments

  1. 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

  1. Apply the configuration with kubectl apply -f deployment.yaml.
  2. Scale the Deployment:
    • Scale the deployment to 5 replicas with kubectl scale deployment my-deployment --replicas=5.
  3. Check Deployment Status:
    • Run kubectl get deployments to list deployments.
    • Use kubectl describe deployment my-deployment for detailed information.

Exercise 4: Exposing Services

  1. Create a Service:

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
  1. Apply the configuration with kubectl apply -f service.yaml.
  2. Access the Service:
    • Run kubectl get services to list services and find the external IP or URL.

Exercise 5: Managing Namespaces

  1. Create a Namespace:

Create a YAML file named namespace.yaml with the following content:

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace
  1. Apply the configuration with kubectl apply -f namespace.yaml.
  2. Use the Namespace:
    • Switch to the new namespace with kubectl config set-context --current --namespace=my-namespace.
  3. List Resources in the Namespace:
    • Create resources in the new namespace and list them using kubectl get pods -n my-namespace.

 

Conclusion

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.

Zero-to-Hero Program: We Train and Mentor you to land your first Tech role