Kubernetes環境でのNew Relic Infrastructure Agent導入

はじめに

Kubernetes環境でのインフラ監視は、ノードレベルの監視とクラスター全体の可視性を同時に実現する必要があります。New Relic Infrastructure Agentは、Helm ChartやDaemonSetを使用してKubernetesクラスターに効率的に導入でき、ノード、Pod、サービスの包括的な監視を提供します。

本記事では、Helm Chartを使用した標準的な導入から、カスタムDaemonSet設定、マルチクラスター環境での運用まで、実践的な導入手順を詳しく解説します。また、Kubernetes特有の監視要件とパフォーマンス最適化についても併せて説明します。

Kubernetes監視の基本概念

Infrastructure AgentのKubernetes対応

KubernetesでのInfrastructure Agentは、DaemonSetとして展開されることで、各ワーカーノードに1つずつPodが配置されます。これにより、すべてのノードの詳細な監視が可能になります。

監視対象の範囲

Kubernetes環境でのInfrastructure Agentは、以下の情報を収集します。

ノードレベル情報

  • ワーカーノードのCPU、メモリ、ディスク使用量
  • ノードの健全性とリソース可用性
  • kubeletのメトリクス

クラスターレベル情報

  • Pod、Deployment、Serviceの状態
  • コンテナのリソース消費量
  • ネットワークポリシーとトラフィック

Kubernetesメタデータ

  • ラベル、アノテーション
  • ネームスペース情報
  • オーナーリファレンス

Helm Chartを使用した導入

Helmリポジトリの追加

New Relic公式Helmリポジトリを追加して、最新のChartを使用できるようにします。

bash
# New Relic Helmリポジトリの追加
helm repo add newrelic https://helm-charts.newrelic.com

# リポジトリの更新
helm repo update

# 利用可能なChartの確認
helm search repo newrelic/newrelic-infrastructure

基本的なHelm Chart導入

最もシンプルな設定での導入例です。

bash
# 基本的なインストール
helm install newrelic-infra newrelic/newrelic-infrastructure \
  --set licenseKey=YOUR_LICENSE_KEY \
  --set cluster=production-cluster \
  --namespace=newrelic \
  --create-namespace

カスタム値ファイルを使用した導入

より詳細な設定を含むvalues.yamlファイルを作成して導入します。

yaml
# values.yaml
licenseKey: "YOUR_LICENSE_KEY"
cluster: "production-cluster"

# 詳細設定
global:
  fargate: false
  nrStaging: false

image:
  repository: newrelic/infrastructure-k8s
  tag: ""  # 空文字列で最新版を使用
  pullPolicy: IfNotPresent

# DaemonSet設定
daemonSet:
  # ノードセレクター(特定ノードにのみ配置する場合)
  nodeSelector: {}
  
  # Tolerations(taintがあるノードにも配置する場合)
  tolerations:
    - operator: "Exists"
      effect: "NoSchedule"
    - operator: "Exists"
      effect: "NoExecute"
  
  # リソース制限
  resources:
    limits:
      cpu: 500m
      memory: 512Mi
    requests:
      cpu: 100m
      memory: 128Mi

# サービスアカウント設定
serviceAccount:
  create: true
  name: ""
  annotations: {}

# RBAC設定
rbac:
  create: true
  pspEnabled: false

# Prometheus設定(メトリクス公開)
prometheus:
  enabled: true
  port: 8080

# カスタム属性
config:
  custom_attributes:
    environment: "production"
    team: "platform"
    cluster_location: "tokyo"

# ログレベル設定
verboseLog: false
bash
# カスタム設定での導入
helm install newrelic-infra newrelic/newrelic-infrastructure \
  -f values.yaml \
  --namespace=newrelic \
  --create-namespace

DaemonSetを使用した手動導入

基本的なDaemonSet設定

Helm Chartを使用せずに、DaemonSetを直接定義する場合の設定例です。

yaml
# newrelic-infrastructure-daemonset.yaml
apiVersion: v1
kind: Namespace
metadata:
  name: newrelic
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: newrelic-infra
  namespace: newrelic
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: newrelic-infra
rules:
- apiGroups: [""]
  resources: ["nodes", "nodes/metrics", "nodes/stats", "nodes/proxy", "pods", "services"]
  verbs: ["get", "list"]
- apiGroups: ["batch"]
  resources: ["jobs", "cronjobs"]
  verbs: ["get", "list"]
- apiGroups: ["apps"]
  resources: ["deployments", "replicasets", "daemonsets", "statefulsets"]
  verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: newrelic-infra
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: newrelic-infra
subjects:
- kind: ServiceAccount
  name: newrelic-infra
  namespace: newrelic
