Home
%3CLINGO-SUB%20id%3D%22lingo-sub-288406%22%20slang%3D%22en-US%22%3EKubernetes%20Operations%3A%20Prioritize%20Workload%20in%20Overcommitted%20Clusters%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-288406%22%20slang%3D%22en-US%22%3E%3CP%3EOne%20of%20the%20benefits%20in%20adopting%20a%20system%20like%20Kubernetes%20is%20facilitating%20burstable%20and%20scalable%20workload.%20Horizontal%20application%20scaling%20involves%20adding%20or%20removing%20instances%20of%20an%20application%20to%20match%20demand.%26nbsp%3B%3CSTRONG%3EKubernetes%20Horizontal%20Pod%20Autoscaler%3C%2FSTRONG%3E%20enables%20automated%20pod%20scaling%20based%20on%20demand.%20This%20is%20cool%2C%20however%20can%20lead%20to%20unpredictable%20load%20on%20the%20cluster%2C%20which%20may%20put%20the%20cluster%20into%20an%20overcommitted%20state.%20Fortunately%2C%20with%20a%20goal%20of%20squeezing%20every%20bit%20of%20CPU%20and%20memory%20from%20a%20cluster%2C%20overcommitment%20may%20not%20only%20be%20ok%20but%20desirable.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20following%20image%20represents%20a%20three%20node%20cluster%20that%20runs%20three%20applications.%20Pink%20is%20the%20most%20critical.%20Red%20is%20burst-able%20and%20durable.%20This%20means%20if%20we%20need%20to%20stop%20a%20few%20instances%20of%20red%2C%20things%20will%20be%20ok.%20Blue%20is%20non-critical.%20I%20have%20also%20tried%20to%20depict%20in%20this%20image%20a%20cluster%20that%20is%20a%20fully%20maxed%20out%20state.%20There%20are%20no%20more%20resources%20available%20for%20additional%20workload.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60173iE718ECA740CE02A5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.png%22%20title%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EImaging%20now%20that%20a%20scale%20out%20operation%20is%20needed%20on%20the%20pink%20application.%20This%20puts%20the%20cluster%20in%20an%20overcommitted%20state%20with%20critical%20workload%20requiring%20scheduling.%20How%20can%20Kubernetes%20facilitate%20this%20critical%20request%20in%20an%20overcommitted%20state%3F%20One%20option%20is%20to%20use%20Pod%20Priority%20and%20Preemption%2C%20which%20allows%20a%20priority%20weight%20to%20be%20added%20to%20a%20scheduling%20request.%20In%20the%20event%20of%20overcommitment%2C%20priority%20is%20evaluated%2C%20and%20lower%20priority%20workload%20is%20restarted%20(preemption)%20to%20allow%20for%20scheduling%20of%20the%20priority%20workload.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22podpriorityandpreemptiontutorial%22%20id%3D%22toc-hId-1703005245%22%20id%3D%22toc-hId-1731661268%22%3EPod%20Priority%20and%20Preemption%20tutorial%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20article%2C%20we%20will%20walk%20through%20an%20end-to-end%20demonstration%20of%20using%20Pod%20Priority%20and%20Pre-emption%20to%20ensure%20critical%20workload%20has%20priority%20to%20cluster%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20complete%20this%20tutorial%2C%20you%20need%20a%20Kubernetes%20cluster%20that%20consists%20of%20three%20nodes.%20I've%20included%20steps%20for%20deploying%20an%20appropriately%20sized%20Azure%20Kubernetes%20cluster.%20If%20you%20need%20an%20Azure%20Subscription%20or%20would%20like%20to%20read%20up%20on%20additional%20operational%20practices%20for%20Azure%20Kubernetes%20Service%2C%20see%20the%20following%20links.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fazure.microsoft.com%2Fen-us%2Ffree%2F%3FWT.mc_id%3DITOpsTalk-blog-nepeters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Free%20Trial%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Faks%2Ftutorial-kubernetes-prepare-app%3FWT.mc_id%3DITOpsTalk-blog-nepeters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Kubernetes%20Service%20operations%20best%20practices%3C%2FA%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CH2%20id%3D%22toc-hId--849151716%22%20id%3D%22toc-hId--820495693%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22createanazurekubernetesservicecluster%22%20id%3D%22toc-hId-893658619%22%20id%3D%22toc-hId-922314642%22%3ECreate%20an%20Azure%20Kubernetes%20Service%20Cluster%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFirst%20things%20first%2C%20ensure%20you%20have%20an%20appropriately%20sized%20Kubernetes%20cluster%20for%20this%20tutorial%20(three%20nodes).%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20a%20resource%20group.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Eaz%20group%20create%20--name%20AKSOperationsDemos%20--location%20eastus%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20the%20cluster.%20Note%2C%20the%20Azure%20CLI%20defaults%20are%20suitable%20for%20this%20demo.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Eaz%20aks%20create%20--resource-group%20AKSOperationsDemos%20--name%20AKSOperationsDemos%20--kubernetes-version%201.11.3%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EConnect%20to%20the%20cluster%20as%20cluster%20admin.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Eaz%20aks%20get-credentials%20--resource-group%20AKSOperationsDemos%20--name%20AKSOperationsDemos%20--admin%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId--1658498342%22%20id%3D%22toc-hId--1629842319%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22createapriorityclassforcriticalworkload%22%20id%3D%22toc-hId-84311993%22%20id%3D%22toc-hId-112968016%22%3ECreate%20a%20priority%20class%20for%20critical%20workload%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20an%20instance%20of%20a%20Pod%20Priority%20Class%20with%20a%20weight%20of%20%3CCODE%3E1000000%3C%2FCODE%3E.%20This%20can%20be%20used%20to%20ensure%20that%20high%20priority%20workload%20is%20given%20priority%20to%20cluster%20resource.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ETo%20do%20so%2C%20create%20a%20file%20names%20%3CCODE%3Epc.yml%3C%2FCODE%3E%20and%20copy%20in%20the%20following%20yaml.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EapiVersion%3A%20scheduling.k8s.io%2Fv1beta1%0Akind%3A%20PriorityClass%0Ametadata%3A%0A%20%20name%3A%20high-priority%0Avalue%3A%201000000%0AglobalDefault%3A%20false%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20the%20priority%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20create%20-f%20pc.yml%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-1827122328%22%20id%3D%22toc-hId-1855778351%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22consumeallcpucores%22%20id%3D%22toc-hId--725034633%22%20id%3D%22toc-hId--696378610%22%3EConsume%20all%20CPU%20cores%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20some%20workload%20to%20consume%20all%20CPU%20cores%20in%20the%20cluster.%20In%20the%20following%20example%2C%20a%20deployment%20consisting%20of%20three%20replicas%20is%20started%20with%20a%20CPU%20request%20of%20one%20core%20each.%20This%20will%20effectively%20consume%20the%20available%20CPU%20resources%20of%20the%20cluster.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20a%20file%20named%20%3CCODE%3Eslam-cpu.yml%3C%2FCODE%3E%20and%20copy%20in%20the%20following%20yaml.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EapiVersion%3A%20apps%2Fv1%0Akind%3A%20Deployment%0Ametadata%3A%0A%20%20name%3A%20consume-cpu%0Aspec%3A%0A%20%20replicas%3A%203%0A%20%20selector%3A%0A%20%20%20%20matchLabels%3A%0A%20%20%20%20%20%20app%3A%20consume-cpu%0A%20%20template%3A%0A%20%20%20%20metadata%3A%0A%20%20%20%20%20%20labels%3A%0A%20%20%20%20%20%20%20%20app%3A%20consume-cpu%0A%20%20%20%20spec%3A%0A%20%20%20%20%20%20containers%3A%0A%20%20%20%20%20%20-%20name%3A%20nepetersv1%0A%20%20%20%20%20%20%20%20image%3A%20neilpeterson%2Faks-helloworld%3Av1%0A%20%20%20%20%20%20%20%20resources%3A%0A%20%20%20%20%20%20%20%20%20%20requests%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20%20%20%20%20memory%3A%20128Mi%0A%20%20%20%20%20%20%20%20%20%20limits%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20%20%20%20%20memory%3A%20128Mi%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20the%20deployment.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20create%20-f%20slam-cpu.yml%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-1017775702%22%20id%3D%22toc-hId-1046431725%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22startlowpriorityworkload%22%20id%3D%22toc-hId--1534381259%22%20id%3D%22toc-hId--1505725236%22%3EStart%20low-priority%20workload%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20start%20another%20pod%20without%20specifying%20a%20priority%20class.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20a%20file%20named%20%3CCODE%3Epod-no-priority.yml%3C%2FCODE%3E%20and%20copy%20in%20the%20following%20YAML.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EapiVersion%3A%20v1%0Akind%3A%20Pod%0Ametadata%3A%0A%20%20name%3A%20pod-no-priority%0Aspec%3A%0A%20%20containers%3A%0A%20%20-%20name%3A%20pod-no-priority%0A%20%20%20%20image%3A%20neilpeterson%2Faks-helloworld%3Av1%0A%20%20%20%20resources%3A%0A%20%20%20%20%20%20requests%3A%0A%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20memory%3A%20128Mi%0A%20%20%20%20%20%20limits%3A%0A%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20memory%3A%20256Mi%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20the%20pod.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20create%20-f%20pod-no-priority.yml%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAt%20this%20point%2C%20what%20you%20will%20find%20is%20that%20the%20new%20pod%20cannot%20be%20scheduled%20due%20to%20lack%20of%20CPU%20resources.%20To%20see%20this%2C%20list%20the%20pods%20on%20the%20cluster%20and%20note%20that%20the%20%3CCODE%3Epod-no-priority%3C%2FCODE%3E%20is%20in%20a%20%3CCODE%3EPending%3C%2FCODE%3E%20state.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20get%20pods%0A%0Aconsume-cpu-6c8d576684-gf5sk%20%20%200%2F1%20%20%20%20%20%20%20ContainerCreating%20%20%200%20%20%20%20%20%20%20%20%20%2052s%0Aconsume-cpu-6c8d576684-mtvmn%20%20%200%2F1%20%20%20%20%20%20%20ContainerCreating%20%20%200%20%20%20%20%20%20%20%20%20%2052s%0Aconsume-cpu-6c8d576684-pnkff%20%20%200%2F1%20%20%20%20%20%20%20ContainerCreating%20%20%200%20%20%20%20%20%20%20%20%20%2052s%0Apod-no-priority%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%20%20%20%20%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%2010s%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EReturn%20a%20list%20of%20events%20for%20the%20pod%20to%20see%20the%20actual%20issue.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20describe%20pod%20pod-no-priority%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EParsing%20the%20output%20you%20should%20see%20that%20the%20pod%20cannot%20be%20scheduled%20to%20insufficient%20cpu.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EEvents%3A%0A%20%20Type%20%20%20%20%20Reason%20%20%20%20%20%20%20%20%20%20%20%20Age%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20From%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Message%0A%20%20----%20%20%20%20%20------%20%20%20%20%20%20%20%20%20%20%20%20----%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20----%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-------%0A%20%20Warning%20%20FailedScheduling%20%201s%20(x18%20over%2053s)%20%20default-scheduler%20%200%2F3%20nodes%20are%20available%3A%203%20Insufficient%20cpu.%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH2%20id%3D%22toc-hId-208429076%22%20id%3D%22toc-hId-237085099%22%3E%26nbsp%3B%3C%2FH2%3E%0A%3CH2%20id%3D%22runhighpriorityworkload%22%20id%3D%22toc-hId--1458451320%22%20id%3D%22toc-hId--1429795297%22%3ERun%20high%20priority%20workload%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFinally%20run%20another%20pod%2C%20however%20this%20time%20assign%20the%20high-priority%20class%20to%20the%20pod.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ECreate%20a%20file%20named%20%3CCODE%3Epod-priority.yml%3C%2FCODE%3E%20and%20copy%20in%20the%20following%20yaml.%20Take%20note%20that%20the%20pod%20spec%20includes%20the%20priority%20class%20created%20in%20a%20previous%20step.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3EapiVersion%3A%20v1%0Akind%3A%20Pod%0Ametadata%3A%0A%20%20name%3A%20pod-with-priority%0Aspec%3A%0A%20%20containers%3A%0A%20%20-%20name%3A%20pod-with-priority%0A%20%20%20%20image%3A%20neilpeterson%2Fnepetersv1%0A%20%20%20%20resources%3A%0A%20%20%20%20%20%20requests%3A%0A%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20memory%3A%20128Mi%0A%20%20%20%20%20%20limits%3A%0A%20%20%20%20%20%20%20%20cpu%3A%201%0A%20%20%20%20%20%20%20%20memory%3A%20256Mi%0A%20%20priorityClassName%3A%20high-priority%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20the%20pod.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20create%20-f%20pod-priority.yaml%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENow%20return%20a%20list%20of%20pods.%20If%20done%20quickly%20you%20may%20be%20able%20to%20catch%20one%20of%20the%20lower%20priority%20pods%20being%20terminated.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20get%20pods%0A%0ANAME%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20READY%20%20%20%20%20STATUS%20%20%20%20%20%20%20%20RESTARTS%20%20%20AGE%0Aconsume-cpu-6c8d576684-gf5sk%20%20%201%2F1%20%20%20%20%20%20%20Running%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%207m%0Aconsume-cpu-6c8d576684-mtvmn%20%20%201%2F1%20%20%20%20%20%20%20Running%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%207m%0Aconsume-cpu-6c8d576684-p7tqx%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%203s%0Aconsume-cpu-6c8d576684-pnkff%20%20%201%2F1%20%20%20%20%20%20%20Terminating%20%20%200%20%20%20%20%20%20%20%20%20%207m%0Apod-no-priority%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%206m%0Apod-with-priority%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%20%20%20%20%200%20%20%20%20%20%20%20%20%20%203s%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20lower%20priority%20pod%20has%20been%20terminated%2C%20the%20pod%20with%20priority%20is%20started%20in%20its%20place.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3E%3CCODE%3Ekubectl%20get%20pods%0A%0ANAME%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20READY%20%20%20%20%20STATUS%20%20%20%20RESTARTS%20%20%20AGE%0Aconsume-cpu-6c8d576684-gf5sk%20%20%201%2F1%20%20%20%20%20%20%20Running%20%20%200%20%20%20%20%20%20%20%20%20%208m%0Aconsume-cpu-6c8d576684-mtvmn%20%20%201%2F1%20%20%20%20%20%20%20Running%20%20%200%20%20%20%20%20%20%20%20%20%208m%0Aconsume-cpu-6c8d576684-p7tqx%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%200%20%20%20%20%20%20%20%20%20%201m%0Apod-no-priority%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%200%2F1%20%20%20%20%20%20%20Pending%20%20%200%20%20%20%20%20%20%20%20%20%208m%0Apod-with-priority%20%20%20%20%20%20%20%20%20%20%20%20%20%201%2F1%20%20%20%20%20%20%20Running%20%20%200%20%20%20%20%20%20%20%20%20%201m%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EVery%20cool%20indeed.%20Feel%20free%20to%20contact%20me%20on%20Twitter%20(%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2Fnepeters%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%40nepeters%3C%2FA%3E)%20or%20comment%20below%20for%20discussion%20on%20the%20topic.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-288406%22%20slang%3D%22en-US%22%3E%3CP%3EKubernetes%20Horizontal%20Pod%20Autoscaler%20enables%20automated%20pod%20scaling%20based%20on%20demand.%20This%20is%20cool%2C%20however%20can%20lead%20to%20unpredictable%20load%20on%20the%20cluster%2C%20which%20may%20put%20the%20cluster%20into%20an%20overcommitted%20state.%20In%20this%20article%2C%20Neil%20walks%20through%20an%20end-to-end%20demonstration%20of%20using%20Pod%20Priority%20and%20Pre-emption%20to%20ensure%20critical%20workload%20has%20priority%20to%20cluster%20resources.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F60174i50DD661C20F1209E%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%221.png%22%20title%3D%221.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-288406%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EKubernetes%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ENeil%20Peterson%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

