newrelic/kubernetes-operatorNew Relic Kubernetes Operator是一个Kubernetes Operator,用于从Kubernetes配置中管理New Relic资源。通过自定义Kubernetes对象管理New Relic资源的方式,与管理内置Kubernetes对象的方式一致。
目前,该Operator支持管理以下资源:
适用于需要在Kubernetes环境中集中管理New Relic监控配置的用户,特别是希望通过Kubernetes原生工具(如kubectl)进行配置管理、版本控制和自动化部署的场景。
注意: 以下快速启动说明不需要克隆代码仓库。
安装docker、kubectl、kustomize和kind
bashbrew cask install docker brew install kubernetes-cli kustomize kind
使用kind创建测试集群
bashkind create cluster --name newrelic kubectl cluster-info
安装cert-manager
bashkubectl apply --validate=false -f [***]
注意: 此步骤需要1-2分钟完成,请等待一分钟后再进行下一步。
可通过以下命令确认cert-manager是否运行:kubectl rollout status deployment -n cert-manager cert-manager-webhook
在测试集群中安装Operator
bashkustomize build github.com/newrelic/newrelic-kubernetes-operator/configs/default | kubectl apply -f -
注意: 这将在kubectl当前配置的Kubernetes集群上安装Operator。
如果需要使用自定义容器部署Operator,可以通过kustomize.yaml文件覆盖镜像名称。
创建新的kustomize.yaml文件
yamlapiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization namespace: newrelic-kubernetes-operator-system resources: - github.com/newrelic/newrelic-kubernetes-operator/configs/default images: - name: newrelic/k8s-operator:snapshot newName: <自定义镜像名称> newTag: <自定义标签>
应用该文件:
bashkustomize build . | kubectl apply -f -
完成快速启动后,即可通过New Relic Kubernetes对象配置New Relic资源。
使用以下示例策略配置文件。需要将api_key字段更新为New Relic个人API密钥。
examples/example_policy.yaml
yamlapiVersion: nr.k8s.newrelic.com/v1 kind: Policy metadata: name: my-policy spec: # 在此处添加API密钥 api_key: <你的New Relic个人API密钥> name: k8s创建的策略 incident_preference: "PER_POLICY" region: "us" conditions: - spec: nrql: query: "SELECT count(*) FROM Transactions" since_value: "10" enabled: true terms: - threshold: "75.0" time_function: "all" duration: "5" priority: "critical" operator: "above" name: "K8s生成的告警条件" - spec: type: "apm_app_metric" enabled: true metric: "apdex" condition_scope: application entities: - "5950260" terms: - threshold: "0.9" time_function: "all" duration: "30" priority: "critical" operator: "above" name: "K8s生成的APM告警条件2"
添加API密钥后,应用到本地集群:
bashkubectl apply -f examples/example_policy.yaml
注意: 也可以使用Kubernetes Secret提供API密钥。我们提供了示例Secret配置文件,使用时需将
api_key替换为api_key_secret。
使用以下命令查看配置的策略:
bashkubectl describe policies.nr.k8s.newrelic.com
注意: 你也应该能在New Relic账户中看到新创建的策略。
Operator将通过kubectl apply -f <文件名>应用配置文件,根据需要创建和更新告警策略及NRQL告警条件。
使用以下示例NRQL告警条件配置文件。需要将api_key字段更新为New Relic个人API密钥。
examples/example_nrql_alert_condition.yaml
yamlapiVersion: nr.k8s.newrelic.com/v1 kind: NrqlAlertCondition metadata: name: my-alert-condition spec: # 在此处添加API密钥 api_key: <你的New Relic个人API密钥> name: "K8s生成的告警条件" nrql: # 注意:这只是示例。 # 你需要使用更具体的参数来匹配相关Kubernetes对象的查询。 query: "SELECT count(*) FROM Transactions" since_value: "10" enabled: true terms: - threshold: "75.0" time_function: "all" duration: "5" priority: "critical" operator: "above" existing_policy_id: 26458245 # 注意:必须匹配你账户中现有的策略ID region: "us"
卸载Operator的操作与安装相反,即通过kustomize构建Kubernetes资源文件并运行kubectl delete:
bashkustomize build github.com/newrelic/newrelic-kubernetes-operator/configs/default | kubectl delete -f -
本节将帮助你正确设置Operator的开发环境。
克隆代码仓库
bashgit clone ***:newrelic/newrelic-kubernetes-operator.git
按照对应操作系统的说明安装kubebuilder。此安装还会包含测试所需的etcd和kube-apiserver。
注意: 不要使用
brew安装kubebuilder。Homebrew的kubebuilder包不会提供运行测试所需的所有依赖。
运行测试套件(使用Ginkgo测试框架)。使用make目标是开始测试的最快方式:
make运行测试
bashmake test # 运行所有测试 make test-unit # 仅运行单元测试 make test-integration # 仅运行集成测试
bashmake lint
执行快速启动部分的步骤,完成初始必要设置,并通过kubectl apply应用Operator配置。
确认配置已部署到本地Kubernetes集群(使用kind创建的集群):
newrelic-kubernetes-operator-system
bashkubectl get namespaces
newrelic-kubernetes-operator-system命名空间中的节点
应看到类似以下输出:bashkubectl get nodes -n newrelic-kubernetes-operator-system
NAME STATUS ROLES AGE VERSION newrelic-control-plane Ready master 163m v1.18.2
后续步骤:
bash# 描述当前配置的策略 kubectl describe policies.nr.k8s.newrelic.com # 描述当前配置的告警条件 kubectl describe nrqlalertconditions.nr.k8s.newrelic.com # 获取New Relic Operator使用的节点 kubectl get nodes -n newrelic-kubernetes-operator-system # 描述New Relic Operator使用的节点 kubectl describe node <节点名称> # 跟踪Operator的manager容器日志(开发时有用) # 使用上述`describe node`命令定位manager控制器 kubectl logs -f -n newrelic-kubernetes-operator-system -c manager newrelic-kubernetes-operator-controller-manager-<哈希值>

manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务