Sign In

Curriculum 6: Creating Resources

kubectl create

12 min · 15 XP

kubectl create -- Imperative Resource Creation

The kubectl create command lets you create Kubernetes resources directly from the command line without writing YAML files. It is fast, convenient, and perfect for learning and quick tasks.

Imperative vs Declarative

Kubernetes supports two styles of resource management:

  • Imperative (kubectl create) -- you tell Kubernetes exactly what to do right now
  • Declarative (kubectl apply) -- you describe the desired state in a file and let Kubernetes figure out the changes

Think of it this way: imperative is like giving turn-by-turn directions, while declarative is like giving someone an address and letting GPS figure out the route.

Common create Subcommands

kubectl create has many subcommands for different resource types:

# See all available subcommands
kubectl create --help

The most commonly used ones are:

# Create a deployment
kubectl create deployment my-app --image=nginx --replicas=3

# Create a namespace
kubectl create namespace development

# Create a service (ClusterIP)
kubectl create service clusterip my-service --tcp=80:8080

# Create a configmap from literal values
kubectl create configmap app-settings \
  --from-literal=DATABASE_HOST=db.example.com \
  --from-literal=LOG_LEVEL=info

# Create a configmap from a file
kubectl create configmap nginx-config --from-file=nginx.conf

# Create a secret from literal values
kubectl create secret generic api-keys \
  --from-literal=API_KEY=abc123 \
  --from-literal=API_SECRET=xyz789

# Create a job
kubectl create job backup-job --image=busybox -- /bin/sh -c "echo backing up data"

# Create a cronjob
kubectl create cronjob nightly-backup --image=busybox --schedule="0 2 * * *" -- /bin/sh -c "echo backup complete"

# Create a service account
kubectl create serviceaccount app-runner

Creating Deployments

The deployment subcommand is one you will use frequently:

# Basic deployment
kubectl create deployment nginx --image=nginx

# With replicas
kubectl create deployment web --image=nginx --replicas=3

# With a specific port
kubectl create deployment api --image=node:20 --port=3000

# Verify it was created
kubectl get deployment nginx
kubectl get pods -l app=nginx

Creating Services

After creating a Deployment, you often need a Service to expose it:

# Expose a deployment as a ClusterIP service
kubectl expose deployment nginx --port=80 --target-port=80

# Expose as a NodePort service
kubectl expose deployment nginx --port=80 --type=NodePort

# Expose as a LoadBalancer service
kubectl expose deployment nginx --port=80 --type=LoadBalancer

When to Use create vs apply

ScenarioUse
Quick testing and experimentationkubectl create
Learning and exploring resourceskubectl create
Production deploymentskubectl apply -f
Version-controlled infrastructurekubectl apply -f
CI/CD pipelineskubectl apply -f
Generating YAML templateskubectl create --dry-run=client -o yaml

A powerful technique is combining both: use create with --dry-run to generate YAML, then manage that YAML with apply:

# Generate a deployment YAML without actually creating it
kubectl create deployment my-app --image=nginx --replicas=3 \
  --dry-run=client -o yaml > deployment.yaml

# Review and edit the generated file, then apply
kubectl apply -f deployment.yaml

This gives you the speed of imperative creation with the reproducibility of declarative management.

Key Takeaways

  • kubectl create is the imperative way to create resources quickly
  • It supports many resource types: deployments, services, configmaps, secrets, jobs, and more
  • Use create for learning and quick tasks; use apply for production
  • The --dry-run=client -o yaml trick generates YAML templates from imperative commands
  • kubectl expose is a quick way to create a Service for an existing Deployment