One of the benefits in adopting a system like Kubernetes is facilitating burstable and scalable workload. Horizontal application scaling involves adding or removing instances of an application to match demand. Kubernetes Horizontal Pod Autoscaler enables automated pod scaling based on demand. This is cool, however can lead to unpredictable load on the cluster, which may put the cluster into an overcommitted state. Fortunately, with a goal of squeezing every bit of CPU and memory from a cluster, overcommitment may not only be ok but desirable.

 

The following image represents a three node cluster that runs three applications. Pink is the most critical. Red is burst-able and durable. This means if we need to stop a few instances of red, things will be ok. Blue is non-critical. I have also tried to depict in this image a cluster that is a fully maxed out state. There are no more resources available for additional workload.

 

1.png

 

Imaging now that a scale out operation is needed on the pink application. This puts the cluster in an overcommitted state with critical workload requiring scheduling. How can Kubernetes facilitate this critical request in an overcommitted state? One option is to use Pod Priority and Preemption, which allows a priority weight to be added to a scheduling request. In the event of overcommitment, priority is evaluated, and lower priority workload is restarted (preemption) to allow for scheduling of the priority workload.

 

Pod Priority and Preemption tutorial

 

In this article, we will walk through an end-to-end demonstration of using Pod Priority and Pre-emption to ensure critical workload has priority to cluster resources.

 

