Kubernetes Advanced Concepts: A Beginner’s Guide

Kubernetes Advanced Concepts: A Beginner’s Guide

Are you starting your Kubernetes journey and aiming to understand the advanced features that make Kubernetes powerful? This blog is a beginner-friendly guide to help you grasp essential Kubernetes concepts that are crucial for anyone stepping into the DevOps world. Let’s dive in!


1. Namespaces

What Are Namespaces?

Namespaces provide a way to group and isolate resources in a Kubernetes cluster. They are useful for dividing cluster resources among multiple users or teams.

How to Create a Namespace

You can create a namespace using a YAML manifest file. Here's an example:

kind: Namespace
apiVersion: v1
metadata:
  name: nginx
  • Save this file as namespace.yml.

  • Apply it using:

      kubectl apply -f namespace.yml
    

To view all namespaces:

kubectl get ns

Deleting a Namespace

To delete a namespace, use:

kubectl delete -f namespace.yml


2. Pods

What Are Pods?

Pods are the smallest deployable units in Kubernetes. Each pod encapsulates one or more containers, storage resources, and options that govern how the container(s) should run.

Creating a Pod

Here’s a sample YAML file for creating a pod:

kind: Pod
apiVersion: v1
metadata:
  name: nginx-pod
  namespace: nginx
spec:
  containers:
    - name: nginx
      image: nginx
      ports:
        - containerPort: 80
  • Save the file as pod.yml.

  • Perform a dry run to validate the configuration:

      kubectl apply -f pod.yml --dry-run=client
    

  • Apply it:

      kubectl apply -f pod.yml
    

To list pods in a specific namespace:

kubectl get pods -n nginx


3. Deployments

What Are Deployments?

Deployments ensure a desired state for your pods and manage their lifecycle. They allow for updates, scaling, and self-healing.

Deployment YAML Example

Here’s how you can create a deployment:

kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-deployment
  namespace: nginx
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx
          ports:
            - containerPort: 80
  • Save as deployment.yml.

  • Validate using a dry run:

      kubectl apply -f deployment.yml --dry-run=client
    

  • Apply it:

      kubectl apply -f deployment.yml
    

To view deployments:

kubectl get deployment -n nginx

Scaling a Deployment

You can scale deployments dynamically:

kubectl scale deployment nginx-deployment -n nginx --replicas=5

4. ReplicaSets

What Are ReplicaSets?

ReplicaSets maintain a stable set of replica pods at any given time. They ensure high availability by creating new pods when others fail.

Note: Deployments are preferred over ReplicaSets because they offer additional features like rolling updates.


5. Services

What Are Services?

Services expose applications running in pods to the network, providing a single stable endpoint for your workloads.

Types of Services

  1. ClusterIP: Exposes the service within the cluster.

  2. NodePort: Exposes the service on a static port on each node.

  3. LoadBalancer: Exposes the service externally using a load balancer.

  4. ExternalName: Maps the service to an external name.

Example Service YAML

kind: Service
apiVersion: v1
metadata:
  name: nginx-service
  namespace: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30001
  type: NodePort
  • Save as service.yml.

  • Apply it:

      kubectl apply -f service.yml
    

To view services:

kubectl get svc -n nginx

Accessing Services

To expose the service externally, you can use:

kubectl port-forward service/nginx-service -n nginx 80:80 --address=0.0.0.0 &


Bonus: Running a Custom App 🚀

Deploy your Notes Application with Kubernetes using the image amitabhdevops/cicd-note-app.

Steps to Deploy the App

  1. Create Configuration Files

    • namespace.yml

        kind: Namespace
        apiVersion: v1
        metadata:
            name: nodejs
      
    • deployment.yml

        kind: Deployment
        apiVersion: apps/v1
        metadata:
          name: nodejs-deployment
          namespace: nodejs
          labels:
            app: nodejs
      
        spec:
          replicas: 3
          selector:
            matchLabels:
              app: nodejs
          template:
            metadata:
              name: nodejs-pod
              namespace : nodejs
              labels:
                app: nodejs
            spec :
              containers:
                - name: nodejs
                  image: amitabhdevops/cicd-note-app:latest
                  ports:
                    - containerPort: 8000
      
    • service.yml

        kind: Service
        apiVersion: v1
        metadata:
          name: nodejs-service
          namespace: nodejs
      
        spec:
          selector:
            app: nodejs
          ports:
            - protocol: TCP
              targetPort: 8000 # pod vala port
              port: 8000 # service port
          type: NodePort
      
  2. Apply All Files
    Combine the configurations and deploy them:

     kubectl apply -f namespace.yml -f deployment.yml -f service.yml
    
  3. Forward the Port and Access the App
    Run the following command to forward the port:

     kubectl port-forward service/nodejs-service -n nodejs 8000:8000 --address=0.0.0.0 &
    
  4. Access the Application
    Open your browser and visit:

     http://<your_instance_public_ip>:8000
    


Conclusion

Understanding these Kubernetes concepts—Namespaces, Pods, Deployments, ReplicaSets, and Services—will give you a strong foundation. As a fresher, mastering these topics is enough to start managing real-world Kubernetes clusters.

The Bonus section provides you with a hands-on example to practice deploying a custom app using Kubernetes.

Have questions? Drop them in the comments below, and let’s discuss!