DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Kubernetes Tutorial

Introduction to Kubernetes
Kubernetes Architecture
Installing Kubernetes
kubectl Basics
Kubernetes Pods
Kubernetes Deployments
Kubernetes Services
ConfigMaps and Secrets
Kubernetes Storage
Kubernetes Namespaces
Kubernetes Resource Management
Helm for Kubernetes
Kubernetes Monitoring

kubectl exec, logs, and port-forward

PreviousPrev
Next

Debug running containers, stream logs, inspect previous crashes, and forward local ports into the cluster.

Why These Commands Matter

Once workloads are running, the next challenge is seeing what they are doing. exec, logs, and port-forward are some of the most practical debugging tools in Kubernetes.

Run a Command Inside a Pod

kubectl exec -it my-pod -- sh
kubectl exec -it my-pod -- bash

Use sh when bash is not installed in the container image.

If the pod has multiple containers:

kubectl exec -it my-pod -c app -- sh

Read Logs

kubectl logs my-pod
kubectl logs my-pod -c app

Follow Logs Live

kubectl logs -f my-pod

This is useful when watching a startup sequence or investigating a live problem.

Read Logs from a Crashed Container

kubectl logs --previous my-pod

That command is especially useful when a container restarts quickly and the current logs no longer show the first failure.

Port Forward into the Cluster

kubectl port-forward pod/my-pod 8080:80
kubectl port-forward service/web 8080:80

This opens a local port on your machine and forwards traffic to a pod or service in the cluster.

Think of port-forward as a temporary private tunnel into the cluster for testing and debugging.

A Practical Debug Loop

kubectl logs my-pod
kubectl logs --previous my-pod
kubectl exec -it my-pod -- sh
kubectl port-forward service/web 8080:80

When to Use What

CommandBest for
kubectl logsSeeing application stdout and stderr
kubectl execInspecting container state from inside
kubectl port-forwardTesting cluster services from your laptop

Caution

Avoid treating exec as the main way to operate production apps. It is a debugging tool, not a substitute for good observability or proper configuration management.

Exercise

Previous Logs

Which command helps you inspect the logs from the last crashed container instance?

Exercise

Port Forward Use

Why would you use `kubectl port-forward service/web 8080:80`?

PreviousPrev
Next

Continue Learning

kubectl Basics

Learn how kubectl fits into Kubernetes workflows and when to use imperative commands versus declarative manifests.

15 min·Easy

kubectl get and describe

Inspect pods, nodes, deployments, and services with kubectl get and use describe output to understand object behavior and events.

18 min·Easy

kubectl apply and delete

Create, update, compare, and remove Kubernetes resources using both declarative manifests and imperative helper commands.

18 min·Easy

Explore Related Topics

Do

Docker Tutorials

Build the containers Kubernetes orchestrates

Te

Terraform Tutorials

Provision Kubernetes clusters as code

Try the Tool

YAML Validator

Validate Kubernetes manifests and any YAML files instantly.

On This Page

Why These Commands MatterRun a Command Inside a PodRead LogsFollow Logs LiveRead Logs from a Crashed ContainerPort Forward into the ClusterA Practical Debug LoopWhen to Use WhatCaution