Kubernetes Manifest
So far, we have basically run commands by putting information such as Pods directly into the command, and we have shown command examples each time for what we wanted to do. A typical example is the way we started a Deployment in the previous section and then increased the number of Pods. Giving commands for what you want to do each time like this is called imperative. This approach focuses on how.
By contrast, specifying the final state you want and then creating it is called declarative. In the declarative approach, creation is performed by focusing on information, or what.
In Kubernetes, the declarative method is to write information in a YAML file, also called a manifest. By writing the final desired information, such as Pod information, in this YAML file, Kubernetes automatically creates the target object. At this point, Kubernetes users do not need to think about how to create it, so the ability to abstract that how is an advantage of the declarative approach.
For example, the following two examples describe the same thing imperatively with kubectl run and declaratively with pod.yaml.
Imperative: kubectl run
kubectl run --image gcr.io/google-samples/hello-app:1.0 --restart Never Helloworld
Declarative: pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: helloworld
name: helloworld
spec:
containers:
- env:
- name: TEST_ENV
value: Hello_World
image: gcr.io/google-samples/hello-app:1.0
ports:
- containerPort: 8080
You can also convert from imperative creation to declarative creation with the command below.
kubectl run --image gcr.io/google-samples/hello-app:1.0 --restart Never helloworld -o yaml helloworld > pod.yaml
To create the same Pod as kubectl run by specifying YAML in this way, specify the YAML file you created with the kubectl apply command.
kubectl apply -f pod.yaml
Next, let’s look at the contents written in the YAML file. However, covering everything here would make the article too long, so only part of it is described. Refer to the official documentation for other fields.
The pod.yaml file explained above is probably the simplest form. Larger configurations follow the same idea.
Let’s look at the first level of this YAML file. You can see that there are four major fields. Each field has the following meaning.
| key | Meaning |
|---|---|
apiVersion |
Specifies the version of the Kubernetes Object to represent |
kind |
Specifies the type to use from the Kubernetes Object set specified by apiVersion Examples: Pod, ReplicaSet, Deployment |
metadata |
Specifies object information, such as a name and other information for unique identification |
spec |
Specifies what you want to apply to the object |
The four fields above are required when writing a YAML file. Also, the fields written below the second level vary depending on the type of object being created, so the detailed explanation is omitted.
The file created here contains the following settings.
| 1st level | 2nd level | 3rd level | Description |
|---|---|---|---|
| metadata | - | - | - |
| - | creationTimestamp | - | Sets whether to keep the node creation time |
| - | labels | - | Kubernetes node labels (official documentation) |
| - | name | - | Sets the name of an object that is unique within the namespace |
| spec | - | - | - |
| - | containers | - | Sets container specifications |
| - | - | env | Specifies environment variables for the container |
| - | - | image | Docker image repository and tag |
| - | - | ports | List of open ports that receive requests from outside the Pod |