דלג לתוכן הראשי

Most useful Commands

- Command 1: show logs of pod

The command:

kubectl logs POD_NAME -n NAMESPACE -f

Description:

Show logs inside a pod.


- Command 2: port-forward

The command:

kubectl port-forward TYPE/NAME [options] [LOCAL_PORT:]REMOTE_PORT [...[LOCAL_PORT_N:]REMOTE_PORT_N]

Description:

You way of connecting to a pod from the host.

The kubectl port-forward command in Kubernetes allows you to forward one or more local ports to a port on a Pod (or other Kubernetes resource like a Service). This is useful for debugging or temporarily accessing services running inside your cluster without exposing them externally.

Use resource type/name such as deployment/my_deployment to select a pod. Resource type defaults to pod if omitted.

Common Use-Case:

Accessing a web app running in a pod:

kubectl port-forward svc/my-service 8080:80

You can now access the service at http://localhost:8080.

Notes:

  • This is a temporary and local-only tunnel; it's not for production use.
  • It requires kubectl to stay running in your terminal.
  • You must have access to the pod via your Kubernetes context.

- Command 3: kubectl exec

The command:

kubectl exec -it -n NAMESPACE POD_NAME -c CONTAINER_NAME -- sh

Description:

Use this command to:

  1. Execute a command inside a running pod.
  2. Execute a command inside a running container inside a running pod.
  • If the -n flag is omitted, namespace defaults to default.
  • If the -c flag is omitted, ssh is to the pod.
  • If the -c flag is used, ssh is to the container inside the pod.

This command is very similar to the docker exec command.

Common Use-Case:

kubectl exec -it -n solve-dev solvebe-admin-server-3d45e6 -c solve-admin-server -- sh

- Command 4: manually delete a pod

The command:

kubectl delete pod POD_NAME

Description:

Deletes a pod by name.

A deleted pod, which was manually created, would be deleted forever.
A deleted pod, which was created by a deployment, would also be deleted, but a few seconds later, the deployment would create a new pod that would have a different hash and a IP address.


- Command 5: rollout status deploy

The command:

kubectl rollout status deploy DEPLOYMENT_NAME

Description:

The terminal is kept busy until the rollout is complete.
You get online updates about completed steps.
Most commonly used with:
kubectl scale deployment DEPLOYMENT_NAME --replicas=#number

Example Usage:

kubectl rollout status deploy ai-gateway-service -n dev-chatbot

- Command 6: manually scale a deployment

The command:

kubectl scale deployment DEPLOYMENT_NAME --replicas=#number

Example Usage:

kubectl rollout status deploy ai-gateway-service -n dev-chatbot