0% found this document useful (0 votes)
56 views3 pages

Kubernetes Command Reference Guide

This document provides examples of commands for interacting with a Kubernetes cluster using kubectl including: - Getting information on nodes, pods, services, versions and cluster configuration - Managing pods, replication controllers, and services - Scaling replication controllers - Mapping ports for services - Deleting pods, replication controllers, and services - Draining and deleting nodes - Executing commands on pods and getting logs - Initializing and joining nodes to a Kubernetes cluster - Creating and deleting namespaces and secrets

Uploaded by

sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views3 pages

Kubernetes Command Reference Guide

This document provides examples of commands for interacting with a Kubernetes cluster using kubectl including: - Getting information on nodes, pods, services, versions and cluster configuration - Managing pods, replication controllers, and services - Scaling replication controllers - Mapping ports for services - Deleting pods, replication controllers, and services - Draining and deleting nodes - Executing commands on pods and getting logs - Initializing and joining nodes to a Kubernetes cluster - Creating and deleting namespaces and secrets

Uploaded by

sundar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

kubectl get services # List all services

kubectl get pods # List all pods


kubectl get nodes -w # Watch nodes continuously
kubectl version # Get version information
kubectl cluster-info # Get cluster information
kubectl config view # Get the configuration
kubectl describe node <node> # Output information about a node
kubectl get pods # List the current pods
kubectl describe pod <name> # Describe pod <name>
kubectl get rc # List the replication controllers
kubectl get rc --namespace="<namespace>" # List the replication controllers in
<namespace>
kubectl describe rc <name> # Describe replication controller <name>
kubectl get svc # List the services
kubectl describe svc <name> # Describe service <name>

kubectl run <name> --image=<image-name> # Launch a pod


called <name>
# using image
<image-name>
kubectl create -f <[Link]> # Create a
service described
# in
<[Link]>
kubectl scale --replicas=<count> rc <name> # Scale
replication controller
# <name> to
<count> instances
kubectl expose rc <name> --port=<external> --target-port=<internal> # Map port
<external> to
# port
<internal> on replication
# controller
<name>
kubectl delete pod <name> # Delete pod
<name>
kubectl delete rc <name> # Delete
replication controller <name>
kubectl delete svc <name> # Delete service
<name>
kubectl drain <n> --delete-local-data --force --ignore-daemonsets # Stop all pods
on <n>
kubectl delete node <name> # Remove <node>
from the cluster
kubectl exec <service> <command> [-c <$container>] # execute <command> on
<service>, optionally
# selecting container
<$container>
kubectl logs -f <name> [-c <$container>] # Get logs from service <name>,
optionally
# selecting container
<$container>
watch -n 2 cat /var/log/[Link] # Watch the Kublet logs
kubectl top node # Show metrics for nodes
kubectl top pod # Show metrics for pods
kubeadm init # Initialize your master
node
kubeadm join --token <token> <master-ip>:<master-port> # Join a node to your
Kubernetes cluster
kubectl create namespace <namespace> # Create namespace <name>
kubectl taint nodes --all [Link]/master- # Allow Kubernetes master
nodes to run pods
kubeadm reset # Reset current state
kubectl get secrets # List all secrets

============================ cluster =====================================

# start up a cluster
KUBERNETES_PROVIDER=vagrant ./cluster/[Link]

# start a simple vagrant cluster


NUM_NODES=1 KUBERNETES_PROVIDER=vagrant KUBE_ENABLE_CLUSTER_MONITORING=none
KUBE_ENABLE_CLUSTER_UI=false ./cluster/[Link]

# validate cluster
./cluster/[Link]
kubectl cluster-info

# delete all rc & svc


kubectl delete svc,rc --all
kubectl delete $(kubectl get rc,svc -o name)

# watch for events


kubectl get ev -w

# schema / avaiable fields for rc/pods/svc ...


[Link]

# a simple service
[Link]
guide/walkthrough/[Link]

# available signals for a pod


[Link]

# debug
kubectl logs --previous <pod>

# start a simple container


kubectl run busybox --image=busybox

# get system services


kubectl get svc --all-namespaces

