K8s Operators Workshop

website·Indexed 29 days ago

https://tom-ha.github.io/k8s-operators-workshop/docs/

A workshop on Kubernetes Operators covering Custom Resource Definitions (CRDs), Custom Resources (CRs), Helm, Helmfile, GitOps, and Argo CD. Includes hands-on guides for provisioning PostgreSQL databases, managing credentials, and deploying applications.

Tokens
2.6K
Snippets
11
Records
24
Agent score
72%

What's inside K8s Operators Workshop

  1. Understand GitOps deployment methodology

    GitOps is a deployment methodology where the desired state of a deployment is declared in a Git repository, which serves as the single source of truth. For example, if deployment and service manifests are pushed to a Git repository, the GitOps process ensures those manifests are reflected in the Kubernetes cluster.
  2. Use Argo CD for GitOps continuous delivery

    Argo CD is a continuous delivery tool used to implement GitOps. It monitors resources defined in a Git repository and applies them to a Kubernetes cluster. It resolves configuration drift (caused by manual actions) by continuously comparing the desired state in Git with the current state in the cluster, providing visualization of changes and re-synchronization capabilities.
  3. Understand the role of Kubernetes Operators

    Kubernetes Operators are software extensions that extend the Kubernetes API by incorporating domain-specific knowledge. They use Custom Resources (CRs) to manage applications and their components, following the core Kubernetes principle of the 'control loop'—continuously monitoring the desired state defined in a custom resource and taking action to match the actual state of the cluster to that desired state. For example, a database operator can automate the creation, configuration, and deletion of database instances based on the specifications provided in a Custom Resource.
  4. Deploy the Argo CD Root Application using the App of Apps pattern

    The Root Application is an Argo CD Application resource that tells Argo CD to sync manifests from a specific directory (e.g., apps/app-sets). This implements the 'App of Apps' pattern, where the root application manages other ApplicationSet resources, which in turn dynamically create child applications based on the repository's folder structure (such as infra and services folders).
    kubectl apply -f ./values/argo-cd/argocd-root-app.yaml
  5. Implement the Kubernetes Operator pattern for automated provisioning

    The Operator pattern extends Kubernetes by combining Custom Resource Definitions (CRDs) and a custom controller (the Operator) to manage complex applications.

    In this pattern:

    1. A CRD defines the desired state of a resource (e.g., a Postgres resource with a specific database name).
    2. An Operator constantly watches the Kubernetes API for new or updated Custom Resources (CRs) of that specific kind.
    3. When the Operator detects a change, it performs the necessary real-world actions to match the desired state (e.g., provisioning a physical database instance named after the value in the CR's specification).
  6. Test sample-pg-app connectivity using port-forwarding

    To verify that the sample-pg-app can communicate with its database, you can establish a local connection via port-forwarding and interact with the application's API. This process involves forwarding local traffic to the Kubernetes service and using curl to POST and GET data.
    # 1. Forward localhost port 8081 to sample-pg-app service port 80
    kubectl -n services port-forward svc/sample-pg-app 8081:80
    
    # 2. Store data in the database via POST request
    curl --location --request POST 'localhost:8081/data' \
        --header 'Content-Type: application/json' \
        --data-raw '{"data":"k8s-operators-workshop"}'
    
    # 3. Retrieve the data via GET request to verify storage
    curl --location --request GET 'localhost:8081/data'
  7. Install required CLI utilities for the workshop

    Ensure the following tools are installed on your local machine to manage the cluster and deployments:

    • kubectl: Kubernetes cluster management
    • helm: Kubernetes package manager
    • helmfile: Declarative helm chart management
    • helm-diff: A plugin for helm to see differences between releases
  8. Provision a PostgreSQL user using the PostgresUser CRD

    To create a database user and password stored as a Kubernetes secret, define a PostgresUser custom resource. The database field must reference the name of the existing Postgres Custom Resource. The privileges field supports OWNER, READ, or WRITE.
    apiVersion: db.movetokube.com/v1alpha1
    kind: PostgresUser
    metadata:
      name: sample-pg-app-auth
      namespace: services
    spec:
      role: sample-user
      database: sample-pg-app-db       # This references the Postgres CR
      secretName: postgres
      privileges: OWNER     # Can be OWNER/READ/WRITE