1. Answers
  2. Using Kubernetes Service With Ingress

Using Kubernetes Service With Ingress

Introduction

In this guide, we will create a Kubernetes Service with an Ingress resource using Pulumi in TypeScript. We will use AWS as our cloud provider, as per the organization’s system prompts. The key services involved are Kubernetes for container orchestration and AWS for cloud infrastructure.

Step-by-Step Explanation

Step 1: Set Up Pulumi and AWS

  1. Ensure you have Pulumi CLI installed. If not, follow the installation guide.
  2. Configure Pulumi to use AWS as the cloud provider. You can do this by setting up your AWS credentials.

Step 2: Create a New Pulumi Project

  1. Create a new Pulumi project using TypeScript:
    pulumi new aws-typescript
    
  2. Follow the prompts to set up your project.

Step 3: Define the Kubernetes Provider

  1. Install the necessary Pulumi packages:
    npm install @pulumi/pulumi @pulumi/aws @pulumi/kubernetes
    
  2. Define the Kubernetes provider in your index.ts file:
    import * as pulumi from "@pulumi/pulumi";
    import * as aws from "@pulumi/aws";
    import * as k8s from "@pulumi/kubernetes";
    
    const cluster = new aws.eks.Cluster("my-cluster", { ... });
    
    const k8sProvider = new k8s.Provider("k8sProvider", {
        kubeconfig: cluster.kubeconfig.apply(JSON.stringify)
    });
    

Step 4: Create a Kubernetes Service

  1. Define a Kubernetes Service in your index.ts file:
    const appLabels = { app: "my-app" };
    
    const service = new k8s.core.v1.Service("my-service", {
        metadata: { labels: appLabels },
        spec: {
            ports: [{ port: 80, targetPort: 80 }],
            selector: appLabels,
            type: "ClusterIP"
        }
    }, { provider: k8sProvider });
    

Step 5: Create a Kubernetes Ingress

  1. Define a Kubernetes Ingress resource in your index.ts file:
    const ingress = new k8s.networking.v1.Ingress("my-ingress", {
        metadata: {
            name: "example-ingress",
            annotations: {
                "nginx.ingress.kubernetes.io/rewrite-target": "/"
            }
        },
        spec: {
            rules: [{
                http: {
                    paths: [{
                        path: "/",
                        pathType: "Prefix",
                        backend: {
                            service: {
                                name: service.metadata.name,
                                port: { number: 80 }
                            }
                        }
                    }]
                }
            }]
        }
    }, { provider: k8sProvider });
    

Step 6: Deploy the Stack

  1. Run pulumi up to deploy your stack.
  2. Confirm the changes and wait for the deployment to complete.

Summary

In this guide, we created a Kubernetes Service and an Ingress resource using Pulumi in TypeScript. We used AWS as our cloud provider and set up the necessary configurations. We defined the Kubernetes provider, created a Service, and then created an Ingress resource to route traffic to the Service. Finally, we deployed the stack using Pulumi.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as k8s from "@pulumi/kubernetes";

// Create an EKS cluster
const cluster = new aws.eks.Cluster("my-cluster", {
    roleArn: "arn:aws:iam::123456789012:role/EKSRole",
    vpcConfig: {
        subnetIds: ["subnet-12345678", "subnet-87654321"],
    },
});

// Export the cluster's kubeconfig
export const kubeconfig = pulumi.all([cluster.endpoint, cluster.certificateAuthority, cluster.name]).apply(([endpoint, certificateAuthority, name]) => {
    return \`apiVersion: v1
clusters:
- cluster:
    server: \${endpoint}
    certificate-authority-data: \${certificateAuthority.data}
  name: \${name}
contexts:
- context:
    cluster: \${name}
    user: aws
  name: \${name}
current-context: \${name}
kind: Config
preferences: {}
users:
- name: aws
  user:
    exec:
      apiVersion: client.authentication.k8s.io/v1alpha1
      command: aws-iam-authenticator
      args:
        - "token"
        - "-i"
        - \${name}
\`;
});

// Create a Kubernetes provider instance
const k8sProvider = new k8s.Provider("k8sProvider", {
    kubeconfig: kubeconfig
});

// Define Kubernetes Service
const appLabels = { app: "my-app" };
const service = new k8s.core.v1.Service("my-service", {
    metadata: { labels: appLabels },
    spec: {
        ports: [{ port: 80, targetPort: 80 }],
        selector: appLabels,
        type: "ClusterIP"
    }
}, { provider: k8sProvider });

// Define Kubernetes Ingress
const ingress = new k8s.networking.v1.Ingress("my-ingress", {
    metadata: {
        name: "example-ingress",
        annotations: {
            "nginx.ingress.kubernetes.io/rewrite-target": "/"
        }
    },
    spec: {
        rules: [{
            http: {
                paths: [{
                    path: "/",
                    pathType: "Prefix",
                    backend: {
                        service: {
                            name: service.metadata.name,
                            port: { number: 80 }
                        }
                    }
                }]
            }
        }]
    }
}, { provider: k8sProvider });

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up