Crossplane คืออะไร?

What is Crossplane?

Nopnithi Khaokaew (Game)
5 min readJul 9, 2023

บทความนี้เป็นเวอร์ชั่นเก่าแล้ว เวอร์ชั่นที่ถูกปรับปรุงใหม่จะถูกย้ายไปที่ Crossplane คืออะไร? อธิบาย Concept ด้วยภาพและตัวอย่าง ครับ

และหลังจากนี้ผมจะเขียนบทความที่ nopnithi.com เป็นหลักครับ

ปัจจุบัน infrastructure as code (IaC) เป็น concept ที่แทบทุกคนต้องใช้กัน เพราะมันเข้ามาช่วยให้การจัดการ infrastructure ง่ายและเป็นระบบมากขึ้น

และด้วยความที่ infrastructure ก็มักจะมาคู่กันกับ application อยู่แล้ว ดังนั้นการจัดการสองสิ่งนี้ด้วยวิธีการ, tool หรือ platform เดียวกันก็เป็นอะไรที่น่าสนใจไม่น้อย

[ENGLISH VERSION IS AT THE BOTTOM]

ก่อนจะเริ่ม…รบกวนกด Like/Follow/Subscribe ให้ผมด้วยนะครับ 🙏

Crossplane คืออะไร?

Crossplane คือ open-source IaC tool ที่ทำงานอยู่บน Kubernetes cluster ทำให้เราสามารถใช้ความเจ๋งของ Kubernetes มาจัดการกับ infrastructure ได้เหมือนกับ application (container)

ใครรู้จัก Crossplane อยู่แล้วเชิญทางนี้เลยครับ → “อธิบาย Crossplane Concept พร้อมตัวอย่าง

ด้วย Crossplane เราสามารถสร้าง infrastructure ผ่านการใช้ Kubernetes YAML file ได้ ทำให้สามารถจัดการกับ application และ infrastructure ด้วยกันได้ใน control plane เดียว ซึ่งเป็นสิ่งที่เหมาะกับการสร้าง developer platform นักหละ

จุดเด่นของ Crossplane

ผมจะขอเขียนเฉพาะสิ่งที่…

  • IaC tool ตัวอื่น (เช่น Terraform) ทำไม่ได้
  • หรือทำได้ แต่ต้องใช้ effort เยอะกว่าในการทำหรือ operate
  • หรือทำได้ แต่ต้องพึ่งพา tool/service ตัวอื่นจากภายนอก

1. Unified Tool

สมมุติว่าเรามี application ที่จะต้อง run อยู่บน Kubernetes และต้องการ database ด้วย ปกติแล้ว database อาจจะถูก provision ด้วย Terraform ส่วน application อาจจะ deploy ผ่าน kubectl หรือ Helm

แต่เมื่อใช้ Crossplane เราสามารถที่จะใช้ tool เดียวกันในการ deploy ทั้งสองอย่างร่วมกันได้

2. อัพเดทสถานะแบบ Real-Time

Crossplane สามารถอัพเดท state ของ infrastructure รวมถึง health check ได้แบบ real-time ผ่าน Kubernetes API ซึ่งมีประโยชน์กับการจัดการ infrastructure

3. การบังคับใช้ Infrastructure Policy

สมมุติว่าบริษัทมี policy ว่า S3 bucket จะต้องห้ามเปิด public

หากเป็น Terraform การจัดการกับ policy แบบนี้ค่อนข้างยุ่งยาก แม้จะสามารถเขียน module มาเพื่อตีกรอบได้ แต่ user ก็สามารถแก้ไข resource โดยตรงบน cloud ได้ ทำให้ต้องใช้ IAM ในการควบคุมอีกที ซึ่งเพิ่มความซับซ้อนในการจัดการ

กลับกัน Crossplane สามารถจัดการกับเรื่องนี้ได้ดีกว่า เพราะ Kubernetes reconciliation loop จะคอยปรับ state ของ resource ให้ตรงกับที่เรากำหนดไว้ใน YAML เสมอ

4. Kubernetes RBAC และ Namespace

Kubernetes เป็น platform ที่มี RBAC (role-based access control) สำหรับใช้ในการจัดการว่า “ใคร ทำอะไร ที่ไหน และอย่างไร” ได้ดีอยู่แล้ว และยังมี namespace ที่ใช้ในการแยก resource อีก การใช้ Crossplane เป็น infrastructure platform ก็จะได้ความสามารถนี้ไป ทำให้เราจัดการสิทธิ์ได้ถึงระดับ API

5. GitOps สำหรับ Infrastructure ด้วย ArgoCD

GitOps คือการใช้ Git เป็น source of truth สำหรับเก็บ desired state ซึ่งในกรณีนี้คือ infrastructure โดยเราสามารถเขียน YAML ไปเก็บบน Git แล้วใช้ tool อย่าง ArgoCD เข้ามาช่วยทำ auto-sync เพื่อ deploy ได้ และยังได้ความสามารถอื่น ๆ อย่างการ visualize resource ผ่าน UI, การทำ automated rollback และอื่น ๆ ด้วย

รู้จักกับ Kubernetes Extensions

ความสามารถบางอย่างของ Kubernetes เราสามารถที่จะ modify เพิ่มได้ เช่น

  • เพิ่ม sub-command ของ kubectl
  • เพิ่ม resource scheduling ผ่าน custom scheduler ได้
  • เพิ่ม resource type ใหม่ (นอกจาก pod, service, deployment และอื่น ๆ) เพื่อขยาย Kubernetes API และ controller

ทำให้ความเจ๋งของ Kubernetes ไม่ได้ถูกจำกัดอยู่แค่ที่ container แต่ยังใช้กับ resource อื่น ๆ ได้อีกด้วย

