ruk·si

☸️ Kubernetes
Objects / Resources

Updated at 2018-12-31 02:41

Kubernetes objects are persistent entities in the Kubernetes system. Objects are used to represent the desired and current state of your cluster.

  • What applications (pods) are running on which machines (nodes)?
  • How much resources have been allocated for the applications and how much is left?
  • Policies how the application behave.
Resource Types TL;DR:

  container  = a process
  pod        = an application
  deployment = a managed application
  service    = an internal address
  ingress    = an external address
  configmap  = visible environment variables
  secret     = hidden environment variables
  volume     = a persistent file storage

You create, modify and delete objects through Kubernetes API. The underlying API is REST but there are wrapper libraries for many languages.

Every object includes two nested fields; object spec and object status. Object spec is the desired state that you provide + default values. Object status is the actual state of the object that Kubernetes system maintains.

Kubernetes deployment object:
  Spec: I want 3 instances of my application running:
  Status: We have 0 of such instances running so we will start 3 new ones.

# if any of the instances fail, status updates and a new instance is created
# so the spec and status are in sync

The most common way to define an object is with a YAML file. kubectl converts this to JSON and sends it to the API.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.7.9
          ports:
            - containerPort: 80
kubectl create -f nginx-deployment.yaml --record

Object YAML files have 5 top-level fields:

  • apiVersion: which version of the Kubernetes API are you using
  • kind: type of the object you want to create
  • metadata: object identifiers, name, uuid and namespace
  • spec: configuration for this specific object type
  • status: the latest known status of the object, might be out-of-date or missing

metadata.name should be unique. You can have duplicate names if the objects have different kind. Name should be lower-case-with-dashes.and.optional.dots.

Example pod spec:

apiVersion: v1
kind: Pod
metadata:
  name: pod-example
spec:
  containers:
    - name: ubuntu
      image: ubuntu:trusty
      command: [ "echo" ]
      args: [ "Hello World" ]

Example deployment spec:

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  # Unique key of the Deployment instance
  name: deployment-example
spec:
  # 3 Pods should exist at all times.
  replicas: 3
  template:
    metadata:
      labels:
        # Apply this label to pods and default
        # the Deployment label selector to this value
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.10

Example job spec:

apiVersion: batch/v1
kind: Job
metadata:
  # Unique key of the Job instance
  name: example-job
spec:
  template:
    metadata:
      name: example-job
    spec:
      containers:
        - name: pi
          image: perl
          command: [ "perl" ]
          args: [ "-Mbignum=bpi", "-wle", "print bpi(2000)" ]
      # Do not restart containers after they exit
      restartPolicy: Never

Sources