Wednesday, August 16, 2023

Creating sample pod using YAML file


Creating sample pod using YAML file 

 To accomplish this task, you can follow these steps to create the time-check pod with the specified requirements:

1. Create a Pod with ConfigMap, Volume, and Container:

   Create a YAML file (for example, `time-check-pod.yaml`) with the following content:

Yaml

   apiVersion: v1

   kind: Pod

   metadata:

     name: time-check

     namespace: datacenter

   spec:

     containers:

     - name: time-check

       image: busybox:latest

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

       args:

       - while true; do date; sleep $TIME_FREQ; done >> /opt/devops/time/time-check.log

       env:

       - name: TIME_FREQ

         valueFrom:

           configMapKeyRef:

             name: time-config

             key: TIME_FREQ

       volumeMounts:

       - name: log-volume

         mountPath: /opt/devops/time

     volumes:

     - name: log-volume

       emptyDir: {}


2. Create the ConfigMap:

   Run the following command to create the ConfigMap `time-config` with the desired data:

#   kubectl create configmap time-config --from-literal=TIME_FREQ=7 -n datacenter


3. Apply the Pod Definition:

   Apply the pod definition using the following command:

#  kubectl apply -f time-check-pod.yaml

   This will create the pod, configmap, and set up the container as described.


4. Verify:

   You can verify the pod's creation and status using:

 #  kubectl get pods -n datacenter

      To check the logs of the time-check pod:

  # kubectl logs time-check -n datacenter

  

The above steps will create the required pod named "time-check" in the "datacenter" namespace, using the busybox image.


To check the contents of the `/opt/devops/time/time-check.log` file within the "time-check" pod in the "datacenter" namespace, you'll need to use the `kubectl exec` command to run a shell within the pod and then read the contents of the file. Here's how you can do it:

# kubectl exec -it time-check -n datacenter -- cat /opt/devops/time/time-check.log


This command does the following:

- `exec`: Execute a command in a running container.

- `-it`: Use an interactive terminal.

- `time-check`: The name of the pod you want to execute the command in.

- `-n datacenter`: Specifies the namespace where the pod is located.

- `--`: Indicates the end of options for `kubectl`.

- `cat /opt/devops/time/time-check.log`: The command to display the contents of the specified log file.


This will print the contents of the `/opt/devops/time/time-check.log` file to your terminal. Replace `time-check` and `datacenter` with the actual pod name and namespace if they are different in your environment.

No comments:

Post a Comment

Troubleshoot issue with Pods - Part 2

  Troubleshoot issue with Pods - Part 2 To verify if there are any port conflicts between the containers and ensure that the container ports...