Pods, ReplicaSets & Deployments · Part 1515

Demo — Kubernetes YAML TipsWriting Kubernetes YAML easily with VS Code

VS Code, the Red Hat YAML extension and Kubernetes schema validation: auto-completion, catching indentation errors early, the outline view, then creating a Pod from nginx.yaml. Plus what the editor can't catch.

Translated from the original Bangla with AI. If anything reads oddly, the Bangla version is the reference.

VS Code editing nginx.yaml with the Red Hat YAML extension: a Kubernetes auto-completion list for kind, a red squiggle on a badly indented image line, and the yaml.schemas setting
FIG. 15 Red Hat YAML extension + Kubernetes schema: suggestions, indentation errors and an outline, before you ever apply to the cluster.

In the previous tutorial, we saw how to create a Kubernetes Pod YAML file with the vim editor.

For example:

vim pod.yaml

We then wrote the Pod configuration in that file and created the Pod in the Kubernetes cluster with kubectl.

But think about one thing.

Writing a small YAML file is not very hard. But what happens when the Kubernetes configuration starts to grow?

Say a YAML file contains many nested properties, lists, dictionaries and Kubernetes-specific fields.

There, even one small indentation mistake can make the whole configuration invalid.

To reduce this problem, we can use an IDE (Integrated Development Environment).

In this tutorial, we will see how to write Kubernetes YAML files more easily with Visual Studio Code (VS Code) and the Red Hat YAML extension.

1. The goal of this tutorial

The main goal of this demo is to configure a good development environment for writing Kubernetes YAML files.

In the previous demo, we used:

vim pod.yaml

That certainly works.

But with plain text editors, mistakes in large Kubernetes manifests are often hard to spot.

The core idea: an IDE with YAML and Kubernetes support lets you write configuration faster and catch many syntax and structure mistakes early.

In this demo, we will learn:

  • What an IDE is and why it is used
  • Why VS Code is useful
  • Installing the Red Hat YAML extension
  • Configuring Kubernetes schema support
  • Using auto-completion
  • Identifying YAML indentation errors
  • Using the YAML outline view
  • Creating a Kubernetes Pod from a YAML file

2. What problems can basic text editors cause?

You can also write Kubernetes YAML files in an ordinary text editor.

For example:

  • Vim
  • Notepad
  • Plain text editors

But a basic editing setup doesn't always give you advanced validation or auto-completion.

As a result, these problems can be hard to spot:

  • Wrong indentation
  • Incorrect YAML structure
  • Invalid Kubernetes fields
  • Wrong field types
  • Missing required sections

Example

spec:
  containers:
    - name: nginx
    image: nginx

This YAML structure is incorrect.

Because image and name are properties of the same container.

So they have to be aligned correctly.

Correct:

spec:
  containers:
    - name: nginx
      image: nginx

In a small YAML file, this mistake may be easy to see.

But in a large configuration file, a mistake like this is much harder to find.

That is why using an IDE helps.

3. What is an IDE?

IDE stands for Integrated Development Environment.

An IDE is software that brings together various development features for writing, editing and managing code or configuration files.

Popular IDEs and code editors:

  • Visual Studio Code
  • IntelliJ IDEA
  • PyCharm
  • Eclipse
  • NetBeans

Advantages of using an IDE

FeatureWhat it does
Syntax HighlightingHighlights different syntax elements separately
Auto-CompletionSuggests code and configuration
Error DetectionHelps detect syntax or configuration mistakes
File NavigationMakes navigating project files easy
Extensions / PluginsLets you add extra features
Better Editing ExperienceMakes large files easier to read and edit

These are very useful when writing Kubernetes YAML files.

This tutorial recommends Microsoft Visual Studio Code (VS Code).

Because it is:

  • Free
  • A code editor built on open source
  • Available on Linux, Windows and macOS
  • Easy to install
  • Extensible with extensions
  • Useful for writing YAML and Kubernetes manifests

There are some licensing and distribution differences between Microsoft's VS Code distribution and the open-source Code-OSS project.

But for Kubernetes YAML development, VS Code is a beginner-friendly choice.

Downloading VS Code

Official website:

https://code.visualstudio.com/

You don't have to use VS Code.

You can use IntelliJ IDEA, PyCharm or another supported editor.

But in this demo we will use VS Code.

5. Installing the YAML extension in VS Code

After installing VS Code, you need to configure YAML support.

Step 1: open VS Code

Launch VS Code.

Step 2: go to the Extensions section

Select the Extensions icon in the left sidebar.

