KubeCtl
Friday, August 18, 2023
Troubleshoot issue with Pods - Part 2
Troubleshoot Issue With Pods
Troubleshoot Issue With Pods - Part 1
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
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...
-
Adding limits for resources utilization Create a pod named httpd-pod and a container under it named as httpd-container, use httpd image with...
-
Troubleshoot issue with Pods - Part 2 To verify if there are any port conflicts between the containers and ensure that the container ports...
-
Create the Namespace in Kubernetes when trying to create the ConfigMap in that namespace. You need to create the namespace first before cr...