Step 1: Preparing the YAML File
Choose your favorite text editor to create the YAML file. On Windows, Notepad++ is a recommended option due to its syntax support, while on Linux, editors like vi or vim work well. In this example, we will use vim. Open your terminal and create a new file namedpod.yaml:
pod.yaml, we define four top-level properties: apiVersion, kind, metadata, and spec. Consider the following key points:
- apiVersion: Use
"v1"for a pod. - kind: Set this to
"Pod"(note the case sensitivity with a capital “P”). - metadata: This section acts as a dictionary where you can specify the pod’s name and labels. In our example, we assign the name
"nginx"and labels such asapp: nginxandtier: frontend. - spec: Under this key, you define a list of containers. Each container has attributes like a name and an image. Here, we define a single container also named
"nginx"using the Docker Hub imagenginx.
Ensure consistent indentation in your YAML file. Use two spaces per indent level (avoid using tabs) to guarantee that keys like
name and labels are recognized as children of the metadata key, and similarly for entries under spec.:wq.
Step 2: Verifying the YAML File
To confirm that your YAML file is correctly saved and formatted, use thecat command:
Step 3: Creating the Pod
To deploy the pod to your Kubernetes cluster, you have the option of using either thecreate or apply commands. Although both function similarly when creating new objects, we will use the apply command:
Step 4: Checking Pod Status
Once created, it is important to verify that the pod is running properly. First, list all pods using:For further troubleshooting or monitoring, refer to the detailed events in the
kubectl describe pod nginx output. This information is crucial for debugging pod-related issues.Next Steps
In the following section, we will share some tips and tricks to simplify YAML development with various integrated development environments (IDEs). This will help you efficiently manage and deploy Kubernetes configurations.For more detailed Kubernetes documentation, visit Kubernetes Documentation.
If you need additional resources or support, consider exploring: