Skip to content

intelops/k8s-custom-resource-demo

Repository files navigation

k8s-custom-resource-demo

Demo for k8s-custom-resource in nodejs.

  • To run this code, please clone this repository using below commands.
git clone https://github.com/intelops/k8s-custom-resource-demo.git
  • Navigate to the directory k8s-custom-resource-demo
cd k8s-custom-resource-demo

Setup k8s cluster on local computer

kubectl apply -f crds/
  • Once you have installed the CRD on your cluster, you should be able to list down employees by following command. As there are no employees yet created, you can expect No resources found as a response.
kubectl get employees -A
  • Let's try creating an employee manually with below specification. Create mahendra.yaml with below content.
apiVersion: intelops.ai/v1alpha1
kind: Employee
metadata:
  name: mahendra
  namespace: employee-system
spec:
  department: IT
  role: developer
kubectl apply -f mahendra.yaml

The above command will produce employee.intelops.ai/mahendra created. We can now check by listing employees using below command.

kubectl get employees -n employee-system

This will produce below output:

NAME       AGE
mahendra   68s

Run code on local

  • Fire npm install in the directory.
  • Let's run the code now with below command.
npm run dev
  • You can try creating an employee by calling POST REST api like below.
curl -X POST -H "Content-Type: application/json" -d '{"department":"IT","name": "mahendra", "role": "developer"}' http://localhost:5000/employees
  • You should see the below output when the call is successful.
{
   "employee":{
      "department":"IT",
      "name":"mahendra",
      "role":"developer"
   },
   "message":"mahendra employee is created."
}
  • Similarly, you can call other REST apis, e.g. GET api
curl -X GET -H "Content-Type: application/json"  http://localhost:5000/employees
  • The above command shall produce.
[
   {
      "department":"IT",
      "name":"mahendra",
      "role":"developer"
   }
]

About

k8s-custom-resource in nodejs demo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published