/webpage-crd

Example CRD and Controller created with Kubebuilder

Primary LanguageGoApache License 2.0Apache-2.0

webpage-crd

This is a sample CustomResourceDefinition (CRD) and Controller to extend the API running in Kubernetes clusters. Blog post about it here.

Usage

Download and install Kubebuilder, Kustomize, and Kind to run your Kubernetes cluster locally. I prefer Kind over Minikube since it starts up faster, but you’re welcome to use any tool to deploy your cluster.

Then compile, install, and run locally:

$ make
go build -o bin/manager main.go
$ make install
kustomize build config/crd | kubectl apply -f -
customresourcedefinition.apiextensions.k8s.io/webpages.sandbox.rvmiller.com created
$ make run
2020-07-04T22:21:21.748-0400    INFO    setup   starting manager
...

Deploy an example custom resource:

webpage.yaml

apiVersion: sandbox.rvmiller.com/v1beta1
kind: WebPage
metadata:
  name: sample-web-page
spec:
  html: |
    <html>
      <head>
        <title>WebPage CRD</title>
      </head>
      <body>
        <h2>This page served from a Kubernetes CRD!</h2>
      </body>
    </html>
$ kubectl apply -f webpage.yaml 
webpage.sandbox.rvmiller.com/sample-web-page created
$ kubectl get configmaps
NAME                     DATA   AGE
sample-web-page-config   1      10m
$ kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
sample-web-page-nginx   1/1     Running   0          10m
$ kubectl port-forward sample-web-page-nginx 7070:80
Forwarding from 127.0.0.1:7070 -> 80
Forwarding from [::1]:7070 -> 80

Then you can see nginx serving the webpage: