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:
apiVersion:
kind:
metadata:
specs:
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.
apiVersion: v1
kind: Pods
metadata:
specs:
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.
apiVersion: v1
kind: Pods
metadata:
name: myapp-pod
labels:
app: myapp
type: backend
specs:
containers:
- name: nginx-container
image: nginx
The following is the definition of each field:
- This
metadata
containsname
andlabels
, which we can fill in according to our needs, which can be filled in with akey
andvalue
structure. - This
spec
sets a list ofcontainers
that we will create in one POD. In this file we only have one container set withname
nginx-container withimage
nginx.
Once the file has been created, we try to execute the file with the command below.
➜ kubectl apply -f code/pod/pod-definition.yaml
pod/myapp-pod created
Then we see whether it has been created with the command
➜ kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-pod 0/1 ContainerCreating 0 3s
If we want to see in more detail the POD that we have created, we can use the command below
➜ kubectl describe pod myapp-pod
Name: myapp-pod
Namespace: default
Priority: 0
Service Account: default
Node: minikube/192.168.49.2
Start Time: Sun, 03 Mar 2024 20:19:23 +0700
Labels: app=myapp
type=backend
Annotations: <none>
Status: Running
IP: 10.244.0.23
IPs:
IP: 10.244.0.23
Containers:
nginx-container:
Container ID: docker://fdb7680e1a75ab77bdf200d4c17086af2a6604e18d22abc5ea2e26258d80775f
Image: nginx
Image ID: docker-pullable://nginx@sha256:c26ae7472d624ba1fafd296e73cecc4f93f853088e6a9c13c0d52f6ca5865107
Port: <none>
HostPort: <none>
State: Running
Started: Sun, 03 Mar 2024 20:19:26 +0700
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-bfkcl (ro)
Conditions:
TypeStatus
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-bfkcl:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 64s default-scheduler successfully assigned default/myapp-pod to minikube
Normal Pulling 65s kubelet Pulling image "nginx"
Normal Pulled 62s kubelet Successfully pulled image "nginx" in 3.201s (3.201s including waiting)
Normal Created 62s kubelet Created container nginx-container
Normal Started 62s kubelet Started container nginx-container
The detailed information from the PODs above is very complete so that we can see events from the PODs when changes occur to the POD.