Keyboard shortcut:

Windows / Linux → Ctrl + Shift + X
macOS           → Cmd + Shift + X

Step 3: search for YAML

In the Extensions search box, type:

YAML

The extension recommended in this tutorial:

YAML — Red Hat

Extension ID:

redhat.vscode-yaml

Step 4: install the extension

Select the extension and install it.

This extension provides various helpful features when writing YAML files.

For example:

  • YAML validation
  • YAML formatting
  • Auto-completion
  • Kubernetes schema support

In some development environments, the extension may already be installed.

6. Enabling Kubernetes schema support

Basic YAML validation doesn't always give the editor enough information about Kubernetes YAML.

Because general YAML syntax and Kubernetes-specific fields are not the same thing.

In Kubernetes manifests, we use fields like:

apiVersion:
kind:
metadata:
spec:
containers:
image:

So that VS Code understands the expected structure and data types of these fields, you can configure Kubernetes schema support.

Step 1: open the VS Code settings

Open VS Code's settings.

Then choose the option to edit the settings.json file.

You can also open it from the Command Palette:

Preferences: Open User Settings (JSON)

Step 2: configure the Kubernetes schema

Add the following configuration to settings.json:

{
  "yaml.schemas": {
    "kubernetes": "*.yaml"
  }
}

What does this configuration mean?

yaml.schemas
     |
     ▼
Kubernetes Schema
     |
     ▼
*.yaml Files

Here we are telling VS Code:

Validate files with the .yaml extension using the Kubernetes schema.

Important

Schema name:

kubernetes

It must be lowercase.

After changing the configuration, reloading or restarting VS Code makes sure the new settings are applied.

Practical note

The setting in this demo associates the Kubernetes schema with every .yaml file.

That means the same schema may be applied to YAML files that have nothing to do with Kubernetes.

If you want the configuration to apply only to Kubernetes files, you can make the file patterns more specific.

And in many cases the Red Hat YAML extension can detect Kubernetes manifests automatically, so manual schema mapping may not be needed in every environment.

7. Creating a new Kubernetes YAML file

Now we will create a new YAML file in VS Code.

File name:

nginx.yaml

Step 1: create a new file

Create a new file in VS Code and save it as:

nginx.yaml

Step 2: start with apiVersion

Write in the file:

apiVersion: v1

With a Kubernetes-aware YAML extension configured, the editor can now offer suggestions based on the Kubernetes schema.

That makes writing the fields of Kubernetes YAML easier.

8. How does auto-completion help?

An important VS Code feature is auto-completion.

Say you have written:

apiVersion: v1

Now, as you write the next field, the editor can make suggestions.

For example:

kind
metadata
spec

Example

If you type k, the editor may suggest:

kind:

Then, as you write the value of kind, it can suggest Kubernetes resource types.

For example:

Pod
Deployment
Service

If you want to create a Pod, you will use:

kind: Pod

Why is auto-completion useful?

With auto-completion:

  • Field names are easier to remember.
  • Spelling mistakes go down.
  • You get suggestions for Kubernetes object types.
  • Configuration is quicker to write.
  • Supported fields are easier to identify.

The suggestions depend on the schema association and the extension configuration, though.

9. VS Code can understand Kubernetes field types

Each field in Kubernetes YAML can expect a particular kind of value.

For example:

metadata:
  name:

Here, the name field expects a string value.

But the value is empty.

With schema validation configured, VS Code can highlight this kind of problem.

Correct example

metadata:
  name: nginx-2

Now the name field has a valid string value.

Structure

metadata
    |
    ▼
name
    |
    ▼
nginx-2

This feature is useful because different fields in Kubernetes YAML files can expect different data types.

For example:

  • String
  • Integer
  • Boolean
  • Dictionary
  • List

The editor can detect errors and make suggestions based on the schema.

10. A complete Kubernetes Pod YAML example

Now we will write the demo's complete YAML file.

File name:

nginx.yaml

Complete configuration

apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    env: production
spec:
  containers:
    - name: nginx
      image: nginx

This YAML file tells Kubernetes:

Create a Pod named nginx-2, attach the label env=production to it, and run a container inside the Pod using the nginx image.

YAML breakdown

FieldMeaning
apiVersion: v1Kubernetes API version
kind: PodA Pod object is to be created
metadata.nameThe Pod's name will be nginx-2
labels.envThe environment label will be production
spec.containersThe list of containers
name: nginxContainer name
image: nginxContainer image

Structure

