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?

自分用メモ: Karpenterでスワップ自動設定:EC2インスタンスに20GBスワップ領域を追加する方法

Last updated at Posted at 2025-05-21

注意: 未検証

ユーザーデータスクリプトの指定方法

EC2NodeClassリソースのuserDataフィールドにスクリプトを指定します。

20GBスワップファイル設定用のユーザーデータスクリプト

設定例

apiVersion: karpenter.k8s.aws/v1
kind: EC2NodeClass
metadata:
  name: default-with-swap
spec:
  amiFamily: AL2
  role: "KarpenterNodeRole-${CLUSTER_NAME}"
  subnetSelectorTerms:
    - tags:
        karpenter.sh/discovery: "${CLUSTER_NAME}"
  securityGroupSelectorTerms:
    - tags:
        karpenter.sh/discovery: "${CLUSTER_NAME}"
  userData: |
    #!/bin/bash
    
    # 20GBのスワップファイルを作成
    dd if=/dev/zero of=/swapfile bs=1M count=20480 status=progress
    
    # スワップファイルに適切なパーミッションを設定
    chmod 600 /swapfile
    
    # ファイルにスワップ領域を設定
    mkswap /swapfile
    
    # スワップファイルを有効化
    swapon /swapfile
    
    # fstabに追加して永続化
    echo '/swapfile none swap sw 0 0' >> /etc/fstab
    
    # スワップ使用度の調整(オプション)
    echo 'vm.swappiness=10' >> /etc/sysctl.conf
    
    # sysctlの変更を適用
    sysctl -p
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?