kubernates(K8s) resources Overview with examples with explanation

Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2023-11-17 08:23:58 Viewed : 524


1. Pod:

A Pod is the smallest deployable unit in Kubernetes, representing a single instance of a running process. It can contain one or more containers.

yaml
apiVersion: v1 kind: Pod metadata: name: example-pod spec: containers: - name: nginx-container image: nginx

Explanation:

  • This YAML file defines a basic Pod named "example-pod."
  • It contains a single container named "nginx-container," which runs the Nginx image.

2. Service:

A Service exposes a set of Pods as a network service. It allows other applications to discover and communicate with the Pods.

yaml
apiVersion: v1 kind: Service metadata: name: example-service spec: selector: app: example-app ports: - protocol: TCP port: 80 targetPort: 8080

Explanation:

  • This YAML file creates a Service named "example-service."
  • It selects Pods with the label "app: example-app" and exposes them on port 80, forwarding traffic to port 8080 in the selected Pods.

3. Namespace:

A Namespace provides a way to divide cluster resources between multiple users or teams.

yaml
apiVersion: v1 kind: Namespace metadata: name: example-namespace

Explanation:

  • This YAML file creates a Namespace named "example-namespace."

4. Deployment:

A Deployment manages the deployment and scaling of Pods, allowing declarative updates to applications.

yaml
apiVersion: apps/v1 kind: Deployment metadata: name: example-deployment spec: replicas: 3 selector: matchLabels: app: example-app template: metadata: labels: app: example-app spec: containers: - name: nginx-container image: nginx

Explanation:

  • This YAML file creates a Deployment named "example-deployment."
  • It ensures there are three replicas of Pods with the label "app: example-app," each running the Nginx container.

5. ReplicaSet:

A ReplicaSet ensures a specified number of replicas of a Pod are running.

yaml
apiVersion: apps/v1 kind: ReplicaSet metadata: name: example-replicaset spec: replicas: 2 selector: matchLabels: app: example-app template: metadata: labels: app: example-app spec: containers: - name: nginx-container image: nginx

Explanation:

  • This YAML file creates a ReplicaSet named "example-replicaset."
  • It ensures there are two replicas of Pods with the label "app: example-app," each running the Nginx container.

6. ConfigMap:

A ConfigMap allows you to decouple configuration artifacts from the container image.

yaml
apiVersion: v1 kind: ConfigMap metadata: name: example-configmap data: key1: value1 key2: value2

Explanation:

  • This YAML file creates a ConfigMap named "example-configmap."
  • It defines key-value pairs (key1: value1, key2: value2) that can be used as configuration data in Pods.

7. Secret:

A Secret stores sensitive information, such as passwords or API keys, in a Base64-encoded format.

yaml
apiVersion: v1 kind: Secret metadata: name: example-secret type: Opaque data: username: <base64-encoded-username> password: <base64-encoded-password>

Explanation:

  • This YAML file creates a Secret named "example-secret" with a username and password encoded in Base64 format.

8. Volume:

A Volume is a directory that is accessible to containers in a Pod, providing a way to persist data.

yaml
apiVersion: v1 kind: Pod metadata: name: example-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: example-volume mountPath: /usr/share/nginx/html volumes: - name: example-volume emptyDir: {}

Explanation:

  • This YAML file defines a Pod with an Nginx container and an emptyDir Volume mounted at "/usr/share/nginx/html."

9. Ingress:

An Ingress provides HTTP and HTTPS routing to services based on rules, enabling external access to services in a cluster.

yaml
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: example-ingress spec: rules: - host: example.com http: paths: - path: / pathType: Prefix backend: service: name: example-service port: number: 80

Explanation:

  • This YAML file creates an Ingress named "example-ingress."
  • It defines a rule for routing HTTP traffic from "example.com" to the Service named "example-service" on port 80.

These examples cover some of the fundamental Kubernetes resources and how they are configured. You can customize these examples based on your specific application requirements.


Search
Related Articles

Leave a Comment: