Exercise : 4 Starting Kubernetes at local and deploying selenium grid - Raneesh02/QA_Infra_Tests GitHub Wiki

Steps to Start Kubernetes Locally Using Docker Desktop

  1. Open Docker Desktop:

    • Launch Docker Desktop from your applications.
  2. Enable Kubernetes:

    • Click on the Docker Desktop icon in your system tray.
    • Navigate to the Settings or Preferences menu.
    • Select the Kubernetes tab from the left sidebar.
    • Check the box that says Enable Kubernetes.
    • Click Apply & Restart to start Kubernetes.
  3. Verify Kubernetes is Running:

    • Open a terminal and run the following command to check the status of Kubernetes:
      kubectl cluster-info
      
    • If Kubernetes is running correctly, you should see output similar to:
      Kubernetes control plane is running at https://localhost:6443
      CoreDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
      
  4. Check Nodes Status:

    • Ensure that your Kubernetes node is ready by running:
      kubectl get nodes
      
    • You should see something like:
      NAME             STATUS   ROLES           AGE   VERSION
      docker-desktop   Ready    control-plane   10m   v1.25.4
      
  5. Create a Selenium Grid Deployment for the Hub:

    • Create a file named selenium-hub-deployment.yaml with the following content:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: selenium-hub-deployment
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: selenium-hub
        template:
          metadata:
            labels:
              app: selenium-hub
          spec:
            containers:
            - name: selenium-hub
              image: selenium/hub:4.17.0-20240123
              ports:
              - containerPort: 4444
              env:
              - name: SE_EVENT_BUS_PUBLISH_PORT
                value: "4442"
              - name: SE_EVENT_BUS_SUBSCRIBE_PORT
                value: "4443"
      

Explanation:

  • apiVersion: apps/v1: Specifies the API version used to create the deployment.

  • kind: Deployment: Indicates that this YAML file is creating a Kubernetes Deployment resource.

  • metadata:

    • name: selenium-hub-deployment: The name of the deployment.
  • spec:

    • replicas: 1: Specifies that only one replica (or pod) of the Selenium Hub should be running.
    • selector:
      • matchLabels: Ensures that the deployment manages pods with the specified labels.
        • app: selenium-hub: This label is used to identify the pods that belong to this deployment.
    • template: Defines the pod template used by the deployment to create pods.
      • metadata:
        • labels: Labels for the pod that must match the selector.
          • app: selenium-hub: This label is attached to the pod created by the deployment.
      • spec:
        • containers: Defines the container(s) within the pod.
          • name: selenium-hub: The name of the container.
          • image: selenium/hub:4.17.0-20240123: The Docker image for the Selenium Hub, specifying the version 4.17.0-20240123.
          • ports:
            • containerPort: 4444: Exposes port 4444 within the container for the Selenium Grid web interface.
          • env: Defines environment variables passed to the container.
            • SE_EVENT_BUS_PUBLISH_PORT: "4442": Sets the publish port for the event bus.
            • SE_EVENT_BUS_SUBSCRIBE_PORT: "4443": Sets the subscribe port for the event bus.
  • Apply the deployment using:

    kubectl apply -f selenium-hub-deployment.yaml
    
  • Verify the deployment by running:

    kubectl get pods
    

    You should see a pod with the name selenium-hub-deployment running.

This step will deploy the Selenium Grid hub in your Kubernetes cluster.

  1. Start the Selenium Hub Service:
    • Create a file named selenium-hub-service.yaml with the following content:

      apiVersion: v1
      kind: Service
      metadata:
        name: selenium-hub-service
        labels:
          app: selenium-hub
      spec:
        selector:
          app: selenium-hub
        ports:
          - protocol: TCP
            port: 4444
            targetPort: 4444
            name: port0
          - protocol: TCP
            port: 4443
            targetPort: 4443
            name: port1
          - protocol: TCP
            port: 4442
            targetPort: 4442
            name: port2
        type: LoadBalancer
      
    • Apply the service configuration using:

      kubectl apply -f selenium-hub-service.yaml
      
    • Verify the service is running by checking its status:

      kubectl get services
      

      You should see the selenium-hub-service listed, along with its corresponding ports.