Kubernetes Pod YAML
       |
       ├── apiVersion: v1
       |
       ├── kind: Pod
       |
       ├── metadata
       |      |
       |      ├── name: nginx-2
       |      |
       |      └── labels
       |             |
       |             └── env: production
       |
       └── spec
              |
              └── containers
                     |
                     └── Container
                            |
                            ├── name: nginx
                            |
                            └── image: nginx

11. Detecting YAML indentation mistakes

Indentation mistakes are very common when writing YAML.

Say you wrote the following configuration:

spec:
  containers:
    - name: nginx
    image: nginx

This is incorrect.

Because the image field is at the wrong indentation level.

name and image are properties of the same container dictionary.

So they have to be aligned at the same level.

Correct example

spec:
  containers:
    - name: nginx
      image: nginx

Incorrect vs correct structure

Incorrect:

containers
    |
    ├── - name: nginx
    |
    └── image: nginx
Correct:

containers
    |
    └── Container
           |
           ├── name: nginx
           |
           └── image: nginx

The Red Hat YAML extension can detect problems like this and show an error or warning.

Example error:

Bad indentation of a mapping entry

This way, many syntax mistakes can be fixed before the YAML is ever applied to the Kubernetes cluster.

12. What can't the YAML extension validate?

VS Code's YAML and Kubernetes extension is very useful, but it can't detect every kind of problem.

Say you wrote:

image: nginx

Or:

image: fake-image-name

The image name in the second example may look syntactically valid.

But YAML schema validation cannot confirm whether that image actually exists in the image registry.

What does the extension usually check?

  • YAML syntax
  • Indentation
  • Expected Kubernetes fields
  • Data types
  • Schema-based required properties

What doesn't the extension confirm?

  • Whether the container image exists in the registry
  • Whether the image tag is valid and available
  • Whether private registry credentials are correct
  • Whether the Kubernetes cluster can pull the image
  • Whether the application will start successfully

Example

spec:
  containers:
    - name: nginx
      image: fake-image-name

Even if the YAML structure is valid, Kubernetes can fail when pulling the image.

So successful YAML validation does not mean the application will run successfully.

YAML validation and Kubernetes runtime validation are two different things.

13. Adding multiple containers

In the previous tutorials, we learned that a Pod can contain more than one container.

Because the containers field is a list.

Example

spec:
  containers:
    - name: nginx
      image: nginx

    - name: busybox
      image: busybox

Two containers are defined here.

Pod
 |
 ├── NGINX Container
 |
 └── BusyBox Container

Each list item starts with a dash (-).

And each container has its own name and image.

This demo shows the structure of multiple containers briefly.

Then the second container is removed, and a simple single-container Pod is used.

Final configuration:

spec:
  containers:
    - name: nginx
      image: nginx

Note: just adding the BusyBox image doesn't make the container long-running. You may need to configure a suitable process or command for that.

14. The VS Code outline view

Another useful VS Code feature is the outline view.

The outline view can show a YAML file's structure in a hierarchical format.

Example

The outline of our nginx.yaml file might look like this:

apiVersion
kind
metadata
  |
  ├── labels
  |     |
  |     └── env
  |
  └── name
spec
  |
  └── containers
         |
         └── [0]
              |
              ├── name
              |
              └── image

Here:

  • metadata is a dictionary.
  • labels is a nested dictionary.
  • spec is a dictionary.
  • containers is a list.
  • [0] is the first item in the list.
  • name and image are the container's properties.

Why is the outline view helpful?

When a YAML file gets large, following its nested structure can be hard.

With the outline view, you can:

  • Understand parent-child relationships.
  • Identify nested fields.
  • Navigate large YAML files more easily.
  • Visualize the configuration's structure.

It is especially useful for Kubernetes Deployments and other complex manifests.

15. Creating a Kubernetes Pod from the YAML file

Our nginx.yaml file is now ready.

Complete configuration:

apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    env: production
spec:
  containers:
    - name: nginx
      image: nginx

After saving the file, open a terminal.

First, you can check that the Kubernetes cluster is running:

kubectl get nodes

Then create the Pod:

kubectl create -f nginx.yaml

Expected output:

pod/nginx-2 created

Alternative: kubectl apply

kubectl apply -f nginx.yaml

For a new resource, both commands can create the Pod.

Command difference

CommandPurpose
kubectl create -f nginx.yamlCreates a new Pod from the YAML file
kubectl apply -f nginx.yamlCreates or updates the resource according to the configuration

Creating the Pod from the YAML file is our main goal in this demo.

