Kubernetes commands

Kubernetes commands





In Kubernetes, you can use several commands to get a comprehensive overview of the cluster, its resources, and their statuses. Here are some key commands to check various aspects of your Kubernetes cluster:

1. General Cluster Information

  • Cluster Information

kubectl cluster-info

kubectl cluster-info dump

  • Nodes Information

kubectl get nodes

kubectl describe nodes

2. Namespaces

  • List All Namespaces

kubectl get namespaces

  • Get Resources in All Namespaces

kubectl get all --all-namespaces

3. Pods

  • List All Pods

kubectl get pods --all-namespaces

  • Describe a Specific Pod

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

  • Logs for a Pod

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

4. Deployments

  • List All Deployments

kubectl get deployments --all-namespaces

  • Describe a Specific Deployment

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

 

 

5. Services

  • List All Services

kubectl get services --all-namespaces

  • Describe a Specific Service

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

6. Ingress

  • List All Ingress Resources

kubectl get ingress --all-namespaces

  • Describe a Specific Ingress Resource

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

7. ConfigMaps and Secrets

  • List All ConfigMaps

kubectl get configmaps --all-namespaces

  • Describe a Specific ConfigMap

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

  • List All Secrets

kubectl get secrets --all-namespaces

  • Describe a Specific Secret

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

8. Persistent Volumes and Claims

  • List All PersistentVolumes

kubectl get pv

  • Describe a Specific PersistentVolume

kubectl describe pv <pv-name>

 

 

 

 

  • List All PersistentVolumeClaims

kubectl get pvc --all-namespaces

  • Describe a Specific PersistentVolumeClaim

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

9. DaemonSets, StatefulSets, and Jobs

  • List All DaemonSets

kubectl get daemonsets --all-namespaces

  • List All StatefulSets

kubectl get statefulsets --all-namespaces

  • List All Jobs

kubectl get jobs --all-namespaces

10. Events

  • Get Events

kubectl get events --all-namespaces

11. Custom Resource Definitions (CRDs)

  • List All CRDs

kubectl get crds

  • List All Custom Resources of a Specific Kind

kubectl get <custom-resource-kind> --all-namespaces

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comprehensive Command

If you want to get a snapshot of the cluster’s state, you can combine some of these commands into a script. Here’s a simplified example:

#!/bin/bash

echo "Cluster Info:"

kubectl cluster-info

echo "Nodes:"

kubectl get nodes

echo "Namespaces:"

kubectl get namespaces

echo "All Resources in All Namespaces:"

kubectl get all --all-namespaces

echo "Events:"

kubectl get events --all-namespaces

 

 

You can run this script to quickly gather and review the current state of your Kubernetes cluster.

Note:

  • Replace placeholders like <pod-name>, <namespace>, <deployment-name>, <service-name>, <ingress-name>, <configmap-name>, <secret-name>, <pv-name>, and <pvc-name> with actual names relevant to your setup.

 

  • Ensure you have the necessary permissions and context set for your kubectl command to access the resources in your cluster.

 

End of the 1st Notes:

 

 

 

 

 

 

 

To thoroughly check the health and status of a Kubernetes cluster, you can use various commands and tools. Below is a comprehensive list of commands and steps to check different aspects of the Kubernetes cluster:

General Cluster Information

  1. Check Cluster Nodes

kubectl get nodes

This command lists all nodes in the cluster along with their status.

  1. Detailed Node Information

kubectl describe nodes

This provides detailed information about each node.

Pod and Deployment Information

  1. List All Pods in All Namespaces

kubectl get pods --all-namespaces

This shows all the pods running in the cluster across all namespaces.

  1. Describe Pods

kubectl describe pods -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the pods in that namespace.

  1. List All Deployments

kubectl get deployments --all-namespaces

This lists all deployments in the cluster.

  1. Describe Deployments

kubectl describe deployments -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the deployments.

Service and Endpoint Information

  1. List All Services

kubectl get services --all-namespaces

This shows all services in the cluster.

  1. Describe Services

kubectl describe services -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the services.

  1. List All Endpoints

kubectl get endpoints --all-namespaces

This shows all endpoints for services in the cluster.

ConfigMap and Secret Information

  1. List All ConfigMaps

kubectl get configmaps --all-namespaces

This shows all ConfigMaps in the cluster.

  1. Describe ConfigMaps

kubectl describe configmaps -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the ConfigMaps.

  1. List All Secrets

kubectl get secrets --all-namespaces

This shows all secrets in the cluster.

  1. Describe Secrets

kubectl describe secrets -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the secrets.

Resource Utilization

  1. Get Resource Usage of Nodes (if Metrics Server is installed)

kubectl top nodes

 

 

  1. Get Resource Usage of Pods (if Metrics Server is installed)

kubectl top pods --all-namespaces

Logs and Events

  1. View Logs of a Specific Pod

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

Replace <pod-name> and <namespace> with the specific pod name and namespace.

  1. View Events

kubectl get events --all-namespaces

