How do I redirect traffic from an old domain to a new domain using Ingress?
To redirect traffic from an old domain to a new domain using Kubernetes Ingress, you can use an Ingress resource to define rules that match incoming requests and redirect them to the new domain. This is particularly useful when you want to ensure that users accessing the old domain are automatically redirected to the new domain without having to update their bookmarks or links.
Here’s a step-by-step guide on how to achieve this using Pulumi and Kubernetes:
Create an Ingress Resource: We will define an Ingress resource that matches requests to the old domain and redirects them to the new domain.
Define the Rules: The rules within the Ingress resource will specify the host (old domain) and the redirect action to the new domain.
Deploy the Ingress Resource: Using Pulumi, we will deploy this Ingress resource to your Kubernetes cluster.
Below is a Pulumi program written in TypeScript that accomplishes this:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Define the old and new domains
const oldDomain = "old-domain.com";
const newDomain = "new-domain.com";
// Create a Kubernetes Ingress resource
const ingress = new k8s.networking.v1.Ingress("redirect-ingress", {
metadata: {
name: "redirect-ingress",
namespace: "default",
},
spec: {
rules: [
{
host: oldDomain,
http: {
paths: [
{
path: "/",
pathType: "Prefix",
backend: {
service: {
name: "redirect-service",
port: {
number: 80,
},
},
},
},
],
},
},
],
},
});
// Create a ConfigMap to hold the NGINX configuration for the redirect
const nginxConfig = new k8s.core.v1.ConfigMap("nginx-config", {
metadata: {
name: "nginx-config",
namespace: "default",
},
data: {
"default.conf": `
server {
listen 80;
server_name ${oldDomain};
return 301 http://${newDomain}$request_uri;
}
`,
},
});
// Create a Deployment for the NGINX server
const nginxDeployment = new k8s.apps.v1.Deployment("nginx-deployment", {
metadata: {
name: "nginx-deployment",
namespace: "default",
},
spec: {
selector: {
matchLabels: {
app: "nginx",
},
},
replicas: 1,
template: {
metadata: {
labels: {
app: "nginx",
},
},
spec: {
containers: [
{
name: "nginx",
image: "nginx:latest",
volumeMounts: [
{
name: "nginx-config",
mountPath: "/etc/nginx/conf.d",
},
],
},
],
volumes: [
{
name: "nginx-config",
configMap: {
name: "nginx-config",
},
},
],
},
},
},
});
// Export the Ingress URL
export const ingressUrl = pulumi.interpolate`http://${oldDomain}`;
Explanation of the Code
Define the Old and New Domains: We start by defining the old domain (
oldDomain
) and the new domain (newDomain
).Create the Ingress Resource: The
k8s.networking.v1.Ingress
resource is created with a rule to match requests to the old domain. The rule specifies that any request to the old domain should be handled by a backend service namedredirect-service
.NGINX Configuration: We create a ConfigMap (
nginxConfig
) to store the NGINX configuration. This configuration sets up a server block that listens on port 80, matches requests to the old domain, and returns a 301 redirect to the new domain.NGINX Deployment: We create a Deployment (
nginxDeployment
) to run the NGINX server with the redirect configuration. The Deployment uses the ConfigMap to configure NGINX.Export the Ingress URL: Finally, we export the URL of the Ingress resource, which is the old domain.
This setup ensures that any traffic to the old domain is redirected to the new domain using a 301 redirect, which is a permanent redirect. This is useful for SEO and ensures that users and search engines are aware of the new domain.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.