Step 1: Create the YAML File
Open your terminal and create a file named “pod.yaml” using Vim:Step 2: Define the Pod Structure
Begin your YAML file by specifying the four essential root-level properties: apiVersion, kind, metadata, and spec. Below are some key points to remember:- apiVersion: For pods, this should be set to
v1. - kind: Must be defined as
Pod(case sensitive). - metadata: This section contains a dictionary where you can specify the pod’s name and additional labels.
- spec: Contains pod specifications including the list of containers.
Always use spaces for indentation (preferably two spaces) and avoid tabs to ensure YAML validity.
Step 3: Configure Container Specifications
Within the spec section, include the containers property, which is a list of container objects. Each container should have:- A unique name (e.g.,
nginx) - An image reference (e.g.,
nginx)
Complete YAML Configuration
Below is a complete YAML configuration for a pod running a single container with the nginx image:Step 4: Save Your YAML File
Save the file and exit Vim by typing:Step 5: Verify the YAML File
To confirm the file’s content and proper formatting, use thecat command as shown below:
Step 6: Create the Pod
You can now create the pod using either the create or apply command with the-f option, which points to your YAML file:
Step 7: Check Pod Status
To view the status of your pod, execute:By following these steps, you have successfully defined and created a Kubernetes pod using a YAML configuration file. This method enhances reproducibility and clarity in your Kubernetes deployments. For more Kubernetes basics and deep dives into YAML configurations, check out these resources: