Kubernetes 101: Objects

What are Objects?

Simply put, Kubernetes Objects are definitions that represent a “desired state“. Kubernetes itself will work around the clock to try to ensure that the desired state of your object definitions are met (such as having a deployment with 10 pods, or “replicas”).

There a several object definitions that make up different kinds of state. The most commonly used object definitions are:

Object definitions are commonly expressed in yaml format (there is json as well). This is an example of a Deployment represented with yaml:

apiVersion: apps/v1                 # API Version of this Object
kind: Deployment                    # This Object Type
metadata:                           # Allows you to specify custom metadata
  name: nginx                       # Specifies the name of this object
spec:                               # The official specification matching object type schema
  selector:                         # Label selector for pods
    matchLabels:                    # Must match these label(s)
      app: nginx                    # Custom label with value
  template:                         # Template describes the pods that are created
    metadata:                       # Standard objects metadata
      labels:                       # Labels used to group/categorize objects
        app: nginx                  # The name of this template
    spec:                           # Specification of the desired behaviour of this pod
      containers:                   # List of containers belonging to this pod (cannot be changed/updated)
      - name: nginx                 # Name of this container
        image: nginx                # Docker image used for this container
        ports:                      # Port mapping(s)
          - containerPort: 80       # Number of port to expose on this pods ip address
        readinessProbe:             # Periodic probe of container readiness. Container will be removed from endpoints if not ready
          tcpSocket:                # tcpSocket is the action "type" of the probe
            port: 80                # Number of port to open a connection to
          initialDelaySeconds: 10   # Number of seconds to wait before probing
          periodSeconds: 10         # How often to perform probe check
        livenessProbe:              # Periodic probe of container readiness. Container will be removed from endpoints if not "alive"
          tcpSocket:                # tcpSocket is the action "type" of the probe
            port: 80                # Number of port to open a connection to
          initialDelaySeconds: 30   # Number of seconds to wait before probing
          periodSeconds: 30         # How often to perform probe check

How do I use objects?

The most common way to “install” an object is to use the kubectl command line interface which talks to the Kubernetes API. An example of deploying a manifest (yaml) file would be:

$ kubectl apply -f mydeployment.yaml [-n mynamespace]

Similarly, you can also delete objects:

$ kubectl delete -f mydeployment.yaml [-n mynamespace]

List currently installed services, deployments, etc..:

You can view the current state of your object(s) multiple ways. The most common way is to use the kubectl command line interface as well:

$ kubectl get service,deployment [-n mynamespace]

Describe, in detail, an object:

$ kubectl describe service <the service name> [-n mynamespace]

For a full list of useful kubectl commands check out: kubectl top useful commands

See also: