2
0

ワーカーノードとしてEC2・Fargateが混在するEKSクラスタを実現する

Last updated at Posted at 2024-02-08

諸事情(DaemonSetを使いたい、Podを起動させるための時間を短縮させたい、特定のインスタンスタイプを使いたい、等)で、一つのEKSクラスタの中で(かつ、同じ名前空間の中で)ワーカノードとしてFargateとEC2を使い分けたい場面があったので、その時の方法を自分向けのアウトプット用として記録。

やること

  • Fargateプロファイルのセレクタとして、名前空間に加えて「Podセレクタ」を指定する
  • Fargateで実行したいPodには、「Podセレクタ」で指定したラベルを設定する
    • 逆に、EC2で起動したいPodには、当該ラベルを設定しない

Fargateプロファイルを作成する

  • 名前空間:test
  • Podセレクタ:「nodeType/fargate」

スクリーンショット 2024-02-08 185712.png

ラベルのついていないDeploymentをApplyした場合

redis_deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment
  labels:
    app: redis
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        ports:
        - containerPort: 6379

EC2で起動しました。

[root@ip-192-168-0-50 fargate]# k apply -f redis_deployment.yaml
deployment.apps/redis-deployment created
[root@ip-192-168-0-50 fargate]# k get po -n test -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP             NODE                                 NOMINATED NODE   READINESS GATES
redis-deployment-65d8857849-zhh2z   1/1     Running   0          10s   192.168.0.27   ip-192-168-0-51.ap-northeast-1.compute.internal   <none>           <none>

ラベルのついたDeploymentをApplyした場合

redis_deployment_labeled.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis-deployment-labeled
  labels:
    app: redis
  namespace: test
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
        nodeType: fargate # replicasetの方に付ける
    spec:
      containers:
      - name: redis
        image: redis:latest
        ports:
        - containerPort: 6379

Fargateで起動しました。

[root@ip-192-168-0-50 fargate]# k apply -f redis_deployment_labeled.yaml
deployment.apps/redis-deployment-labeled created
[root@ip-192-168-0-50 fargate]# k get po -n test -o wide
NAME                                        READY   STATUS    RESTARTS   AGE    IP           NODE                                                      NOMINATED NODE  READINESS GATES
redis-deployment-labeled-778b484f5f-b7r7v   1/1     Running   0          3m4s   192.168.3.25   fargate-ip-192-168-3-25.ap-northeast-1.compute.internal   <none>  <none>

これで一つのEKSクラスターの中で、EC2で起動するPodとFargateで起動するPodを共存させることが出来ました。

[root@ip-192-168-0-50 fargate]# k get po -n test -o wide
NAME                                        READY   STATUS    RESTARTS   AGE     IP            NODE                                                      NOMINATED NODE   READINESS GATES
redis-deployment-65d8857849-lxvzw           1/1     Running   0          8s      192.168.0.27   ip-192-168-0-51.ap-northeast-1.compute.internal           <none>   <none>
redis-deployment-labeled-778b484f5f-b7r7v   1/1     Running   0          8m35s   192.168.3.25   fargate-ip-192-168-3-25.ap-northeast-1.compute.internal   <none>   <none>

Cronjobの場合

バッチ処理のような常時起動しないPodをFargateで起動させたい場合も同様で、Podセレクタに対応したラベルを設定することで実現可能です。

cronjob_labeled.yaml
apiVersion: batch/v1
kind: CronJob
metadata:
  name: sample-cronjob
  namespace: test
spec:
  schedule: "*/5 * * * *"
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            nodeType: fargate # こちらもCronjob本体ではなく、Podにラベルをつける
        spec:
          containers:
          - name: sample-container
            image: busybox
            command: ["sh", "-c", "echo 'Hello, World!'"]
            imagePullPolicy: IfNotPresent
          restartPolicy: OnFailure

Job実行用のPodがFargateで起動しました。

[root@ip-192-168-0-50 cronjob]# k get po -n test -o wide -w
NAME                            READY   STATUS      RESTARTS   AGE     IP  NODE                                              NOMINATED NODE              READINESS GATES
sample-cronjob-28454655-k4vw2   0/1     Pending     0          32s     <none>  <none>                                            082dcdea0f-28589384ab174005857b2b40898b87d4   <none>
sample-cronjob-28454655-k4vw2   0/1     Pending     0          42s     <none>  fargate-ip-192-168-3-167.ap-northeast-1.compute.internal   082dcdea0f-28589384ab174005857b2b40898b87d4   <none>
sample-cronjob-28454655-k4vw2   0/1     ContainerCreating   0          43s     <none>         fargate-ip-192-168-3-167.ap-northeast-1.compute.internal   <none>                               <none>
sample-cronjob-28454655-k4vw2   0/1     Completed           0          48s     192.168.3.167   fargate-ip-192-168-3-167.ap-northeast-1.compute.internal   <none>                                <none>

Pod間通信は?

nginxをFargateとEC2の両方で起動する

[root@ip-192-168-0-50 fargate]# k get po -n test -o wide
NAME                READY   STATUS    RESTARTS   AGE   IP             NODE                         NOMINATED NODE   READINESS GATES
nginx-pod           1/1     Running   0          67s   192.168.0.27   ip-192-168-0-51.ap-northeast-1.compute.internal           <none>           <none>
nginx-pod-labeled   1/1     Running   0          59s   192.168.3.77   fargate-ip-192-168-3-77.ap-northeast-1.compute.internal   <none>           <none>
[root@ip-192-168-0-50 fargate]# k exec -n test nginx-pod -- curl -I 192.168.3.77
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   615    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Server: nginx/1.25.3
Date: Thu, 08 Feb 2024 00:21:18 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 24 Oct 2023 13:46:47 GMT
Connection: keep-alive
ETag: "6537cac7-267"
Accept-Ranges: bytes

[root@ip-192-168-0-50 fargate]# k exec -n test nginx-pod -- curl -I 192.168.0.27
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0   615    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
HTTP/1.1 200 OK
Server: nginx/1.25.3
Date: Thu, 08 Feb 2024 00:21:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Tue, 24 Oct 2023 13:46:47 GMT
Connection: keep-alive
ETag: "6537cac7-267"
Accept-Ranges: bytes

NW/EKSクラスタの設計にも依るところはありますが、デフォルトの動作であればFargateのENIにもEC2にもワーカノード管理用のセキュリティグループが付与されているので、基本的にはPod間で通信することが可能なようです。

2
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
2
0