Beginner's Guide to Services and Namespaces in Kubernetes

Services in Kubernetes

  • Used to access the Kubernetes Pods

Cluster-IP Service

  • It is used to the enable the communication between the pods within the cluster.

Node-Port Service

  • It is used to expose the pod to the internet.

  • Uses the port range : 3000 - 32767

  • Created for each micro service / Application Layer

Load Balancer Service

  • Is used to reduce the number of IPs address to be maintained for NodePort Service.

  • Load Balancer Service Created on top of the NodePort Service, will receives the request from end user through internet and Route the request to the corresponding nodeport service and access the pod within that.

  • Load Balancer Service will be created at the Application Level.

Ingress Controller Service

  • Is used to reduce the Load-Balancer Service and map the services to Load-Balancer

Let’s create a NodePort Service

  • Create Deployment file

    •   apiVersion: apps/v1
        kind: Deployment
        metadata:
          name: my-node-app
        spec:
          replicas: 3
          selector:
            matchLabels:
              app: my-node-app
          template:
            metadata:
              labels:
                app: my-node-app
            spec:
              containers:
              - name: my-node-app
                image: jayk2810/mynodeimg:v1
                ports:
                - containerPort: 3000
      
  • Create Service YAML file

    •   apiVersion: v1
        kind: Service
        metadata:
          name: my-service
        spec:
          type: NodePort
          selector:
            app: my-node-app
          ports:
            - port: 80
              targetPort: 3000
              nodePort: 30028
      
  • Access the deployment from browser using public ip of the instance with nodeport

Kubernetes Host Path Volume

  • Maintain Persistant Data

  • Hostpath Volume is created at the pods level. Not at the Container level.

  •   apiVersion: v1
      kind: Pod
      metadata:
        name: nginx-hostpath
      spec:
        containers:
          - name: nginx-container
            image: nginx
            volumeMounts:
            - mountPath: /test-mnt
              name: test-vol
        volumes:
        - name: test-vol
          hostPath:
            path: /test-vol
    
  • Create and Display HostPath

    •   kubectl create -f nginx-hostpath.yaml
        kubectl get po
        kubectl exec nginx-hostpath df /test-mnt
      
  • Master node

  • Worker node

Namespaces

  • Logical Partition of the Kubernetes Cluster.

  • This will create an isolated path for deployments

  • Namespaces can be created based on the Environments/Teams