Debug School

Cover image for Configure Kubernetes Cluster
Suyash Sambhare
Suyash Sambhare

Posted on

Configure Kubernetes Cluster

To configure a Kubernetes Cluster you'll primarily use the kubectl command-line tool and YAML configuration files. Here are some essential commands and examples of YAML configurations:

Key kubectl Commands

  1. List all nodes: kubectl get nodes
  2. Describe a specific node: kubectl describe node md4-node1-es
  3. Apply a configuration from a YAML file: kubectl apply -f config.yaml
  4. Get detailed information about a pod in YAML format: kubectl get pod pod-node1-es -o yaml

Example YAML Configuration for a Node

Here’s a basic example of a YAML file to configure a node:

apiVersion: v1
kind: Node
metadata:
  name: <node-name>
  labels:
    role: worker
spec:
  taints:
  - key: "key1"
    value: "value1"
    effect: "NoSchedule"
Enter fullscreen mode Exit fullscreen mode

Applying the Configuration

To apply this configuration, save it to a file (e.g., node-config.yaml) and run: kubectl apply -f node-config.yaml

To add a label to an existing node in your Kubernetes cluster, you can use the kubectl label command. Here’s how you can do it:

  1. Open your terminal and run the following command, replacing <node-name> with the name of your node and <label-key>=<label-value> with your desired label: For example, if you want to add a label environment=production to a node named worker-node-1, you would run:
   kubectl label nodes worker-node-1 environment=production
Enter fullscreen mode Exit fullscreen mode
  1. Verify the label has been added by listing the nodes with their labels:
   kubectl get nodes --show-labels
Enter fullscreen mode Exit fullscreen mode

This will display all nodes along with their labels, allowing you to confirm that the label has been successfully applied.

There are several common operations you might perform on nodes in a Kubernetes cluster. Here are some of the most frequently used ones:

1. Cordon a Node

Prevents new pods from being scheduled on the node.

   kubectl cordon <node-name>
Enter fullscreen mode Exit fullscreen mode

2. Uncordon a Node

Allows new pods to be scheduled on the node again.

   kubectl uncordon <node-name>
Enter fullscreen mode Exit fullscreen mode

3. Drain a Node

Safely evicts all pods from the node. This is useful for maintenance.

   kubectl drain <node-name> --ignore-daemonsets --delete-local-data
Enter fullscreen mode Exit fullscreen mode

4. Delete a Node

Removes a node from the cluster.

   kubectl delete node <node-name>
Enter fullscreen mode Exit fullscreen mode

5. View Node Logs

Check the logs for a specific node.

   kubectl logs <node-name>
Enter fullscreen mode Exit fullscreen mode

6. Update Node Labels

Modify existing labels on a node.

   kubectl label nodes <node-name> <label-key>=<label-value> --overwrite
Enter fullscreen mode Exit fullscreen mode

7. Annotate a Node

Add annotations to a node for additional metadata.

   kubectl annotate node <node-name> <annotation-key>=<annotation-value>
Enter fullscreen mode Exit fullscreen mode

8. Check Node Status

Get detailed status information about a node.

   kubectl describe node <node-name>
Enter fullscreen mode Exit fullscreen mode

These operations help manage the nodes effectively, ensuring the cluster runs smoothly and efficiently.

Kubernetes

Troubleshooting node-related issues in a Kubernetes cluster

This kind of troubleshooting involves several steps to identify and resolve the problem. Here are some common strategies and tools you can use:

1. Check Node Status

Use kubectl to get the status of your nodes:

   kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

For more detailed information:

   kubectl describe node <node-name>
Enter fullscreen mode Exit fullscreen mode

2. Inspect Node Logs

Check the logs for any errors or warnings:

   kubectl logs <node-name>
Enter fullscreen mode Exit fullscreen mode

3. Monitor Resource Usage

Ensure that the node has sufficient resources (CPU, memory, disk space):

   kubectl top nodes
Enter fullscreen mode Exit fullscreen mode

4. Check Pod Status

Sometimes node issues are related to the pods running on them. Check the status of the pods:

   kubectl get pods --all-namespaces -o wide
Enter fullscreen mode Exit fullscreen mode

5. Review Events

Kubernetes events can provide insights into what might be going wrong:

   kubectl get events --all-namespaces
Enter fullscreen mode Exit fullscreen mode

6. Network Connectivity

Verify that the node can communicate with other nodes and the control plane:

   kubectl exec -it <pod-name> -- ping <node-ip>
Enter fullscreen mode Exit fullscreen mode

7. Node Conditions

Nodes have conditions that can indicate issues (e.g., Ready, MemoryPressure, DiskPressure):

   kubectl get nodes -o json | jq '.items[].status.conditions'
Enter fullscreen mode Exit fullscreen mode

8. Check for Taints and Tolerations

Ensure that there are no taints preventing pods from being scheduled on the node:

   kubectl describe node <node-name> | grep -i taint
Enter fullscreen mode Exit fullscreen mode

9. Review Configuration Files

Sometimes misconfigurations in YAML files can cause issues. Double-check your node configuration files for errors.

10. Consult Documentation and Community Resources

The Kubernetes documentation and community forums like Stack Overflow can be invaluable resources for troubleshooting.

Troubleshooting networking issues

On a Kubernetes node there are several steps to identify and resolve the problem. Here are some common strategies and tools you can use:

1. Check Node Status

Ensure the node is in a healthy state:

   kubectl get nodes
   kubectl describe node <node-name>
Enter fullscreen mode Exit fullscreen mode

2. Inspect Network Configuration

Verify the network configuration on the node:

   ifconfig
   ip a
Enter fullscreen mode Exit fullscreen mode

3. Check Network Policies

Ensure there are no network policies blocking traffic:

   kubectl get networkpolicies --all-namespaces
Enter fullscreen mode Exit fullscreen mode

4. Ping Test

Test connectivity between nodes and pods:

   kubectl exec -it <pod-name> -- ping <node-ip>
Enter fullscreen mode Exit fullscreen mode

5. DNS Resolution

Verify DNS resolution within the cluster:

   kubectl exec -it <pod-name> -- nslookup <service-name>
Enter fullscreen mode Exit fullscreen mode

6. Check for IP Forwarding

Ensure IP forwarding is enabled on the node:

   sysctl net.ipv4.ip_forward
Enter fullscreen mode Exit fullscreen mode

If it’s disabled, enable it:

   sysctl -w net.ipv4.ip_forward=1
Enter fullscreen mode Exit fullscreen mode

7. Inspect iptables Rules

Check the iptables rules to ensure they are correctly configured:

   iptables -L -v -n
Enter fullscreen mode Exit fullscreen mode

8. Review Logs

Check the logs for any errors or warnings related to networking:

   journalctl -u kubelet
Enter fullscreen mode Exit fullscreen mode

9. Use Network Troubleshooting Tools

Tools like netshoot can be very helpful. You can deploy a netshoot pod to troubleshoot networking issues:

   kubectl run netshoot --rm -it --image nicolaka/netshoot -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

10. Consult Documentation and Community Resources

Refer to the Kubernetes documentation and community forums for additional guidance.

Ref: https://kubernetes.io/docs/reference/kubectl/

Top comments (0)