kubectl-create
Create Kubernetes resources from file or input
TLDR
Create a resource using the resource definition file
Create a resource from stdin
Create a deployment
Create a deployment with replicas
Create a service
Create a namespace
SYNOPSIS
kubectl create [TYPE] [NAME] [flags]
kubectl create -f FILENAME [flags]
kubectl create -k DIRECTORY [flags]
FILENAME can be a file, directory, or URL specifying the resource definition(s).
TYPE refers to a Kubernetes resource type (e.g., deployment
, service
, pod
).
NAME is the specific name for the resource.
PARAMETERS
-f, --filename stringArray
Specifies the path to one or more files, directories, or URLs containing the resource configuration in YAML or JSON format. Use '-' for standard input.
--dry-run string
If set to 'client', only prints the object that would be sent without actually creating it. If 'server', sends the request but asks the server not to persist changes. Use 'none' to disable.
-o, --output string
Prints the created object in the specified format (e.g., json
, yaml
) when combined with --dry-run
.
-k, --kustomize string
Processes a Kustomization base directory to generate and create resources.
-R, --recursive
If true, processes the directory specified with -f
or --filename
recursively.
--validate
If true, performs schema validation of the input before sending the request to the API server.
--save-config
If true, the configuration of the created object is saved as an annotation, which can be useful for subsequent kubectl apply operations.
DESCRIPTION
kubectl create is a fundamental command in Kubernetes for generating and creating new resources within your cluster. It primarily operates by taking resource definitions from files, directories, or standard input, allowing users to define their desired state declaratively. For instance, you can create a Deployment, Service, or ConfigMap by pointing kubectl create to its YAML or JSON manifest file. While primarily used for declarative creation via files (-f
), it also supports imperative creation for certain resource types like secrets or configmaps directly from the command line. This command is crucial for initial deployments, setting up new applications, or provisioning specific infrastructure components within a Kubernetes environment. Unlike kubectl apply, create will fail if the resource already exists, making it non-idempotent for updates.
CAVEATS
kubectl create is not idempotent; if the resource it attempts to create already exists, the command will fail with an error. For continuous deployment workflows or when updating existing resources, kubectl apply is generally preferred as it is idempotent and handles updates gracefully. Care should be taken when creating resources from untrusted sources, as the command directly interacts with your cluster's API.
DECLARATIVE VS. IMPERATIVE CREATION
kubectl create supports both declarative and imperative approaches. Using the -f
flag with a YAML/JSON manifest is a declarative way to define your desired state, allowing Kubernetes to reconcile it. Creating resources directly from the command line, such as kubectl create secret generic my-secret --from-literal=key=value
, is an imperative approach, specifying the action directly. While create -f is common for declarative setups, kubectl apply is often the more robust declarative tool for managing resource lifecycle beyond initial creation.
IDEMPOTENCY
A key difference between kubectl create and kubectl apply lies in idempotency. create is not idempotent; it will fail if a resource with the same name and type already exists in the target namespace. In contrast, kubectl apply is idempotent: it will create the resource if it doesn't exist, and update it if it does, making it suitable for continuous integration/continuous deployment (CI/CD) pipelines where operations might be retried.
SEE ALSO
kubectl apply(1), kubectl delete(1), kubectl get(1), kubectl edit(1), kubectl run(1)