Defining a Custom Resource

You'll need a proxy to the Kubernetes API server. You may still have a proxy running in your first terminal window from step 2 if you've followed this learning path in one sitting. If not, start a proxy to the API:

kubectl proxy --port=8001

Open up another terminal by clicking the + button and selecting Open New Terminal.

Create a new Custom Resource Definition (CRD) object manifest for a PostgreSQL database server:

cat >> postgres-crd.yaml <<EOF
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: postgreses.rd.example.com
spec:
  group: rd.example.com
  names:
    kind: Postgres
    listKind: PostgresList
    plural: postgreses
    singular: postgres
    shortNames:
    - pg
  scope: Namespaced
  versions:
  - name: v1alpha1
    schema:
      openAPIV3Schema:
        type: object
        x-kubernetes-preserve-unknown-fields: true
    served: true
    storage: true
EOF

Create the new Custom Resource Definition (CRD) specified in the manifest:

kubectl create -f postgres-crd.yaml

You should now see the Kubernetes API reflect a brand new api group called rd.example.com:

curl http://localhost:8001/apis | jq .groups[].name

This will also be reflected in the `kubectl api-versions` command:

kubectl api-versions

Within the rd.example.com group there will be an api version v1alpha1 as specified in your CRD. The database resource lives here.

curl http://localhost:8001/apis/rd.example.com/v1alpha1 | jq

Notice how `kubectl` now recognizes postgres as a present resource, although there will be no current postgres resources yet -- just their API definition.

kubectl get postgres

Create a new Custom Resource (CR) object manifest to specify a running instance of a PostgreSQL database server:

cat >> wordpress-database.yaml <<EOF
apiVersion: "rd.example.com/v1alpha1"
kind: Postgres
metadata:
  name: wordpressdb
spec:
  user: postgres
  password: postgres
  database: primarydb
  nodes: 3
EOF

Create the new object:

kubectl create -f wordpress-database.yaml

Verify the resource was created:

kubectl get postgres

View the details about the wordpressdb custom object, like any other API resource:

kubectl get postgres wordpressdb -o yaml
kubectl describe postgres wordpressdb