练习 - 在 Azure Kubernetes 服务群集上部署应用程序

已完成

在本练习中,你要将公司的后端消息服务作为测试应用部署到 Azure Kubernetes 服务 (AKS) 上。 该服务连接到在上一练习中创建的 Redis PaaS 服务。

注意

服务的代码在 GitHub 存储库中提供。

在 Redis 中创建列表

需要在 Redis 中创建列表,并使用一些随机元素填充该列表以模拟接收数据的队列。 队列中的每个项都表示微服务将处理的内容。 对于本练习,你将添加静态项目数。 稍后在练习中,你将微服务缩放为队列中的项目数。

  1. 确保 Docker 在计算机上运行。

  2. 使用 docker run 命令在本地创建 Redis 容器,以连接到 Azure Cache for Redis:

    docker run -it --rm redis redis-cli -h $REDIS_HOST -a $REDIS_KEY
    

    输出应类似于以下示例输出:

    redis-contoso-video.redis.cache.windows.net:6379>
    
  3. 使用 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
    
  4. 使用 llen keda 命令验证列表的长度:

    llen keda
    
  5. 通过键入 exit 来退出 Redis shell。

创建部署清单

创建部署清单文件以部署应用程序。 使用清单文件可以定义要部署的资源类型以及与工作负载相关的详细信息。

Kubernetes 将容器分组为逻辑结构(称为 Pod),这些逻辑结构没有智能。 部署会添加缺少的智能,以创建应用程序。

  1. 在 Cloud Shell 中,使用 touch 命令为名为 deployment.yaml 的 Kubernetes 部署创建一个清单文件:

    touch deployment.yaml
    
  2. 输入 code .,在 Cloud Shell 中打开集成编辑器

  3. 打开 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 ***
    
  4. 保存清单文件 (CTRL + S),并关闭编辑器 (CTRL + Q)。

应用清单

  1. 使用 kubectl apply 命令将清单部署到群集:

    kubectl apply -f ./deployment.yaml
    

    输出应类似于以下示例输出:

    deployment.apps/contoso-microservice created
    
  2. 使用 kubectl get deployment 命令验证部署是否成功:

    kubectl get deployment contoso-microservice
    

    输出应类似于以下示例输出:

    NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
    contoso-microservice   1/1     1            0           16s
    
  3. 使用 kubectl get pods 命令检查 Pod 是否正在运行:

    kubectl get pods
    

    输出应类似于以下示例输出:

    NAME                                    READY   STATUS    RESTARTS   AGE
    contoso-microservice-7c58c5f699-r79mv   1/1     Running   0          63s