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
| Scenario | Use |
|---|---|
| Quick testing and experimentation | kubectl create |
| Learning and exploring resources | kubectl create |
| Production deployments | kubectl apply -f |
| Version-controlled infrastructure | kubectl apply -f |
| CI/CD pipelines | kubectl apply -f |
| Generating YAML templates | kubectl 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 createis the imperative way to create resources quickly- It supports many resource types: deployments, services, configmaps, secrets, jobs, and more
- Use
createfor learning and quick tasks; useapplyfor production - The
--dry-run=client -o yamltrick generates YAML templates from imperative commands kubectl exposeis a quick way to create a Service for an existing Deployment