Kubernetes Node Labeling

You can constrain a pod to only be able to run on particular nodes or to prefer to run on particular nodes. There are several ways to do this, and they all use label selectors to make the selection.

1. Add label to the node

You can add label to the node using this command kubectl label nodes <node-name> <label-key>=<label-value>. So for example the command will look like this.

kubectl label nodes 10.200.0.22 disktype=ssd  

2. Create deploment file

kind: Deployment  
apiVersion: extensions/v1beta1  
metadata:  
  labels:
    app: nginx
  name: nginx-ssd
  namespace: default
spec:  
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        resources:
          limits:
            cpu: 100m
            memory: 200Mi
          requests:
            cpu: 100m
            memory: 100Mi
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /
            port: 80
          initialDelaySeconds: 30
          timeoutSeconds: 1
      nodeSelector:
        disktype: ssd

3. Create deployment

kubectl create -f nginx-ssd.yaml  

Lukman

Radio astronomer pretending to be system engineer.

Bandung, Indonesia