Kubernetes uses YAML files as inputs for the creation of objects such as POD’s, replicas, deployment services, etc. All of these follow a similar structure. Kubernetes’ definition file always contains 4 top-level fields.
apiVersion, kind, metadata, and spec. These are the top level or root level properties. These are also required fields so you must have them in your configuration file.
apiVersion: This is the version of the Kubernetes API you are using to create the objects. Depending on what we are trying to create we must use the right API version.
kind: the kind refers to the type of object we are trying to create.
metadata: the metadata is data about the object like its name labels etc. This is in the form of a dictionary. Everything under metadata is intended to the right a little bit and so names and labels are children of metadata the number of spaces before the two properties name and labels don’t matter, but they should be the same as they are the siblings. name’s value is string and labels’ value is a dictionary. Under labels, you can have any kind of key or value pairs.
spec: depending on the object we are going to create This is where we would provide additional information to Kubernetes. spec: is a dictionary. containers: is List or Array. The – is 1st item in List.
apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx-container image: nginx ports: - containerPort: 80 protocol: TCP
$ kubectl create -f nginx-pod.yaml pod/nginx-pod created $ kubectl get pods NAME READY STATUS RESTARTS AGE nginx-pod 1/1 Running 0 21s $ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-pod 1/1 Running 0 37s 172.17.0.3 minikube <none> <none>