# 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:

```yaml
kind: Namespace
apiVersion: v1
metadata:
  name: nginx
```

* Save this file as `namespace.yml`.
    
* Apply it using:
    
    ```bash
    kubectl apply -f namespace.yml
    ```
    

To view all namespaces:

```bash
kubectl get ns
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067695090/04a7fa8f-ff9b-441c-9fd8-6573784b3c0b.png align="center")

### Deleting a Namespace

To delete a namespace, use:

```bash
kubectl delete -f namespace.yml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067706856/ddd33f51-f97d-46bc-9c96-b55f62ada3cd.png align="center")

---

## **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:

```yaml
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:
    
    ```bash
    kubectl apply -f pod.yml --dry-run=client
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067793111/ab794b62-4768-4950-8c99-8857f98d500a.png align="center")
    
* Apply it:
    
    ```bash
    kubectl apply -f pod.yml
    ```
    

To list pods in a specific namespace:

```bash
kubectl get pods -n nginx
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067806320/b001ac28-25f2-4c74-a977-a207fc034901.png align="center")

---

## **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:

```yaml
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:
    
    ```bash
    kubectl apply -f deployment.yml --dry-run=client
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067912452/c1b87838-e7f5-4b19-ac79-071f7d6a57df.png align="center")
    
* Apply it:
    
    ```bash
    kubectl apply -f deployment.yml
    ```
    

To view deployments:

```bash
kubectl get deployment -n nginx
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067977794/0e63d508-785a-42e4-8414-4ed80023096b.png align="center")

### Scaling a Deployment

You can scale deployments dynamically:

```bash
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

```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:
    
    ```bash
    kubectl apply -f service.yml
    ```
    

To view services:

```bash
kubectl get svc -n nginx
```

### Accessing Services

To expose the service externally, you can use:

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733068110083/c813cae6-9c62-4aa6-b434-7d0f8dab35ce.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733068129683/6357f65b-777b-4500-89dc-131ed01da2e7.png align="center")

---

## **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`
        
        ```yaml
        kind: Namespace
        apiVersion: v1
        metadata:
        	name: nodejs
        ```
        
    * `deployment.yml`
        
        ```yaml
        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`
        
        ```yaml
        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:
    
    ```bash
    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:
    
    ```bash
    kubectl port-forward service/nodejs-service -n nodejs 8000:8000 --address=0.0.0.0 &
    ```
    
4. **Access the Application**  
    Open your browser and visit:
    
    ```bash
    http://<your_instance_public_ip>:8000
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1733067613592/33491ead-c685-4997-9f85-f09cfff36b20.png align="center")

---

## **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!
