Shashikant shah

Sunday, 25 February 2024

What is Command, Arguments and yaml file in kubernetes.

 1. What is a YAML file in Kubernetes?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format used for configuration files. YAML files are commonly used to define Kubernetes resources such as pods, deployments, services, ReplicaSets, and more.

A YAML file consists of key-value pairs and lists, making it easy to represent hierarchical data structures. Kubernetes YAML files typically contain the following sections:

apiVersion:

Version of API.

kind:

Kind of object you to create.

metadata:

Name of object that uniquely identifies it.

spec:

Desired state of the object.














#  kubectl get pod pod-nginx -o yaml

2.Yaml file for pods.

apiVersion: v1

kind: Pod

metadata:

  name: my-first-pod

  labels:

    app: my-app

spec:

  containers:

  - name: my-container

    image: nginx:latest

    ports:

    - containerPort: 80


apiVersion: Specifies the API version being used. In this case, it's v1, which is the most common version for pods.

kind: Specifies the type of Kubernetes resource being defined. Here, it's Pod.

metadata: Contains metadata about the pod, such as its name and labels.

name: Specifies the name of the pod.

labels: Specifies labels for the pod, which can be used for selecting and grouping pods.

spec: Contains the specification of the pod, including its containers.

containers: Specifies a list of containers to run in the pod.

name: Specifies the name of the container.

image: Specifies the Docker image to use for the container.

ports: Specifies a list of ports to expose from the container.

containerPort: Specifies the port number the container listens on.


More details for Pods  :-

https://kubernetes.io/docs/reference/kubernetes-api/workload-resources/pod-v1/

Detailed information about Kubernetes resources.

# kubectl  explain  pods


































# kubectl  explain  pods.spec.containers

















What is annotation in Kubernetes ?

Annotations in Kubernetes (K8s) are metadata used to express additional information related to a resource or object.

Annotations consist of key-value pairs, each pair used to describe the resource’s metadata or provide additional information. For example, it can be used to record a resource’s creator, version, change history, relationship to a particular component, and so on.

# cat first.yaml

apiVersion: apps/v1

kind: ReplicaSet

metadata:

  name: test-replicaset

  annotations:

    developer: shashi@linux.com

 

spec:

  replicas: 3

  selector:

    matchLabels:

      tier: frontend

  template:

    metadata:

      labels:

        tier: frontend

    spec:

      containers:

      - name: nginx-container

        image: nginx

# kubectl describe rs test-replicaset
















### Command and Arguments ###

command and arguments for a container within a pod using the command and args fields in the container specification.

we can override the detault entrypoint and CMD with command and args field.

Dockerfile vs k8s Manifest perspective.

Docker Field Name

Kubernetes Field name

Description

ENTRYPOINT

command

Command that will be run by container.

CMD

args

Argument passed to the container.



commands

Arguments

echo

“Hello”

sleep

“3600”

bash

“demo.sh”


Use case -1

apiVersion: v1

kind: Pod

metadata:

  name: command

spec:

  containers:

  - name: count

    image: busybox

    command: ["sleep","3600"]

# kubectl  get pods






# kubectl exec -it command sh













Use case -2

apiVersion: v1

kind: Pod

metadata:

  name: command1

spec:

  containers:

  - name: count

    image: busybox

    command: ["sleep"]

    args: ["3600"]









Use case -3

apiVersion: v1

kind: Pod

metadata:

  name: command2

spec:

  containers:

  - name: count

    image: busybox

    args: ["sleep","3600"]










Use case -4

run multiple commands in a container

apiVersion: v1

kind: Pod

metadata:

  name: command2

spec:

  containers:

  - name: count

    image: busybox

    command: ["/bin/sh", "-c"]

    args:

      - |

        echo "Executing the first command"

        date

        echo "Executing the second command"

        printenv HOSTNAME

# kubectl logs command2














No comments:

Post a Comment