In order to complete this tutorial, you need a Kubernetes cluster that consists of three nodes. I've included steps for deploying an appropriately sized Azure Kubernetes cluster. If you need an Azure Subscription or would like to read up on additional operational practices for Azure Kubernetes Service, see the following links.

 

 

Create an Azure Kubernetes Service Cluster

 

First things first, ensure you have an appropriately sized Kubernetes cluster for this tutorial (three nodes). 

 

Create a resource group.

 

az group create --name AKSOperationsDemos --location eastus

 

Create the cluster. Note, the Azure CLI defaults are suitable for this demo.

 

az aks create --resource-group AKSOperationsDemos --name AKSOperationsDemos --kubernetes-version 1.11.3

 

Connect to the cluster as cluster admin.

 

az aks get-credentials --resource-group AKSOperationsDemos --name AKSOperationsDemos --admin

 

Create a priority class for critical workload

 

Create an instance of a Pod Priority Class with a weight of 1000000. This can be used to ensure that high priority workload is given priority to cluster resource.

 

To do so, create a file names pc.yml and copy in the following yaml.

 

apiVersion: scheduling.k8s.io/v1beta1
kind: PriorityClass
metadata:
  name: high-priority
value: 1000000
globalDefault: false

 

Create the priority class.

 

kubectl create -f pc.yml

 

