Kubernetes Tutorials

 




1. Prerequisites

  • Linux System: This guide assumes you’re using a Linux distribution (Ubuntu/Debian examples are provided).
  • Docker: Minikube uses a container runtime. Docker is a common choice—ensure it’s installed and running.
  • Internet Connection: To download packages and container images.

2. Installing Kubernetes Tools

2.1 Install kubectl (Kubernetes CLI)

  1. Download kubectl:
    Use the following commands to download, make executable, and move kubectl to your PATH:

    bash
    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/
  2. Verify Installation:
    Check the client version:

    bash
    kubectl version --client

2.2 Install Minikube (Local Kubernetes Cluster)

  1. Download Minikube:
    Download and install the latest Minikube binary:

    bash
    curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 chmod +x minikube sudo mv minikube /usr/local/bin/
  2. Verify Installation:
    Check the Minikube version:

    bash
    minikube version

3. Starting Your Kubernetes Cluster with Minikube

  1. Start the Cluster:
    Launch Minikube which creates a single-node Kubernetes cluster:

    bash
    minikube start

    This command downloads necessary components and configures a local cluster.

  2. Verify Cluster Status:
    Check the cluster information and list nodes:

    bash
    kubectl cluster-info kubectl get nodes

4. Deploying an Application

4.1 Create a Deployment

  1. Deploy an Nginx Container:
    Create a deployment that runs an Nginx container:

    bash
    kubectl create deployment nginx-deployment --image=nginx
  2. Check the Deployment and Pods:
    List deployments and pods to verify that your application is running:

    bash
    kubectl get deployments kubectl get pods

4.2 Expose the Deployment as a Service

  1. Create a Service:
    Expose your deployment on port 80 by creating a NodePort service:

    bash
    kubectl expose deployment nginx-deployment --type=NodePort --port=80
  2. List Services:
    Verify the service is running:

    bash
    kubectl get services
  3. Access the Application:
    Use Minikube’s service command to open your application in a browser:

    bash
    minikube service nginx-deployment

5. Managing Your Deployment

5.1 Scaling the Deployment

  1. Scale Up or Down:
    For example, to scale to 3 replicas:
    bash
    kubectl scale deployment/nginx-deployment --replicas=3 kubectl get pods

5.2 Rolling Updates

  1. Update the Deployment Image:
    To update the Nginx image version:

    bash
    kubectl set image deployment/nginx-deployment nginx=nginx:1.19.2
  2. Monitor the Update:
    Check the rollout status:

    bash
    kubectl rollout status deployment/nginx-deployment

6. Using YAML Files for Declarative Configuration

6.1 Create a Deployment YAML File

  1. Write a YAML File (deployment.yaml):

    yaml
    apiVersion: apps/v1 kind: Deployment metadata: name: my-deployment spec: replicas: 2 selector: matchLabels: app: myapp template: metadata: labels: app: myapp spec: containers: - name: my-container image: nginx:latest ports: - containerPort: 80
  2. Apply the Configuration:
    Create the deployment using the YAML file:

    bash
    kubectl apply -f deployment.yaml

7. Monitoring and Debugging

7.1 Viewing Logs

  • Check Pod Logs:
    Replace <pod-name> with the actual pod name:
    bash
    kubectl logs <pod-name>

7.2 Inspecting Resources

  • Describe a Pod:
    Get detailed information on a pod:

    bash
    kubectl describe pod <pod-name>
  • List All Resources:
    See everything running in your namespace:

    bash
    kubectl get all

8. Cleaning Up

8.1 Deleting Resources

  • Remove a Deployment and Service:
    Delete the Nginx deployment and its associated service:

    bash
    kubectl delete deployment nginx-deployment kubectl delete service nginx-deployment
  • Stop the Minikube Cluster:
    When finished, stop your local Kubernetes cluster:

    bash
    minikube stop

Final Thoughts

  • Explore More:
    Kubernetes offers many advanced features—look into ConfigMaps, Secrets, StatefulSets, DaemonSets, Ingress Controllers, and more.

  • Documentation:
    For a deeper dive into Kubernetes concepts and commands, visit the official Kubernetes documentation.

This tutorial covers the basics to help you get started with Kubernetes—from installation using Minikube and kubectl to deploying, scaling, and managing applications. Happy clustering!

Comments