Is there something like Swarm's Task-Slot in Kubernetes?
11 Months late, but here is the solution:
To get stable container (pod) names within K8S you must use a StatefulSet
. StatefulSets
are designed for applications that must maintain state, however, if you aren't using K8S volumes for state (keeping them ephemeral) you can use StatefulSets
without an issue. There is a simple process of converting your Deployment
to a StatefulSet
:
- Change your
apiVersion:
toapps/v1
- Change the
kind:
toStatefulSet
- Under
spec:
add theselector:
tag. This tag will contain everything you use to select the appropriate service. In addition you must ensure that any items you have underspec:template:metadata:labels
match those underspec:selector:matchLabels
(This will make more sense in the provided example) - If you have any sort of update strategy, called
strategy
in aDeployment
, change this toupdateStrategy
. For future reference, if you do not update this, you will end up with both aStatefulSet
and aReplicaSet
being deployed as K8S is trying to fill thestrategy
and yourStatefulSet
requirements.
After applying these changes, you will have your StatefulSet
deployed. How do we get task slots from this? The hostname.
Since K8S is maintaining stable pod names, you will have names such as:
pod/mypod-0 1/1 Running 0 10mpod/mypod-1 1/1 Running 0 9m
when running kubetctl
. After that, it is a simple matter of parsing the number out of your pod name.
Below is the YAML for a StatefulSet
:
apiVersion: apps/v1kind: StatefulSetmetadata: name: myStatefulSetName labels: app: SomeLabel1 label2: SomeLabel2spec: replicas: 100 selector: matchLabels: app: SomeLabel1 label2: SomeLabel2 updateStrategy: type: RollingUpdate template: metadata: labels: app: SomeLabel1 label2: SomeLabel2 spec: containers: - name: myPodName image: myPod:latest imagePullPolicy: Always ports: - name: myPodPort containerPort: 8080
The differences become apparent on a equivalent Deployment
:
apiVersion: extensions/v1beta1kind: Deploymentmetadata: name: myDeploymentName labels: app: SomeLabel1 label2: SomeLabel2spec: replicas: 100 strategy: type: RollingUpdate template: metadata: labels: app: SomeLabel1 label2: SomeLabel2 spec: containers: - name: myPodName image: myPod:latest imagePullPolicy: Always ports: - name: myPodPort containerPort: 8080