Deploy a high-performance database for containerized applications: Amazon MemoryDB for Redis with Kubernetes

海外精选
海外精选的内容汇集了全球优质的亚马逊云科技相关技术内容。同时,内容中提到的“AWS” 是 “Amazon Web Services” 的缩写,在此网站不作为商标展示。
0
0
{"value":"More and more organizations are building their applications using microservices for operational efficiency, agility, scalability, and faster time to market. Microservices and containers have emerged as building blocks for modern applications, and Kubernetes has become the de facto standard for managing containers at scale. Applications running on Kubernetes need a database that provides ultra-fast performance, high availability and durability, security and manageability. Provisioning and integrating databases with containerized applications is often complex. Customers who are using Kubernetes to automatically deploy, scale, and manage their containerized applications are looking to do the same for their database.\n\nIn August 2021, we launched [Amazon MemoryDB for Redis](https://aws.amazon.com/memorydb/), which is purpose-built for modern applications with microservices architectures. MemoryDB is ideal for applications that need high performance, security, availability, durability, and scalability. Recently, we have launched a developer preview of the MemoryDB service controller for [AWS Controllers for Kubernetes](https://aws-controllers-k8s.github.io/community/docs/community/overview/) (ACK), which helps developers define and use MemoryDB resources directly from a Kubernetes cluster.\n\nIn this blog post, we first explain why MemoryDB is the right database for microservices applications. We then walk through deploying a sample gaming leaderboard application into a Kubernetes cluster provided by [Amazon Elastic Kubernetes Service](https://aws.amazon.com/eks/) ([Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail)). We use MemoryDB as the database and ACK to create and manage a MemoryDB cluster using native Kubernetes APIs.\n\n### **Why use Amazon MemoryDB for microservices**\nModern applications with microservices architecture require massive scale, low read and write latencies, and high throughput performance. Developers like building microservices applications with Redis since it provides performance and scale, and because it provides a number of built-in data structures and commands that simplify performing common application tasks. As these developers deploy more business-critical applications using microservices architectures, they look for their database layer to provide not only high performance but also high durability. Developers often use [Amazon ElastiCache for Redis](https://aws.amazon.com/elasticache/redis/), a fully managed in-memory caching service, as a low latency cache alongside a durable AWS database service such as [Amazon Aurora](https://aws.amazon.com/rds/aurora/) or [Amazon DynamoDB](https://aws.amazon.com/dynamodb/). However, this requires custom code to manage data between two independent data stores and additional costs for running two services. Developers want to build durable applications using Redis with less application complexity.\n\nAmazon MemoryDB is a Redis-compatible, durable, in-memory database that makes it simple to build modern applications that require microsecond read and single-digit millisecond write performance, data durability, and high availability. MemoryDB durably stores data using a Multi-AZ transactional log, enabling fast recovery in the face of a failure event. MemoryDB is Redis-compatible, and customers can build applications quickly using Redis’s flexible and friendly data structures and APIs. Instead of using a low latency cache with a durable database, customers can now simplify their architecture and use MemoryDB as a single, primary database for the most demanding, business-critical applications that require low latency, high throughput, and durability.\n\n#### **Traditional approach to building microservices**\n\n![image.png](https://dev-media.amazoncloud.cn/7787de3b60f54869b7b0c219e35b362b_image.png)\n\n#### **Modern/simpler approach to building microservices**\n\n![image.png](https://dev-media.amazoncloud.cn/09044dea8100467eb591fb9610bf8228_image.png)\n\n#### **ACK for MemoryDB**\n\nACK is a collection of Kubernetes custom resource definitions (CRDs) and custom controllers working together to extend the Kubernetes API and manage AWS resources on your behalf. ACK for MemoryDB enables you to provision and manage MemoryDB clusters as part of your Kubernetes applications using your normal infrastructure management workflows.\n\nACK can be installed on Kubernetes clusters using Helm Charts (available on [Amazon ECR](https://gallery.ecr.aws/aws-controllers-k8s/memorydb-chart)). Once installed, the MemoryDB cluster can be created as a Kubernetes custom resource using native Kubernetes tooling (example: kubectl).\n\nThe following diagram provides an overview of installing the ACK MemoryDB controller using Helm Charts under the “ack-system” namespace on a Kubernetes cluster. It results in the ACK MemoryDB controller running inside a pod on a Kubernetes worker node and MemoryDB custom resources definitions for the following resources:\n\n1. cluster\n2. user\n3. acl\n4. parametergroup\n5. subnetgroup\n6. snapshot\n\n![image.png](https://dev-media.amazoncloud.cn/842e649f342446be8a2da70eda651e5c_image.png)\n\n#### **Define and use MemoryDB resources directly from Kubernetes**\n\nThe ACK MemoryDB controller enables application developers to create a namespaced custom resource (MemoryDB cluster, in this case) in any of their Kubernetes clusters. The MemoryDB service controller installed by cluster admins can create, update, or delete MemoryDB resources based on the intent found in the custom resource.\n\nIn the following diagram, the application manifest is applied to the Kubernetes cluster. It contains specifications for a MemoryDB cluster, a Kubernetes ConfigMap, and a microservice application. It results in the ACK MemoryDB controller creating the MemoryDB cluster. The MemoryDB cluster endpoint details are available to the microservice application as container environment variables via the Kubernetes config map.\n\nThe containerized microservice app on Kubernetes then connects to the MemoryDB Cluster.\n\n![image.png](https://dev-media.amazoncloud.cn/231a96397cec4739b4bb4d0c297fd553_image.png)\n\n### **Example gaming leaderboard application using MemoryDB**\nLet’s walk you through an example gaming leaderboard that uses MemoryDB.\n\n#### **Prerequisites**\n\nFor a detailed tutorial on installing ACK MemoryDB controller on your Kubernetes cluster, review the [documentation at AWS Controllers K8s GitHub](https://aws-controllers-k8s.github.io/community/docs/tutorials/memorydb-example/).\n\nThis example assumes that you have:\n\n1. Installed the following tools on the client machine used to access your Kubernetes cluster:\n\ta. [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html) – A command line tool for interacting with AWS services.\n\tb. [kubectl](https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html) – A command line tool for working with Kubernetes clusters.\n\tc. [eksctl](https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html) – A command line tool for working with EKS clusters.\n\td. [Helm 3.7+](https://helm.sh/docs/intro/install/) – A tool for installing and managing Kubernetes applications.\n\n2. [AWS Identity and Access Management](https://aws.amazon.com/cn/iam/?trk=cndc-detail) (IAM) permissions to create roles and attach policies to roles.\n\n#### **Setting up [Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail) cluster**\n\nWe will be using [eksctl](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html) to create a Kubernetes cluster with [Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail). Here we create the cluster with Linux-managed nodes. Please refer to [Getting started with Amazon EKS – eksctl](https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html) for detailed steps.\n```\\nEKS_CLUSTER_NAME=\\"<your cluster name>\\"\\nREGION=\\"<your region>\\"\\n\\neksctl create cluster \\\\\\n --name \\"\${EKS_CLUSTER_NAME}\\" \\\\\\n --region \\"\${REGION}\\" \\\\\\n --with-oidc\\n```\n\nWhen the EKS cluster is available, set up [the Amazon VPC Container Network Interface (CNI) plugin for Kubernetes](https://docs.aws.amazon.com/eks/latest/userguide/managing-vpc-cni.html) for the EKS cluster.\n\n#### **Install ACK MemoryDB controller**\nYou can deploy the ACK service controller for MemoryDB using the [memorydb-chart Helm Chart](https://gallery.ecr.aws/aws-controllers-k8s/memorydb-chart). You can download it to your workspace using the following command:\n\n```\\nhelm pull oci://public.ecr.aws/aws-controllers-k8s/memorydb-chart --version=v0.0.1\\n```\nYou will need to decompress and extract the Helm Chart. You can do so with the following command:\n\n```\\ntar xzvf memorydb-chart-v0.0.1.tgz\\n```\nYou can now use the Helm Chart to deploy the ACK service controller for MemoryDB to your EKS cluster. At a minimum, you need to specify the AWS Region to execute the MemoryDB API calls.\n\nFor example, to specify that the MemoryDB API calls go to the US East (N. Virginia) region, you can deploy the service controller with the following command:\n\n```\\nhelm install memorydb-chart --generate-name --set=aws.region=us-east-1\\n```\nFor a full list of available values for the Helm Chart, please review the [values.yaml](https://github.com/aws-controllers-k8s/memorydb-controller/blob/main/helm/values.yaml) file.\n\n#### **Configure IAM permissions**\nOnce the service controller is deployed, you will need to configure the [IAM permissions](https://aws-controllers-k8s.github.io/community/docs/user-docs/irsa/) for the controller to query the MemoryDB API. For full details, please review the AWS Controllers for Kubernetes documentation for [how to configure the IAM permissions](https://aws-controllers-k8s.github.io/community/docs/user-docs/irsa/). If you follow the examples in the documentation, use the value of MemoryDB for SERVICE.\n\n#### **Create MemoryDB cluster**\nYou can create MemoryDB clusters using the cluster custom resource. The examples below show how to create it from your Kubernetes environment. For a full list of options available in the cluster custom resource definition, you can use the ```kubectl explain cluster``` command.\n\nThis example creates the MemoryDB cluster in the same VPC as the EKS cluster using the same subnets and security groups. It creates a MemoryDB SubnetGroup custom resource and then uses it to create a MemoryDB cluster.\n\nThe following example creates MemoryDB subnet group:\n\n```\\nREGION=\\"<your region>\\"\\nEKS_CLUSTER_NAME=\\"<your EKS cluster name>\\"\\nEKS_VPC_ID=\$(aws –region \$REGION eks describe-cluster –name \$EKS_CLUSTER_NAME –query cluster.resourcesVpcConfig.vpcId)\\nEKS_SUBNET_IDS=\$(aws –region \$REGION ec2 describe-subnets \\\\\\n --filters \\"Name=vpc-id,Values=\${EKS_VPC_ID}\\" \\\\\\n --query 'Subnets[*].SubnetId' \\\\\\n --output text\\n)\\n\\nMEMORYDB_SUBNETGROUP_NAME=\\"<your subnet group name>\\"\\n\\ncat <<EOF > memorydb-subnetgroup.yaml\\napiVersion: memorydb.services.k8s.aws/v1alpha1\\nkind: SubnetGroup\\nmetadata:\\n name: \\"\${MEMORYDB_SUBNETGROUP_NAME}\\"\\nspec:\\n name: \\"\${MEMORYDB_SUBNETGROUP_NAME}\\"\\n description: \\"MemoryDB cluster subnet group\\"\\n subnetIDs:\\n\$(printf \\" - %s\\\\n\\" \${EKS_SUBNET_IDS})\\n\\nEOF\\n\\nkubectl apply -f memorydb-subnetgroup.yaml\\nkubectl describe subnetgroup \\"\${MEMORYDB_SUBNETGROUP_NAME}\\"\\n```\nIf you observe that the ```ACK.Terminal``` condition is set for the ```SubnetGroup```, and the error is similar to the following:\n```\\nStatus:\\n Conditions:\\n Message: SubnetNotAllowedFault: Subnets: [subnet-1d111111, subnet-27d22222] are not in a supported availability zone. Supported availability zones are [us-east-1c, us-east-1d, us-east-1b].\\n Status: True\\n Type: ACK.Terminal\\n```\nThen update the subnetIDs in the input YAML and provide the subnet IDs that are in a supported Availability Zone.\n\nThe following example creates a MemoryDB cluster. It uses the db.t4g.small node type for the MemoryDB cluster. Please review the [MemoryDB node types](https://docs.aws.amazon.com/memorydb/latest/devguide/nodes.supportedtypes.html) to select the most appropriate one for your workload.\n```\\nREGION=\\"<your region>\\"\\nMEMORYDB_CLUSTER_NAME=\\"<your cluster name>\\"\\nSECURITY_GROUP_ID=\$(aws –region \$REGION eks describe-cluster –name \$EKS_CLUSTER_NAME –query cluster.resourcesVpcConfig.clusterSecurityGroupId)\\n\\ncat <<EOF > memorydb-cluster.yaml\\napiVersion: memorydb.services.k8s.aws/v1alpha1\\nkind: Cluster\\nmetadata:\\n name: \\"\${MEMORYDB_CLUSTER_NAME}\\"\\nspec:\\n name: \\"\${MEMORYDB_CLUSTER_NAME}\\"\\n nodeType: db.t4g.small\\n aclName: open-access\\n securityGroupIDs:\\n - \${SECURITY_GROUP_ID}\\n subnetGroupName: \${MEMORYDB_SUBNETGROUP_NAME}\\nEOF\\n\\nkubectl apply -f memorydb-cluster.yaml\\nkubectl describe cluster \\"\${MEMORYDB_CLUSTER_NAME}\\"\\n```\nYou can track the status of the provisioned database using ```kubectl describe``` on the Cluster custom resource:\n```\\nkubectl describe cluster \\"\${MEMORYDB_CLUSTER_NAME}\\"\\n```\nWhen the ```Cluster Status``` says ```available```, you can connect to the database instance.\n\n#### **Connect gaming leaderboard application to MemoryDB cluster**\n\nThe ```Cluster status``` contains the information for connecting to a MemoryDB cluster. The host information can be found in ```status.clusterEndpoint.address```, and the port information can be found in ```status.clusterEndpoint.port```. For example, you can get the connection information for a cluster created in one of the previous examples using the following commands:\n\n```\\nkubectl get cluster \\"\${MEMORYDB_CLUSTER_NAME}\\" -o jsonpath='{.status.clusterEndpoint.address}'\\nkubectl get cluster \\"\${MEMORYDB_CLUSTER_NAME}\\" -o jsonpath='{.status.clusterEndpoint.port}'\\n```\n#### **Set up ConfigMap for MemoryDB cluster endpoint information**\nThe MemoryDB cluster endpoint information can be made available to application pods via ```ConfigMap``` using a ```FieldExport``` resource. The following example makes the MemoryDB cluster endpoint and port available as ConfigMap data:\n\n```\\nMEMORYDB_CLUSTER_NAME=\\"<your cluster name>\\"\\nMEMORYDB_CLUSTER_CONN_CM=\\"\${MEMORYDB_CLUSTER_NAME}-conn-cm\\"\\n\\ncat <<EOF > memorydb-field-exports.yaml\\n---\\napiVersion: v1\\nkind: ConfigMap\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\ndata: {}\\n---\\napiVersion: services.k8s.aws/v1alpha1\\nkind: FieldExport\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_NAME}-host\\nspec:\\n to:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n kind: configmap\\n from:\\n path: \\".status.clusterEndpoint.address\\"\\n resource:\\n group: memorydb.services.k8s.aws\\n kind: Cluster\\n name: \${MEMORYDB_CLUSTER_NAME}\\n---\\napiVersion: services.k8s.aws/v1alpha1\\nkind: FieldExport\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_NAME}-port\\nspec:\\n to:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n kind: configmap\\n from:\\n path: \\".status.clusterEndpoint.port\\"\\n resource:\\n group: memorydb.services.k8s.aws\\n kind: Cluster\\n name: \${MEMORYDB_CLUSTER_NAME}\\nEOF\\n\\nkubectl apply -f memorydb-field-exports.yaml\\n```\nConfirm that the MemoryDB endpoint details are available in the config map by running the following command.\n\n```\\nkubectl get configmap/\${MEMORYDB_CLUSTER_CONN_CM} -o jsonpath='{.data}'\\n```\n#### **Deploy container example and access MemoryDB cluster endpoint**\nThe MemoryDB cluster endpoint details (Address, Port) can be injected into a container either as environmental variables or files. For example, here is a snippet of a deployment definition that will add the MemoryDB cluster connection info into an example leaderboard app:\n```\\ncat <<EOF > game_leaderboard.yaml\\napiVersion: apps/v1\\nkind: Deployment\\nmetadata:\\n name: leaderboard-deployment\\n labels:\\n app: leaderboard\\nspec:\\n replicas: 1\\n selector:\\n matchLabels:\\n app: leaderboard\\n template:\\n metadata:\\n labels:\\n app: leaderboard\\n spec:\\n containers:\\n - name: leaderboard\\n image: public.ecr.aws/sam/build-python3.8:latest\\n tty: true\\n stdin: true\\n env:\\n - name: MEMORYDB_CLUSTER_HOST\\n valueFrom:\\n configMapKeyRef:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n key: \\"\${MEMORYDB_CLUSTER_NAME}-host\\"\\n - name: MEMORYDB_CLUSTER_PORT\\n valueFrom:\\n configMapKeyRef:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n key: \\"\${MEMORYDB_CLUSTER_NAME}-port\\"\\nEOF\\n\\nkubectl apply -f game_leaderboard.yaml\\n```\nConfirm that the leaderboard application container has been deployed successfully by running the following:\n```\\nkubectl get pods –selector=app=leaderboard\\n```\nVerify that the Pod Status is “Running.”\n\nGet a shell to the running leaderboard container.\n```\\nLEADERBOARD_POD_NAME=\$(kubectl get pods –selector=app=leaderboard -o jsonpath='{.items[*].metadata.name}')\\nkubectl exec –stdin –tty \${LEADERBOARD_POD_NAME} -- /bin/bash\\n```\nIn the running leaderboard container shell, run the following commands and confirm that the MemoryDB cluster host and port are available as environment variables.\n```\\n# Confirm that the memorydb cluster host, port are available as environment variables\\necho \$MEMORYDB_CLUSTER_HOST\\necho \$MEMORYDB_CLUSTER_PORT\\n```\n#### **Add gaming leaderboard scripts to running application container**\nFor the steps in this section, execute the commands from the shell running in the leaderboard container.\n\nInstall dependencies for the example leaderboard scripts.\n```\\npip install redis\\n```\nCreate example leaderboard scripts:\n\nThis script connects to the MemoryDB cluster by discovering its endpoint address and port from the container environment variables.\n\n1. ```game_latest_scores.py``` – This script writes example scores for players into the MemoryDB cluster using Redis API.\n2. ```Game_latest_scores.py``` – This script reads top-ranked players by highest scores for the leaderboard from the MemoryDB cluster using Redis API.\n\nCreate a directory to place leaderboard scripts:\n\n```\\nmkdir leaderboard\\ncd leaderboard\\n```\nCreate ```game_latest_scores.py``` script:\n\n```\\ncat <<EOF > game_latest_scores.py\\nimport redis\\nfrom random import randrange\\nimport os\\n\\nmemorydb_cluster_endpoint_address = os.environ['MEMORYDB_CLUSTER_HOST']\\nmemorydb_cluster_endpoint_port = int(os.environ['MEMORYDB_CLUSTER_PORT'])\\nmemorydb = redis.Redis(host=memorydb_cluster_endpoint_address,\\n port=memorydb_cluster_endpoint_port, db=0)\\n\\ndef write_latest_scores():\\n for i in range(1000):\\n memorydb.zadd(\\"leaderboard\\", {f\\"Player-{i}\\": randrange(1000)})\\n\\nif __name__ == \\"__main__\\":\\n write_latest_scores()\\nEOF\\n```\nCreate ```game_leaderboard.py``` script:\n\n```\\ncat <<EOF > game_leaderboard.py\\nimport redis\\nimport os\\n\\nmemorydb_cluster_endpoint_address = os.environ['MEMORYDB_CLUSTER_HOST']\\nmemorydb_cluster_endpoint_port = int(os.environ['MEMORYDB_CLUSTER_PORT'])\\nmemorydb = redis.Redis(host=memorydb_cluster_endpoint_address,\\n port=memorydb_cluster_endpoint_port, db=0)\\n\\ndef print_header():\\n separator = ''.join([char * 30 for char in '-'])\\n columns = f\\"{'Rank': >{4}}\\\\t{'Player': <{15}}{'Score': >{6}}\\"\\n header = f\\"{separator}\\\\n{columns}\\\\n{separator}\\"\\n print(header)\\n\\ndef print_leaderboard():\\n print_header()\\n leaders = memorydb.zrevrange(\\"leaderboard\\", 0, 10, \\"WITHSCORES\\")\\n\\n rank = 0\\n for player_score in leaders:\\n player = player_score[0].decode(\\"utf-8\\")\\n score = player_score[1]\\n rank = rank + 1\\n print(f\\"#{rank: >{3}}\\\\t{player: <{15}} {score: >{6}}\\")\\n\\nif __name__ == \\"__main__\\":\\n print_leaderboard()\\nEOF\\n```\n#### **Run gaming leaderboard application**\nLoad game scores into the MemoryDB cluster by running ```game_latest_scores.py``` script:\n\n```\\n# load the latest game scores into memorydb cluster\\npython game_latest_scores.py\\n```\nNote: If you are unable to connect to the MemoryDB cluster from the Kubernetes pod, then ensure that the [Amazon VPC Container Network Interface (CNI) plugin for Kubernetes](https://docs.aws.amazon.com/eks/latest/userguide/managing-vpc-cni.html) has been set up for the EKS cluster, and also review [Access Patterns for Accessing a MemoryDB Cluster in an Amazon VPC](https://docs.aws.amazon.com/memorydb/latest/devguide/memorydb-vpc-accessing.html), based upon the MemoryDB cluster VPC subnet groups and security groups.\n\nGet the latest leaderboard printed on the console by running ```game_leaderboard.py``` script:\n\n```\\n# get leaderboard with players ranked per their game score from MemoryDB cluster\\npython game_leaderboard.py\\n```\nObserve an output similar to the following list:\n\n```\\n-----------------------------\\nRank Player Score\\n------------------------------\\n# 1 Player-667 998.0\\n# 2 Player-51 997.0\\n# 3 Player-186 997.0\\n# 4 Player-674 996.0\\n# 5 Player-150 995.0\\n# 6 Player-102 993.0\\n# 7 Player-813 991.0\\n# 8 Player-227 991.0\\n# 9 Player-455 990.0\\n# 10 Player-260 989.0\\n# 11 Player-233 986.0\\n```\n\nExit the shell running in the leaderboard container:\n\n```\\nexit\\n```\n### **Cleanup**\n- To delete the sample gaming leaderboard application, use the following command:\n```\\nkubectl delete -f game_leaderboard.yaml\\n```\n\n- To delete the ConfigMap and FieldExport, use the following command:\n```\\nkubectl delete -f memorydb-field-exports.yaml\\n```\n\n- To delete the MemoryDB cluster subnet group, use the following command:\n```\\nkubectl delete -f memorydb-subnetgroup.yaml\\n```\n\n- To delete the MemoryDB cluster, use the following command:\n```\\nkubectl delete -f memorydb-cluster.yaml\\n```\n\n- To uninstall the ACK service controller for MemoryDB, refer to [Uninstall an ACK Controller](https://aws-controllers-k8s.github.io/community/docs/user-docs/cleanup/). Following the example, set the value of ```SERVICE``` to ```memorydb```.\n- To delete your [Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail) cluster, refer to [Deleting an Amazon EKS cluster](https://docs.aws.amazon.com/eks/latest/userguide/delete-cluster.html).\n```\\neksctl delete cluster \\\\\\n --name \\"\${EKS_CLUSTER_NAME}\\" \\\\\\n --region \\"\${REGION}\\"\\n```\n### **Conclusion**\nWe demonstrated how ACK lets you deploy a MemoryDB cluster directly from your [Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail) environment and connect a sample gaming leaderboard application to it. To learn more, visit the [Amazon Memory DB for Redis Features page](https://aws.amazon.com/memorydb/features/) and the [AWS Controllers K8s GitHub documentation](https://aws-controllers-k8s.github.io/community/docs/tutorials/memorydb-example/). Let us know your feedback. ACK is open source; you can request new features and report issues on the [ACK community GitHub repository](https://github.com/aws-controllers-k8s/community).\n\n#### **About the Authors**\n\n![image.png](https://dev-media.amazoncloud.cn/db3e96f05553426a8f2316cbf781590c_image.png)\n\n**Kumar Gaurav Sharma** is a Senior Software Development Engineer at Amazon Web Services. He is passionate about building large-scale, distributed, performant systems.\n\n![image.png](https://dev-media.amazoncloud.cn/4c88792f2d8247138fb7e4d3aeccecfd_image.png)\n\n**Shirish Kulkarni** is an In-Memory DB Specialist Solution Architect for the APAC region and is based in Sydney, Australia. He is passionate about distributed technologies and NoSQL databases. In his free time, you can see him playing with his twin kids.\n\n![image.png](https://dev-media.amazoncloud.cn/be3c80cfce22452da1ec66b6b4f70a5d_image.png)\n\n**Karthik Konaparthi** is a Senior Product Manager on the Amazon In-Memory Databases team and is based in Seattle, WA. He is passionate about all things data and spends his time working with customers to understand their requirements and building exceptional products. In his spare time, he enjoys traveling to new places and spending time with his family.","render":"<p>More and more organizations are building their applications using microservices for operational efficiency, agility, scalability, and faster time to market. Microservices and containers have emerged as building blocks for modern applications, and Kubernetes has become the de facto standard for managing containers at scale. Applications running on Kubernetes need a database that provides ultra-fast performance, high availability and durability, security and manageability. Provisioning and integrating databases with containerized applications is often complex. Customers who are using Kubernetes to automatically deploy, scale, and manage their containerized applications are looking to do the same for their database.</p>\n<p>In August 2021, we launched <a href=\\"https://aws.amazon.com/memorydb/\\" target=\\"_blank\\">Amazon MemoryDB for Redis</a>, which is purpose-built for modern applications with microservices architectures. MemoryDB is ideal for applications that need high performance, security, availability, durability, and scalability. Recently, we have launched a developer preview of the MemoryDB service controller for <a href=\\"https://aws-controllers-k8s.github.io/community/docs/community/overview/\\" target=\\"_blank\\">AWS Controllers for Kubernetes</a> (ACK), which helps developers define and use MemoryDB resources directly from a Kubernetes cluster.</p>\\n<p>In this blog post, we first explain why MemoryDB is the right database for microservices applications. We then walk through deploying a sample gaming leaderboard application into a Kubernetes cluster provided by <a href=\\"https://aws.amazon.com/eks/\\" target=\\"_blank\\">Amazon Elastic Kubernetes Service</a> ([Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail)). We use MemoryDB as the database and ACK to create and manage a MemoryDB cluster using native Kubernetes APIs.</p>\\n<h3><a id=\\"Why_use_Amazon_MemoryDB_for_microservices_6\\"></a><strong>Why use Amazon MemoryDB for microservices</strong></h3>\\n<p>Modern applications with microservices architecture require massive scale, low read and write latencies, and high throughput performance. Developers like building microservices applications with Redis since it provides performance and scale, and because it provides a number of built-in data structures and commands that simplify performing common application tasks. As these developers deploy more business-critical applications using microservices architectures, they look for their database layer to provide not only high performance but also high durability. Developers often use <a href=\\"https://aws.amazon.com/elasticache/redis/\\" target=\\"_blank\\">Amazon ElastiCache for Redis</a>, a fully managed in-memory caching service, as a low latency cache alongside a durable AWS database service such as <a href=\\"https://aws.amazon.com/rds/aurora/\\" target=\\"_blank\\">Amazon Aurora</a> or <a href=\\"https://aws.amazon.com/dynamodb/\\" target=\\"_blank\\">Amazon DynamoDB</a>. However, this requires custom code to manage data between two independent data stores and additional costs for running two services. Developers want to build durable applications using Redis with less application complexity.</p>\\n<p>Amazon MemoryDB is a Redis-compatible, durable, in-memory database that makes it simple to build modern applications that require microsecond read and single-digit millisecond write performance, data durability, and high availability. MemoryDB durably stores data using a Multi-AZ transactional log, enabling fast recovery in the face of a failure event. MemoryDB is Redis-compatible, and customers can build applications quickly using Redis’s flexible and friendly data structures and APIs. Instead of using a low latency cache with a durable database, customers can now simplify their architecture and use MemoryDB as a single, primary database for the most demanding, business-critical applications that require low latency, high throughput, and durability.</p>\n<h4><a id=\\"Traditional_approach_to_building_microservices_11\\"></a><strong>Traditional approach to building microservices</strong></h4>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/7787de3b60f54869b7b0c219e35b362b_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"Modernsimpler_approach_to_building_microservices_15\\"></a><strong>Modern/simpler approach to building microservices</strong></h4>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/09044dea8100467eb591fb9610bf8228_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"ACK_for_MemoryDB_19\\"></a><strong>ACK for MemoryDB</strong></h4>\\n<p>ACK is a collection of Kubernetes custom resource definitions (CRDs) and custom controllers working together to extend the Kubernetes API and manage AWS resources on your behalf. ACK for MemoryDB enables you to provision and manage MemoryDB clusters as part of your Kubernetes applications using your normal infrastructure management workflows.</p>\n<p>ACK can be installed on Kubernetes clusters using Helm Charts (available on <a href=\\"https://gallery.ecr.aws/aws-controllers-k8s/memorydb-chart\\" target=\\"_blank\\">Amazon ECR</a>). Once installed, the MemoryDB cluster can be created as a Kubernetes custom resource using native Kubernetes tooling (example: kubectl).</p>\\n<p>The following diagram provides an overview of installing the ACK MemoryDB controller using Helm Charts under the “ack-system” namespace on a Kubernetes cluster. It results in the ACK MemoryDB controller running inside a pod on a Kubernetes worker node and MemoryDB custom resources definitions for the following resources:</p>\n<ol>\\n<li>cluster</li>\n<li>user</li>\n<li>acl</li>\n<li>parametergroup</li>\n<li>subnetgroup</li>\n<li>snapshot</li>\n</ol>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/842e649f342446be8a2da70eda651e5c_image.png\\" alt=\\"image.png\\" /></p>\n<h4><a id=\\"Define_and_use_MemoryDB_resources_directly_from_Kubernetes_36\\"></a><strong>Define and use MemoryDB resources directly from Kubernetes</strong></h4>\\n<p>The ACK MemoryDB controller enables application developers to create a namespaced custom resource (MemoryDB cluster, in this case) in any of their Kubernetes clusters. The MemoryDB service controller installed by cluster admins can create, update, or delete MemoryDB resources based on the intent found in the custom resource.</p>\n<p>In the following diagram, the application manifest is applied to the Kubernetes cluster. It contains specifications for a MemoryDB cluster, a Kubernetes ConfigMap, and a microservice application. It results in the ACK MemoryDB controller creating the MemoryDB cluster. The MemoryDB cluster endpoint details are available to the microservice application as container environment variables via the Kubernetes config map.</p>\n<p>The containerized microservice app on Kubernetes then connects to the MemoryDB Cluster.</p>\n<p><img src=\\"https://dev-media.amazoncloud.cn/231a96397cec4739b4bb4d0c297fd553_image.png\\" alt=\\"image.png\\" /></p>\n<h3><a id=\\"Example_gaming_leaderboard_application_using_MemoryDB_46\\"></a><strong>Example gaming leaderboard application using MemoryDB</strong></h3>\\n<p>Let’s walk you through an example gaming leaderboard that uses MemoryDB.</p>\n<h4><a id=\\"Prerequisites_49\\"></a><strong>Prerequisites</strong></h4>\\n<p>For a detailed tutorial on installing ACK MemoryDB controller on your Kubernetes cluster, review the <a href=\\"https://aws-controllers-k8s.github.io/community/docs/tutorials/memorydb-example/\\" target=\\"_blank\\">documentation at AWS Controllers K8s GitHub</a>.</p>\\n<p>This example assumes that you have:</p>\n<ol>\\n<li>\\n<p>Installed the following tools on the client machine used to access your Kubernetes cluster:<br />\\na. <a href=\\"https://docs.aws.amazon.com/cli/latest/userguide/install-cliv1.html\\" target=\\"_blank\\">AWS CLI</a> – A command line tool for interacting with AWS services.<br />\\nb. <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/install-kubectl.html\\" target=\\"_blank\\">kubectl</a> – A command line tool for working with Kubernetes clusters.<br />\\nc. <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/eksctl.html\\" target=\\"_blank\\">eksctl</a> – A command line tool for working with EKS clusters.<br />\\nd. <a href=\\"https://helm.sh/docs/intro/install/\\" target=\\"_blank\\">Helm 3.7+</a> – A tool for installing and managing Kubernetes applications.</p>\\n</li>\n<li>\\n<p>AWS Identity and Access Management (IAM) permissions to create roles and attach policies to roles.</p>\n</li>\\n</ol>\n<h4><a id=\\"Setting_up_Amazon_EKS_cluster_63\\"></a><strong>Setting up Amazon EKS cluster</strong></h4>\\n<p>We will be using <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html\\" target=\\"_blank\\">eksctl</a> to create a Kubernetes cluster with [Amazon EKS](https://aws.amazon.com/cn/eks/?trk=cndc-detail). Here we create the cluster with Linux-managed nodes. Please refer to <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/getting-started-eksctl.html\\" target=\\"_blank\\">Getting started with Amazon EKS – eksctl</a> for detailed steps.</p>\\n<pre><code class=\\"lang-\\">EKS_CLUSTER_NAME=&quot;&lt;your cluster name&gt;&quot;\\nREGION=&quot;&lt;your region&gt;&quot;\\n\\neksctl create cluster \\\\\\n --name &quot;\${EKS_CLUSTER_NAME}&quot; \\\\\\n --region &quot;\${REGION}&quot; \\\\\\n --with-oidc\\n</code></pre>\\n<p>When the EKS cluster is available, set up <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/managing-vpc-cni.html\\" target=\\"_blank\\">the Amazon VPC Container Network Interface (CNI) plugin for Kubernetes</a> for the EKS cluster.</p>\\n<h4><a id=\\"Install_ACK_MemoryDB_controller_78\\"></a><strong>Install ACK MemoryDB controller</strong></h4>\\n<p>You can deploy the ACK service controller for MemoryDB using the <a href=\\"https://gallery.ecr.aws/aws-controllers-k8s/memorydb-chart\\" target=\\"_blank\\">memorydb-chart Helm Chart</a>. You can download it to your workspace using the following command:</p>\\n<pre><code class=\\"lang-\\">helm pull oci://public.ecr.aws/aws-controllers-k8s/memorydb-chart --version=v0.0.1\\n</code></pre>\\n<p>You will need to decompress and extract the Helm Chart. You can do so with the following command:</p>\n<pre><code class=\\"lang-\\">tar xzvf memorydb-chart-v0.0.1.tgz\\n</code></pre>\\n<p>You can now use the Helm Chart to deploy the ACK service controller for MemoryDB to your EKS cluster. At a minimum, you need to specify the AWS Region to execute the MemoryDB API calls.</p>\n<p>For example, to specify that the MemoryDB API calls go to the US East (N. Virginia) region, you can deploy the service controller with the following command:</p>\n<pre><code class=\\"lang-\\">helm install memorydb-chart --generate-name --set=aws.region=us-east-1\\n</code></pre>\\n<p>For a full list of available values for the Helm Chart, please review the <a href=\\"https://github.com/aws-controllers-k8s/memorydb-controller/blob/main/helm/values.yaml\\" target=\\"_blank\\">values.yaml</a> file.</p>\\n<h4><a id=\\"Configure_IAM_permissions_98\\"></a><strong>Configure IAM permissions</strong></h4>\\n<p>Once the service controller is deployed, you will need to configure the <a href=\\"https://aws-controllers-k8s.github.io/community/docs/user-docs/irsa/\\" target=\\"_blank\\">IAM permissions</a> for the controller to query the MemoryDB API. For full details, please review the AWS Controllers for Kubernetes documentation for <a href=\\"https://aws-controllers-k8s.github.io/community/docs/user-docs/irsa/\\" target=\\"_blank\\">how to configure the IAM permissions</a>. If you follow the examples in the documentation, use the value of MemoryDB for SERVICE.</p>\\n<h4><a id=\\"Create_MemoryDB_cluster_101\\"></a><strong>Create MemoryDB cluster</strong></h4>\\n<p>You can create MemoryDB clusters using the cluster custom resource. The examples below show how to create it from your Kubernetes environment. For a full list of options available in the cluster custom resource definition, you can use the <code>kubectl explain cluster</code> command.</p>\\n<p>This example creates the MemoryDB cluster in the same VPC as the EKS cluster using the same subnets and security groups. It creates a MemoryDB SubnetGroup custom resource and then uses it to create a MemoryDB cluster.</p>\n<p>The following example creates MemoryDB subnet group:</p>\n<pre><code class=\\"lang-\\">REGION=&quot;&lt;your region&gt;&quot;\\nEKS_CLUSTER_NAME=&quot;&lt;your EKS cluster name&gt;&quot;\\nEKS_VPC_ID=\$(aws –region \$REGION eks describe-cluster –name \$EKS_CLUSTER_NAME –query cluster.resourcesVpcConfig.vpcId)\\nEKS_SUBNET_IDS=\$(aws –region \$REGION ec2 describe-subnets \\\\\\n --filters &quot;Name=vpc-id,Values=\${EKS_VPC_ID}&quot; \\\\\\n --query 'Subnets[*].SubnetId' \\\\\\n --output text\\n)\\n\\nMEMORYDB_SUBNETGROUP_NAME=&quot;&lt;your subnet group name&gt;&quot;\\n\\ncat &lt;&lt;EOF &gt; memorydb-subnetgroup.yaml\\napiVersion: memorydb.services.k8s.aws/v1alpha1\\nkind: SubnetGroup\\nmetadata:\\n name: &quot;\${MEMORYDB_SUBNETGROUP_NAME}&quot;\\nspec:\\n name: &quot;\${MEMORYDB_SUBNETGROUP_NAME}&quot;\\n description: &quot;MemoryDB cluster subnet group&quot;\\n subnetIDs:\\n\$(printf &quot; - %s\\\\n&quot; \${EKS_SUBNET_IDS})\\n\\nEOF\\n\\nkubectl apply -f memorydb-subnetgroup.yaml\\nkubectl describe subnetgroup &quot;\${MEMORYDB_SUBNETGROUP_NAME}&quot;\\n</code></pre>\\n<p>If you observe that the <code>ACK.Terminal</code> condition is set for the <code>SubnetGroup</code>, and the error is similar to the following:</p>\\n<pre><code class=\\"lang-\\">Status:\\n Conditions:\\n Message: SubnetNotAllowedFault: Subnets: [subnet-1d111111, subnet-27d22222] are not in a supported availability zone. Supported availability zones are [us-east-1c, us-east-1d, us-east-1b].\\n Status: True\\n Type: ACK.Terminal\\n</code></pre>\\n<p>Then update the subnetIDs in the input YAML and provide the subnet IDs that are in a supported Availability Zone.</p>\n<p>The following example creates a MemoryDB cluster. It uses the db.t4g.small node type for the MemoryDB cluster. Please review the <a href=\\"https://docs.aws.amazon.com/memorydb/latest/devguide/nodes.supportedtypes.html\\" target=\\"_blank\\">MemoryDB node types</a> to select the most appropriate one for your workload.</p>\\n<pre><code class=\\"lang-\\">REGION=&quot;&lt;your region&gt;&quot;\\nMEMORYDB_CLUSTER_NAME=&quot;&lt;your cluster name&gt;&quot;\\nSECURITY_GROUP_ID=\$(aws –region \$REGION eks describe-cluster –name \$EKS_CLUSTER_NAME –query cluster.resourcesVpcConfig.clusterSecurityGroupId)\\n\\ncat &lt;&lt;EOF &gt; memorydb-cluster.yaml\\napiVersion: memorydb.services.k8s.aws/v1alpha1\\nkind: Cluster\\nmetadata:\\n name: &quot;\${MEMORYDB_CLUSTER_NAME}&quot;\\nspec:\\n name: &quot;\${MEMORYDB_CLUSTER_NAME}&quot;\\n nodeType: db.t4g.small\\n aclName: open-access\\n securityGroupIDs:\\n - \${SECURITY_GROUP_ID}\\n subnetGroupName: \${MEMORYDB_SUBNETGROUP_NAME}\\nEOF\\n\\nkubectl apply -f memorydb-cluster.yaml\\nkubectl describe cluster &quot;\${MEMORYDB_CLUSTER_NAME}&quot;\\n</code></pre>\\n<p>You can track the status of the provisioned database using <code>kubectl describe</code> on the Cluster custom resource:</p>\\n<pre><code class=\\"lang-\\">kubectl describe cluster &quot;\${MEMORYDB_CLUSTER_NAME}&quot;\\n</code></pre>\\n<p>When the <code>Cluster Status</code> says <code>available</code>, you can connect to the database instance.</p>\\n<h4><a id=\\"Connect_gaming_leaderboard_application_to_MemoryDB_cluster_175\\"></a><strong>Connect gaming leaderboard application to MemoryDB cluster</strong></h4>\\n<p>The <code>Cluster status</code> contains the information for connecting to a MemoryDB cluster. The host information can be found in <code>status.clusterEndpoint.address</code>, and the port information can be found in <code>status.clusterEndpoint.port</code>. For example, you can get the connection information for a cluster created in one of the previous examples using the following commands:</p>\\n<pre><code class=\\"lang-\\">kubectl get cluster &quot;\${MEMORYDB_CLUSTER_NAME}&quot; -o jsonpath='{.status.clusterEndpoint.address}'\\nkubectl get cluster &quot;\${MEMORYDB_CLUSTER_NAME}&quot; -o jsonpath='{.status.clusterEndpoint.port}'\\n</code></pre>\\n<h4><a id=\\"Set_up_ConfigMap_for_MemoryDB_cluster_endpoint_information_183\\"></a><strong>Set up ConfigMap for MemoryDB cluster endpoint information</strong></h4>\\n<p>The MemoryDB cluster endpoint information can be made available to application pods via <code>ConfigMap</code> using a <code>FieldExport</code> resource. The following example makes the MemoryDB cluster endpoint and port available as ConfigMap data:</p>\\n<pre><code class=\\"lang-\\">MEMORYDB_CLUSTER_NAME=&quot;&lt;your cluster name&gt;&quot;\\nMEMORYDB_CLUSTER_CONN_CM=&quot;\${MEMORYDB_CLUSTER_NAME}-conn-cm&quot;\\n\\ncat &lt;&lt;EOF &gt; memorydb-field-exports.yaml\\n---\\napiVersion: v1\\nkind: ConfigMap\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\ndata: {}\\n---\\napiVersion: services.k8s.aws/v1alpha1\\nkind: FieldExport\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_NAME}-host\\nspec:\\n to:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n kind: configmap\\n from:\\n path: &quot;.status.clusterEndpoint.address&quot;\\n resource:\\n group: memorydb.services.k8s.aws\\n kind: Cluster\\n name: \${MEMORYDB_CLUSTER_NAME}\\n---\\napiVersion: services.k8s.aws/v1alpha1\\nkind: FieldExport\\nmetadata:\\n name: \${MEMORYDB_CLUSTER_NAME}-port\\nspec:\\n to:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n kind: configmap\\n from:\\n path: &quot;.status.clusterEndpoint.port&quot;\\n resource:\\n group: memorydb.services.k8s.aws\\n kind: Cluster\\n name: \${MEMORYDB_CLUSTER_NAME}\\nEOF\\n\\nkubectl apply -f memorydb-field-exports.yaml\\n</code></pre>\\n<p>Confirm that the MemoryDB endpoint details are available in the config map by running the following command.</p>\n<pre><code class=\\"lang-\\">kubectl get configmap/\${MEMORYDB_CLUSTER_CONN_CM} -o jsonpath='{.data}'\\n</code></pre>\\n<h4><a id=\\"Deploy_container_example_and_access_MemoryDB_cluster_endpoint_236\\"></a><strong>Deploy container example and access MemoryDB cluster endpoint</strong></h4>\\n<p>The MemoryDB cluster endpoint details (Address, Port) can be injected into a container either as environmental variables or files. For example, here is a snippet of a deployment definition that will add the MemoryDB cluster connection info into an example leaderboard app:</p>\n<pre><code class=\\"lang-\\">cat &lt;&lt;EOF &gt; game_leaderboard.yaml\\napiVersion: apps/v1\\nkind: Deployment\\nmetadata:\\n name: leaderboard-deployment\\n labels:\\n app: leaderboard\\nspec:\\n replicas: 1\\n selector:\\n matchLabels:\\n app: leaderboard\\n template:\\n metadata:\\n labels:\\n app: leaderboard\\n spec:\\n containers:\\n - name: leaderboard\\n image: public.ecr.aws/sam/build-python3.8:latest\\n tty: true\\n stdin: true\\n env:\\n - name: MEMORYDB_CLUSTER_HOST\\n valueFrom:\\n configMapKeyRef:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n key: &quot;\${MEMORYDB_CLUSTER_NAME}-host&quot;\\n - name: MEMORYDB_CLUSTER_PORT\\n valueFrom:\\n configMapKeyRef:\\n name: \${MEMORYDB_CLUSTER_CONN_CM}\\n key: &quot;\${MEMORYDB_CLUSTER_NAME}-port&quot;\\nEOF\\n\\nkubectl apply -f game_leaderboard.yaml\\n</code></pre>\\n<p>Confirm that the leaderboard application container has been deployed successfully by running the following:</p>\n<pre><code class=\\"lang-\\">kubectl get pods –selector=app=leaderboard\\n</code></pre>\\n<p>Verify that the Pod Status is “Running.”</p>\n<p>Get a shell to the running leaderboard container.</p>\n<pre><code class=\\"lang-\\">LEADERBOARD_POD_NAME=\$(kubectl get pods –selector=app=leaderboard -o jsonpath='{.items[*].metadata.name}')\\nkubectl exec –stdin –tty \${LEADERBOARD_POD_NAME} -- /bin/bash\\n</code></pre>\\n<p>In the running leaderboard container shell, run the following commands and confirm that the MemoryDB cluster host and port are available as environment variables.</p>\n<pre><code class=\\"lang-\\"># Confirm that the memorydb cluster host, port are available as environment variables\\necho \$MEMORYDB_CLUSTER_HOST\\necho \$MEMORYDB_CLUSTER_PORT\\n</code></pre>\\n<h4><a id=\\"Add_gaming_leaderboard_scripts_to_running_application_container_293\\"></a><strong>Add gaming leaderboard scripts to running application container</strong></h4>\\n<p>For the steps in this section, execute the commands from the shell running in the leaderboard container.</p>\n<p>Install dependencies for the example leaderboard scripts.</p>\n<pre><code class=\\"lang-\\">pip install redis\\n</code></pre>\\n<p>Create example leaderboard scripts:</p>\n<p>This script connects to the MemoryDB cluster by discovering its endpoint address and port from the container environment variables.</p>\n<ol>\\n<li><code>game_latest_scores.py</code> – This script writes example scores for players into the MemoryDB cluster using Redis API.</li>\\n<li><code>Game_latest_scores.py</code> – This script reads top-ranked players by highest scores for the leaderboard from the MemoryDB cluster using Redis API.</li>\\n</ol>\n<p>Create a directory to place leaderboard scripts:</p>\n<pre><code class=\\"lang-\\">mkdir leaderboard\\ncd leaderboard\\n</code></pre>\\n<p>Create <code>game_latest_scores.py</code> script:</p>\\n<pre><code class=\\"lang-\\">cat &lt;&lt;EOF &gt; game_latest_scores.py\\nimport redis\\nfrom random import randrange\\nimport os\\n\\nmemorydb_cluster_endpoint_address = os.environ['MEMORYDB_CLUSTER_HOST']\\nmemorydb_cluster_endpoint_port = int(os.environ['MEMORYDB_CLUSTER_PORT'])\\nmemorydb = redis.Redis(host=memorydb_cluster_endpoint_address,\\n port=memorydb_cluster_endpoint_port, db=0)\\n\\ndef write_latest_scores():\\n for i in range(1000):\\n memorydb.zadd(&quot;leaderboard&quot;, {f&quot;Player-{i}&quot;: randrange(1000)})\\n\\nif __name__ == &quot;__main__&quot;:\\n write_latest_scores()\\nEOF\\n</code></pre>\\n<p>Create <code>game_leaderboard.py</code> script:</p>\\n<pre><code class=\\"lang-\\">cat &lt;&lt;EOF &gt; game_leaderboard.py\\nimport redis\\nimport os\\n\\nmemorydb_cluster_endpoint_address = os.environ['MEMORYDB_CLUSTER_HOST']\\nmemorydb_cluster_endpoint_port = int(os.environ['MEMORYDB_CLUSTER_PORT'])\\nmemorydb = redis.Redis(host=memorydb_cluster_endpoint_address,\\n port=memorydb_cluster_endpoint_port, db=0)\\n\\ndef print_header():\\n separator = ''.join([char * 30 for char in '-'])\\n columns = f&quot;{'Rank': &gt;{4}}\\\\t{'Player': &lt;{15}}{'Score': &gt;{6}}&quot;\\n header = f&quot;{separator}\\\\n{columns}\\\\n{separator}&quot;\\n print(header)\\n\\ndef print_leaderboard():\\n print_header()\\n leaders = memorydb.zrevrange(&quot;leaderboard&quot;, 0, 10, &quot;WITHSCORES&quot;)\\n\\n rank = 0\\n for player_score in leaders:\\n player = player_score[0].decode(&quot;utf-8&quot;)\\n score = player_score[1]\\n rank = rank + 1\\n print(f&quot;#{rank: &gt;{3}}\\\\t{player: &lt;{15}} {score: &gt;{6}}&quot;)\\n\\nif __name__ == &quot;__main__&quot;:\\n print_leaderboard()\\nEOF\\n</code></pre>\\n<h4><a id=\\"Run_gaming_leaderboard_application_367\\"></a><strong>Run gaming leaderboard application</strong></h4>\\n<p>Load game scores into the MemoryDB cluster by running <code>game_latest_scores.py</code> script:</p>\\n<pre><code class=\\"lang-\\"># load the latest game scores into memorydb cluster\\npython game_latest_scores.py\\n</code></pre>\\n<p>Note: If you are unable to connect to the MemoryDB cluster from the Kubernetes pod, then ensure that the <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/managing-vpc-cni.html\\" target=\\"_blank\\">Amazon VPC Container Network Interface (CNI) plugin for Kubernetes</a> has been set up for the EKS cluster, and also review <a href=\\"https://docs.aws.amazon.com/memorydb/latest/devguide/memorydb-vpc-accessing.html\\" target=\\"_blank\\">Access Patterns for Accessing a MemoryDB Cluster in an Amazon VPC</a>, based upon the MemoryDB cluster VPC subnet groups and security groups.</p>\\n<p>Get the latest leaderboard printed on the console by running <code>game_leaderboard.py</code> script:</p>\\n<pre><code class=\\"lang-\\"># get leaderboard with players ranked per their game score from MemoryDB cluster\\npython game_leaderboard.py\\n</code></pre>\\n<p>Observe an output similar to the following list:</p>\n<pre><code class=\\"lang-\\">-----------------------------\\nRank Player Score\\n------------------------------\\n# 1 Player-667 998.0\\n# 2 Player-51 997.0\\n# 3 Player-186 997.0\\n# 4 Player-674 996.0\\n# 5 Player-150 995.0\\n# 6 Player-102 993.0\\n# 7 Player-813 991.0\\n# 8 Player-227 991.0\\n# 9 Player-455 990.0\\n# 10 Player-260 989.0\\n# 11 Player-233 986.0\\n</code></pre>\\n<p>Exit the shell running in the leaderboard container:</p>\n<pre><code class=\\"lang-\\">exit\\n</code></pre>\\n<h3><a id=\\"Cleanup_406\\"></a><strong>Cleanup</strong></h3>\\n<ul>\\n<li>To delete the sample gaming leaderboard application, use the following command:</li>\n</ul>\\n<pre><code class=\\"lang-\\">kubectl delete -f game_leaderboard.yaml\\n</code></pre>\\n<ul>\\n<li>To delete the ConfigMap and FieldExport, use the following command:</li>\n</ul>\\n<pre><code class=\\"lang-\\">kubectl delete -f memorydb-field-exports.yaml\\n</code></pre>\\n<ul>\\n<li>To delete the MemoryDB cluster subnet group, use the following command:</li>\n</ul>\\n<pre><code class=\\"lang-\\">kubectl delete -f memorydb-subnetgroup.yaml\\n</code></pre>\\n<ul>\\n<li>To delete the MemoryDB cluster, use the following command:</li>\n</ul>\\n<pre><code class=\\"lang-\\">kubectl delete -f memorydb-cluster.yaml\\n</code></pre>\\n<ul>\\n<li>To uninstall the ACK service controller for MemoryDB, refer to <a href=\\"https://aws-controllers-k8s.github.io/community/docs/user-docs/cleanup/\\" target=\\"_blank\\">Uninstall an ACK Controller</a>. Following the example, set the value of <code>SERVICE</code> to <code>memorydb</code>.</li>\\n<li>To delete your Amazon EKS cluster, refer to <a href=\\"https://docs.aws.amazon.com/eks/latest/userguide/delete-cluster.html\\" target=\\"_blank\\">Deleting an Amazon EKS cluster</a>.</li>\\n</ul>\n<pre><code class=\\"lang-\\">eksctl delete cluster \\\\\\n --name &quot;\${EKS_CLUSTER_NAME}&quot; \\\\\\n --region &quot;\${REGION}&quot;\\n</code></pre>\\n<h3><a id=\\"Conclusion_434\\"></a><strong>Conclusion</strong></h3>\\n<p>We demonstrated how ACK lets you deploy a MemoryDB cluster directly from your Amazon EKS environment and connect a sample gaming leaderboard application to it. To learn more, visit the <a href=\\"https://aws.amazon.com/memorydb/features/\\" target=\\"_blank\\">Amazon Memory DB for Redis Features page</a> and the <a href=\\"https://aws-controllers-k8s.github.io/community/docs/tutorials/memorydb-example/\\" target=\\"_blank\\">AWS Controllers K8s GitHub documentation</a>. Let us know your feedback. ACK is open source; you can request new features and report issues on the <a href=\\"https://github.com/aws-controllers-k8s/community\\" target=\\"_blank\\">ACK community GitHub repository</a>.</p>\\n<h4><a id=\\"About_the_Authors_437\\"></a><strong>About the Authors</strong></h4>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/db3e96f05553426a8f2316cbf781590c_image.png\\" alt=\\"image.png\\" /></p>\n<p><strong>Kumar Gaurav Sharma</strong> is a Senior Software Development Engineer at Amazon Web Services. He is passionate about building large-scale, distributed, performant systems.</p>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/4c88792f2d8247138fb7e4d3aeccecfd_image.png\\" alt=\\"image.png\\" /></p>\n<p><strong>Shirish Kulkarni</strong> is an In-Memory DB Specialist Solution Architect for the APAC region and is based in Sydney, Australia. He is passionate about distributed technologies and NoSQL databases. In his free time, you can see him playing with his twin kids.</p>\\n<p><img src=\\"https://dev-media.amazoncloud.cn/be3c80cfce22452da1ec66b6b4f70a5d_image.png\\" alt=\\"image.png\\" /></p>\n<p><strong>Karthik Konaparthi</strong> is a Senior Product Manager on the Amazon In-Memory Databases team and is based in Seattle, WA. He is passionate about all things data and spends his time working with customers to understand their requirements and building exceptional products. In his spare time, he enjoys traveling to new places and spending time with his family.</p>\n"}
目录
亚马逊云科技解决方案 基于行业客户应用场景及技术领域的解决方案
联系亚马逊云科技专家
亚马逊云科技解决方案
基于行业客户应用场景及技术领域的解决方案
联系专家
0
目录
关闭