Building a Kubernetes Platform at Pinterest

748
Pinterest
Pinterest is a social bookmarking site where users collect and share photos of their favorite events, interests and hobbies. One of the fastest growing social networks online, Pinterest is the third-largest such network behind only Facebook and Twitter.

By Lida Li, June Liu, Rodrigo Menezes, Suli Xu, Harry Zhang, Roberto Rodriguez Alcala | Pinterest Software Engineers, Cloud Management Platform

Why Kubernetes?

Over the years, 300 million Pinners have saved more than 200 billion Pins on Pinterest across more than 4 billion boards. To serve this vast user base and content pool, we’ve developed thousands of services, ranging from microservices of a handful CPUs to huge monolithic services that occupy a whole VM fleet. There are also various kinds of batch jobs from all kinds of different frameworks, which can be CPU, memory or I/O intensive.

To support these diverse workloads, the infrastructure team at Pinterest is facing multiple challenges:

  • Engineers don’t have a unified experience when launching their workload. Stateless services, stateful services and batch jobs are deployed and managed by totally different tech stacks. This has created a steep learning curve for our engineers, as well as huge maintenance and customer support burdens for the infrastructure team.
  • Engineers managing their own VM fleets is creating a huge maintenance load for the infra team. Simple operations such as an OS or AMI upgrade can take weeks to months. Production workloads are also disturbed during those processes, which are supposed to be transparent to them.
  • It’s hard to build infrastructure governance tools on top of separated management systems. It’s even more difficult for us to determine who owns which machines and if they can be safely recycled.

Container orchestration systems provide a way to unify workload management. They also pave the way to faster developer velocity and easier infra governance since all running resources are managed by a centralized system.

Figure 1: Infrastructure priorities (Service Reliability, Developer Productivity and Infra Efficiency)

The Cloud Management Platform team at Pinterest started their journey on Kubernetes back in 2017. We dockerized most of our production workloads, including the core API and Web fleets, by the first half of 2017. Extensive evaluation on different container orchestration systems was then done by building prod clusters and operating real workloads on them. By the end of 2017, we decided to go down the path of Kubernetes because of its flexibility and extensive community support.

So far, we’ve built our own cluster bootstrap tools based on Kops and integrated existing infrastructure components into our Kubernetes cluster, such as network, security, metrics, logging, identity management and traffic. We introduced Pinterest-specific custom resources to model our unique workloads while hiding the runtime complexity from developers. We’re now focusing on cluster stability, scalability, and customer onboarding.

Kubernetes, the Pinterest way

Running Kubernetes to support workloads at Pinterest scale, while also making it a platform loved by our engineers, has many challenges.

As a large organization, we have invested heavily in infrastructure tools, such as security tools that handle certificates and key distribution, traffic components that enable service registration and discovery, and visibility components that ship logs and metrics. These are components built on lessons learned the hard way, so we want to integrate them into Kubernetes instead of reinventing the wheel. This also makes migration much easier, as the required support is already there for our internal applications.

On the other hand, the Kubernetes native workload model, such as deployment, jobs and daemonsets, are not enough for modeling our own workloads. Usability issues are huge blockers on the way to adopt Kubernetes. For example, we’ve heard service developers complaining about missing or misconfigured ingress messing up their endpoints. We’ve also seen batch job users using template tools to generate hundreds of copies of the same job specification and ending up with a debugging nightmare.

Runtime support for the workloads is also evolving, so it would be extremely hard to support different versions on the same Kubernetes cluster. Just imagine the complexity of customer support if we needed to face many versions of the runtime, together with the difficulties of upgrading or bug-patching for them.

Pinterest custom resources and controllers

In order to pave an easier way for our engineers to adopt Kubernetes and make infra development faster and smoother, we designed our own Custom Resource Definitions (CRDs).

The CRDs provide the following functionalities:

  1. Bundle various native Kubernetes resources together so they work as a single workload. For example, the PinterestService resource puts together a deployment, a service, an ingress and a configmap, so service developer will not need to worry about setting up DNS for their service.
  2. Inject necessary runtime support for the applications. The user only needs to focus on the container spec for their own business logic, while the CRD controller injects necessary sidecars, init containers, environment variables and volumes into their pod spec. This provides an out-of-box experience to the application engineers.
  3. CRD controllers also do life cycle management for the native resources and handle visibility and debuggability. This includes but is not limited to reconciling the desired spec and the actual spec, CRD status updating and event recording. Without CRDs, app engineers must manage a much larger set of resources, and this process has proved to be error prone.

Here’s an example of PinterestService and the native resource translated by our controller:

Figure 2: CRD to native resources. The left is the Pinterest CR written by user, and the right is the native resource definition generated by the controller.

As shown, to support a user’s container, we need to insert an init container and several sidecars for security, visibility and network traffic. Additionally, we introduced configuration map templates and PVC template support on batch jobs, as well as many environment variables to track identity, resource utilization, and garbage collection.

It’s hard to imagine engineers would be willing to hand-write these configuration files without CRD support, let alone maintain and debug the configurations.

Application Deploy Workflow

Figure 3: Pinterest CRD Overview

Figure 3 shows how to deploy a Pinterest custom resource to the Kubernetes cluster:

  1. Developers interact with our Kubernetes cluster via CLI and UI.
  2. The CLI/UI tools retrieve workflow configuration YAML files and other build properties (such as version ID) from Artifactory and send them to the Job Submission Service. This ensures only reviewed and landed workloads will be submitted to the Kubernetes cluster.
  3. The Job Submission service is the gateway to various computing platforms, including Kubernetes. User authentication, quota enforcement and partial Pinterest CRD configuration validation happens here.
  4. Once the CRD passes the Job Submission service validation, it’s sent to the Kubernetes API.
  5. Our CRD controller watches events on all custom resources. It transforms the CR into Kubernetes native resources, adds necessary sidecars into user defined pods, sets appropriate environment variables and does other necessary housekeeping work to ensure the user’s application containers have enough infrastructure support.
  6. The CRD controller then writes the resulting native resources back to the Kubernetes API so they can be picked up by the scheduler and start to run.

Note: This is the pre-release deploy workflow used by early adopters of the new Kubernetes-based Compute Platform. We are in the process of revamping this experience to be fully integrated with our new CI/CD platform to avoid exposing a lot of Kubernetes-specific details. We look forward to sharing the motivation, progress and subsequent impact in an upcoming blog post — “Building a CI/CD platform for Pinterest.”

Custom Resource Types

Based on Pinterest’s specific needs, we designed the following CRDs that suit different workflows:

  • PinterestService is the long running stateless service. Many core systems are based on a set of such services.
  • PinterestJobSet models the batch jobs that run to completion. A very common pattern within Pinterest is that multiple jobs runs the same containers in parallel, each grabbing a fraction of a workload without depending on each other.
  • PinterestCronJob is widely adopted by teams with lightweight periodic workloads. PinterestCronJob is a wrapper around the native cron job, with Pinterest-specific support such as security, traffic, log and metrics.
  • PinterestDaemon is limited to the infrastructure-related daemons. The family of PinterestDaemon is still growing as we are adding more support on our clusters.
  • PinterestTrainingJob wraps around Tensorflow and Pytorch jobs, providing the same level of runtime support as all other CRDs. Since Pinterest is a heavy user of Tensorflow and other machine learning frameworks, it makes sense to build a dedicated CRD around them.

We also have PinterestStatefulSet under construction, which will soon be adopted for storage and other stateful systems.

Runtime Support

When an application pod starts on Kubernetes, it automatically gets a certificate to identify itself. This cert is used to access the secrets store or talk to other services via mTLS. Meanwhile, the config management init containers and daemon will ensure all necessary dependencies downloaded before the application container starts. When the application container is ready, the traffic sidecar and daemon will register the pod IP to our Zookeeper in order to make it discoverable by clients. Networking has been set up for the pod by network daemon before the pod even starts.

The above are examples of typical runtime support for service workloads. Other workload types may need slightly different support, but they all come in the form of pod-level sidecars, node-level daemonsets or VM-level daemons. We make sure all of them are deployed by the infrastructure team so they are consistent between all applications, which greatly reduces the maintenance and customer support burden for us.

Testing and QA

We built an end-to-end test pipeline on top of the native Kubernetes test infra. These tests are deployed to all clusters. This pipeline has caught many regression before they reach the production cluster.

Besides the testing infra, there is also monitoring and alerting systems that watch the system components’ health status, resource utilization and other critical metrics consistently, notifying us when human intervention is needed.

Alternatives

We considered some alternatives to custom resources, such as mutation admission controllers and templating systems. However, the alternatives all come with major issues, so we chose the path of CRDs.

  • Mutating admission controller has been used to inject sidecars, environment variables and other runtime support. However, it has difficulties bundling resources together as well as managing their life cycle, whereas CRD comes with reconciling, status update and lifecycle management.
  • Templating systems such as Helm charts are also widely used to launch applications with similar configurations. However, our workloads are too diverse to be managed by templates. We also need to support continuous deployment, which would be extremely error prone with templates.

Future Work

Currently, we are running mixed workloads on all of our Kubernetes clusters. In order to support workloads of different sizes and types, we are working on the following areas:

  • Cluster Federation spreads large applications over different clusters for scalability and stability.
  • Cluster Stability, Scalability and Visibility that makes sure applications reach their SLA.
  • Resource and Quota Management to make sure applications do not step on each other’s feet and the cluster scale is under control.
  • New CI/CD Platform to support Application Deployment on Kubernetes

Acknowledgements

Many engineers at Pinterest helped build the platform from the ground up. Micheal Benedict and Yongwen Xu, who lead our engineering productivity effort, have worked together on setting the direction of the compute platform, discussing the design and helping with feature prioritization from the very beginning. Jasmine Qin and Kaynan Lalone helped on the Jenkins and Artifactory integration support. Fuyuan Bie, Brain Overstreet, Wei Zhu, Ambud Sharma, Yu Yang, Jeremy Karch, Jayme Cox, and many others helped build the config management, metrics, logging, security, networking and other infra support. Jooseong Kim and George Wu helped build the Submission Service. Lastly, our early adopters Prasun Ghosh, Michael Permana, Jinfeng Zhuang and Ashish Singh provided a lot of useful feedback and feature requirements.

This post was originally posted on the Pinterest Engineering Blog

Pinterest
Pinterest is a social bookmarking site where users collect and share photos of their favorite events, interests and hobbies. One of the fastest growing social networks online, Pinterest is the third-largest such network behind only Facebook and Twitter.
Tools mentioned in article
Open jobs at Pinterest
Backend Engineer, Measurement Attribu...
Seattle

Effective advertisers learn how their ads perform, then adapt and optimize based on that information. Join the group that is helping advertisers gain a deep understanding of their ad performance, and generating helpful insights so they can make good decisions about their ad campaigns. The tools we build are both within our own stack, as well as built to allow the advertisers to see the data they want in the tools they already trust.

 

What you’ll do:

 

  • Design and build data processing algorithms and APIs around tracking and reporting against conversions
  • Move across the whole ads stack adding better reporting, novel insights, and better tools for our advertisers
  • Integrate with third-party data processing tools like Apache Spark, Flink, and Hadoop

 

 

What we’re looking for: 

  • Passionate about delivering value to customers in the advertising space
  • Interested in finding new ways to process data more efficiently
  • Interested in gaining new insights from data and leveraging them to improve Pinterest
  • 1-5 years engineering experience

 

#LI-GK1

Engineering Manager, Discovery Measur...
San Francisco

Pinterest personalizes millions of experiences by using machine learning algorithms to sift through our catalog of one hundred billion Pins to find the best content for each Pinner. It is critical to measure the users experience across Pinterest and identify opportunities for improvement. The Discovery Measurements team’s charter is to establish human-powered ground truth for major Pinterest products, e.g. Search and Ads, and develop company critical measurements about relevance, domain quality, session experience, retention, etc. As we look to scale these platforms both vertically and horizontally, we’re looking for an engineering manager to join the team to drive technical excellence and curiosity. We need someone who will take risks with the team’s roadmap while providing a concrete vision both internally and externally. This position will require an engineering manager who has experience as a developer and drive to dive into challenging data mining problems, provide technical guidance, and lead a highly impactful team.

What you will do:

  • Design and scale company-wide online & offline measurement platforms for organic and ad content
  • Design and develop company critical measurements, including relevance, domain quality, session experience, retention, user satisfaction
  • Establish technical roadmap for providing insightful signals about Pin and Pinners that could power other ML models in the Pinterest ecosystem.
  • Drive short term and long term vision for the team, expanding impact horizontally and vertically
  • Partner with cross-functional stakeholders to align engineering efforts for high impact technical initiatives; Provide visibility for senior leadership into the team’s global impact

What we are looking for:

  • Strong programmer in one of the following languages such as Python, Java or C++
  • Familiar with large data process and measurement. Data mining experience is a strong plus.
  • 1+ year experience in leading a highly impactful engineering team
  • 5+ years of experience in software development in a major language
  • Curiosity for leveraging data and metrics to identify challenging opportunities and build impactful solutions

 

#LI-LP1





Engineering Manager, Logging Platform
San Francisco, CA

Pinterest brings millions of people the inspiration to create a life they love for everything; whether that be tonight’s dinner, next summer’s vacation, or a dream house down the road. As the manager for logging platform team at Pinterest, you'll work on large Kafka deployment to enable the rest of the company to collect log messages and derive insights for product decisions. You’ll be part of the data engineering team, working on some of the most interesting infrastructure challenges with a world-class team of engineers towards the mission of enabling data-driven products and insights at Pinterest.

What you’ll do:

  • Build the team responsible for log collection and transportation.
  • Work with cross functional partners on various data collection and streaming use cases.
  • Work in an agile environment to manage and operationalize Kafka, and Zookeeper deployment.
  • Drive Kafka performance issues out of Operations, in collaboration with various internal organizations.

What we're looking for:

  • 2+ years of management experience.
  • 4+ years of solid experience in managing, monitoring, scaling and troubleshooting performance issues with Kafka.

#LI-KL1

Fullstack Engineer, Growth Search Tra...
San Francisco, CA

Pinterest brings millions of people the inspiration to create a life they love for everything; whether that be tonight’s dinner, next summer’s vacation, or a dream house down the road. The Growth Search Traffic team is on a mission to make Pinterest the best destination for search engine users to get inspired and discover ideas to create a life they love. This is the opportunity to join the team that brings a lot of users to Pinterest. We're looking for a strong candidate to work across all parts of our core product and help us migrate the codebase from Python to Node.js. If this sounds like fun, we’d love to hear from you!

What You’ll Do:

  • Migrate the existing landing pages codebase from Python to Node.js
  • Roll out the new codebase, monitor performance and business metrics
  • Collaborate with other teams to ensure their functionality is migrated and operational

What We’re Looking For:

  • 4+ years of full-stack application development experience
  • Proficiency in both JavaScript and Python
  • A desire to improve code quality and performance
Verified by
Tech Lead, Big Data Platform
Software Engineer
Talent Brand Manager
Sourcer
Software Engineer
You may also like