Introduction to Microservices
Introduction
Characteristics
- decentralized - no unifying schema
- polyglot - heterogenous approach
- independent - they don’t affect each other
- do 1 thing well - if it is too complex, split it
- black box - each microservice is a box, communication is done via API
- you build it, you run it
Benefits
- Agility - different teams own different services
- Innovation - any in any language
- Quality - gets POO advantages: reusability, composability, maintenaibility
- Scalability - vertical (bigger machines) horizontal (more machines) + change failing components
- Availability - easier failure isolation
Challenges
- distributed systems (bandwith, network usage)
- migration (define patterns)
- versions (define pipeline release)
- organization (build effective team structure)
Migration to Microservices
- Brownfield applications are common (old not build with latest approaches)
- Use structure of web application (individual URIs) to divide them in functional domains, and replace those domains with new microservices
- Strangler application patterns: a vine strangles the tree that it is wrapped around
- Strangler application steps:
- 1- Transform: create paralel site
- 2- Coexist: leave the old one, but redirect every new functionallity as it is implemented incrementally
- 3- Eliminate: remove old functionality
- Strangler application steps:
- Pre-requisites
- Web or API based monolith: base on URL structure
- Standarized URL structure: careful with intermediate layers
- Meta UIs: larger chucnk so UI constructed on the fly
- Bad patterns to apply
- No 1 page at a time: it may result on consitency problems: the smallest sliver is a microservice
- All at once
- Good patterns
- Refactor back (Inside part)
- identify bouned contexts in application design (domain driven design)
- chooe the smalles bounded bounded context, easier to refactor
- conceptual plan of microservices within the context (rough URL structure)
- Refactor front to accommodate the microservices on back (Outside part)
- analize relationships between existing screens
- apply principle of least astonishment to the aspects of model manipulation
- conceptually plan the microservices within the context
- choose wether to release an entire chunk at a time, or each chunk as a series os slivers
- Refactor back (Inside part)
- Relase process (Agile)
- MVP (Minimal Viable Product) -> User Stories
- User Stories may be grouped in EPICS (high level fencionality)
- Squads implements EPICS (samlles element of implementation)
Complexity
- Architectural
Model | Complexity | Meassure |
---|---|---|
Monolithic | Code complexity | Dependencies |
Microservices | Complexity in interactions | Interaactions of the individual services’ domain |
- Operational
- scaling + cost-efficient
- operate 100 microservice components without multuplying effort
- keeop track of pipelines
- monitor system health
- track debug interactions
- analyze high ammounts of log data in distributed applications
- deal with lack of standards
- value diversity without locking technologies
- versioning
- ensure are in services use without consitent pattern usage
- ensure decoupling abd communication
Microservices and the Cloud
- AWS: Cloud services provider
- Advantages
- High availability (access from any device)
- Fault tolerant (version storage)
- Scalabiliy (add/grow services easily)
- Elasticity (remove/shrink services easily)
- Services provided
- Amazon S3 (large “unlimited” storage)
- Amazon EC2 (computer power) - part of VPC, the user connects here
- Amazon RDS (databases) - part of VPC
- Resolves most important challenges
- On demand-resources
- Programmability
- Experiment with low cost and risk
- Infrastructure as code
- Continuous delivery
- Service orientation
- Managed services
- Poliglot
- Advantages
Simple Microservices Architecture
Split functionalities into cohesive “verticals”
User Interface | Microservices | Datastore |
---|---|---|
Cloud front | Application load balancer | ElastiCache |
(Static Content) | ECS | RDS |
S3 | DynamoDB |
User Interface (CDN and WAF)
Route 53 provides features that can be leveraged for service discovery (user -> DNS resolver (doesn’t have the user) -> Route 53 (authority))
- Adds health checks and simple failover recovery (if health check fails, activate secoundary server, either calculated or with latency measurement health checks)
- Routing policies: multiple records in a single DNS (different weight)
Virtual private clouds (VPCs)
Cloud front is global Content Delivery Network that accelerates delivery of websites, APIs, video content… (example: on JS calls, images and CSS are on CDNs)
- Fastest access
- Reduce risks
- Possibility to cache contents in an easy way
- Use of CDN is an standard among companies with selling capabilities on Internet
- Empower possibiliy to use HTT/2 which is much performant protocol adapted to smartphone and devices bandwith consume and features pattern
WAF (Web Application Firewall) - control traffic + custom rules for common attack patterns + API to handle rules, AWS WAF payment: price depends on number of rules.
- Part of CDM solutions or Application Load Balancer (ALB) that fronts web servers or origin servers running on EC2.
- Increased protection against web attacks
- Security integrated with how you develop applications
- Ease of payment and maintenance
- Improved web traffic visibility
- Cost effective web application protection
Microservices
- OSI model (remember)
- 7: Application
- 6: Presentation
- 5: Session
- 4: Transport
- 3: Network
- 2: Data-link
- 1: Physical
graph LR A[Amazon Route 53] B[ELB 1] C[ELB2] D[EC2 instance 1] E[EC2 instance 2] subgraph ELB-VPC B C end subgraph Customer-VPC D E end A --> B; B --> D; A --> C; C --> E;
- Components used
- API of a microservice: REST
- ELB (Elastic Load Balancer) distributes traffic
- detects which instances are online and distributes trafic among them, mitigates DDOS
- Idle timeouts (60-36000 seconds): close connections by load balancer when they are not longer used
- Listeners (1-10 per ELB) with routung rules (actions to forward requests, use path patterns formats)
- Multiple availability zones: 2 or more subnets in deiiferent zones with the load balancer
- ELB sends latency information to cloudwatch -> which will request auto-scaling(AS) via AS Policy -> which will send action to ELB
- ELB metrics available
- Healthy Host Count: healthy instances in each availability zone
- Latency: elapsed time from when the request leaves the load balancer until the response is received
- Rejected Connection count: when ELB can not establish connection with healthy target in order to route the request
- ALB Application Load Balancer) -> level 7 OSI model: rules to redirect `+ target groups of VPCs
- ECS (Container Service) + autoscaling
graph TD A[Load balancer] B[Listener] C[Rule 1] D[Rule 2] E[EC2] F[EC2] G(Health check) H(Health check) I[Listener] J[Rule 3] K[EC2] L(Health check) subgraph Listener layer A B C D end subgraph Login E G end subgraph Img F H end subgraph Listener layer B I J end subgraph Payment K L end A --> B; B --> C; C --> E; B --> D; D --> F; A --> I; I --> J; J --> K;
Key features
- High availability
- Health checks
- Security groups
- SSL Termination
- Sticky sessions
- VPCs
- Idle connection timeout
- Connection draining
- Dynamic Por Mapping
- Protocols
- Backend server Auth (on classic, not ALB)
- Cloudwatch Metrics
- Access Logs
- Path-based routing
- Deletion Protection
EC2 (Amazon ECS) or Amazon EC2 Container Service: processing activities
Supports Docker containers and allows to easily run applications on a managed cluster of amazon EC2 instances
Scaled in/out, use Auto-scaling
Amazon EKS provides container services for instance orchestration via Kubernetes
Docker on AWS
- Configuration and deployment
- Microservices
Batch processing
Amazon ECS Container Agent: checks the clusters
Clusters: regional resource poll for grouping container instances, which start empty and can be scaled
Typical workflow
- 1- User pushes an image to DockerHub
- 2- User creates task definition on Amazon ECS (declare source requirements)
- 3- User runs instances on EC2 (custom AMI with Docker support and ECS Agent. Instances will be registered with default cluster)
- 4- User describes cluster on Amazon ECS (get information about cluster state and available resources)
- 5- User runs task on amazon ECS (using task definition from step 2, which schedules it)
- 6- User describes cluster again and checks the cluster has the docker image loaded
Ways to start a task
- StartTask
1
aws ecs starttask -cluster default -task-definition sleep360:1 -container-instance <instance arn>
- RunTask
1
aws ecs run-task --cluster default -task-definition sleep360:1 --count 1
- Bring your own scheduler (Mesos, Marathon, custom)
- StartTask
Use of AWS CLI:
Action | Example |
---|---|
List all clusters | aws ecs list-clusters -- profile myProfile |
List all container instances in cluster | aws ecs list-container-instances --cluster default --profile myProfile |
List all task definition | aws ecs list-task-definitions --profile myProfile |
List tasks running | aws ecs list-tasks --cluster dafult -- profile myProfile |
Describe task via taskArn | aws ecs describe-tasks --cluster dafult -- profile myProfile -- tasks f94dbd87-7d84-4e27-ab70-0461d455d1ba |
Describe container instance via containerInstanceArn | aws ecs describe-container-intances --cluster dafult -- profile myProfile -- container_instances 25340-a6ff-45de-b3eb-fa43a88e9313 |
Run task | aws ecs run-task --task-definion sleep360:2 --cluster default --profile myProfile |
- Amazon Elastic File System (Amazon EFS): storage for EC2, can be mouted AS-IS when connected to Amazon VPC (Virtual Private Cloud)
- NFSv4 protocol, multiple EC2s can share it
Data storage
Persist data:
- Amazon ElastiCache Service
- Amazon Relational Database Service (MS SQL server, Oracle, MySQL, MariaDB, PostgreSQL, Amazon Aurora)
- Amazon DynamoDB: NoSQL: no strict schema (can not join table, info must be merged in applications)
Learning about RDS
- AWS proviisioned database storage
- Amazon Dynamo DB
- read consistency -> strong consistent
- provisioned throughput capacity (read/write throughputs)
Tools
Springboot
Spring framework
Dependency injection and integration (@Component, @Autowire, @Context)
Super easy central configuration service creation (@EnableConfigServer creates new SpringBoot Application)
Straightforward service discovery registry (Registry = also a microservice, other services can register themselves)
- Integration with Eureka REST service registry by Netflix
- Spring Security: athentication and authorization
- Spring Boot: top of the Framework: (SpringBoot = (Spring Framework) + (Embedded HTTP Servers) - (
XML <bean> Configuration or @Configuration
))
Benefits
- Easy to develop Java or Groovy
- Reduces dev time
- Avoid boilerplate code, annotations, configuration
- Easy integration with other Spring resources
- Provides CLI to develop and test Springboot apps
- Provides plugins to test, build (Maven, Gradle) or work with embedded DBs
Docker
“Docker doesn’t create containers, but it does containers for you making it easier by using an standard”
- Linux server
- Docker daemon
- Login/interact DockerHub
- Use Docker Client to connect to Docker Daemon
Example: go to DockerHub, find apache image,then send it to daemon which will send it to the Linux Kernel
1 | docker run apache |
Advantages
- Uses LXCs (Linux Containers), which are user space interface for the Linux Kernel Containment
- Several LXCs run in one control hist LXCs
- LXCs are an alternative to hypervisors (Virtualbox, VMware…)
- Prevents “Runs in your machine, but not in mine”
Benefits
- Portability
- Productivity
- Efficiency
- Control
Therefore, on Amazon AWS:
- Amazon ECS eliminates the eed to install, operate and scale cluster management infrastructure
- Use API to launch Docker-enabled applications, query the status…
- Amazon EDS is scalable and elastic
- Amazon EKS leverages and replaces ECS to be used under the context of Kibernetes orchestrator
Kubernetes
Introduction
*Container orchestration to deploy containers- Portable
- Extensible
- Self-healing
Benefits
- Deploy is fast and predictive
- Scale on the fly
- Roll out new features seamlessly
- Limit hardware usage
Common needs satified: it provides simplicity of Platform as a Service (PaaS) with felixibility of Infrastructure as a Service (IaaS)
- Co-location helper processes
- Mounting storage systems
- Distributing secrets
- Checking application health
- Monitoring resources
- Using horizonatl pod autoscaling
- Naming and discovery
- Balancing loads
- Rolling updates
- Accessing and ingesting logs
- Debugging applications
- Providing authentication and authorization
- Replicating application instances
Amazon EKS
Amazon Elastic Container Service for Kubernetes: install and manage Kubernetes clusters
- Integrated with many AWS services
- Elastic load balancing
- IAM authen tication
- Amazon VPC for isolation
- AWS PrivateLink for private network access
- AWS CloudTrail for logging
Benefits
- Fully managed and highly available
- Secure
- Fully compatible with Kubernetes Community Tools