16. Checking the Pod status

After creating the Pod:

kubectl get pods

Expected example:

NAME      READY   STATUS    RESTARTS   AGE
nginx-2   1/1     Running   0          20s

Here:

  • nginx-2 is the Pod name.
  • 1/1 means one container is ready.
  • Running means the Pod is in the Running phase.
  • RESTARTS 0 means the container has not restarted.

At first you may see ContainerCreating.

Once Kubernetes finishes pulling the image and preparing the container, the status can change to Running.

17. Inspecting the Pod with kubectl describe

To see detailed information about the Pod:

kubectl describe pod nginx-2

This command shows various information.

For example:

  • Pod name
  • Labels
  • Assigned node
  • Pod IP address
  • Container name
  • Container image
  • Container status
  • Pod events
  • Errors, if any

Example

Name:       nginx-2
Namespace:  default
Labels:     env=production
Node:       minikube/192.168.49.2
Status:     Running

Containers:
  nginx:
    Image:  nginx
    State:  Running

The actual output can contain more fields and information.

Why check the Pod details?

If the Pod doesn't start successfully, the Events section of kubectl describe can make the problem easier to identify.

18. Complete hands-on demo — From VS Code to a running Pod

This tutorial's full workflow together:

Step 1: install VS Code

Official website:

https://code.visualstudio.com/

Step 2: install the YAML extension

From Extensions, install:

YAML — Red Hat

Step 3: configure the Kubernetes schema

settings.json:

{
  "yaml.schemas": {
    "kubernetes": "*.yaml"
  }
}

Step 4: create nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-2
  labels:
    env: production
spec:
  containers:
    - name: nginx
      image: nginx

Step 5: save the file

nginx.yaml

Step 6: create the Pod

kubectl create -f nginx.yaml

Step 7: check the Pod status

kubectl get pods

Step 8: inspect the Pod details

kubectl describe pod nginx-2

Complete workflow

VS Code
   |
   ▼
Red Hat YAML Extension
   |
   ▼
Kubernetes Schema Validation
   |
   ▼
Create nginx.yaml
   |
   ▼
Auto-Completion & Error Detection
   |
   ▼
Save YAML File
   |
   ▼
kubectl create -f nginx.yaml
   |
   ▼
Kubernetes API Server
   |
   ▼
Pod Created
   |
   ▼
NGINX Container Running

19. Important commands & configuration summary

Command / ConfigurationPurpose
redhat.vscode-yamlRed Hat YAML extension
yaml.schemasYAML schema mapping
apiVersion: v1Kubernetes API version
kind: PodPod resource type
metadataPod name and labels
specPod configuration
containersContainer definitions
kubectl create -f nginx.yamlCreate the Pod
kubectl apply -f nginx.yamlCreate or update the resource
kubectl get podsCheck the Pod status
kubectl describe pod nginx-2Inspect the Pod details

20. Final takeaway — Kubernetes YAML tips

The main lesson of this tutorial is to use a good editor for writing Kubernetes YAML files.

You can write YAML in a basic text editor.

But an editor like VS Code makes writing Kubernetes configuration easier.

In particular, with the Red Hat YAML extension and Kubernetes schema support, we get:

  • Syntax highlighting
  • Auto-completion
  • YAML validation
  • Indentation error detection
  • Kubernetes field suggestions
  • YAML outline view

But one thing is important to remember.

VS Code is not a substitute for learning YAML.

To write Kubernetes YAML files correctly, we still need to understand:

  • Dictionaries
  • Lists
  • Indentation
  • Kubernetes root-level fields
  • metadata
  • spec
  • containers

The final mental model

Learn YAML Basics
        |
        ▼
Install VS Code
        |
        ▼
Install Red Hat YAML Extension
        |
        ▼
Enable Kubernetes Schema
        |
        ▼
Write Kubernetes Manifest
        |
        ▼
Use Auto-Completion
        |
        ▼
Check Validation Errors
        |
        ▼
Save YAML File
        |
        ▼
kubectl create/apply
        |
        ▼
Running Kubernetes Resource

Final one-line summary:

Configuring VS Code with the Red Hat YAML extension and Kubernetes schema support makes writing Kubernetes YAML manifests easier. You get auto-completion, indentation help, schema validation and an outline view, which help reduce configuration mistakes.

Official Documentation

The key point: you still have to understand Kubernetes YAML's syntax and structure yourself. VS Code only helps you write that configuration faster, more clearly and with fewer mistakes.

Was this guide helpful?

Search the archive

Press Esc to close