Using CRDs

While Aggregated APIs are more flexible, CRDs are easier to user. Let's try and create the example CRD from the Kubernetes documentation.

First, you'll need to spin up your Minikube cluster and the GKE cluster on GCP, which will be one of your own clusters or a playground such as Katacoda. Let's jump into a Google Cloud Shell and give this a try.

Once on your GCP home page, click the CLI icon, which is circled in red in the following screenshot:

Once you're in the shell, create a quick Kubernetes cluster. You may need to modify the cluster version in case older versions aren't supported:

gcloud container clusters create gswk8s 
--cluster-version 1.10.6-gke.2
--zone us-east1-b
--num-nodes 1
--machine-type n1-standard-1
<lots of text>
...
Creating cluster gsk8s...done.
Created [https://container.googleapis.com/v1/projects/gsw-k8s-3/zones/us-east1-b/clusters/gsk8s].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-east1-b/gsk8s?project=gsw-k8s-3
kubeconfig entry generated for gsk8s.
NAME LOCATION MASTER_VERSION MASTER_IP MACHINE_TYPE NODE_VERSION NUM_NODES STATUS
gsk8s us-east1-b 1.10.6-gke.2 35.196.63.146 n1-standard-1 1.10.6-gke.2 1 RUNNING

Next, add the following text to resourcedefinition.yaml:

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
# name must match the spec fields below, and be in the form: <plural>.<group>
name: crontabs.stable.example.com
spec:
# group name to use for REST API: /apis/<group>/<version>
group: stable.example.com
# list of versions supported by this CustomResourceDefinition
version: v1
# either Namespaced or Cluster
scope: Namespaced
names:
# plural name to be used in the URL: /apis/<group>/<version>/<plural>
plural: crontabs
# singular name to be used as an alias on the CLI and for display
singular: crontab
# kind is normally the CamelCased singular type. Your resource
manifests use this.
kind: CronTab
# shortNames allow shorter string to match your resource on the CLI
shortNames:
- cront

Once you've added that, we can create it:

anonymuse@cloudshell:~ (gsw-k8s-3)$ kubectl apply -f resourcedefinition.yaml
customresourcedefinition "crontabs.stable.example.com" created

Great! Now, this means that our RESTful endpoint will be available at the following URI:
/apis/stable.example.com/v1/namespaces/*/crontabs/. We can now use this endpoint to manage custom objects, which is the other half of our key CRD value.

Let's create a custom object called os-crontab.yaml so that we can insert some arbitrary JSON data into the object. In our case, we're going to add the OS metadata for cron and the crontab interval.

Add the following:

apiVersion: "stable.example.com/v1"
kind: CronTab
metadata:
name: cron-object-os-01
spec:
intervalSpec: "* * 8 * *"
os: ubuntu
anonymuse@cloudshell:~ (gsw-k8s-3)$ kubectl create -f os-crontab.yaml
crontab "cron-object-os-01" created

Once you've created the resource, you can get it as you would any other Deployment, StatefulSet, or other Kubernetes object:

anonymuse@cloudshell:~ (gsw-k8s-3)$ kubectl get crontab
NAME                AGE
cron-object-os-01   38s

If we inspect the object, we would expect to see a bunch of standard configuration, plus the intervalSpec and OS data that we encoded into the CRD. Let's check and see if it's there.

We can use the alternative name, cront, that we gave in the CRD in order to look it up. I've highlighted the data as follows—nice work!

anonymuse@cloudshell:~ (gsw-k8s-3)$ kubectl get cront-o yaml
apiVersion: v1
items:
- apiVersion: stable.example.com/v1
  kind: CronTab
  metadata:
    clusterName: ""
    creationTimestamp: 2018-09-03T23:27:27Z
    generation: 1
    name: cron-object-os-01
    namespace: default
    resourceVersion: "2449"
    selfLink: /apis/stable.example.com/v1/namespaces/default/crontabs/cron-object-os-01
    uid: eb5dd081-afd0-11e8-b133-42010a8e0095
  spec:
    intervalSpec: '* * 8 * *'
    os: Ubuntu
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.115