This step sets up a LoadBalancer service that exposes the Selenium Grid hub on ports 4444, 4443, and 4442, making it accessible for your test executions.

  1. Start the Selenium Node:
    • Create a file named selenium-node-chrome-deployment.yaml with the following content:
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: selenium-node-chrome
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: selenium-node-chrome
        template:
          metadata:
            labels:
              app: selenium-node-chrome
          spec:
            containers:
            - name: selenium-node-chrome
              image: selenium/node-chrome:latest
              env:
              - name: TZ
                value: Asia/Kolkata
              - name: SE_NODE_MAX_SESSIONS
                value: "1"
              - name: SE_NODE_SESSION_TIMEOUT
                value: "21600"
              - name: SE_NODE_OVERRIDE_MAX_SESSIONS
                value: "true"
              - name: SE_OPTS
                value: --log-level FINE
              - name: SE_EVENT_BUS_HOST
                value: selenium-hub-service.default.svc.cluster.local
              - name: SE_EVENT_BUS_PUBLISH_PORT
                value: "4442"
              - name: SE_EVENT_BUS_SUBSCRIBE_PORT
                value: "4443"
              ports:
              - containerPort: 5555
      

Note : For Mac, change chrome Image to seleniarm/node-chromium:latest

  • Deploy the node using:

    kubectl apply -f selenium-node-chrome.yaml
    
  • Verify that the Selenium Node is running:

    kubectl get pods
    

    You should see a pod named selenium-node-chrome running, which is connected to the Selenium Hub.

This step configures and starts a Selenium Node with Chrome, which connects to the Selenium Hub, allowing you to run tests in a Chrome browser within your Kubernetes cluster.

  1. Check Logs for Selenium Hub and Node:

    • Check Logs for Selenium Hub:

      • First, get the name of the Selenium Hub pod:
        kubectl get pods -l app=selenium-hub
        
      • You should see a pod with a name that includes selenium-hub. Use this name to check the logs:
        kubectl logs <selenium-hub-pod-name>
        
      • Replace <selenium-hub-pod-name> with the actual name of your Selenium Hub pod. This will show the logs generated by the Selenium Hub.
    • Check Logs for Selenium Node:

      • Similarly, get the name of the Selenium Node pod:
        kubectl get pods -l app=selenium-node-chrome
        
      • You should see a pod with a name that includes selenium-node-chrome. Use this name to check the logs:
        kubectl logs <selenium-node-chrome-pod-name>
        
      • Replace <selenium-node-chrome-pod-name> with the actual name of your Selenium Node pod. This will show the logs generated by the Selenium Node.

These commands will help you diagnose issues or monitor the status of your Selenium Grid components by examining their logs.

Reference for kubectl commands : https://kubernetes.io/docs/reference/kubectl/quick-reference/


Do the local execution by pointing to http://localhost:4444


  1. Delete Deployments One by One:

    • Delete the Selenium Node Deployment:

      • Run the following command to delete the Selenium Node deployment:
        kubectl delete deployment selenium-node-chrome
        
    • Delete the Selenium Hub Deployment:

      • Run the following command to delete the Selenium Hub deployment:
        kubectl delete deployment selenium-hub-deployment
        
    • Delete the Selenium Hub Service:

      • Run the following command to delete the Selenium Hub service:
        kubectl delete service selenium-hub-service
        

These commands will remove the deployments and service from your Kubernetes cluster, cleaning up the resources used by the Selenium Grid setup.

Make sure to run below commands again to check no pods/ services running.

kubectl get pods 

and

kubectl get svc 

Please note not to use kubectl delete all commands if you are not fully sure as that can delete all deployments in a namespace