Common Kubernetes Commands

Overview

kubectl is a command line tool for interacting with the Kubernetes Control Plane, which manages infrastructure deployment into Kubernetes. This document lists several common kubectl commands that can be run on the Kubernetes cluster that runs the Kinetic Data platform.

Generic kubectl Commands

The following section includes a non-comprehensive list of generic kubectl commands that are often used when interacting with the Kubernetes cluster. These commands aren’t specific to Kinetic Data, but we often use them.

Server (Node) Commands

Nodes can be virtual or physical machines, depending on the cluster type. Pods are scheduled by Kubernetes to run on these nodes. We’ll discuss Pods more later in this document.

  • Get nodes: Gets all of the nodes (servers) that make up the Kubernetes cluster.
    kubectl get nodes
    
  • Cordon a node: This stops Kubernetes from scheduling future pods on the cordoned node. This is typically used when doing underlying server changes.
    kubectl cordon <NODE>
    
  • Uncordon a node: This allows Kubernetes to schedule future pods on the node. This is typically used after doing underlying server changes.
    kubectl uncordon <NODE>
    
  • Drain a node: This will tell Kubernetes to evict all pods that are not part of daemonsets from the node. Barring resource and affinity constraints, Kubernetes will attempt to put the pods onto one of the other nodes in the cluster.
    kubectl drain <NODE> --ignore-daemonsets --delete-empty-dir
    

Deployment Commands

Deployments and their flavors are used to manage pod collections that run workloads on Kubernetes nodes.

  • Get deployments: Get all deployments that are deployed in a given namespace.
    kubectl get deployments -n <NAMESPACE>
    
  • Deployment details: Describe a deployment.
    kubectl describe deployment <NAME> -n <NAMESPACE>
    
  • Restart deployment: Perform a rolling restart of a deployment in a namespace.
    kubectl rollout restart deployment <NAME> -n <NAMESPACE>
    
  • Deployment restart status: Get the status of an active rolling restart of a deployment.
    kubectl rollout status deployment <NAME> -n <NAMESPACE>
    
  • Scale deployment: Scale the number of pods to COUNT in a deployment. Not applicable for daemonsets.
    kubectl scale deployment <NAME> -n <NAMESPACE> --replicas=<COUNT>
    

Pod Commands

Pods are the smallest deployable units that can be created in Kubernetes. Pods are typically not deployed directly. Instead, they are usually deployed via workload resources such as Deployments.

A pod can be comprised of one or more containers, which are software packages built as images that are run within a pod.

  • Get all pods: Returns all pods in a given namespace.
    kubectl get pods -n <NAMESPACE>
    
  • Pod details: Get information on a pod, such as volumes mounted on the pod.
    kubectl describe pod <POD> -n <NAMESPACE>
    
  • Delete a pod: Deletes the pod. If the pod is part of a deployment, Kubernetes will attempt to replace the pod.
    kubectl delete pod <POD> -n <NAMESPACE>
    
  • View log files: Follow the container logs within a pod.
    -kubectl logs <POD> -n <NAMESPACE> -c <CONTAINER>
    
  • Shelling into a container: Shell into a container within a pod (if the container has a shell available). The first example is for a shell, the second is for a bash.
    kubectl exec -it <POD> -n <NAMESPACE> -c <CONTAINER> -- sh
    
    or
    kubectl exec -it <POD> -n <NAMESPACE> -c <CONTAINER> -- bash
    
  • Run commands in a container: Execute a command within a container that is located within a pod). Example shows running the "whoami" command.
    kubectl exec -it <POD> -n <NAMESPACE> -c <CONTAINER> -- whoami
    
  • Copy container files: Copy files off of a container within a pod to local machine.
    kubectl cp -n <NAMESPACE> -c <CONTAINER>  <POD>:\<PATH_ON_POD> \<PATH_LOCALLY>
    

Use Case Examples

Following the Core Pod Logs

When troubleshooting issues with the Core service in Kinetic Data, it’s sometimes helpful to follow the application.log file within the Core pod in Kubernetes. From there, we can perform an action that interacts with the Core service and then watch the logs in real time .

We can do this a couple of different ways:

Method 1: Shell into the Core Pod and Follow the Logs

# Get all of the pods running in the kinetic namespace
kubectl get pods -n kinetic

# --- Output --- (shorted for brevity, other pods are also running)
# NAME                                       READY   STATUS      RESTARTS       AGE
# agent-7bbb8cb5d5-6r8fp                     2/2     Running     0              1d
# core-8b554599c-5g85f                       2/2     Running     0              1d
# ...

