• Explore
  • About Us
  • Log In
  • Get Started
  • Explore
  • About Us
  • Log In
  • Get Started
05:00

Creating a Pod with an Image and Label

While YAML files are the declarative standard for managing Kubernetes objects in production, imperative commands are an indispensable tool for quick tasks, debugging, and scripting. The kubectl run command allows you to create certain cluster resources on the fly without writing a manifest file. Mastering these commands is essential for both day-to-day administration and for success on the CKA exam, where speed and efficiency are critical.

Crucial Note: Evolution of kubectl run

Important: In older Kubernetes versions (before 1.18), the kubectl run command would create a Deployment object. In modern Kubernetes, its primary function is to create a single Pod, which is ideal for testing and debugging. Be aware of this change when consulting older documentation or exam materials. This evolution makes kubectl run more focused and predictable for its current primary use cases.

Scenario

You are a developer who needs to quickly deploy a simple NGINX pod for a test. You don't need a persistent configuration file; you just need to get the pod running immediately to verify a network setting. Additionally, the pod must have a specific label so it can be easily identified and cleaned up later. Your task is to accomplish this using a single kubectl command.

Requirements

  • Create a new Pod named test-pod using the nginx:1.25-alpine container image.
  • Assign the label env=test to the newly created Pod.
  • Accomplish this using a single, imperative kubectl command.

Acceptance Criteria

  • A Pod named test-pod exists in the default namespace.
  • The test-pod is in a Running state.
  • The test-pod is using the nginx:1.25-alpine image.
  • The test-pod has the label env=test correctly applied.

Environment Setup

This exercise does not require any initial setup. You will be creating the required resources from scratch using imperative commands.

Resources

  • Official kubectl run Documentation
  • Official Kubernetes Documentation: Labels and Selectors

Possible Ways to Implement

  • The primary command for creating a Pod imperatively is kubectl run.
  • How do you specify the container image to use with the kubectl run command? Check its help (kubectl run --help) for the relevant flag.
  • How do you apply labels to an object using an imperative command? Look for a --labels flag.

Real-World Significance

Important Note on Image Versions: In this exercise, we use nginx:1.25-alpine as an example. The Alpine variant is preferred as it provides a smaller, more secure base image with a reduced attack surface. In production environments, you should always:

  • Check for the latest stable version from official sources (Docker Hub, vendor documentation)
  • Use specific version tags rather than latest for predictable deployments
  • Consider Alpine variants for reduced image size and attack surface
  • Regularly update images to include security patches
  • Verify image integrity and scan for vulnerabilities before deployment

While declarative YAML is the best practice for production workloads, imperative commands like kubectl run are essential for efficiency in many situations:

  • Rapid Prototyping and Testing: Quickly launching a pod to test a configuration, a network connection, or an application feature without the overhead of writing a YAML file.
  • Debugging: Launching a temporary "toolbox" pod with debugging tools into a specific namespace to troubleshoot live issues. A common use case is launching an interactive toolbox pod to debug network issues from within the cluster:
    kubectl run net-debug --image=busybox:1.36 -it --rm -- /bin/sh
    
    This gives you a shell inside the cluster to run tools like ping and nslookup.
  • Scripting and Automation: Imperative commands are easily integrated into shell scripts for automating repetitive tasks.
  • Certification Exams: The CKA and CKAD exams are time-sensitive. Being able to create and modify resources quickly with imperative commands is a critical skill for passing. Mastering kubectl run allows you to work more efficiently and respond to issues more quickly in your day-to-day Kubernetes operations.

    CKA Practice Exercises

    Unlock All Exercises

  • Cluster Architecture, Installation & Configuration
    • Setting up a Kubernetes Cluster with Kubeadm
    • Managing Cluster Certificates
    • Upgrading a Kubernetes Cluster
    • Implementing RBAC for Users and ServiceAccounts
    • Configuring Kubeconfig Files
    • Using Helm to Deploy Applications
    • Managing Kubernetes Manifests with Kustomize
    • Understanding CNI, CSI, CRI
    • Managing etcd Backups and Restores
    • API Server Authentication and Authorization Basics
  • Workloads & Scheduling
    • Deploying Applications with Deployments
    • Performing Rolling Updates and Rollbacks
    • Configuring ConfigMaps and Secrets
    • Implementing Horizontal Pod Autoscaling
    • Managing Pod Scheduling with Taints and Tolerations
    • Controlling Pod Placement with Node Selectors and Affinity
    • Configuring Pod Security Context
  • Services & Networking
    • Creating ClusterIP, NodePort, and LoadBalancer Services
    • Configuring Ingress with Gateway API
    • Understanding CoreDNS and DNS Resolution
    • Implementing Network Policies for Pod Isolation
    • Troubleshooting Network Connectivity
  • Storage
    • Creating Persistent Volumes and Claims
    • Implementing Storage Classes and Dynamic Provisioning
    • Configuring Volume Access Modes
    • Using Local Persistent Volumes
  • Troubleshooting
    • Troubleshooting Pod Startup Issues
    • Debugging Application Logs
    • Troubleshooting Node Issues
    • Debugging Service Connectivity
    • Troubleshooting Network Policy Issues
    • Diagnosing Control Plane Component Failures
    • Troubleshooting Storage Issues
    • Monitoring Resource Usage
    • Inspecting etcd with etcdctl
    • Checking Control Plane Component Logs
  • Imperative Kubectl Practice
    • Create a Pod with Image and Label
    • Expose Deployment as Service
    • Scale Deployment Imperatively
    • Update Image Imperatively