# a debug container
kubectl run curlpod --image=radial/busyboxplus:curl --command -- /bin/sh -c "while
true; do echo hi; sleep 10; done"
kubectl exec -it curlpod-5f0mh nslookup redis

# upload files
kubectl exec -i ghost-deployment-1955090760-zivlz -- /bin/bash -c 'cat >
/tmp/[Link]' < [Link]

# more debug
[Link]

# create a new config


PROJECT_ID='mdl-k8s'
CLUSTER='cluster-4'
CLUSTER_ZONE='europe-west1-c'

gcloud config set project $PROJECT_ID


gcloud config set container/cluster $CLUSTER
gcloud config set compute/zone $CLUSTER_ZONE
gcloud container clusters get-credentials $CLUSTER

kubectl config use-context gke_${PROJECT_ID}_${CLUSTER_ZONE}_${CLUSTER}

Common questions

Powered by AI

`kubectl exec <service> <command>` allows execution of commands in a running container, which is useful for troubleshooting by directly interacting with application processes . While it provides a powerful way to diagnose issues like misconfigurations and test application behavior in real-time, it has limitations: it may not be effective for analyzing non-interactive workloads, and there's inherent risk if used inappropriately, as direct command execution can inadvertently modify application state.

Initializing a master node using `kubeadm` is done by executing `kubeadm init`. This step sets up the control plane and configures essential components like the API Server, Controller Manager, and Scheduler . It's crucial for setting up a Kubernetes cluster because it forms the foundation for managing and orchestrating other nodes and Pods within the cluster, enabling centralized control and coordination of deployments.

Executing `kubectl describe pod <name>` provides detailed information about a specific pod, including its status, resource usage, events, and condition messages. This detailed output is particularly useful during debugging as it helps identify the state and potential issues with the pod, such as configuration errors, resource constraints, or network issues . Understanding these details is crucial for resolving problems that affect the pod's performance or stability.

You can scale a replication controller to a specific number of instances in Kubernetes using the command `kubectl scale --replicas=<count> rc <name>`. When scaling, it's essential to consider the resource limits and quotas of your cluster to ensure that sufficient resources are available for the scaled instances to operate effectively . Furthermore, scaling should be done with awareness of the application’s stateful or stateless nature, as stateful applications may require additional configuration or data management considerations.

To expose an external port on a Kubernetes service, use the `kubectl expose rc <name> --port=<external> --target-port=<internal>` command. This maps an external port to an internal one on a replication controller . The significance of this mapping lies in controlling network access: internal ports facilitate communication within the cluster, while external ports allow external entities to access services, crucial for public-facing applications. Careful mapping ensures secure and efficient traffic management.

To create a new namespace in Kubernetes, use the command `kubectl create namespace <namespace>` . Namespaces are used to organize and manage resources within a cluster, providing a means to separate different environments or teams and control access to resources. This segregation enhances security, ease of management, and scalability by logically dividing cluster resources.

You can validate the status of a Kubernetes cluster using the `kubectl cluster-info` command. This provides information about the cluster's master and other control-plain components . Validation is essential to ensure all components are correctly configured and operational, helping identify any misconfigurations or connectivity issues affecting the cluster's functionality.

`kubectl logs` helps monitor application health by providing real-time logs from the running application or service, offering insights into application behavior, errors, and performance issues . However, using logs alone has limitations: they may not capture all aspects of system health, such as hardware or network status, and may lack granularity for specific application metrics. Logs also require effective management and analysis tools to derive meaningful insights, and excessive logging can impact performance.

Configuration management in Kubernetes involves managing and setting the cluster configurations to ensure a consistent and scalable environment. Using `kubectl config view` allows you to visualize the current Kubernetes configurations, including cluster-related information, contexts, and user settings . This is critical for managing multiple Kubernetes clusters efficiently, ensuring consistency, and simplifying transitions between environments. However, it requires careful handling and understanding of configuration files to prevent misconfigurations that can lead to security vulnerabilities or operational inefficiencies.

The `kubectl delete node <name>` command removes a node from the cluster, which can be beneficial for maintenance, upgrading hardware, or reallocating resources . However, it also carries risks such as potential data loss if pods aren't carefully drained and moved beforehand. It might also affect workload distribution and cluster performance if not properly planned and executed, particularly in clusters with high load.

You might also like