How do I use Kubernetes with Amazon ElastiCache?
To use Kubernetes with Amazon ElastiCache, you can create an ElastiCache cluster and configure a Kubernetes Deployment to connect to it. Below is a Pulumi program written in TypeScript that demonstrates how to achieve this. The program creates an ElastiCache cluster and a Kubernetes Deployment that connects to the ElastiCache cluster.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as k8s from "@pulumi/kubernetes";
// Create a VPC for the ElastiCache cluster
const vpc = new aws.ec2.Vpc("vpc", {
cidrBlock: "10.0.0.0/16",
enableDnsHostnames: true,
enableDnsSupport: true,
});
// Create subnets for the VPC
const subnet1 = new aws.ec2.Subnet("subnet1", {
vpcId: vpc.id,
cidrBlock: "10.0.1.0/24",
availabilityZone: "us-west-2a",
});
const subnet2 = new aws.ec2.Subnet("subnet2", {
vpcId: vpc.id,
cidrBlock: "10.0.2.0/24",
availabilityZone: "us-west-2b",
});
// Create a security group for the ElastiCache cluster
const securityGroup = new aws.ec2.SecurityGroup("securityGroup", {
vpcId: vpc.id,
ingress: [
{ protocol: "tcp", fromPort: 6379, toPort: 6379, cidrBlocks: ["0.0.0.0/0"] },
],
egress: [
{ protocol: "tcp", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] },
],
});
// Create an ElastiCache subnet group
const subnetGroup = new aws.elasticache.SubnetGroup("subnetGroup", {
subnetIds: [subnet1.id, subnet2.id],
});
// Create an ElastiCache cluster
const cacheCluster = new aws.elasticache.Cluster("cacheCluster", {
engine: "redis",
nodeType: "cache.t2.micro",
numCacheNodes: 1,
subnetGroupName: subnetGroup.name,
securityGroupIds: [securityGroup.id],
});
// Create a Kubernetes provider
const k8sProvider = new k8s.Provider("k8sProvider", {
kubeconfig: "<your-kubeconfig-path>",
});
// Create a Kubernetes namespace
const namespace = new k8s.core.v1.Namespace("namespace", {}, { provider: k8sProvider });
// Create a Kubernetes Secret to store the ElastiCache endpoint
const secret = new k8s.core.v1.Secret("redis-secret", {
metadata: { namespace: namespace.metadata.name },
stringData: {
"redis-endpoint": cacheCluster.cacheNodes.apply(nodes => nodes[0].address),
},
}, { provider: k8sProvider });
// Create a Kubernetes Deployment that uses the ElastiCache cluster
const appLabels = { app: "redis-app" };
const deployment = new k8s.apps.v1.Deployment("redis-deployment", {
metadata: { namespace: namespace.metadata.name },
spec: {
selector: { matchLabels: appLabels },
replicas: 1,
template: {
metadata: { labels: appLabels },
spec: {
containers: [{
name: "redis-app",
image: "redis:latest",
env: [{
name: "REDIS_ENDPOINT",
valueFrom: {
secretKeyRef: {
name: secret.metadata.name,
key: "redis-endpoint",
},
},
}],
ports: [{ containerPort: 6379 }],
}],
},
},
},
}, { provider: k8sProvider });
This program does the following:
- Creates a VPC and subnets for the ElastiCache cluster.
- Sets up a security group to allow access to the ElastiCache cluster.
- Creates an ElastiCache subnet group and a Redis cluster.
- Sets up a Kubernetes provider and namespace.
- Creates a Kubernetes Secret to store the ElastiCache endpoint.
- Creates a Kubernetes Deployment that connects to the ElastiCache cluster using the endpoint stored in the Secret.
By running this program, you will have an ElastiCache cluster and a Kubernetes Deployment that can communicate with each other.
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.