Friday, August 18, 2023

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 are correctly mapped to the pod's ports, you can use the following command:


# kubectl describe pod <pod-name> -n <namespace>


Replace `<pod-name>` with the name of your pod and `<namespace>` with the appropriate namespace where the pod is located.

In the output of the `kubectl describe` command, you'll see a section labeled "Containers." Under each container, there should be a "Ports" section indicating the container ports and their mappings. Make sure that the container ports are correctly configured and not conflicting with each other or with the host.

Additionally, you can use the following command to view the detailed configuration of the pod in YAML format, which includes the port specifications:


# kubectl get pod <pod-name> -n <namespace> -o yaml


This will display the full YAML configuration of the pod, including the ports configured for each container. You can compare the container ports with the pod's ports to ensure that they are correctly mapped.

Troubleshoot Issue With Pods

 
Troubleshoot Issue With Pods - Part 1 


To troubleshoot and resolve the issue with the failing pod, you can follow these steps:

1. First, SSH into the host to access the Kubernetes cluster using the configured 

`kubectl` utility.

2. Check the status and logs of the failing pod to identify the specific errors or issues causing the pod not to come up:

   
   # kubectl get pods

   # kubectl describe pod webserver

   # kubectl logs webserver -c httpd-container

   # kubectl logs webserver -c sidecar-container
   

3. Based on the logs and descriptions, identify the root cause of the problem. Common issues could include image pull failures, misconfigured resources, or conflicting ports.

4. If the issue is related to image pull, ensure names `httpd:latest` and `ubuntu:latest' are accessible. If not, consider using specific image tags that are available.

5. Check if there are any resource constraints causing the pod to fail, such as CPU or memory limits.

kubectl describe pod <pod-name> -n <namespace>

Replace <pod-name> with the name of your pod and <namespace> with the appropriate namespace where the pod is located.

In the output of the kubectl describe command, you will find information about the pod's resource limits and requests under the "Limits" and "Requests" sections. Look for the cpu and memory fields to determine if there are any limitations set for CPU and memory.

Wednesday, August 16, 2023

Check the logs of a Kubernetes pod

 

Check the logs of a Kubernetes pod


To check the logs of a Kubernetes pod, you can use the `kubectl logs` command. Here's how you can use it to check the logs of the "time-check" pod in the "datacenter" namespace:


# kubectl logs time-check -n datacenter


This command will display the logs generated by the "time-check" container within the specified pod. Replace `time-check` with the actual name of your pod if it's different.


If the pod has multiple containers, and you want to specify a specific container within the pod, you can do so using the `-c` flag:


# kubectl logs -c container-name time-check -n datacenter


Replace `container-name` with the name of the container whose logs you want to view.


Remember that logs are displayed in real-time, so you can see new log entries as they are generated by the container. You can also use options like `--tail` to limit the number of lines shown and `-f` to follow the logs in real-time (similar to using `tail -f` on a regular log file).

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.

Create the Namespace in Kubernetes

 

Create the Namespace in Kubernetes

when trying to create the ConfigMap in that namespace. You need to create the namespace first before creating resources within it. Here's how you can do it:


1. Create the Namespace:

   Run the following command to create the "datacenter" namespace:

#   kubectl create namespace datacenter


2. Create the ConfigMap:

   After creating the namespace, run the ConfigMap creation command:

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

  

This sequence of commands should first create the necessary namespace and then create the ConfigMap in that namespace. You can then proceed with the other steps to create the pod and associated resources as described in the previous response.

Tuesday, August 15, 2023

Create few jobs in Kubernetes cluster


Create few jobs in Kubernetes cluster

Creation of  example job countdown-devops.

The spec template should be named as countdown-devops (under metadata), and the container should be named as container-countdown-devops

Use image centos with latest tag only and remember to mention tag i.e centos:latest, and restart policy should be Never.

Use command sleep 5

Yaml Code 

apiVersion: batch/v1
kind: Job
metadata:
  name: countdown-devops
spec:
  template:
    metadata:
      name: countdown-devops
    spec:
      containers:
      - name: container-countdown-devops
        image: centos:latest
        command: ["sleep", "5"]
      restartPolicy: Never

You can save this as a YAML file, for example, countdown-devops-job.yaml, and then apply it using the kubectl command 

# kubectl apply -f countdown-devops-job.yaml

This will create a job named countdown-devops with a container named container-countdown-devops running the centos:latest image and executing the command sleep 5. The job will not restart automatically (restartPolicy: Never).

To see the status of the job:

# kubectl get jobs

To get more details about the job and its pods:

# kubectl describe job countdown-devops

Friday, August 11, 2023

Create a ReplicaSet - Example


Create a ReplicaSet using nginx image with latest tag only and remember to mention tag i.e nginx:latest and name it as nginx-replicaset.

Labels app should be nginx_app, labels type should be front-end.

The container should be named as nginx-container; also make sure replicas counts are 4. 


here's the template for creating a ReplicaSet in Kubernetes as per requirements:

yaml

apiVersion: apps/v1

kind: ReplicaSet

metadata:

  name: nginx-replicaset

  labels:

    app: nginx_app

    type: front-end

spec:

  replicas: 4

  selector:

    matchLabels:

      app: nginx_app

      type: front-end

  template:

    metadata:

      labels:

        app: nginx_app

        type: front-end

    spec:

      containers:

      - name: nginx-container

        image: nginx:latest


You can use this YAML template to create the desired ReplicaSet with the specified labels, container name, and number of replicas. Just apply this configuration using the `kubectl apply -f filename.yaml` command, replacing "filename.yaml" with the actual name of your YAML file.


To check if the ReplicaSet has been successfully created and the pods are running, you can use the following commands with the `kubectl` utility:


1. Check the status of the ReplicaSet:

#   kubectl get replicasets

  

2. View the details of the created ReplicaSet:

#  kubectl describe replicasets nginx-replicaset

   

3. Verify the running pods associated with the ReplicaSet:

#   kubectl get pods -l app=nginx_app,type=front-end


4. Get detailed information about a specific pod:

#   kubectl describe pod <pod-name>


These commands will help you monitor the status of your ReplicaSet and the associated pods. If everything is set up correctly, you should see the desired number of pods running with the nginx image and the specified labels.

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...