Skip to main content

Kubernetes: 5. Services

  • A service is a stable endpoint to connect to "something"
  • An abstract way to expose an application running on a set of pods as a network service.
  • Services enable communication between various components within and outside of the application
  • With Kubernetes Services there is no need to configure the application for service discovery
  • Kubernetes Service is an object just like Pod, ReplicaSet etc
  • There is always a service running when Kubernetes is installed, Kubernetes API itself
  • When a service is created, kubernetes creates the endpoints (kubectl get endpoints)
  • The endpoints has all the pods associated with that service

Headless Service
  • A headless service is obtained by setting clusterIP  field to None
  • Since there is no virtual IP address, there is no load balancer either
  • The DNS service will return the pods' IP addresses as multiple A records
  • This gives us an easy way to discover all the replicas for a deployment
  • This is useful for creating stateful services like DB, ElasticCache which requires persistent connection with a pod
What is Service Discovery
This is the process of making a client aware to which server to connect to.
In good old ways we used to store the server information in a configuration file in Web application to which the client connects.
But now with Cloud and Micro Services architecture, the nodes are created/destroyed depending on the load. So we cannot have a Static IP to connect to. To overcome this a Service Registry is maintained, which holds the information of each node, when it comes alive and updates the information based on the heart beat. There are 2 types of service discovery

1. Client Side Service Discovery. Client connects to the registry and gets the information for which 
    node to connect to.
2. Server Side Service Discovery. Client connects to LB which then talks to service registry to get the 
    information. Eg AWS ELB
  • The default type (ClusterIP) only works for internal traffic
  • If we want to accept external traffic, we can use one of these:
    • NodePort (expose a service on a TCP port between 30000-32768)
    • LoadBalancer (provision a cloud load balancer for our service)
    • ExternalIP (use one node's external IP address)
    • Ingress (a special mechanism for HTTP services)
Service Types:
  1. NodePort
  2. Cluster IP
  3. Load Balancer
Node Port Service
  • A node port service listens to traffic on a host-node port and forwards it to a port on the pod port
  • This is called node port service, as the service is listening on the node port
  • Node Ports can only be between 30000 - 32767
  • Service gets an IP called clusterIP, but this is not used in NodePort
    1. NodePort -> Port exposed on the node. This is not mandatory, if not specified Kubernetes automatically allocates this.
    2. Port -> The port exposed on the sevice, nodePort forwards traffic to this port. This is mandatory.
    3. TargetPort -> The port exposed on the container, service port forwards traffic to this port. This is not mandatory, if not specified Kubernetes allocates the same port as mentioned in the service
  • When we execute kubectl get services, we can see that the Type is NodePort, the PORT(S) will specify the service port and the node port using which the application is accessed
 
 
service-nodeport-definition.yaml
apiVersion: v1
kind: Service
metadata:
    name: myapp-service

spec:
    type: NodePort
    ports:
    - targetPort: 80
      port: 80
      nodePort: 30008
selector:
    app: myapp
    type: front-end


ClusterIP
  • In this service, Kubernetes creates a Virtual IP inside the cluster to enable communication between different services
  • When we execute kubectl get services, we can see that the Type is clusterIP, the CLUSTER-IP address has to be used to access the application
service-clusterip-definition.yaml
apiVersion: v1
kind: Service
metadata:
    name: backend-service

spec:
    type: clusterIP
    ports:
    - targetPort: 80      --container-port
      port: 80            --service-port
    selector:
        app: myapp
        type: front-end


LoadBalancer
  • In this service a LB is created in the selected Cloud Platform
  • LB distributes the traffic across different services
kubectl create -f service-nodeport-definition.yaml 
-> Create service definition

kubectl get services 
-> Get the list of existing services

kubectl get endpoints
-> Get the list of endpoints associated with the services

Comments

Popular posts from this blog

Kubernetes: 6. Imperative Commands

Imperative commands are useful in quickly creating the resources on Kubernetes kubectl --dry-run -> When created with this option, Kubernetes just validates the definition and does not actually create the resource -o=yaml -> Gets the output in YAML format kubectl run nginx-pod --image=nginx:alpine  -> Create a pod with name nginx-pod and image nginx:alpine kubectl run httpd --image=httpd:alpine  -> By default, run implies run-a-pod kubectl run redis --image=redis:alpine --labels=tier=db  -> Create a pod with name redis and image redis:alpine and labels set to tier=db kubectl run custom-nginx --image=nginx --port=8080  -> Create a pod with name custom-nginx and image nginx to run on port 8080 kubectl expose pod redis --port=6379 --name=redis-service  -> Create a service with name redis-service to expose pod named redis on service-port 6379 kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3  -> Create a depl...

Kubernetes: 21. Secrets

Passwords In the webapps we store the properties file for storing and retrieving the data required by application But we never store the application passwords, truststore, keystore passwords etc here We might store them in an encrypted format, but storing them as plain text is not the correct way In Kubernetes we store these sensitive information in Secrets https://medium.com/avmconsulting-blog/secrets-management-in-kubernetes-378cbf8171d0 Secrets Secrets are used to store the sensitive information They are similar to ConfigMaps, except that they are stored in hashed or encoded format Note that they are only encoded (using base64) but are not encrypted So secrets are a safe option to store sensitive information but infact they are not the safest option As such secret objects should be not checked into source code tools, its best to store them encrypted at REST in ETCD Again as in ConfigMaps, we have to create the secrets object first and then inject them into the pods There are 2 ways ...

Kubernetes: 7. Manual Scheduling

As new pod definition files are created, Kubernetes goes through them and looks for the property nodeName If this property does not exist, then Kubernetes has the job of scheduling this pod It then looks for the nodes that can host this pod and schedules it there And the pod definition is updated with the nodeName where it is running The property nodeName in the below definition file is optional. If the property is specified then kubernetes automatically schedules the pod on the selected node. This is called manual scheduling. Once the Kubernetes identifies the node on which to run, it creates a binding object that binds the pod with the node on which the pod will run pod-definition.yaml apiVersion: v1 kind: Pod metadata:      name: myapp-pod      labels:           app: myapp           location: IN spec:      nodeName: node01      contai...