K8s objects with practicals

Let’s dive deep into Kubernetes Objects with detailed explanations, YAML examples, and practical steps. Kubernetes objects are the building blocks of your cluster, and understanding them is crucial for managing applications effectively.


2. Kubernetes Objects

Kubernetes objects are persistent entities that represent the state of your cluster. They describe:

  • What containerized applications are running (and on which nodes).

  • The resources available to those applications.

  • Policies around how those applications behave.

Below, we’ll cover the most important Kubernetes objects with YAML examples and practical steps.


1. Pods

A Pod is the smallest deployable unit in Kubernetes. It can contain one or more containers that share storage, network, and specifications.

YAML Example: Pod

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  labels:
    app: my-app
spec:
  containers:
  - name: my-container
    image: nginx
    ports:
    - containerPort: 80

Practical Steps

  1. Save the above YAML as pod.yaml.

  2. Create the Pod:

     kubectl apply -f pod.yaml
    
  3. Verify the Pod:

     kubectl get pods
    
  4. Describe the Pod for details:

     kubectl describe pod my-pod
    
  5. Delete the Pod:

     kubectl delete -f pod.yaml
    

2. ReplicaSets

A ReplicaSet ensures that a specified number of Pod replicas are running at any given time.

YAML Example: ReplicaSet

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: my-replicaset
  labels:
    app: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx
        ports:
        - containerPort: 80

Practical Steps

  1. Save the YAML as replicaset.yaml.

  2. Create the ReplicaSet:

     kubectl apply -f replicaset.yaml
    
  3. Verify the ReplicaSet and Pods:

     kubectl get replicaset
     kubectl get pods
    
  4. Scale the ReplicaSet:

     kubectl scale replicaset my-replicaset --replicas=5
    
  5. Delete the ReplicaSet:

     kubectl delete -f replicaset.yaml
    

3. Deployments

A Deployment provides declarative updates for Pods and ReplicaSets. It’s the most commonly used object for managing applications.

YAML Example: Deployment

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

Practical Steps

  1. Save the YAML as deployment.yaml.

  2. Create the Deployment:

     kubectl apply -f deployment.yaml
    
  3. Verify the Deployment and Pods:

     kubectl get deployments
     kubectl get pods
    
  4. Update the Deployment (e.g., change the image):

     kubectl set image deployment/my-deployment my-container=nginx:1.19
    
  5. Rollback the Deployment:

     kubectl rollout undo deployment/my-deployment
    
  6. Delete the Deployment:

     kubectl delete -f deployment.yaml
    

4. Services

A Service provides stable networking for Pods. It abstracts the Pods behind a single IP address.

YAML Example: Service (ClusterIP)

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
  type: ClusterIP

Practical Steps

  1. Save the YAML as service.yaml.

  2. Create the Service:

     kubectl apply -f service.yaml
    
  3. Verify the Service:

     kubectl get services
    
  4. Access the Service:

     kubectl port-forward service/my-service 8080:80
    

    Then open http://localhost:8080 in your browser.

  5. Delete the Service:

     kubectl delete -f service.yaml
    

5. ConfigMaps and Secrets

  • ConfigMap: Stores configuration data as key-value pairs.

  • Secret: Stores sensitive data like passwords or tokens.

YAML Example: ConfigMap

apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config
data:
  app.properties: |
    color=blue
    size=large

YAML Example: Secret

apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  username: YWRtaW4=  # base64 encoded
  password: MWYyZDFlMmU2N2Rm  # base64 encoded

Practical Steps

  1. Create the ConfigMap and Secret:

     kubectl apply -f configmap.yaml
     kubectl apply -f secret.yaml
    
  2. Verify:

     kubectl get configmaps
     kubectl get secrets
    
  3. Use them in a Pod:

     env:
     - name: COLOR
       valueFrom:
         configMapKeyRef:
           name: my-config
           key: color
     - name: PASSWORD
       valueFrom:
         secretKeyRef:
           name: my-secret
           key: password
    

6. PersistentVolumes and PersistentVolumeClaims

  • PersistentVolume (PV): Represents physical storage in the cluster.

  • PersistentVolumeClaim (PVC): Requests storage from a PV.

YAML Example: PV and PVC

# PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
  name: my-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /mnt/data

# PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: my-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

Practical Steps

  1. Create the PV and PVC:

     kubectl apply -f pv.yaml
     kubectl apply -f pvc.yaml
    
  2. Verify:

     kubectl get pv
     kubectl get pvc
    
  3. Use the PVC in a Pod:

     volumes:
     - name: my-storage
       persistentVolumeClaim:
         claimName: my-pvc
    

7. Namespaces

Namespaces provide logical partitioning of resources in a cluster.

YAML Example: Namespace

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

Practical Steps

  1. Create the Namespace:

     kubectl apply -f namespace.yaml
    
  2. Verify:

     kubectl get namespaces
    
  3. Create resources in the Namespace:

     kubectl apply -f pod.yaml -n my-namespace
    

8. Jobs and CronJobs

  • Job: Runs a task to completion.

  • CronJob: Runs Jobs on a schedule.

YAML Example: Job

apiVersion: batch/v1
kind: Job
metadata:
  name: my-job
spec:
  template:
    spec:
      containers:
      - name: my-container
        image: busybox
        command: ["echo", "Hello, Kubernetes!"]
      restartPolicy: Never

YAML Example: CronJob

apiVersion: batch/v1
kind: CronJob
metadata:
  name: my-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: my-container
            image: busybox
            command: ["echo", "Hello, Kubernetes!"]
          restartPolicy: OnFailure

Practical Steps

  1. Create the Job or CronJob:

     kubectl apply -f job.yaml
     kubectl apply -f cronjob.yaml
    
  2. Verify:

     kubectl get jobs
     kubectl get cronjobs
    

By practicing these examples, you’ll gain a solid understanding of Kubernetes objects and how to manage them effectively. Let me know if you need further clarification or additional examples!