cd ..
Kubernetes

Creating Deployments in Kubernetes: YAML Manifests and kubectl create

Creating Deployments in Kubernetes: YAML Manifests and kubectl create

Do you really need to write an entire YAML manifest to create a Deployment?

The answer is: it depends.

Kubernetes offers different ways to create a Deployment.

Some are excellent for quick studies.

Others are ideal for production environments.

In this article, we’ll explore these approaches and understand when to use each one.

Creating a Deployment via a Manifest

The most common way in real-world projects is by using YAML files.

It’s in this file that we declare how we want the application to behave.

A simple example:

apiVersion: apps/v1
kind: Deployment

metadata:
  name: nginx-deployment

spec:
  replicas: 3

  selector:
    matchLabels:
      app: nginx-deployment

  template:
    metadata:
      labels:
        app: nginx-deployment

    spec:
      containers:
        - name: nginx
          image: nginx:1.30.4

Then, simply apply the manifest.

kubectl apply -f deployment.yaml

Expected output:

deployment.apps/nginx-deployment created

Creating a Deployment Using Only a Command

It’s also possible to create a Deployment directly from the command line.

kubectl create deployment nginx-deployment \
    --image=nginx:1.30.4

Explaining the parameters:

Expected output:

deployment.apps/nginx-deployment created

This method is very useful for quick tests.

However, it has an important limitation.

All the configuration resides only in the cluster.

You don’t have a versioned manifest.

Defining the Number of Replicas

We can also specify the initial number of Pods.

kubectl create deployment nginx-deployment \
    --image=nginx:1.30.4 \
    --replicas=3

Expected output:

deployment.apps/nginx-deployment created

Now the Deployment will automatically create three Pods.

Generating a Manifest Automatically

There’s an extremely useful feature of kubectl.

We can ask it to generate the YAML without creating anything in the cluster.

kubectl create deployment nginx-deployment \
    --image=nginx:1.30.4 \
    --replicas=3 \
    --dry-run=client \
    -o yaml

Explaining the new parameters:

Expected output:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
...

This command is excellent for quickly starting a manifest.

Saving Directly to a File

We can also redirect this output.

kubectl create deployment nginx-deployment \
    --image=nginx:1.30.4 \
    --replicas=3 \
    --dry-run=client \
    -o yaml > deployment.yaml

Now we have a YAML file ready for editing.

This practice is very common during development.

Applying the Edited Manifest

After adjusting the file, simply apply it.

kubectl apply -f deployment.yaml

From this moment on, the manifest becomes the single source of truth for the application.

Viewing an Existing Deployment

Another very useful feature is exporting an existing Deployment.

kubectl get deployment nginx-deployment \
    -o yaml

Expected output:

apiVersion: apps/v1
kind: Deployment
metadata:
...
spec:
...
status:
...

This output allows you to study how Kubernetes stored that resource.

It can also serve as a basis for new manifests.

Exporting to a File

If you want to save the current Deployment:

kubectl get deployment nginx-deployment \
    -o yaml > temp.yaml

Then, simply remove the fields automatically generated by Kubernetes before reusing this file.

Listing Deployments

To view all Deployments:

kubectl get deployments

Or:

kubectl get deploy

Both forms are equivalent.

Expected output:

NAME               READY   UP-TO-DATE   AVAILABLE
nginx-deployment   3/3     3            3

Describing a Deployment

When we need to investigate a resource in more detail, we use:

kubectl describe deployment nginx-deployment

This command shows:

It’s one of the most used tools for troubleshooting.

Removing a Deployment

If it’s necessary to delete the Deployment:

kubectl delete deployment nginx-deployment

Expected output:

deployment.apps "nginx-deployment" deleted

When the Deployment is removed, the ReplicaSet and the Pods controlled by it are also removed.

Which Approach Should I Use?

MethodWhen to Use
kubectl create deploymentStudies, quick tests, and labs
YAML ManifestProduction, GitOps, and versioning
kubectl applyContinuous updates
kubectl get -o yamlStudying and exporting resources

In practice, professional environments almost always use manifests stored in Git repositories.

Summary

The main ways to create a Deployment are:

YAML Manifest


kubectl apply

or

kubectl create deployment


--dry-run=client


Generate YAML manifest

The second approach is an excellent way to speed up manifest creation during studies.

Conclusion

Now you know the main ways to create a Deployment.

We also saw how to automatically generate manifests, export existing resources, and use kubectl to simplify daily tasks.

In the next article, we will learn how to update an existing Deployment, understand the process of applying changes, and discover how Kubernetes performs updates without completely interrupting the application.

References

What did you think?