1. Managing Egress for ML Training Jobs with Network Policies

    Python

    When managing egress for ML (Machine Learning) training jobs in Kubernetes, Network Policies are a critical security feature that allow you to control the flow of traffic to and from your ML training pods. Network Policies specify how groups of pods are allowed to communicate with each other and other network endpoints.

    Pulumi's infrastructure as code tooling provides a convenient way to manage these policies within your Kubernetes clusters. The pulumi_kubernetes package gives you the resources you need to define Network Policies in a declarative manner.

    Below is a Pulumi program written in Python that sets up a simple Network Policy in Kubernetes. This Network Policy will restrict egress traffic from an ML training job so that it can only communicate with a specific service or external endpoint.

    Here are the steps the program performs:

    1. Creates a namespace for the ML training jobs.
    2. Defines a label selector that selects your ML training pods.
    3. Creates a Network Policy that allows egress to a specific CIDR range (which you would replace with the actual IP range you wish your training jobs to communicate with).
    import pulumi from pulumi_kubernetes.networking.v1 import NetworkPolicy from pulumi_kubernetes.core.v1 import Namespace # Create a Kubernetes namespace for the ML training jobs ml_namespace = Namespace("ml-namespace") # A label selector for selecting the pods that the policy will apply to # Replace with your own labels to match your ML training pods pod_selector = {"matchLabels": {"role": "ml-training"}} # Define the Network Policy ml_network_policy = NetworkPolicy( "ml-network-policy", metadata={ "namespace": ml_namespace.metadata["name"] }, spec={ "podSelector": pod_selector, "policyTypes": ["Egress"], "egress": [ # Here, define where the pod can communicate to. # Replace `192.168.0.0/16` with your desired external IP range. { "to": [ { "ipBlock": { "cidr": "192.168.0.0/16" } } ] } # You can also set up egress to other pods within your cluster # by using the `podSelector` and `namespaceSelector` fields. ] } ) # Export the namespace name pulumi.export("ml_namespace", ml_namespace.metadata["name"])

    In this example, a Network Policy resource (ml_network_policy) is created within a Kubernetes namespace (ml_namespace). The policy uses a podSelector to target pods that have the label 'role': 'ml-training'. In the egress field of the policy, we specify an ipBlock that allows communication to the specified CIDR block. You need to customize the CIDR block in the ipBlock to match the network range you'd like your pods to communicate with.

    Please replace 'role': 'ml-training' with the appropriate labels that match your ML training pods. Similarly, replace 192.168.0.0/16 with the IP range that your training jobs need to access.

    This program assumes you have the Pulumi CLI installed and configured with access to a Kubernetes cluster where you want to apply this policy. To deploy this policy to your cluster, save the above code to a file named __main__.py, and then run pulumi up in the same directory as the file. Pulumi will execute the script, create the resources and show you the changes before they're applied. When prompted, confirm that you want to make the changes, and Pulumi will proceed to set up the Network Policy for your Kubernetes cluster.