Cluster Components and Health

  1. Check Component Status

kubectl get componentstatuses

  1. Check Cluster Info

kubectl cluster-info

Helm (if using Helm)

  1. List All Helm Releases

helm list --all-namespaces

  1. Describe a Helm Release

helm status <release-name> -n <namespace>

Replace <release-name> and <namespace> with the specific release name and namespace.

Network Policies

  1. List All Network Policies

kubectl get networkpolicies --all-namespaces

  1. Describe Network Policies

kubectl describe networkpolicies -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the network policies.

Storage

  1. List All Persistent Volumes (PVs)

kubectl get pv

  1. Describe Persistent Volumes (PVs)

kubectl describe pv <pv-name>

Replace <pv-name> with the specific Persistent Volume name.

  1. List All Persistent Volume Claims (PVCs)

kubectl get pvc --all-namespaces

  1. Describe Persistent Volume Claims (PVCs)

kubectl describe pvc -n <namespace>

Replace <namespace> with the specific namespace to get detailed information about the PVCs.

 

Using these commands, you can comprehensively check and monitor the state and health of your Kubernetes cluster.

 

The commands listed above provide a comprehensive overview of various aspects of a Kubernetes cluster, which can be very useful for real-time monitoring and troubleshooting in a production environment. However, in a production setting at a large organization like Infosys, TCS, HCL, Wipro, Capgemini, Cognigent etc. you might need additional tools and practices to ensure robustness, security, and efficiency. Here are some additional considerations and tools:

Additional Tools and Practices

  1. Monitoring and Alerting:
    • Prometheus & Grafana:

helm install prometheus prometheus-community/prometheus

helm install grafana grafana/grafana

Use these tools to monitor metrics and visualize data.

    • Alertmanager: Configure Prometheus Alertmanager to send alerts based on defined rules.

 

  1. Logging:
    • ELK Stack (Elasticsearch, Logstash, Kibana):

helm install elasticsearch elastic/elasticsearch

helm install logstash elastic/logstash

helm install kibana elastic/kibana

Collect and analyze logs from the cluster.

    • Fluentd/Fluent Bit: Use Fluentd or Fluent Bit for log aggregation and forwarding.
  1. Security:
    • Network Policies: Ensure network policies are in place to control traffic between pods.

kubectl get networkpolicies --all-namespaces

kubectl describe networkpolicies -n <namespace>

    • RBAC (Role-Based Access Control): Define and enforce access controls.

kubectl get roles --all-namespaces

kubectl get rolebindings --all-namespaces

kubectl describe roles -n <namespace>

kubectl describe rolebindings -n <namespace>

    • Secrets Management: Regularly audit secrets and ensure they are encrypted.
  1. Continuous Integration/Continuous Deployment (CI/CD):
    • Jenkins/X: Set up Jenkins or Jenkins X for automated deployment pipelines.
    • Argo CD: Use Argo CD for GitOps-based continuous delivery.
  2. Backup and Disaster Recovery:
    • Velero: Backup and restore Kubernetes cluster resources and persistent volumes.

velero install --provider <cloud-provider> --bucket <bucket-name> --secret-file <path-to-credentials-file>

  1. Service Mesh:
    • Istio: Implement a service mesh for better traffic management, security, and observability.

istioctl install

    • Linkerd: Another service mesh option.

linkerd install | kubectl apply -f -

  1. Kubernetes Operators:
    • Use operators to manage complex applications.
    • KubeDB: Manage databases on Kubernetes.

kubectl apply -f https://github.com/kubedb/installer/blob/v0.13.0/deploy/kubedb.yaml

  1. Autoscaling:
    • Horizontal Pod Autoscaler (HPA): Ensure HPA is configured to scale pods based on metrics.

kubectl get hpa --all-namespaces

kubectl describe hpa -n <namespace>

  1. Cluster Upgrades and Management:
    • Kubeadm: Regularly upgrade the cluster using kubeadm.

kubeadm upgrade plan

kubeadm upgrade apply <version>

    • Cluster Autoscaler: Automatically adjust the size of the cluster.

kubectl apply -f cluster-autoscaler.yaml

  1. Compliance and Auditing:
    • Regularly audit cluster configurations and compliance with policies.
    • OPA (Open Policy Agent) & Gatekeeper: Enforce policies and compliance.

kubectl apply -f https://raw.githubusercontent.com/open-policy-agent/gatekeeper/master/deploy/gatekeeper.yaml

Real-time Monitoring and Maintenance

In a production environment, you should also have automated scripts and tools for real-time monitoring and maintenance:

  • Kube-ops-view: Provides a graphical interface to visualize the cluster status.
  • Kubernetes Dashboard: A web-based UI for Kubernetes clusters.

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml

  • Regular Health Checks: Implement regular health checks and alerts for critical components.

By integrating these tools and practices, you can enhance the monitoring, security, and management of your Kubernetes clusters in a production environment like Infosys.

 

 


Newest
Previous
Next Post »