启用 NGINX 监视接口

如果要通过部署 Unified Agent 来使用 NGINX 监视,那么必须确认是否已启用 NGINX 监视接口。

过程

  • 在要部署 Unified Agent 的机器上运行以下命令:
    http://pod_ip_or_nginx_host:18080/nginx_status
    其中,pod_ip_or_nginx_host 是 NGINX 服务器的标准主机名。

    如果未返回状态,那么表明 NGINX 监视接口未启用。

    NGINX 监视接口要求装入 ngx_http_stub_status_module 模块。 此模块有助于收集基本性能指标。 IBM® Cloud Private 提供了 NGINX Docker 映像 ibmcom/nginx-ingress-controller,该映像启用了 ngx_http_stub_status 模块。 如果工作负载正在使用此映像,那么无需进行任何其他配置。 您只需要获取 Kubernetes 服务或 Pod IP,并确认管理接口已启用。 例如,http://NGINX_service_or_node_or_pod_ip:18080/nginx_status。 下面显示一些命令示例,它们可以帮助您确定 NGINX 工作负载是否正在运行,以及确认其管理接口已启用。
    注: 登录 IBM Cloud Private,以继续下一节:
    > cloudctl login -a <cluster> -u <username> 
    > kubectl get po -n kube-system -o wide |grep nginx
    
    The command returns output similar to the following
    
    nginx-ingress-controller-jx8vb      1/1     Running   0     5h   
    10.1.253.201   9.42.75.39
    
    Verify the NGINX management interface status
    
    > curl http://10.1.253.201:18080/nginx_status
    
    Active connections: 9
    server accepts handled requests
     5372 5372 22532
    Reading: 0 Writing: 2 Waiting: 7
    因为 Pod IP 地址可更改,您可以选择性地创建指向 Pod 以获取静态 IP 地址的服务。 以下命令帮助您创建服务配置:
    > kubectl describe po nginx-ingress-controller-jx8vb -n kube-system 
    
    Name:              nginx-ingress-controller-jx8vb 
    Namespace:         kube-system 
    Priority:          0 
    PriorityClassName: <none> 
    Node:              9.42.75.39/9.42.75.39 
    Start Time:        Fri, 28 Sep 2018 23:46:39 -0400 
    Labels:            app=nginx-ingress-controller
    注: 在这里,选择器为 app=nginx-ingress-controller。 在您的 IBM Cloud Private 环境中,选择器可能与此不同
    创建服务资源文件 (nginx-status.yaml),如下所示:
    {
      "apiVersion": "v1",
      "kind": "Service",
      "metadata": {
        "name": "nginx-status",
        "namespace": "kube-system",
        "labels": {
          "app": "nginx-status"
        }
      },
      "spec": {
        "ports": [
          {
            "name": "nginx-status",
            "protocol": "TCP",
            "port": 18080,
            "targetPort": 18080
          }
        ],
        "selector": {
          "app": "nginx-ingress-controller"
        },
        "type": "ClusterIP",
        "sessionAffinity": "None"
      }
    }
    注: 打开端口 18080,以访问 NGINX 状态。
    使用您之前创建的文件创建 Kubernetes 服务资源,并获取服务 IP
    > kubectl create -f nginx-status.yaml
    > kubectl describe svc nginx-status -n kube-system
    
    Name:              nginx-status
    Namespace:         kube-system
    Labels:            app=nginx-status
    Annotations:       <none>
    Selector:          name=nginx-ingress-controller
    Type:              ClusterIP
    IP:                10.0.0.243
    Port:              nginx-status  18080/TCP
    TargetPort:        18080/TCP
    Endpoints:         9.37.22.210:18080
    Session Affinity:  None
    Events:            <none>
     curl http://10.0.0.243:18080/nginx_status 

    可以对 Helm Chart 配置指定 NGINX 管理接口 URL http://10.0.0.243:18080/nginx_status。 在本地安装中,缺省情况下未启用此模块。 必须先构建此模块,然后使用配置参数 --with-http_stub_status_module 将其启用。 要进行启用,请参阅 NGINX 文档。

  • 如果要在 IBM Cloud Private 3.2.0 或 3.2.1 中监视 NGINX,那么需要执行额外步骤以确保 NGINX 监视可以成功运行。
    1. IBM Cloud Private 上查找 NGINX pod。
      # kubectl get po -n kube-system|grep nginx
      nginx-ingress-controller-ph8t6                                 1/1     Running     0          18m
    2. 导出 nginx.tmpl
      # kubectl cp kube-system/nginx-ingress-controller-ph8t6:template/nginx.tmpl nginx.tmpl
    3. 移除 nginx.tmpl 中的行 deny all
      location /nginx_status {
                  {{ if $all.Cfg.EnableOpentracing }}
                  opentracing off;
                  {{ end }}
      
                  {{ range $v := $all.NginxStatusIpv4Whitelist }}
                  allow {{ $v }};
                  {{ end }}
                  {{ if $all.IsIPV6Enabled -}}
                  {{ range $v := $all.NginxStatusIpv6Whitelist }}
                  allow {{ $v }};
                  {{ end }}
                  {{ end -}}
      ###This line should be removed , or comment out
                  deny all;
      ###End
                  access_log off;
                  stub_status on;
              }
    4. 创建 nginx 模板 configmap。
      kubectl create configmap nginx-template -n kube-system --from-file=nginx.tmpl
    5. 修改 nginx daemonset 以使用此 configmap。
      kubectl edit daemonset nginx-ingress-controller -n kube-system
      要添加到此处的片段:
      spec:
            containers:
            - args:
              ...
              ...
      ### Here begin the config map setting to copy ###
              volumeMounts:
              - mountPath: /etc/nginx/template
                name: nginx-template-volume
                readOnly: true
            volumes:
            - name: nginx-template-volume
              configMap:
                name: nginx-template
                items:
                - key: nginx.tmpl
                  path: nginx.tmpl
      ### End ###
    6. 执行验证。 完成 daemonset 编辑后,将自动重新启动 Nginx 控制器 pod。 如果未启动,请手动将其删除以使编辑生效。
      # kubectl get po -n kube-system -o wide|grep nginx
      nginx-ingress-controller-ph8t6                                 1/1     Running     0          3m    10.1.13.68     9.46.67.224   <none>           <none>
      根据 nginx 配置,将在端口 80 上公开 nginx 状态。 因此,对此 url 运行 curl 命令以进行验证:
      #curl http://10.1.13.68/nginx_status
      Active connections: 48
      server accepts handled requests
       729017 729017 896362
      Reading: 0 Writing: 14 Waiting: 33