用于管理 Kubernetes 中应用程序生命周期和流量切换的 Opinionated StackSet 资源
The Kubernetes StackSet Controller is a concept (along with an implementation) for easing and automating application life cycle for certain types of applications running on Kubernetes.
It is not meant to be a generic solution for all types of applications but it's explicitly focusing on "Web Applications", that is, application which receive HTTP traffic and are continuously deployed with new versions which should receive traffic either instantly or gradually fading traffic from one version of the application to the next one. Think Blue/Green deployments as one example.
By default Kubernetes offers the Deployment resource type which, combined with a Service, can provide some level of application life cycle in the form of rolling updates. While rolling updates are a powerful concept, there are some limitations for certain use cases:
To work around these limitations I propose a different type of resource called
an StackSet which has the concept of Stacks.
The StackSet is a declarative way of describing the application stack as a
whole, and the Stacks describe individual versions of the
application. The StackSet also allows defining a "global" load balancer
spanning all stacks of the stackset which makes it possible to switch
traffic to different stacks at the load balancer (for example Ingress) level.
flowchart
A[Ingress Controller] -->|0%| B[Stack\nVersion 1]
A[Ingress Controller] -->|20%| C[Stack\nVersion 2]
A[Ingress Controller] -->|80%| D[Stack\nVersion 3]The StackSet and Stack resources are implemented as
CRDs. A StackSet looks like this:
…The above StackSet would generate a Stack that looks like this:
…For each Stack, the StackSet controller creates a Service, a Deployment,
and an optional Ingress resource automatically with the right labels. An
optional autoscaler resource can also be created per stack for
horizontally scaling the deployment.
For the most part the Stacks will be dynamically managed by the
controller and the users don't have to touch them. You can think of this similar
to the relationship between Deployments and ReplicaSets.
If the Stack is deleted the related resources like Service and
Deployment will be automatically cleaned up.
The stackLifecycle let's you configure two settings to change the cleanup
behavior for the StackSet:
scaleDownTTLSeconds defines for how many seconds a stack should not receive
traffic before it's scaled down.limit defines the total number of stacks to keep. That is, if you have a
limit of 5 and currently have 6 stacks for the StackSet then it will
clean up the oldest stack which is NOT getting traffic. The limit is
not enforced if it would mean deleting a stack with traffic. E.g. if you set
a limit of 1 and have two stacks with 50% then none of them would be
deleted. However, if you switch to 100% traffic for one of the stacks then
the other will be deleted after it has not received traffic for
scaleDownTTLSeconds.StackSet is updated with a new
version in the stackTemplate.routegroup or ingress specified
in the spec. The controller automatically updates each Stacks'
Ingress/RouteGroup when updating the main StackSet's traffic weights. The
ingress controller must implement the TrafficSegment predicate to
effectively switch traffic. For example, Skipper implements this predicate. my-app.example.org, my-app-v1.example.org, my-app-v2.example.org.StackSet or
Stack resource is deleted. This includes Service,
Deployment, Ingress and optionally HorizontalPodAutoscaler.traffic) for showing and switching traffic between
stacks.Ingress creation with
externalIngress: spec, such that external controllers can manage
the Ingress or CRD creation, that will configure the routing into
the cluster.The StackSet controller works with Kubernetes >=v1.23.
The controller watches for StackSet resources and creates Stack resources
whenever the version is updated in the StackSet stackTemplate. For each
StackSet it will create an optional "main" Ingress resource and keep it up
to date when new Stacks are created for the StackSet. For each Stack it
will create a Deployment, a Service. When specified in the parent
StackSet, the controller will create also for each Stack an Ingress and/or
a HorizontalPodAutoscaler for the Deployment. The corresponding Stack owns
these resources, which are cleaned up if the stack is deleted.
Use an existing cluster or create a test cluster with kind
kind create cluster --name testcluster001The stackset-controller can be run as a deployment in the cluster.
See deployment.yaml.
The controller depends on the StackSet and Stack CRDs. You must install these into your cluster before running the controller:
$ kubectl apply -f docs/stackset_crd.yaml -f docs/stack_crd.yamlAfter the CRDs are installed the controller can be deployed:
please adjust the controller version and cluster-domain to your environment
$ kubectl apply -f docs/rbac.yaml -f docs/deployment.yamlThere are cases where it might be desirable to run multiple instances of the stackset-controller in the same cluster, e.g. for development.
To prevent the controllers from fighting over the same StackSet resources
they can be configured with the flag --controller-id= which
indicates that the controller should only manage the StackSets which has an
annotation stackset-controller.zalando.org/controller= defined.
If the controller-id is not configured, the controller will manage all
StackSets which does not have the annotation defined.
Once you have deployed the controller you can create your first StackSet
resource:
$ kubectl apply -f docs/stackset.yaml
stackset.zalando.org/my-app createdThis will create the stackset in the cluster:
$ kubectl get stacksets
NAME CREATED AT
my-app 21sAnd soon after you will see the first Stack of the my-app
stackset:
$ kubectl get stacks
NAME CREATED AT
my-app-v1 30sIt will also create Ingress, Service, Deployment and
HorizontalPodAutoscaler resources:
…Imagine you want to roll out a new version of your stackset. You can do this
by changing the StackSet resource. E.g. by changing the version:
$ kubectl patch stackset my-app --type='json' -p='[{"op": "replace", "path": "/spec/stackTemplate/spec/version", "value": "v2"}]'
stackset.zalando.org/my-app patchedSoon after, we will see a new stack:
$ kubectl get stacks -l stackset=my-app
NAME CREATED AT
my-app-v1 14m
my-app-v2 46sAnd using the traffic tool we can see how the traffic is distributed (see
below for how to build the tool):
./build/traffic my-app
STACK TRAFFIC WEIGHT
my-app-v1 100.0%
my-app-v2 0.0%If we want to switch 100% traffic to the new stack we can do it like this:
# traffic
./build/traffic my-app my-app-v2 100
STACK TRAFFIC WEIGHT
my-app-v1 0.0%
my-app-v2 100.0%Since the my-app-v1 stack is no longer getting traffic it will be scaled down
after some time and eventually deleted.
If you want to delete it manually, you can simply do:
$ kubectl delete stack my-app-v1
stack.zalando.org "my-app-v1" deletedAnd all the related resources will be gone shortly after:
$ kubectl get ingress,service,deployment.apps,hpa -l stackset=my-app,stack-version=v1
No resources found.This project uses Go modules as introduced in Go 1.11 therefore you need Go >=1.11 installed in order to build. If using Go 1.11 you also need to activate Module support.
Assuming Go has been setup with module support it can be built simply by running:
$ export GO111MODULE=on # needed if the project is checked out in your $GOPATH.
$ makeNote that the Go client interface for talking to the custom StackSet and
Stack CRD is generated code living in pkg/client/ and
pkg/apis/zalando.org/v1/zz_generated_deepcopy.go. If you make changes to
pkg/apis/* then you must run make clean && make to regenerate the code.
To understand how this works see the upstream example for generating client interface code for CRDs.
Clients that write the desired traffic switching value have to move
from ingress annotation zalando.org/stack-traffic-weights: '{"mystack-v1":80, "mystack-v2": 20}'
to stackset spec.traffic:
spec:
traffic:
- stackName: mystack-v1
weight: 80
- stackName: mystack-v2
weight: 20暂无开放 Issues,或尚未同步最近议题。