Provide the YAML that allows you to manage a custom resource of type "Fruit" with Kubernetes: We call this yaml fruit.yaml
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
` `name: fruits.hy548.csd.uoc.gr
spec:
` `group: hy548.csd.uoc.gr
` `versions:
` `- name: v1
` `served: true
` `storage: true
` `schema:
` `openAPIV3Schema:
` `type: object
` `properties:
` `spec:
` `type: object
` `properties:
` `origin:
` `type: string
` `count:
` `type: integer
` `grams:
` `type: integer
` `scope: Namespaced
` `names:
` `plural: fruits
` `singular: fruit
` `kind: Fruit
` `shortNames:
` `- frt
And the fruit-inst.yaml is:
apiVersion: hy548.csd.uoc.gr/v1
kind: Fruit
metadata:
` `name: apple
spec:
` `origin: Krousonas
` `count: 3
` `grams: 980
Then we provide the kubectl commands needed to:
- Install the custom resource.
` minikube start
kubectl apply -f fruit.yaml`
- Create the above instance.
kubectl apply -f fruit-inst.yaml
- Return the new instance in YAML format.
kubectl get fruit apple -o yaml
- Return a list of all available instances.
kubectl get fruits since fruits is defined as the plural of fruit in fruit.yaml
Extend the example so that:
-
The executable controller.py runs inside a container. Provide the Dockerfile. Build and upload the new container to Docker Hub.
The Dockerfile is this:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "controller.py"]
To build and upload the new container to Docker Hub we use these commands:
docker build -t jimaandron/greeting-controller:latest .
docker push jimaandron/greeting-controller:latest
The repo is this https://hub.docker.com/r/jimaandron/greeting-controller
-
Provide greeting-controller.yaml, which will create a Deployment with the container you made. Make sure the necessary permissions are set so the controller can read the "Greeting" CRDs and create the corresponding Deployments with the hello-kubernetes container in all namespaces.
The greeting-controller.yaml is this:
apiVersion: apps/v1
kind: Deployment
metadata:
spec:
apiVersion: v1
kind: ServiceAccount
metadata:
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
rules:
- apiGroups: [""]
- apiGroups: ["apps"]
- apiGroups: ["hy548.csd.uoc.gr"]
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
subjects:
- kind: ServiceAccount
roleRef:
Provide the commands you used to verify that the deployment works correctly.
kubectl apply -f greeting-crd.yaml
kubectl apply -f greeting-cntrl.yaml
kubectl get deployments
kubectl logs deployment/greeting-controller
kubectl get greetings
In logs and in get deployments we see that greetings controller is running. But in get greetings we see that there is nothing there. So after we apply the hello-world.yaml too, we see :
NAME AGE
hello-to-all 52s
hello-world 52s
Which is correct!
The example code for the webhooks is available on GitHub (https://github.com/chazapis/hy548). Extend the example so that:
-
The executable controller.py runs inside a container. Provide the Dockerfile. Build and upload the new container to Docker Hub.
The Dockerfile is this:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "controller.py"]
To build and upload the new container to Docker Hub, we use these:
docker build -t jimaandron/webhook-controller:latest .
docker push jimaandron/webhook-controller:latest
-
The webhook.yaml should use the new container instead of the proxy with Nginx. Provide the new webhook.yaml.
The new webhook.yaml is this:
apiVersion: v1
kind: Namespace
metadata:
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
spec:
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
spec:
apiVersion: v1
kind: Service
metadata:
spec:
apiVersion: v1
kind: ConfigMap
metadata:
data:
apiVersion: apps/v1
kind: Deployment
metadata:
spec:
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
webhooks:
Provide the commands you used to verify the webhook works correctly.
I use Windows so firstly I downloaded helm release from this https://github.com/helm/helm/releases
And then :
windows-amd64\helm.exe repo add jetstack https://charts.jetstack.io
windows-amd64\helm.exe install cert-manager jetstack/cert-manager --namespace cert-manager --create-namespace --version v1.7.2 --set installCRDs=true
Then
kubectl create namespace test
kubectl label namespace test custom-label-injector=enabled
Then I created a simple test.yaml like this:
apiVersion: v1
kind: Pod
metadata:
spec:
And I ran this to verify that webhook is running
kubectl apply -f test.yaml
kubectl get pod test-pod -n test --show-labels