เพราะ Kubernetes มีสิ่งที่เรียกว่า custom resource definitions (CRDs) อยู่ ทำให้เราสามารถสร้าง custom resource (CR) ขึ้นมาเองได้นอกเหนือจาก built-in resource อย่าง pod, service, deployment และอื่น ๆ

ซึ่งไอ้เจ้า CR นี้เวลาเราสร้างขึ้นมาบน Kubernetes แล้ว มันก็จะถูกดูแลโดย custom controller เหมือนกับที่ Kubernetes ดูแล pod ให้เรานั่นแหละครับ

แล้วมันเกี่ยวกับ Crossplane ยังไง?

ถ้าเราต้องการใช้ Kubernetes สร้างและดูแล infrastructure ให้ เราก็ต้องสร้าง CRD และ custom controller ขึ้นมาสำหรับ infrastructure resource นั้นเป็นรายตัวเลย เช่น CRD สำหรับ VPC, CRD สำหรับ subnet, CRD สำหรับ internet gateway หรืออื่น ๆ

เพื่อบอกให้ Kubernetes รู้จักกับ resource เหล่านั้นว่าจะต้องคุยกับมันยังไง สร้างมันขึ้นมายังไง หรือจะเช็คมันยังไงว่ามันอยู่ดีมีสุขหรือตายไปแล้ว

นั่นคือหน้าที่ของ Crossplane เพราะเค้าได้ทำเรื่องนี้ให้เราแล้วครับ

บทความต่อไปผมจะอธิบายเกี่ยวกับ Crossplane Concept พร้อมตัวอย่างประกอบ คลิกที่ link ได้เลยครับ

ถ้าคิดว่าบทความนี้มีประโยชน์ อย่าลืมกด clap และ follow เพื่ออัพเดทเรื่องราวใหม่ ๆ จากผมได้นะครับ

ก่อนจะไป…รบกวนกด Like/Follow/Subscribe ให้ผมด้วยนะครับ 🙏

[ENGLISH VERSION]

Infrastructure as Code (IaC) is a concept widely adopted today. It simplifies and systematizes infrastructure management. As infrastructure often goes hand-in-hand with applications, managing both via the same method, tool, or platform can be beneficial. This is where Crossplane comes in.

What is Crossplane?

Crossplane is an open-source IaC tool that operates on a Kubernetes cluster, enabling the use of Kubernetes’ capabilities to manage infrastructure like applications (containers).

For those who already know about Crossplane, please come here → “Explain Crossplane Concepts with Examples

With Crossplane, infrastructure can be created using Kubernetes YAML files, allowing for management of applications and infrastructure under a unified control plane. And this approach is suitable for creating self-service platforms.

Benefits of Using Crossplane

I will only write about things that…

  • Other IaC tools (like Terraform) can’t do
  • Or can do, but require more effort to implement or operate
  • Or can do, but depend on other external tools/services

1. Unified Tool Suppose you have an application that runs on Kubernetes and requires a database. Typically, the database might be provisioned by the infrastructure team using Terraform, while the application may be deployed through kubectl or Helm. With Crossplane, both can be deployed using the same tool, which can also monitor the live state.

2. Real-Time Status Updates: Crossplane provides real-time status updates and health checks directly in the Kubernetes API. This can be useful for more dynamic and responsive infrastructure management.

3. Infrastructure Policy Enforcement
Crossplane
can manage resources based on policies effectively. For example, if a company policy prohibiting public S3 buckets, managing this policy with Terraform can be somewhat complicated. Although you can write a Terraform modules to control it, users can still directly modify the resources on the cloud, necessitating the use of IAM for control, which further complicates management.

In contrast, Crossplane handles this more effectively because Kubernetes, through its reconciliation loop, constantly adjusts the state of resources to match what is defined in the YAML files.

4. Kubernetes RBAC and Namespace
Kubernetes features role-based access control (RBAC) to manage user permissions and namespaces to isolate resources. Using Crossplane as an infrastructure platform allows you to leverage these capabilities. Allow us to manage permissions down to the API level.

5. GitOps for Infrastructure with ArgoCD
GitOps uses Git as the source of truth to store the desired state, in this case, infrastructure. You can write a YAML file to store on Git and use tools like ArgoCD to auto-sync for deployment. It also provides other capabilities like visualizing resources through a UI, automated rollback, and more.

Understanding Kubernetes Extensions

Some of Kubernetes’ capabilities can be expanded, like…

  • Adding sub-commands to kubectl
  • Using custom schedulers for resource scheduling
  • Or creating new resource types (besides pods, services, deployments, etc.) to extend Kubernetes API and controllers

Kubernetes isn’t limited to containers. Other resources can be managed through it using custom resource definitions (CRDs), allowing the creation of custom resources (CR) beyond built-in resources like pods, services, deployments, etc. These CRs, once created in Kubernetes, are managed by custom controllers, just like Kubernetes manages pods.

How does this relate to Crossplane?

If you want Kubernetes to create and manage infrastructure, you’d need to create a CRD and custom controller for each infrastructure resource, like CRDs for VPC, subnet, internet gateway, etc.

This tells Kubernetes how to communicate, create, and check the status of these resources. However, Crossplane has already taken care of this.

That’s it. In next article, we’ll delve into Crossplane Concepts with examples.

ก่อนจะไป…รบกวนกด Like/Follow/Subscribe ให้ผมด้วยนะครับ 🙏

--

--

Nopnithi Khaokaew (Game)

Cloud Solutions Architect & Hobbyist Developer | 6x AWS Certified, CKA, CKAD, 2x HashiCorp Certified (Terraform, Vault), etc.