---
apiVersion: v1
kind: Secret
metadata:
  name: newrelic-license
  namespace: newrelic
type: Opaque
data:
  license: <BASE64_ENCODED_LICENSE_KEY>
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: newrelic-infra
  namespace: newrelic
  labels:
    app: newrelic-infra
spec:
  selector:
    matchLabels:
      app: newrelic-infra
  template:
    metadata:
      labels:
        app: newrelic-infra
    spec:
      serviceAccountName: newrelic-infra
      hostNetwork: true
      hostPID: true
      hostIPC: true
      
      tolerations:
      - operator: "Exists"
        effect: "NoSchedule"
      - operator: "Exists"
        effect: "NoExecute"
      
      containers:
      - name: newrelic-infra
        image: newrelic/infrastructure-k8s:latest
        securityContext:
          privileged: true
        
        env:
        - name: CLUSTER_NAME
          value: "production-cluster"
        - name: NRIA_LICENSE_KEY
          valueFrom:
            secretKeyRef:
              name: newrelic-license
              key: license
        - name: NRIA_VERBOSE
          value: "1"
        - name: NRIA_CUSTOM_ATTRIBUTES
          value: '{"environment":"production","cluster_location":"tokyo"}'
        
        volumeMounts:
        - name: host-root
          mountPath: /host
          readOnly: true
        - name: host-var-run
          mountPath: /var/run/docker.sock
          readOnly: true
        - name: host-proc
          mountPath: /host/proc
          readOnly: true
        - name: host-sys
          mountPath: /host/sys
          readOnly: true
        
        resources:
          limits:
            cpu: 500m
            memory: 512Mi
          requests:
            cpu: 100m
            memory: 128Mi
      
      volumes:
      - name: host-root
        hostPath:
          path: /
      - name: host-var-run
        hostPath:
          path: /var/run/docker.sock
      - name: host-proc
        hostPath:
          path: /proc
      - name: host-sys
        hostPath:
          path: /sys

DaemonSetの適用

bash
# DaemonSetの適用
kubectl apply -f newrelic-infrastructure-daemonset.yaml

# デプロイメント状態の確認
kubectl get daemonset -n newrelic
kubectl get pods -n newrelic -o wide

高度な設定オプション

マルチクラスター環境での管理

複数のKubernetesクラスターを管理する場合の設定例です。

yaml
# multi-cluster-values.yaml
licenseKey: "YOUR_LICENSE_KEY"

# クラスター固有設定
cluster: "production-cluster-tokyo"

config:
  custom_attributes:
    environment: "production"
    region: "asia-pacific"
    cluster_role: "primary"
    cost_center: "engineering"

# 地域別設定(AsiaとEurope間での設定差異)
global:
  fargate: false
  
image:
  repository: newrelic/infrastructure-k8s
  tag: "latest"

# 地域固有のリソース設定
daemonSet:
  resources:
    limits:
      cpu: 300m  # Asia環境では控えめに設定
      memory: 256Mi
    requests:
      cpu: 50m
      memory: 64Mi
  
  # 時間帯を考慮したtoleration設定
  tolerations:
    - key: "maintenance-window"
      operator: "Equal"
      value: "asia-pacific"
      effect: "NoSchedule"

カスタムメトリクス収集の設定

特定のアプリケーションメトリクスを収集する設定です。

yaml
# custom-metrics-values.yaml
licenseKey: "YOUR_LICENSE_KEY"
cluster: "production-cluster"

config:
  # Prometheusメトリクスの収集設定
  integrations:
    - name: nri-prometheus
      config:
        # スクレイプターゲットの設定
        transformations:
          - description: "Kubernetes pod memory usage"
            source_type: "prometheus"
            promql_query: 'container_memory_usage_bytes{pod!=""}'
            ignore_tags:
              - "container"
        
        targets:
          - description: "Application metrics from pods"
            urls: ["http://localhost:8080/metrics"]
            tls_config:
              insecure_skip_verify: true

# カスタム属性の詳細設定
  custom_attributes:
    cluster_version: "1.28"
    monitoring_tier: "premium"
    data_retention: "90d"

ネットワークポリシーとセキュリティ

NetworkPolicyの設定

セキュリティ要件に応じたネットワークポリシーの例です。

yaml
# network-policy.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: newrelic-infra-netpol
  namespace: newrelic
spec:
  podSelector:
    matchLabels:
      app: newrelic-infra
  
  policyTypes:
  - Egress
  
  egress:
  # New Relic APIへの通信許可
  - to: []
    ports:
    - protocol: TCP
      port: 443
  
  # Kubernetes APIサーバーへの通信許可
  - to:
    - namespaceSelector:
        matchLabels:
          name: kube-system
    ports:
    - protocol: TCP
      port: 443
  
  # DNSクエリ許可
  - to: []
    ports:
    - protocol: UDP
      port: 53

