Top-Level Fields in a Kubernetes YAML File
Every Kubernetes definition file must include the following four fields:-
apiVersion
This field indicates the version of the Kubernetes API you are using. For a Pod, setapiVersion: v1. Depending on the object you define, you might need different versions such as apps/v1, extensions/v1beta1, etc. -
kind
This specifies the type of object being created. In this lesson, since we’re creating a Pod, you’ll define it askind: Pod. Other objects might include ReplicaSet, Deployment, or Service. -
metadata
The metadata section provides details about the object, including its name and labels. It is represented as a dictionary. It is essential to maintain consistent indentation for sibling keys to ensure proper YAML nesting. For example:
Make sure that the properties under metadata (like name and labels) are
indented to the same level. This is crucial for correct YAML parsing.
- spec
The spec section provides specific configuration details for the object. For a Pod, this is where you define its containers. Since a Pod can run multiple containers, thecontainersfield is an array. In our example, with a single container, the array has just one item. The dash (-) indicates a list item, and each container must be defined with at leastnameandimagekeys.
Creating and Verifying the Pod
After you have saved your configuration (for example, aspod-definition.yaml), use the following command to create the Pod:
Using
kubectl describe helps you gain detailed insights into the internal
state of your Pod, which can be invaluable for troubleshooting.