YAML descriptor

You can create Kubernetes resources (e.g. a pod) by sending a YAML (or JSON) descriptor the Kubernetes API server.

You can also use commands like kubectl run, but that doesn't give you all the options.

Getting a resource's YAML descriptor

To get a YAML description of a running pod:

kubectl get pod kubia -o yaml
# apiVersion: v1
# kind: Pod
# metadata:
#   ... (names, labels, etc.)
# spec:
#   ... (containers, volumes, etc.)
# status:
#   ... (current information about the pod)

Writing a YAML descriptor

Here's an example of a basic pod descriptor.

apiVersion: v1
kind: Pod
metadata:
  name: kubia-manual
spec:
  containers:
    - image: lukas/kubia
      name: kubia
      imagePullPolicy: Never
      ports:
        - containerPort: 8080
          protocol: TCP
  • apiVersion: v1 It uses the v1 version of the Kubernetes API.
  • kind: Pod It's a pod, named kubia-manual.
  • It has one container named kubia, which uses the kubia Docker image, and we should not pull the Dcoker image from the registry (use the local image)
  • We also declare explicitly that the kubia container listens to port 8080 (this is for documentation purpose only, but it helps to list the ports this way)

Sending a YAML descriptor to the Kubernetes API server

Creating a new resource

To create a resource using its descriptor, you would run:

kubectl create -f <filename>

For example, assuming you have written a pod descriptor as kubia-manual.yaml, you can create the pod by running the following command:

kubectl create -f kubia-manual.yaml
# pod/kubia-manual created

Applying changes from the descriptor file

When you edit a descriptor file, you can apply its changes by using:

kubectl apply -f <filename>

Deleting resources from the descriptor file

To delete resources described by a descriptor file, you can use:

kubectl delete -f <filename>

Accessing descriptor documentation

To see the fields you can use in the YAML descriptor, head to http://kubernetes.io/docs/api, or use the kubectl explain command:

kubectl explain pod
# ... (some information)
# FIELDS:
#      apiVersion <string>
#        ...
#      kind <string>
#        ...
#      metadata <Object>
#        ...
#      spec <Object>
#        ...
#      status <Object>
#        ...

You can then dig deeper through the available fields, e.g.

kubectl explain pod.metadata
# KIND:     Pod
# VERSION:  v1
# ... (additional fields)

References