Thursday, January 20, 2022

Kubernetes objects with practical examples

 

1. Pod

  • Example: Suppose you have a simple web application with a single container.

  • Definition: A Pod could be defined as follows:


    apiVersion: v1 kind: Pod metadata: name: my-web-app spec: containers: - name: web-container image: nginx:latest ports: - containerPort: 80
  • Explanation: This Pod definition runs an Nginx container, exposing port 80.

2. Service

  • Example: To expose the my-web-app Pod so it can be accessed from other Pods or externally.

  • Definition: A Service could be defined as follows:


    apiVersion: v1 kind: Service metadata: name: my-web-service spec: selector: app: my-web-app ports: - protocol: TCP port: 80 targetPort: 80 type: LoadBalancer
  • Explanation: This Service targets Pods with the label app: my-web-app and exposes port 80. The LoadBalancer type will provision an external IP address (if supported by the cloud provider).

3. Deployment

  • Example: To deploy multiple replicas of your my-web-app Pod and manage updates.

  • Definition: A Deployment could be defined as follows:


    apiVersion: apps/v1 kind: Deployment metadata: name: my-web-deployment spec: replicas: 3 selector: matchLabels: app: my-web-app template: metadata: labels: app: my-web-app spec: containers: - name: web-container image: nginx:latest ports: - containerPort: 80
  • Explanation: This Deployment manages three replicas of my-web-app, ensuring high availability and managing updates seamlessly.

4. ReplicaSet

  • Example: The ReplicaSet is usually managed by a Deployment, but you can define it directly as follows:


    apiVersion: apps/v1 kind: ReplicaSet metadata: name: my-web-replicaset spec: replicas: 3 selector: matchLabels: app: my-web-app template: metadata: labels: app: my-web-app spec: containers: - name: web-container image: nginx:latest ports: - containerPort: 80
  • Explanation: This ReplicaSet ensures that three Pods with the label app: my-web-app are running at all times.

5. StatefulSet

  • Example: For a database application where each instance needs a stable identity and persistent storage.

  • Definition: A StatefulSet could be defined as follows:


    apiVersion: apps/v1 kind: StatefulSet metadata: name: my-db-statefulset spec: serviceName: "my-db-service" replicas: 3 selector: matchLabels: app: my-database template: metadata: labels: app: my-database spec: containers: - name: db-container image: postgres:latest ports: - containerPort: 5432 volumeMounts: - name: db-storage mountPath: /var/lib/postgresql/data volumeClaimTemplates: - metadata: name: db-storage spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 1Gi
  • Explanation: This StatefulSet manages three instances of a PostgreSQL database, each with its own persistent storage.

6. DaemonSet

  • Example: To ensure a logging agent runs on every node.

  • Definition: A DaemonSet could be defined as follows:


    apiVersion: apps/v1 kind: DaemonSet metadata: name: log-collector spec: selector: matchLabels: app: log-collector template: metadata: labels: app: log-collector spec: containers: - name: log-agent image: fluentd:latest volumeMounts: - name: varlog mountPath: /var/log volumes: - name: varlog hostPath: path: /var/log
  • Explanation: This DaemonSet ensures that the log-agent container runs on every node, collecting logs from /var/log.

7. Job

  • Example: To run a database migration task that needs to complete successfully.

  • Definition: A Job could be defined as follows:


    apiVersion: batch/v1 kind: Job metadata: name: db-migration-job spec: template: spec: containers: - name: migration image: my-migration-tool:latest command: ["./migrate.sh"] restartPolicy: OnFailure
  • Explanation: This Job runs a migration script to completion, restarting only if it fails.

8. CronJob

  • Example: To schedule a task to back up a database every day at midnight.

  • Definition: A CronJob could be defined as follows:


    apiVersion: batch/v1 kind: CronJob metadata: name: daily-db-backup spec: schedule: "0 0 * * *" jobTemplate: spec: template: spec: containers: - name: backup image: my-backup-tool:latest command: ["./backup.sh"] restartPolicy: OnFailure
  • Explanation: This CronJob schedules a backup job to run every day at midnight.

9. ConfigMap

  • Example: To provide configuration settings for your application.

  • Definition: A ConfigMap could be defined as follows:


    apiVersion: v1 kind: ConfigMap metadata: name: app-config data: APP_MODE: "production" LOG_LEVEL: "info"
  • Explanation: This ConfigMap provides configuration data that can be consumed by Pods.

10. Secret

  • Example: To store sensitive information like a database password.

  • Definition: A Secret could be defined as follows:


    apiVersion: v1 kind: Secret metadata: name: db-secret type: Opaque data: db-password: c2VjcmV0cGFzc3dvcmQ= # Base64 encoded 'secretpassword'
  • Explanation: This Secret stores a base64-encoded password that can be used by Pods.

11. Namespace

  • Example: To create an isolated environment for different teams.

  • Definition: A Namespace could be defined as follows:


    apiVersion: v1 kind: Namespace metadata: name: dev-environment
  • Explanation: This Namespace isolates resources for a development environment.

12. PersistentVolume (PV) and PersistentVolumeClaim (PVC)

  • Example: To manage persistent storage for your application.

  • PersistentVolume Definition:


    apiVersion: v1 kind: PersistentVolume metadata: name: pv-example spec: capacity: storage: 5Gi accessModes: - ReadWriteOnce hostPath: path: /mnt/data
  • PersistentVolumeClaim Definition:


    apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc-example spec: accessModes: - ReadWriteOnce resources: requests: storage: 5Gi
  • Explanation: The PV provides storage, and the PVC requests that storage. When bound, Pods can use the PVC to access the PV.

13. Ingress

  • Example: To route traffic to different services based on the URL.

  • Definition: An Ingress could be defined as follows:


    apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: my-ingress spec: rules: - host: myapp.example.com http: paths: - path: /api pathType: Prefix backend: service: name: api-service port: number: 80 - path: / pathType: Prefix backend: service: name: web-service port: number: 80
  • Explanation: This Ingress routes requests to different services based on the path (/api to api-service, and / to web-service).

Each of these objects helps in managing different aspects of a Kubernetes deployment, making it easier to handle complex containerized applications and services.

No comments:

Post a Comment