DEV Community

Cover image for Kubectl Demystified: Mastering the `kubectl create` Command
Naveen.S
Naveen.S

Posted on

Kubectl Demystified: Mastering the `kubectl create` Command

Kubernetes has become the de facto standard for container orchestration, and mastering its command-line tool, kubectl, is essential for administrators and developers. One of the most frequently used commands is kubectl create, which allows you to imperatively create Kubernetes resources. This article explains what kubectl create is, how it works, when to use it, and provides practical examples to solidify your understanding.

What is kubectl create?

kubectl create is an imperative command used to create Kubernetes resources directly from the command line or a YAML/JSON manifest file. Unlike kubectl apply (which is declarative and idempotent), create is designed to explicitly generate new resources. If a resource already exists, running kubectl create again will result in an error.

Key Characteristics:

  • Imperative Management: You explicitly tell Kubernetes what to create.
  • Non-Idempotent: Fails if the resource already exists.
  • Supports YAML/JSON Files: Can create resources from manifest files.
  • Subcommand-Based: Offers shortcuts for common resources (e.g., deployment, namespace).

When to Use kubectl create?

  1. One-Time Resource Creation: Ideal for creating resources that don’t require updates (e.g., namespaces, test pods).2. Quick Prototyping: Quickly spin up resources without writing YAML files.3. CKA Exam Scenarios: The Certified Kubernetes Administrator (CKA) exam often expects you to use imperative commands to save time.4. Generating Manifest Templates: Use --dry-run to auto-generate YAML/JSON templates.

kubectl create vs. kubectl apply

Feature kubectl create kubectl apply
Approach Imperative Declarative
Idempotency Fails if resource exists Creates or updates resources
Use Case Initial creation Ongoing management
Manifest Files Optional Required for declarative workflow

Common Usage and Examples

1. Creating a NamespaceNamespaces isolate resources in a cluster. To create one:


bashkubectl create namespace test-ns

  • Explanation: Creates a namespace named test-ns.

2. Creating a Pod from a YAML FileSave the following as pod.yaml:


yamlapiVersion: v1kind: Podmetadata:  name: nginx-podspec:  containers:  - name: nginx    image: nginx:latest

Run:

bashkubectl create -f pod.yaml

  • Explanation: Creates an NGINX pod using the YAML manifest.

3. Creating a Deployment ImperativelyDeployments manage pods and replicas:


bashkubectl create deployment nginx-deploy --image=nginx --replicas=3

  • Explanation: Creates a deployment named nginx-deploy with 3 replicas of the NGINX container.

4. Creating a ServiceExpose a deployment as a service:


bashkubectl create service clusterip my-svc --tcp=80:80

  • Explanation: Creates a ClusterIP service named my-svc routing traffic to port 80.

5. Creating ConfigMaps and SecretsConfigMap (store configuration data):


bashkubectl create configmap app-config --from-literal=ENV=prod

Secret (store sensitive data):

bashkubectl create secret generic db-secret --from-literal=password=1234

6. Creating Jobs and CronJobsJob (run a task once):


bashkubectl create job calculate-pi --image=perl -- perl -Mbignum=bpi -wle 'print bpi(2000)'

CronJob (run tasks periodically):

bashkubectl create cronjob daily-backup --image=busybox --schedule="0 0 * * *" -- /bin/sh -c "date"

Useful Flags for the CKA Exam

  1. --dry-run=client: Test a command without creating resources.    bash   kubectl create deployment test --image=nginx --dry-run=client -o yaml       - Generates a YAML template for the deployment.
  2. -o yaml: Output the resource definition in YAML format.

       bash   kubectl create namespace demo --dry-run=client -o yaml   

  3. --save-config: Stores current configuration in the resource’s annotations (useful for future apply commands).

When Not to Use kubectl create

  • Updating Resources: Use kubectl apply or kubectl edit instead.- Complex Configurations: Prefer writing YAML manifests for better readability and version control. --- ## CKA Exam Tips
  1. Use Imperative Commands: They save time compared to writing YAML from scratch.
  2. Generate Manifests Quickly: Combine --dry-run and -o yaml to create templates.
  3. Practice Common Tasks: Know how to create deployments, services, and namespaces imperatively.

Troubleshooting

  • Error: Resource Already Exists: Delete the resource first with kubectl delete <resource>/<name>.
  • Invalid Syntax: Use kubectl explain <resource> to see required fields (e.g., kubectl explain pod.spec).

Conclusion

The kubectl create command is a powerful tool for imperatively managing Kubernetes resources. While declarative approaches with kubectl apply are preferred for production, create shines in scenarios requiring speed and simplicity, such as the CKA exam. Mastery of this command, along with its flags and subcommands, will significantly enhance your Kubernetes workflow.

Top comments (0)