搜索

5.3. 部署示例应用程序

download PDF
  1. 为示例应用程序创建一个新项目:

    $ oc new-project hello-world
  2. 部署 hello world 应用:

    $ oc new-app -n hello-world --image=docker.io/openshift/hello-openshift
  3. 将预先创建的服务资源转换为 NodePort 服务类型:

    $ oc -n hello-world patch service hello-openshift -p '{"spec":{"type":"NodePort"}}'
  4. 使用 AWS Load Balancer Operator 部署 AWS ALB:

    $ cat << EOF | oc apply -f -
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: hello-openshift-alb
      namespace: hello-world
      annotations:
        alb.ingress.kubernetes.io/scheme: internet-facing
    spec:
      ingressClassName: alb
      rules:
        - http:
            paths:
              - path: /
                pathType: Exact
                backend:
                  service:
                    name: hello-openshift
                    port:
                      number: 8080
    EOF
  5. curl AWS ALB Ingress 端点,以验证 hello world 应用程序是否可访问:

    注意

    AWS ALB 置备需要几分钟。如果您收到显示 curl: (6) Could not resolve host 的错误,请等待再试一次。

    $ INGRESS=$(oc -n hello-world get ingress hello-openshift-alb -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
    $ curl "http://${INGRESS}"

    输出示例

    Hello OpenShift!

5.3.1. 配置 AWS WAF

AWS WAF 服务是一个 web 应用程序防火墙,可让您监控、保护和控制转发到受保护的 web 应用程序资源(如 ROSA)的 HTTP 和 HTTPS 请求。

  1. 创建 AWS WAF 规则文件以应用到我们的 Web ACL:

    $ cat << EOF > ${SCRATCH}/waf-rules.json
    [
        {
          "Name": "AWS-AWSManagedRulesCommonRuleSet",
          "Priority": 0,
          "Statement": {
            "ManagedRuleGroupStatement": {
              "VendorName": "AWS",
              "Name": "AWSManagedRulesCommonRuleSet"
            }
          },
          "OverrideAction": {
            "None": {}
          },
          "VisibilityConfig": {
            "SampledRequestsEnabled": true,
            "CloudWatchMetricsEnabled": true,
            "MetricName": "AWS-AWSManagedRulesCommonRuleSet"
          }
        },
        {
          "Name": "AWS-AWSManagedRulesSQLiRuleSet",
          "Priority": 1,
          "Statement": {
            "ManagedRuleGroupStatement": {
              "VendorName": "AWS",
              "Name": "AWSManagedRulesSQLiRuleSet"
            }
          },
          "OverrideAction": {
            "None": {}
          },
          "VisibilityConfig": {
            "SampledRequestsEnabled": true,
            "CloudWatchMetricsEnabled": true,
            "MetricName": "AWS-AWSManagedRulesSQLiRuleSet"
          }
        }
    ]
    EOF

    这将启用 Core (Common)和 SQL AWS Managed Rule Sets。

  2. 使用上面指定的规则创建 AWS WAF Web ACL:

    $ WAF_ARN=$(aws wafv2 create-web-acl \
      --name ${CLUSTER}-waf \
      --region ${REGION} \
      --default-action Allow={} \
      --scope REGIONAL \
      --visibility-config SampledRequestsEnabled=true,CloudWatchMetricsEnabled=true,MetricName=${CLUSTER}-waf-metrics \
      --rules file://${SCRATCH}/waf-rules.json \
      --query 'Summary.ARN' \
      --output text)
  3. 使用 AWS WAF Web ACL ARN 注解 Ingress 资源:

    $ oc annotate -n hello-world ingress.networking.k8s.io/hello-openshift-alb \
      alb.ingress.kubernetes.io/wafv2-acl-arn=${WAF_ARN}
  4. 等待 10 秒,以便规则传播并测试应用程序是否仍然可以正常工作:

    $ curl "http://${INGRESS}"

    输出示例

    Hello OpenShift!

  5. 测试 WAF 是否拒绝错误请求:

    $ curl -X POST "http://${INGRESS}" \
      -F "user='<script><alert>Hello></alert></script>'"

    输出示例

    <html>
    <head><title>403 Forbidden</title></head>
    <body>
    <center><h1>403 Forbidden</h1></center>
    </body>
    </html

    注意

    激活 AWS WAF 集成有时可能需要几分钟。如果您没有收到 403 Forbidden 错误,请等待几秒钟,然后重试。

    预期的结果是一个 403 Forbidden 错误,这意味着 AWS WAF 正在保护您的应用程序。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.