We will try to create a pod using a YAML file on Kubernetes. Kubernetes usually uses YAML files to create configuration objects related to PODs, replicas, deployments, services and others.
The structure of YAML files in Kubernetes generally has similarities in the top 4 level fields, namely:
1apiVersion:
2kind:
3metadata:
4
5
6specs:These 4 level fields must be present when we create a YAML configuration on Kubernetes.
Here are some Kind differences in filling in apiVersion.
| Kind | apiVersion |
|---|---|
| POD | v1 |
| Services | v1 |
| ReplicaSet | apps/v1 |
| Deployments | apps/v1 |
So when we want to create a POD using a YAML file it will look like the one below.
1apiVersion: v1
2kind: Pods
3metadata:
4
5
6specs:Next, we will try to create a file with the name pod-definition.yaml file in the code/pod/pod-definition.yaml folder then fill in the YAML file as below.
1apiVersion: v1
2kind: Pods
3metadata:
4 name: myapp-pod
5 labels:
6 app: myapp
7 type: backend
8specs:
9 containers:
10 - name: nginx-container
11 image: nginxThe following is the definition of each field:
- This
metadatacontainsnameandlabels, which we can fill in according to our needs, which can be filled in with akeyandvaluestructure. - This
specsets a list ofcontainersthat we will create in one POD. In this file we only have one container set withnamenginx-container withimagenginx.
Once the file has been created, we try to execute the file with the command below.
1➜ kubectl apply -f code/pod/pod-definition.yaml
2pod/myapp-pod createdThen we see whether it has been created with the command
1➜ kubectl get pods
2NAME READY STATUS RESTARTS AGE
3myapp-pod 0/1 ContainerCreating 0 3sIf we want to see in more detail the POD that we have created, we can use the command below
1➜ kubectl describe pod myapp-pod
2Name: myapp-pod
3Namespace: default
4Priority: 0
5Service Account: default
6Node: minikube/192.168.49.2
7Start Time: Sun, 03 Mar 2024 20:19:23 +0700
8Labels: app=myapp
9 type=backend
10Annotations: <none>
11Status: Running
12IP: 10.244.0.23
13IPs:
14 IP: 10.244.0.23
15Containers:
16 nginx-container:
17 Container ID: docker://fdb7680e1a75ab77bdf200d4c17086af2a6604e18d22abc5ea2e26258d80775f
18 Image: nginx
19 Image ID: docker-pullable://nginx@sha256:c26ae7472d624ba1fafd296e73cecc4f93f853088e6a9c13c0d52f6ca5865107
20 Port: <none>
21 HostPort: <none>
22 State: Running
23 Started: Sun, 03 Mar 2024 20:19:26 +0700
24 Ready: True
25 Restart Count: 0
26 Environment: <none>
27 Mounts:
28 /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bfkcl (ro)
29Conditions:
30 TypeStatus
31 Initialized True
32 Ready True
33 ContainersReady True
34 PodScheduled True
35Volumes:
36 kube-api-access-bfkcl:
37 Type: Projected (a volume that contains injected data from multiple sources)
38 TokenExpirationSeconds: 3607
39 ConfigMapName: kube-root-ca.crt
40 ConfigMapOptional: <nil>
41 DownwardAPI: true
42QoS Class: BestEffort
43Node-Selectors: <none>
44Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
45 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
46Events:
47 Type Reason Age From Message
48 ---- ------ ---- ---- -------
49 Normal Scheduled 64s default-scheduler successfully assigned default/myapp-pod to minikube
50 Normal Pulling 65s kubelet Pulling image "nginx"
51 Normal Pulled 62s kubelet Successfully pulled image "nginx" in 3.201s (3.201s including waiting)
52 Normal Created 62s kubelet Created container nginx-container
53 Normal Started 62s kubelet Started container nginx-containerThe detailed information from the PODs above is very complete so that we can see events from the PODs when changes occur to the POD.