Consume all CPU cores

 

Run some workload to consume all CPU cores in the cluster. In the following example, a deployment consisting of three replicas is started with a CPU request of one core each. This will effectively consume the available CPU resources of the cluster.

 

Create a file named slam-cpu.yml and copy in the following yaml.

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: consume-cpu
spec:
  replicas: 3
  selector:
    matchLabels:
      app: consume-cpu
  template:
    metadata:
      labels:
        app: consume-cpu
    spec:
      containers:
      - name: nepetersv1
        image: neilpeterson/aks-helloworld:v1
        resources:
          requests:
            cpu: 1
            memory: 128Mi
          limits:
            cpu: 1
            memory: 128Mi

 

Run the deployment.

 

kubectl create -f slam-cpu.yml

 

Start low-priority workload

 

Now start another pod without specifying a priority class.

 

Create a file named pod-no-priority.yml and copy in the following YAML.

 

apiVersion: v1
kind: Pod
metadata:
  name: pod-no-priority
spec:
  containers:
  - name: pod-no-priority
    image: neilpeterson/aks-helloworld:v1
    resources:
      requests:
        cpu: 1
        memory: 128Mi
      limits:
        cpu: 1
        memory: 256Mi

 

Run the pod.

 

kubectl create -f pod-no-priority.yml

 

At this point, what you will find is that the new pod cannot be scheduled due to lack of CPU resources. To see this, list the pods on the cluster and note that the pod-no-priority is in a Pending state.

 

kubectl get pods

consume-cpu-6c8d576684-gf5sk   0/1       ContainerCreating   0          52s
consume-cpu-6c8d576684-mtvmn   0/1       ContainerCreating   0          52s
consume-cpu-6c8d576684-pnkff   0/1       ContainerCreating   0          52s
pod-no-priority                0/1       Pending             0          10s

 

Return a list of events for the pod to see the actual issue.

 

kubectl describe pod pod-no-priority

 

Parsing the output you should see that the pod cannot be scheduled to insufficient cpu.

 

Events:
  Type     Reason            Age                From               Message
  ----     ------            ----               ----               -------
  Warning  FailedScheduling  1s (x18 over 53s)  default-scheduler  0/3 nodes are available: 3 Insufficient cpu.

 

Run high priority workload

 

Finally run another pod, however this time assign the high-priority class to the pod.

 

Create a file named pod-priority.yml and copy in the following yaml. Take note that the pod spec includes the priority class created in a previous step.

 

apiVersion: v1
kind: Pod
metadata:
  name: pod-with-priority
spec:
  containers:
  - name: pod-with-priority
    image: neilpeterson/nepetersv1
    resources:
      requests:
        cpu: 1
        memory: 128Mi
      limits:
        cpu: 1
        memory: 256Mi
  priorityClassName: high-priority

 

Run the pod.

 

kubectl create -f pod-priority.yaml

 

Now return a list of pods. If done quickly you may be able to catch one of the lower priority pods being terminated.

 

kubectl get pods

NAME                           READY     STATUS        RESTARTS   AGE
consume-cpu-6c8d576684-gf5sk   1/1       Running       0          7m
consume-cpu-6c8d576684-mtvmn   1/1       Running       0          7m
consume-cpu-6c8d576684-p7tqx   0/1       Pending       0          3s
consume-cpu-6c8d576684-pnkff   1/1       Terminating   0          7m
pod-no-priority                0/1       Pending       0          6m
pod-with-priority              0/1       Pending       0          3s

 

Once the lower priority pod has been terminated, the pod with priority is started in its place.

 

kubectl get pods

NAME                           READY     STATUS    RESTARTS   AGE
consume-cpu-6c8d576684-gf5sk   1/1       Running   0          8m
consume-cpu-6c8d576684-mtvmn   1/1       Running   0          8m
consume-cpu-6c8d576684-p7tqx   0/1       Pending   0          1m
pod-no-priority                0/1       Pending   0          8m
pod-with-priority              1/1       Running   0          1m

 

Very cool indeed. Feel free to contact me on Twitter (@nepeters) or comment below for discussion on the topic.