How to solve the issue if the 404 default backend is in the Kubernetes cluster?

215    Asked by DavidEDWARDS in Devops , Asked on Jul 17, 2024

 I am currently working on a particular task that is related to the Kubernetes cluster. While going through the workflow I have noticed that requests to my application sometimes return a 404 error. This error message usually indicates that the “default backend” is serving these requests. Explain to me how can I troubleshoot and resolve this particular issue. 

Answered by Dipika Agarwal

 In the context of DevOps, here are the steps given for how you can troubleshoot and resolve this particular issue:-

Checking ingress rules

First, you would need to inspect the ingress resources to ensure that the path and also the host rules are correctly defined or not.

Kubectl get ingress -n -o yaml

Verify the hostnames and paths

You should also ensure that the requested URL hostnames and path should match the ingress rules.

apiVersion: networking.k8s.io/v1

kind: Ingress

metadata:

  name: example-ingress

spec:
  rules:
Host: “example.com”
    http:
      paths:
Path: /app
pathType: Prefix
        backend:
          service:
            name: example-service
            port:
              number: 80

Checking service Configuration

You should try to ensure that the service band endpoint is correctly Configured and try service points to the right pods.

Kubectl get svc -n

Kubectl get endpoints -n

Inspect Ingress controller logs

You should check the logs for the ingress controller to see if there are any errors or issues.

Kubectl logs -l app= -n

Testing the connectivity

You can test the connectivity to the service from within the cluster.

Kubectl exec -it -- curl http://..svc.cluster.local:

DNS and hostname verification

You should try to verify the DNS settings and try to ensure that the domain name points to the correct IP address of the ingress controller.

Here is a java based example given below which would demonstrate how you can troubleshoot and resolve the issue:-

Import io.kubernetes.client.openapi.ApiClient;
Import io.kubernetes.client.openapi.ApiException;
Import io.kubernetes.client.openapi.Configuration;
Import io.kubernetes.client.openapi.apis.CoreV1Api;
Import io.kubernetes.client.openapi.models.V1Service;
Import io.kubernetes.client.openapi.models.V1ServiceList;
Import io.kubernetes.client.util.Config;
Import java.io.IOException;
Import java.util.List;
Public class KubernetesIngressTroubleshooting {
    Public static void main(String[] args) {
        String namespace = “default”;
        String ingressName = “example-ingress”;
        String serviceName = “example-service”;
        Try {
            // Initialize Kubernetes client
            ApiClient client = Config.defaultClient();
            Configuration.setDefaultApiClient(client);
            // Retrieve and print Ingress resource
            String ingressYaml = getIngressYaml(namespace, ingressName);
            System.out.println(“Current Ingress YAML:”);
            System.out.println(ingressYaml);
            // Check services in the namespace
            List services = listServices(namespace);
            System.out.println(“
Services in namespace “ + namespace + “:”);
            For (V1Service service : services) {
                System.out.println(“ – “ + service.getMetadata().getName());
            }
            // Troubleshoot by checking logs of the Ingress controller
            String ingressControllerPodLabel = “ingress-nginx-controller”;
            String controllerLogs = getIngressControllerLogs(namespace, ingressControllerPodLabel);
            System.out.println(“
Ingress Controller Logs:”);
            System.out.println(controllerLogs);
            // Test connectivity to the service from within a pod
            String podName = “example-pod”;
            String curlCommand = “kubectl exec -it “ + podName + “ – curl http://” + serviceName + “.” + namespace + “.svc.cluster.local”;
            System.out.println(“
Command to test connectivity:”);
            System.out.println(curlCommand);
        } catch (IOException | ApiException e) {
            System.err.println(“Exception occurred: “ + e.getMessage());
            e.printStackTrace();
        }
    }
    Private static String getIngressYaml(String namespace, String ingressName) throws ApiException {
        Return executeCommand(“kubectl get ingress “ + ingressName + “ -n “ + namespace + “ -o yaml”);
    }
    Private static List listServices(String namespace) throws ApiException {
        CoreV1Api api = new CoreV1Api();
        V1ServiceList serviceList = api.listNamespacedService(namespace, null, null, null, null, null, null, null, null);
        Return serviceList.getItems();
    }
    Private static String getIngressControllerLogs(String namespace, String podLabel) throws ApiException {
        Return executeCommand(“kubectl logs -l app=” + podLabel + “ -n “ + namespace);
    }
    Private static String executeCommand(String command) {
        // Placeholder for executing shell commands, e.g., using ProcessBuilder
        // For simplicity, this example assumes command execution and returns output as String
        Return “Command execution result for: “ + command;
    }
}


Your Answer

Interviews

Parent Categories