练习 - 在 Azure Kubernetes 服务群集上部署应用程序
在本练习中,你将将公司的后端消息传送服务作为测试应用部署到 Azure Kubernetes 服务(AKS)。 该服务连接到在上一练习中创建的 Redis PaaS 服务。
注释
该服务的代码在 GitHub 存储库中可用。
在 Redis 中创建列表
需要在 Redis 中创建一个列表,并使用一些随机元素填充该列表,以模拟队列接收数据。 队列中的每个项都表示微服务将处理的内容。 在本练习中,你将添加静态数量的项。 稍后在练习中,你将将微服务缩放为队列中的项数。
确保 Docker 在计算机上运行。
使用
docker run以下命令在本地创建 Redis 容器以连接到 Azure Redis 缓存:docker run -it --rm redis redis-cli -h $REDIS_HOST -a $REDIS_KEY输出应类似于以下示例输出:
redis-contoso-video.redis.cache.windows.net:6379>使用
lpush keda命令创建列表并使用随机元素填充该列表:lpush keda Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris eget interdum felis, ac ultricies nulla. Fusce vehicula mattis laoreet. Quisque facilisis bibendum dui, at scelerisque nulla hendrerit sed. Sed rutrum augue arcu, id maximus felis sollicitudin eget. Curabitur non libero rhoncus, pellentesque orci a, tincidunt sapien. Suspendisse laoreet vulputate sagittis. Vivamus ac magna lacus. Etiam sagittis facilisis dictum. Phasellus faucibus sagittis libero, ac semper lorem commodo in. Quisque tortor lorem, sollicitudin non odio sit amet, finibus molestie eros. Proin aliquam laoreet eros, sed dapibus tortor euismod quis. Maecenas sed viverra sem, at porta sapien. Sed sollicitudin arcu leo, vitae elementum使用
llen keda命令验证列表的长度:llen keda通过键入
exit来退出 Redis shell。
创建部署清单
创建部署清单文件来部署应用程序。 清单文件允许定义要部署的资源类型以及与工作负荷关联的详细信息。
Kubernetes 将容器分组为逻辑结构(称为 Pod),这些逻辑结构没有智能。 部署会添加缺少的智能,以创建应用程序。
在 Cloud Shell 中,使用
deployment.yaml命令为名为touch的 Kubernetes 部署创建清单文件。touch deployment.yaml输入
code .,在 Cloud Shell 中打开集成编辑器deployment.yaml打开该文件并粘贴以下清单代码。 请确保将 Redis 环境变量替换为自己的值。apiVersion: apps/v1 kind: Deployment metadata: name: contoso-microservice spec: replicas: 1 # Tells K8S the number of containers to process the Redis list items selector: # Define the wrapping strategy matchLabels: # Match all pods with the defined labels app: contoso-microservice # Labels follow the `name: value` template template: # Template of the pod inside the Deployment metadata: labels: app: contoso-microservice spec: containers: - image: mcr.microsoft.com/mslearn/samples/redis-client:latest name: contoso-microservice resources: requests: cpu: 100m memory: 128Mi limits: cpu: 100m memory: 128Mi env: - name: REDIS_HOST value: "redis-contoso-video.redis.cache.windows.net" # *** REPLACE with your value *** - name: REDIS_PORT value: "6379" # *** REPLACE with your value *** - name: REDIS_LIST value: "keda" # *** REPLACE with your value *** - name: REDIS_KEY value: "******************************************" # *** REPLACE with your value ***保存清单文件(CTRL + S),并关闭编辑器(CTRL + Q)。
应用清单
使用
kubectl apply以下命令将清单部署到群集:kubectl apply -f ./deployment.yaml输出应类似于以下示例输出:
deployment.apps/contoso-microservice created使用
kubectl get deployment以下命令验证部署是否成功:kubectl get deployment contoso-microservice输出应类似于以下示例输出:
NAME READY UP-TO-DATE AVAILABLE AGE contoso-microservice 1/1 1 0 16s使用
kubectl get pods以下命令验证 Pod 是否正在运行:kubectl get pods输出应类似于以下示例输出:
NAME READY STATUS RESTARTS AGE contoso-microservice-7c58c5f699-r79mv 1/1 Running 0 63s