# Describe the core pod
kubectl describe pod core-8b554599c-5g85f -n kinetic

# --- Output (shorted for brevity, to show the containers running on the pod) ---
# ...
# Containers:
#   nginx:
#     ...
#   core:
# 	  ...

# Shell into the core container, running on the core pod, 
#   this will put us into the container.
# The "output" will be a new shell but from within the container
kubectl exec -it -c core -n kinetic core-8b554599c-5g85f -- bash

# Change Directory into the /app/core/logs/ folder
cd /app/core/logs

# Follow the application.log file
# Ctrl-C to stop following the file at any time
tail -f application.log

# To exit the session
exit

Method 2: Follow the application.log file Without Shelling Into the Container

# Get all of the pods running in the kinetic namespace
kubectl get pods -n kinetic

# --- Output --- (shorted for brevity, other pods are also running)
# NAME                                       READY   STATUS      RESTARTS       AGE
# agent-7bbb8cb5d5-6r8fp                     2/2     Running     0              1d
# core-8b554599c-5g85f                       2/2     Running     0              1d
# ...

# Describe the core pod
kubectl describe pod core-8b554599c-5g85f -n kinetic

# --- Output (shorted for brevity, to show the containers running on the pod) ---
# ...
# Containers:
#   nginx:
#     ...
#   core:
# 	  ...

# Execute the tail command without accessing the shell within the core pod/container
# Ctrl-C will to stop watching the log files anytime
kubectl exec -it -c core -n kinetic core-8b554599c-5g85f -- tail -f /app/core/logs/application.log

Copying the application.log File off of the Core Pod

It can be helpful to copy off the application.log file within the Core pod in Kubernetes. This lets you look at the application logs from the Core service, or send the log file to Kinetic Data Support to assist with troubleshooting.

# Get all of the pods running in the kinetic namespace
kubectl get pods -n kinetic

# --- Output --- (shorted for brevity, other pods are also running)
# NAME                                       READY   STATUS      RESTARTS       AGE
# agent-7bbb8cb5d5-6r8fp                     2/2     Running     0              1d
# core-8b554599c-5g85f                       2/2     Running     0              1d
# ...

# Describe the core pod
kubectl describe pod core-8b554599c-5g85f -n kinetic

# --- Output (shorted for brevity, to show the containers running on the pod) ---
# ...
# Containers:
#   nginx:
#     ...
#   core:
# 	  ...

# Copy the application.log file to my home directory on the server I am on
kubectl cp -n kinetic -c core core-8b554599c-5g85f:/app/core/logs/application.log ~/application.log

# Open the file in vi for viewing
# (or, cat the file)
vi ~/application.log
cat ~/application.log

Restarting the Core Deployment

It can be helpful to restart the Core deployment as part of the troubleshooting process (for example, if Core is behaving abnormally, a restart may fix the problem).

# Get the pods running in the kinetic namespace
# While this isn't necessary for a restart, we may want to verify first the core pods are running
# We pipe the output to grep to find only the core pods
kubectl get pods -n kinetic | grep core

# --- Output ---
# core-8b554599c-5g85f                       2/2     Running     0              1d

# Get our list of deployments
kubectl get deployment -n kinetic

# --- Output ---
# NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
# ...
# core                      1/1     1            1           1d
# ...

# Rolling restart the core deployment
# Kubernetes will spin up replacement pods before terminating the old pods
# to keep availability
kubectl rollout restart deployment core -n kinetic

# --- Output ---
# deployment.apps/core restarted

# Check the status of the rollout restart
# The command will automatically stop when the restart is complete
# Hit Ctrl-C to stop watching for the restart to complete (this will not stop the restart)
kubectl rollout status deployment core -n kinetic

# --- Output ---
# Waiting for deployment "core" rollout to finish: 1 out of 2 new replicas have been updated...
# Waiting for deployment "core" rollout to finish: 1 out of 2 new replicas have been updated...
# Waiting for deployment "core" rollout to finish: 1 out of 2 new replicas have been updated...
# Waiting for deployment "core" rollout to finish: 1 old replicas are pending termination...
# Waiting for deployment "core" rollout to finish: 1 old replicas are pending termination...
# deployment "core" successfully rolled out

Additional Resources

You can find more information on the various Kubernetes components that work with the Kinetic Platform here: