Assigning K8s Pods to a Specific Worker Node

Samuel Addico
CodeOps
Published in
2 min readMar 11, 2019

Sometime last year, we were faced with a situation where we needed to deploy kubernetes containers to specific nodes in the EKS cluster. Well, luckily for us kubernetes had a feature like that: just a simple command. We will be going what I did to get this up.

Note: This session assumes you already have a basic understanding of Kubernetes pod and your cluster is setup.If not follow the getting starting guideline here

Create Labels for Worker Nodes

There are various ways of handling this including NodeSelector, In-Built Node Label among others. We will be using the NodeSelctor for this session. The NodeSelector specifies a map of key-value pairs which is attached to a specific node. For the pod to be assigned to a node, the pod must have each of the indicated key-value pairs as labels as node selectors configured in the deployment manifest. To assign a label to a node, see below

Get the list of Nodes

kubectl get nodes

Set Labels

kubectl label node ip-192-168-173-183.us-west-2.compute.internal  app_name=employee-management env=uat

Configure NodeSelector on Pod

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: employee-managment
spec:
replicas: 2
template:
metadata:
labels:
app: employee-managment
spec:
containers:
- name: employee-managment
image: aws_account_id.dkr.ecr.us-west-2.amazonaws.com/employee-app:59d48ede0fa538a062d3a54f6a2a1f6913d59ff1
ports:
- containerPort: 9000
nodeSelector:
app_name: employee-managment
env: uat

Above will create deployment and assign the pods to nodes ip-192–168–173–183.us-west-2.compute.internal. See below

kubectl get pods
kubectl describe pod employee-managment-5bbc4dd596–5ktlr

This is awesome.thank you for reading :-)

--

--

Samuel Addico
CodeOps

DevOps | SRE | Java | Kubernetes | AWS | Ansible | Terraform | CI/CD