Sample YAML Reference Programs

A traditional YAML file has 4 main key-value pairs:

  • apiVersion - Defines the API version of the kind used in the YAML file.
  • Kind - Kind of Kubernetes object being created. Kubernetes supports many different types of objects or kinds, such as (but not limited to) Pod, Service, Deployment, and Daemonset.
  • Metadata - Object metadata of the object, such as name and labels. Labels are identifiers that facilitate filtering or selecting the correct object from multiple similar objects.
  • Spec - This key will have many things under it that are closely with the type of Kind/Object, such as (but not limited to) Containers, Volumes, NodePorts, or templates.

Sample 1: HTTPD as a Pod (Single YAML)

The following YAML script creates a Kubernetes pod object with a single container that uses CentOS with installed httpd package. This container also exposes a port that can be used from within the cluster to communicate with the container.

apiVersion: v1
kind: Pod
metadata:
  name: pod1
  labels:
    layers: single
    sample: httpd
spec:
  containers:
    - name: c1
      image: centos/httpd
      ports:
      - containerPort: 80

Project 2: HTTPS as a Service with NodePort to Expose the Endpoint

The following YAML script creates a Kubernetes NodePort service object, which forwards the container ports to the external network. NodePort objects do not include a container key; it links to a pod object based on the selector key. (All pods link to service objects via selector keys). This object includes the ports key and can have the targetPort and nodePort sub-keys.

  • The Port field is required. This is the port where the service object is listening.
  • The targetPort defaults to 80 unless specified otherwise. This is the service object output port.
  • The NodePort defaults to a random port number greater than 30000 unless specified otherwise. This is the forwarded part of the key port.
apiVersion: v1
kind: Service
metadata:
  name: svc1
  labels:
    layers: single
    sample: svc-httpd
spec:
  type: NodePort
  selector:
    layers: single
    sample: httpd
  ports:
    - name: httpd
      port: 80

Project 3: HTTPD with NodePort and VolumeMount

This is a bigger YAML script that has two objects/kinds (Pod and Service) separated by ---. The Pod object adds the following keywords:

  • volumes - Used for a different type of storage facility. In this example, it mounts a directory on the host to a location on the container provided by the volumeMounts key.
  • volumeMounts - Location where to mount the storage directory.
apiVersion: v1
kind: Pod
metadata:
  name: pod2
  labels:
    layers: single
    sample: httpd2
spec:
  containers:
  - name: c1
    image: centos/httpd
    ports:
    - containerPort: 80
    volumeMounts:
      - name: indexfile
        mountPath: /var/www/html
  volumes:
    - name: indexfile
      hostPath: 
        path: /tmp
        type: DirectoryOrCreate

---
apiVersion: v1
kind: Service
metadata:
  name: svc2
  labels:
    layers: single
    sample: svc-httpd2
spec:
  type: NodePort
  selector:
    layers: single
    sample: httpd2
  ports:
    - name: httpd
      port: 80

Project 4: HTTPD with a PVC and FS Mount

This YAML file defines three kinds: Pod, Service, and persistentVolumeClaim. persistentVolumeClaim attaches a persistent volume to the container. If this is not defined, then a default persistent volume will be used. This is different than the volumes key used in the previous example, in that it used a hostPath driver while this example uses a persistentVolumeClaim object. The metadata defines the new label hpecp.hpe.com/fsmount. This is exclusive to an HPE Ezmeral Runtime Enterprise Kubernetes cluster where this label mounts a default FS Mount on the Pod.

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-sample
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 2Gi

---
apiVersion: v1
kind: Pod
metadata:
  name: pod3
  labels:
    layers: single
    sample: httpd3
    hpecp.hpe.com/fsmount: <tenant namespace name>
spec:
  containers:
  - name: c1
    image: bluedata/centos7
    ports:
    - containerPort: 80
    volumeMounts:
      - name: pvcloc
        mountPath: /mnt
  volumes:
    - name: pvcloc
      persistentVolumeClaim:
        claimName: pvc-sample

---
apiVersion: v1
kind: Service
metadata:
  name: svc3
  labels:
    layers: single
    sample: svc-httpd3
spec:
  type: NodePort
  selector:
    layers: single
    sample: httpd3
  ports:
    - name: httpd
      port: 80