Cover....1
Title Page....2
Copyright and Credits....3
Contributors....5
Table of Contents....8
Preface....14
Part 1: Introduction to DevOps and role of Python in DevOps....20
Chapter 1: Introducing DevOps Principles....22
Exploring automation....23
Automation and how it relates to the world....23
How automation evolves from the perspective of an operations engineer....23
Understanding logging and monitoring....25
Logging....25
Monitoring....26
Alerts....26
Incident and event response....26
How to respond to an incident (in life and DevOps)....27
Site reliability engineering....28
Incident response teams....29
Post-mortems....30
Understanding high availability....30
SLIs, SLOs, and SLAs....31
RTOs and RPOs....32
Error budgets....33
How to automate for high availability?....34
Delving into infrastructure as a code....34
Pseudocode....35
Summary....36
Chapter 2: Talking about Python....38
Python 101....39
Beautiful-ugly/explicit-implicit....41
Simple-complex-complicated....42
Flat-nested/sparse-dense....42
Readability-special cases-practicality-purity-errors....43
Ambiguity/one way/Dutch....43
Now or never....44
Hard-bad/easy-good....45
Namespaces....45
What Python offers DevOps....46
Operating systems....46
Containerization....47
Microservices....47
A couple of simple DevOps tasks in Python....48
Automated shutdown of a server....48
Autopull a list of Docker images....55
Summary....56
Chapter 3: The Simplest Ways to Start Using DevOps in Python Immediately....58
Technical requirements....59
Introducing API calls....59
Exercise 1 – calling a Hugging Face Transformer API....60
Exercise 2 – creating and releasing an API for consumption....63
Networking....66
Exercise 1 – using Scapy to sniff packets and visualize packet size over time....66
Exercise 2 – generating a routing table for your device....69
Summary....71
Chapter 4: Provisioning Resources....72
Technical requirements....73
Python SDKs (and why everyone uses them)....73
Creating an AWS EC2 instance with Python’s boto3 library....74
Scaling and autoscaling....76
Manual scaling with Python....77
Autoscaling with Python based on a trigger....78
Containers and where Python fits in with containers....80
Simplifying Docker administration with Python....81
Managing Kubernetes with Python....82
Summary....83
Part 2: Sample Implementations of Python in DevOps....84
Chapter 5: Manipulating Resources....86
Technical requirements....87
Event-based resource adjustment....87
Edge location-based resource sharing....88
Testing features on a subset of users....89
Analyzing data....91
Analysis of live data....91
Analysis of historical data....92
Refactoring legacy applications....93
Optimize....94
Refactor....95
Restart....96
Summary....96
Chapter 6: Security and DevSecOps with Python....98
Technical requirements....99
Securing API keys and passwords....99
Store environment variables....100
Extract and obfuscate PII....101
Validating and verifying container images with Binary Authorization....103
Incident monitoring and response....105
Running runbooks....105
Pattern analysis of monitored logs....111
Summary....115
Chapter 7: Automating Tasks....116
Automating server maintenance and patching....117
Sample 1: Running fleet maintenance on multiple instance fleets at once....118
Sample 2: Centralizing OS patching for critical updates....120
Automating container creation....121
Sample 1: Creating containers based on a list of requirements....121
Sample 2: Spinning up Kubernetes clusters....123
Automated launching of playbooks based on parameters....124
Summary....128
Chapter 8: Understanding Event-Driven Architecture....130
Technical requirements....131
Introducing Pub/Sub and employing Kafka with Python using the confluent-kafka library....131
Understanding the importance of events and consequences....133
Exploring loosely coupled architecture....136
Killing your monolith with the strangler fig....138
Summary....141
Chapter 9: Using Python for CI/CD Pipelines....142
Technical requirements....143
The origins and philosophy of CI/CD....143
Scene 1 – continuous integration....143
Scene 2 – continuous delivery....144
Scene 3 – continuous deployment....146
Python CI/CD essentials – automating a basic task....147
Working with devs and infrastructure to deliver your product....150
Performing rollback....153
Summary....155
Part 3: Let’s Go Further, Let’s Build Bigger....156
Chapter 10: Common DevOps Use Cases in Some of the Biggest Companies in the World....158
AWS use case – Samsung electronics....159
Scenario....160
Brainstorming....160
Solution....161
Azure Use Case – Intertech....162
Scenario....162
Brainstorming....163
Solution....164
Google Cloud use case – MLB and AFL....165
Scenario....167
Brainstorming....167
Solution....168
Summary....170
Chapter 11: MLOps and DataOps....172
Technical requirements....173
How MLOps and DataOps differ from regular DevOps....173
DataOps use case – JSON concatenation....173
MLOps use case – overclocking a GPU....174
Dealing with velocity, volume, and variety....175
Volume....175
Velocity....177
Variety....179
The Ops behind ChatGPT....181
Summary....182
Chapter 12: How Python Integrates with IaC Concepts....184
Technical requirements....185
Automation and customization with Python’s Salt library....185
How Ansible works and the Python code behind it....189
Automate the automation of IaC with Python....193
Summary....194
Chapter 13: The Tools to Take Your DevOps to the Next Level....196
Technical requirements....197
Advanced automation tools....197
Advanced monitoring tools....201
Advanced event response strategies....206
Summary....208
Index....210
Other Books You May Enjoy....217
Python stands out as a powerhouse in DevOps, boasting unparalleled libraries and support, which makes it the preferred programming language for problem solvers worldwide. This book will help you understand the true flexibility of Python, demonstrating how it can be integrated into incredibly useful DevOps workflows and workloads, through practical examples.
You'll start by understanding the symbiotic relation between Python and DevOps philosophies and then explore the applications of Python for provisioning and manipulating VMs and other cloud resources to facilitate DevOps activities. With illustrated examples, you'll become familiar with automating DevOps tasks and learn where and how Python can be used to enhance CI/CD pipelines. Further, the book highlights Python's role in the Infrastructure as Code (IaC) process development, including its connections with tools like Ansible, SaltStack, and Terraform. The concluding chapters cover advanced concepts such as MLOps, DataOps, and Python's integration with generative AI, offering a glimpse into the areas of monitoring, logging, Kubernetes, and more.
By the end of this book, you'll know how to leverage Python in your DevOps-based workloads to make your life easier and save time.
This book is for IT professionals venturing into DevOps, particularly programmers seeking to apply their existing programming knowledge to excel in this field. For DevOps professionals without a coding background, this book serves as a resource to enhance their understanding of development practices and communicate more effectively with developers. Solutions architects, programmers, and anyone regularly working with DevOps solutions and Python will also benefit from this hands-on guide.