0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

もう迷わない!TypeScriptでKubernetes PodのIP・ステータスを簡単取得する方法

Posted at

はじめに

本記事では、リモート環境からKubernetes上のPod情報を取得するクラスをTypeScriptで作成した例を紹介します。

Node.jsとTypeScriptを使って、kubectlコマンドをリモート実行し、Podの名前・ステータス・IPアドレスをまとめて取得・加工する仕組みを実装します。

構造図

Pod クラス
├── execRemote(cmd)               // リモート実行
├── searchPods(search)            // Podを検索して一覧取得
│   └── getPodIp(podName)          // PodのIP取得
├── getPodxxx()                // 環境別のxxxPod取得
└── getRunningPod(search)          // Running状態のPodのみ取得

インポート

Pod.ts
import { PodTable } from "@utils/types/internal/data";
import Logger from "../common/Logger";
import SessionDB from "@class/db/SessionDB";
import settings from "@utils/constants/settings";
import Remote from "./Remote";
  • 必要な型定義(PodTable)やロガー(Logger)、セッション管理(SessionDB)、定数(settings)、リモート操作クラス(Remote)を読み込んでいます。

Pod クラス全体

class Pod { ... }
  • Kubernetes上のPodに対する検索状態確認などの機能を持ったクラスです。
  • 主にkubectlコマンドをリモート実行してPod情報を取得します。

Pod検索 (searchPods)

Pod.ts
public async searchPods(search: string): Promise<Array<PodTable>> {
  const cmd = `kubectl get pods | grep ${search}`;
  const searchResult = (await Remote.execRemote(cmd)) as string;
  const podLines = searchResult.trim().split("\n");
  const podObjects = await Promise.all(
    podLines.map(async (line) => {
      const splitLine = line.split(/\s+/);
      const podName = splitLine[0] || "";
      const podIP = await this.getPodIp(podName);

      return {
        name: podName,
        status: splitLine[2] || "",
        ip: podIP || "",
      };
    })
  );

  Logger.info(`These are the search results for ${search}`);
  console.table(podObjects);

  return podObjects;
}
  • 指定文字列を含むPodをリスト形式で探し、Pod名・ステータス・IPアドレスをまとめます。
  • 検索結果をconsole.tableで表示してくれます。

5. PodのIPアドレス取得 (getPodIp)

Pod.ts
private async getPodIp(podName: string): Promise<string> {
  const cmd = `kubectl get pod ${podName} -o jsonpath='{.status.podIP}'`;
  const podIP = (await this.execRemote(cmd)) as string;
  return podIP;
}
  • 指定したPodのIPアドレスを取得する小さいメソッドです。

6. billdocgenserviceのPod取得 (getPodBillDocGenService)

Pod.ts
public async getPodBillDocGenService(): Promise<string[]> {
  const podMap = await this.getRunningPod("billdocgenservice");
  return podMap.map((pod) => pod.name);
}
  • billdocgenservice というサービス名でRunning状態のPodを探して名前だけ返します。

Running中のPodを取得する (getRunningPod)

Pod.ts
private async getRunningPod(search: string): Promise<PodTable[]> {
  const podObjects = await this.searchPods(search);
  const runningPods = podObjects.filter((pod) => pod.status === "Running");

  if (runningPods.length > 0) {
    Logger.success(`Found running pod(s)`);
    return runningPods;
  } else {
    throw new Error(`Couldn't find running pod for ${search}`);
  }
}
  • 検索結果のうち、ステータスが"Running"のPodだけを抽出。
  • 見つからなければエラーを投げます。

xxx用Pod取得 (getPodUtility)

Pod.ts
public async getPodxxx(): Promise<string[]> {
  const env = SessionDB.getEnv();
  let podMap;

  switch (env) {
    case "prod":
      podMap = await this.getRunningPod("xxx");
      break;
    case "stg1":
    case "stg2":
    case "stg3":
      podMap = await this.getRunningPod("xxx");
      break;
  }

  return podMap.map((pod) => pod.name);
}
  • 環境(本番 or ステージング)によって探す対象Pod名を切り替えて取得します。

最後に、インスタンス化してエクスポート

Pod.ts
export default new Pod();
  • クラスPodをインスタンス化して、そのままエクスポートします。
  • シングルトン的な使い方ですね。

まとめ

  • Node.jsとTypeScriptで、Kubernetes上のPodをリモート取得する仕組みを作りました。
  • コマンドをシンプルにまとめ、拡張しやすい構成にできたと思います。
  • 今後の改善案として、
    • Podのリソース使用率や、ログ取得機能も組み込む
    • PodのRestart回数などヘルスチェック情報も取り扱う
      などを検討したいです。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?