项目作者: wdalmut

项目描述 :
Handle multiple conferences as K8S elements
高级语言: JavaScript
项目地址: git://github.com/wdalmut/k8s-crd-events.git
创建时间: 2019-12-22T16:59:36Z
项目社区:https://github.com/wdalmut/k8s-crd-events

开源协议:

下载


Create an application using K8S resources

Manage a conference application with K8S build blocks (PoC).

Everything starts with a Conference custom resource definition. When a new
conference is created or updated/delete a shared informer push the event in a
message queue where a controller deploy a dedicated K8S namespace with the
application deploy.

Road to CloudConf2020? Check it out: https://cloudconf.it

Getting started

Create the cluster

  1. kind create cluster --config kind.yaml

Start development

  1. skaffold run

Play with conferences!

Check existing conferences

  1. kubectl get conferences
  2. No resources found in default namespace.

Now create a new conference

  1. kubectl apply -f examples/cloudconf2020.yaml

Check existing conferences

  1. kubectl get conferences
  2. NAME TITLE ON DAY
  3. cloudconf2020 CloudConf 2020 2020-03-19

Check also the dedicated namespace!

  1. kubectl describe ns cloudconf2020
  2. Name: cloudconf2020
  3. Labels: <none>
  4. Annotations: <none>
  5. Status: Active
  6. No resource quota.
  7. No resource limits.

Now create a conference purchase plan

  1. kubectl apply -f examples/cloudconf2020-plan.yaml

And get the created plan

  1. kubectl get plan -n cloudconf2020
  2. NAME TITLE ACTIVE PRICE
  3. super-early-bird Super Early Bird Ticket true 29.9

Then create an order

  1. kubectl apply -f examples/cloudconf2020-order-1.yaml

Then check the created order

  1. kubectl get orders -n cloudconf2020
  2. NAME PLAN COUPON STATUS CURRENT DESIRED PRICE DATE
  3. order-1 super-early-bird PENDING 0 1 29.9 357d

The order is correctly created but there are not tickets because its status is
“PENDING”

  1. kubectl get tickets -n cloudconf2020
  2. No resources found in cloudconf2020 namespace.

Tickets will be generated only where the order status is confirmed, to confirm
the order you can pay for it (receiving the payment webhook), have a free plan
or the Coupon value is high enough to set a free ticket (price=0)

Ticket creation

Generate a free event

  1. cat <<EOF > meetup.yaml
  2. apiVersion: "app.corley.it/v1"
  3. kind: Conference
  4. metadata:
  5. name: meetup
  6. spec:
  7. title: "Free Meetup"
  8. EOF
  9. kubectl apply -f meetup.yaml

Then a free plan

  1. cat <<EOF > plan.yaml
  2. apiVersion: "app.corley.it/v1"
  3. kind: Plan
  4. metadata:
  5. name: free-plan
  6. namespace: meetup
  7. spec:
  8. title: "Free Ticket"
  9. price: 0
  10. active: true
  11. EOF
  12. kubectl apply -f plan.yaml

Now create an order

  1. cat <<EOF > order.yaml
  2. apiVersion: "app.corley.it/v1"
  3. kind: Order
  4. metadata:
  5. name: order-1
  6. namespace: meetup
  7. spec:
  8. planRef: free-plan
  9. price: 0
  10. date: "2019-01-01T08:00:00Z"
  11. quantity: 1
  12. EOF
  13. kubectl apply -f order.yaml

Becase the order price is 0 (free event in this case) the ticket generation is
completely automated. Just get tickets for this order

  1. kubectl get tickets -n meetup
  2. NAME ORDER FIRSTNAME LASTNAME
  3. order-1-5193329 order-1

You can “scale” the order and get multiple tickets. Check this out:

  1. kubectl scale order --replicas=6 --namespace meetup order-1

Then check your tickets

  1. kubectl get tickets -n meetup
  2. NAME ORDER FIRSTNAME LASTNAME
  3. order-1-2729627 order-1
  4. order-1-3468125 order-1
  5. order-1-3598565 order-1
  6. order-1-5193329 order-1
  7. order-1-9476971 order-1
  8. order-1-9926811 order-1

Pay for a ticket

Just receive the payment event (POST call on the payment gateway)

  1. curl -XPOST -d 'custom=order-1&payment_status=Completed&mc_gross=29.90&mc_currency=EUR' http://localhost/payment-gateway/cloudconf2020

If you check the order event list now you will see the payment confirmation
event and then the tickets will be created.

Delete a conference

If you want to drop out everything about a conference, just drop the
conference element

  1. kubectl delete conference CloudConf2020

And checkout your namespaces:

  1. NAME STATUS AGE
  2. cloudconf2020 Terminating 15m
  3. default Active 43m
  4. ingress-nginx Active 32m
  5. kconference Active 19m
  6. kube-node-lease Active 43m
  7. kube-public Active 43m
  8. kube-system Active 43m

As you see the namespace is now in Terminating and soon everything will be
removed!

Quota and limits for conferences

Just create a resource quota!

  1. cat <<EOF > object-counts.yaml
  2. apiVersion: v1
  3. kind: ResourceQuota
  4. metadata:
  5. name: conference-object-counts
  6. namespace: cloudconf2020
  7. spec:
  8. hard:
  9. "count/orders.app.corley.it": "10"
  10. "count/tickets.app.corley.it": "10"
  11. "count/plans.app.corley.it": "3"
  12. "count/coupons.app.corley.it": "5"
  13. EOF

Now you have limits for your conference!

  1. kubectl apply -f object-counts.yaml

And than check your conference namespace current situation:

  1. kubectl describe namespace cloudconf2020
  2. Name: cloudconf2020
  3. Labels: <none>
  4. Annotations: <none>
  5. Status: Active
  6. Resource Quotas
  7. Name: conference-object-counts
  8. Resource Used Hard
  9. -------- --- ---
  10. count/coupons.app.corley.it 0 5
  11. count/orders.app.corley.it 1 10
  12. count/plans.app.corley.it 1 3
  13. count/tickets.app.corley.it 7 10
  14. No resource limits.

Expose nginx ingress (for KinD)

Deploy nginx ingress

  1. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml
  2. kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml

Then expose the service with socat

  1. make dev