PodSecurityPolicyの設定

より厳しいセキュリティ環境での設定例です。

yaml
# pod-security-policy.yaml
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: newrelic-infra-psp
spec:
  privileged: true
  allowPrivilegeEscalation: true
  allowedCapabilities:
  - '*'
  volumes:
  - '*'
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  hostIPC: true
  hostPID: true
  runAsUser:
    rule: 'RunAsAny'
  seLinux:
    rule: 'RunAsAny'
  fsGroup:
    rule: 'RunAsAny'

運用と監視

アップグレード戦略

Infrastructure Agentのローリングアップデート設定です。

yaml
# upgrade-strategy.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: newrelic-infra
  namespace: newrelic
spec:
  updateStrategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1  # 一度に1ノードずつ更新
  
  template:
    spec:
      containers:
      - name: newrelic-infra
        image: newrelic/infrastructure-k8s:latest
        
        # ヘルスチェック設定
        livenessProbe:
          exec:
            command:
            - sh
            - -c
            - "pgrep -f newrelic-infra"
          initialDelaySeconds: 30
          periodSeconds: 30
          timeoutSeconds: 5
          failureThreshold: 3
        
        readinessProbe:
          exec:
            command:
            - sh
            - -c
            - "pgrep -f newrelic-infra"
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          failureThreshold: 1

Helm Chartのアップグレード

bash
# Chart情報の更新
helm repo update

# 現在のバージョン確認
helm list -n newrelic

# アップグレードの実行
helm upgrade newrelic-infra newrelic/newrelic-infrastructure \
  -f values.yaml \
  --namespace=newrelic

# ロールバック(必要な場合)
helm rollback newrelic-infra 1 -n newrelic

トラブルシューティング

一般的な問題と解決方法

Podが起動しない場合

bash
# Pod状態の確認
kubectl get pods -n newrelic
kubectl describe pod <POD_NAME> -n newrelic

# ログの確認
kubectl logs <POD_NAME> -n newrelic

# ノードのリソース状況確認
kubectl describe nodes
kubectl top nodes

RBAC権限の問題

bash
# ServiceAccountの確認
kubectl get serviceaccount -n newrelic
kubectl describe serviceaccount newrelic-infra -n newrelic

# ClusterRoleBindingの確認
kubectl get clusterrolebinding | grep newrelic
kubectl describe clusterrolebinding newrelic-infra

メトリクスが収集されない場合

bash
# Infrastructure Agentのログ詳細確認
kubectl logs -f <POD_NAME> -n newrelic

# ネットワーク接続テスト
kubectl exec -it <POD_NAME> -n newrelic -- curl -I https://infra-api.newrelic.com

# Kubernetes APIアクセステスト
kubectl exec -it <POD_NAME> -n newrelic -- curl -k https://kubernetes.default.svc.cluster.local

パフォーマンス最適化

リソース使用量を最適化するための設定です。

yaml
# performance-optimized-values.yaml
licenseKey: "YOUR_LICENSE_KEY"
cluster: "production-cluster"

# リソース最適化設定
daemonSet:
  resources:
    limits:
      cpu: 200m      # CPU使用量を制限
      memory: 256Mi  # メモリ使用量を制限
    requests:
      cpu: 50m
      memory: 128Mi

config:
  # 収集間隔の調整
  metrics_process_sample_rate: 60  # デフォルト: 20秒 → 60秒
  metrics_storage_sample_rate: 60  # デフォルト: 20秒 → 60秒
  metrics_network_sample_rate: 30  # デフォルト: 10秒 → 30秒
  
  # 不要な機能の無効化
  enable_win_services: false
  enable_win_firewall: false

アンインストール

Helm Chartのアンインストール

bash
# Helm releaseの削除
helm uninstall newrelic-infra -n newrelic

# ネームスペースの削除(必要に応じて)
kubectl delete namespace newrelic

手動DaemonSetのアンインストール

bash
# DaemonSetとリソースの削除
kubectl delete -f newrelic-infrastructure-daemonset.yaml

# 残存リソースの確認
kubectl get all -n newrelic

まとめ

Kubernetes環境でのNew Relic Infrastructure Agent導入は、Helm ChartまたはDaemonSetを使用することで効率的に実行できます。適切なRBAC設定とリソース制限により、クラスターの安定性を保ちながら包括的な監視を実現できます。

マルチクラスター環境では、クラスター固有の設定を通じて一貫した監視戦略を実装できるでしょう。次回は、AWS統合設定について詳しく解説します。


関連記事: Docker環境での導入関連記事: AWS統合設定