Knowledge Bank
  • My GitBook
  • Miscellaneous
  • Project
    • Rider and Intellij
    • Code
    • Frontend
      • Condition
      • AddConditionModalDialog
    • Backend
    • e2e
      • fragments
  • JAVASCRIPT
    • Promise
    • Destructuring
    • Spread Syntax and Rest Parameters
    • Typescript
      • Examples of Types
      • React Typescript
    • This
    • Dot Notation vs Bracket Notation
    • Shallow vs Deep Clone
    • New ES Edition
  • C#
    • Project Note
    • Basic
    • Shortcut and Debugging
  • Programming Paradigms
    • SOLID Principles
    • Object Oriented Programming (OOP)
      • Evolution of OOP (Procedural to OOP)
      • Instantiation
      • 4 Pillars of OOP
      • Extra
    • Functional Programming (FP)
      • Idempotent
      • Imperative vs Declarative
      • Immutability
      • High Order Function and Closure
      • Currying
      • Partial Application
      • Memoization and Caching
      • Compose and Pipe
      • Extra
      • Example of FP
    • OOP vs FP
      • Composition vs Inheritance
  • DATA STRUCTURE
    • Big O
    • Data Structure
    • Array
    • Hash Table
    • Linked List
    • Queue and Stack
    • Tree
      • Binary Heap
      • Trie
    • Graph
      • Example of Graph
  • React-Redux
    • MobX
    • Best Practices
  • Algorithms
    • Recursion
      • Examples of Recursion
    • Sorting
    • Searching and Traversal
    • Dynamic Programming
  • REFACTORING
    • Clean Code
      • Formatting
      • Error Handling
      • Concurrency
      • Testing
      • SOLID Principles
      • Classes
      • Objects and Data Structures
      • Variables
      • Functions
    • Code Smells
      • Long Function
      • Duplicate Code
      • Loops
      • Double Negative
      • Christmas Tree Code
      • Complex Condition
      • Primitive Obsession
      • Speculative Generality
      • God Class
      • Long Parameter List
  • Junior to Senior
    • AWS
      • Lambda
    • Session + Authentication
    • Redis
    • Kubernetes
      • Networking
      • Services
      • Deployment
      • Replica Set
      • YAML
      • pod-definition.yml
      • Kubectl
      • Pods
      • Fundamentals
    • Docker
      • Operating System - Extra
      • Dockerfile - Docker Image
      • Docker Storage
      • Docker Network
      • Docker Registry
      • Docker Command
      • Docker Compose
      • Docker Compose - Postgres
    • Security
      • Logging
      • HTTPS, Cross-Site-Scripting (XSS) and Cross-Site-Request-Forgery (CSRF)
      • 3rd Party Library
      • Injection
      • Code Secret, Secure Header, Access Control, Data Management, Authentication
    • CI/CD
    • SPA vs Server-Side Rendering
    • Performance
      • Optimized Code
      • Critical Render Path
      • Backend Optimization
      • Minimized Files and Images
      • Minimized Delivery
  • SECURITY
    • Encryption
    • SSH
  • Command
  • Cheatsheet
    • NPM
    • GIT
  • Writing Template
    • Guide
    • API
    • ChangeLog
    • FAQ
  • Linux
Powered by GitBook
On this page

Was this helpful?

  1. Junior to Senior
  2. Kubernetes

Services

PreviousNetworkingNextDeployment

Last updated 4 years ago

Was this helpful?

Kubernetes services are for:

  • Enable communication between various components within (frontend pods with backend pods) and outside the application (frontend pods with end users)

  • Create microservices architecture in our app

So how do you access the pod from your laptop?

For example, Worker node has its IP add (192.168.1.2), Laptop has its IP add (192.168.1.10). There is internal pod network (10.244.0.0) and the pod own IP add (10.244.0.2). I cannot access the pod IP add as it is in the separate network. So what is the options to access?

  • Not recommended - is to SSH into the Node port and from the node we can do a curl into the pod IP add or access the browser by typing http://10.244.0.2 if GUI is provided

  • Recommended - Communication with the pod without SSH into the node, by simply just accessing the Node IP add. We need a middle ware that route request from our laptop to the Node and to the Pod. This middle ware is called Kubernetes Services. It is one of kubernetes object and this service is called Node Port.

There are 3 different kind of services:

  • Nodeport => make the internal pod accessible though the node

  • Cluster IP => create virtual IP inside the cluster to enable communication between different services such as a set of frontend pods to set of backend pods

  • Load balance => provision load balancer for our application in the supported cloud providers

Node Port Service

Service is like a virtual server inside the node and has its own IP add called Cluster IP (in the example above, the IP add is 10.106.1.12).

The only mandatory field of ports is port. If target port is not defined, it is assumed to be same as port. If nodePort is not defined, it is assigned randomly a port within the valid range of 30000 - 32767.

Ports field in the yml file are in array meaning you can create multiple mapping port within a single service.

To connect service to the pod, we use the selector field from yml file and label from the pod. So if you have multiple pods with the same labels, it will be automatically under the services and considered them as endpoints to the forwarded external request coming from users. This is where the load balancing happen (on random algorithm and session affinity is yes).

What happens when multiple pods are distributed across multiple nodes ? Service automatically span across the Nodes in the cluster and map the target port across Nodes in the cluster to the single node port in this case 30008.

So you just curl into different node IP with the same node port.

kubectl create -f service-definition.yml
// create service


kubectl get services
// list all services 

Cluster IP Service

How to establish this connectivity between these services?

The pods has it own IP add but these IP add is not static as the pod can go down and come back up with new IP, so we cannot rely on this internal pod IP add for communication.

Kubernetes will group these pod together and provide a single interface to access the pods in the group.

This makes it easy to create microservices app as each layer can scale and move without impacting the communication.

Each Cluster IP service get an IP and a name assigned it and that is the name that should be used by the pods to access the services.