LoginSignup
33
32

More than 3 years have passed since last update.

kubernetes: 特定のコンテナ(Pod)だけ特定のホストで

Last updated at Posted at 2016-10-03

kuberenetsで複数のコンテナを複数クラスタ上で運用すると、リソースの関係などで特定のコンテナ(Pod)だけ特定のホストで実行したいケースがある。

label機能を使うことで実現できる。

参照) Constraining pods to run on particular nodes
http://kubernetes.io/docs/user-guide/node-selection/

host(minion node)にlabelを付ける

初期状態として以下のworker(minion) nodeとする

$ kubectl get nodes
NAME              LABELS                                   STATUS    AGE
ip-172-31-25-70   kubernetes.io/hostname=ip-172-31-25-70   Ready     1m
ip-172-31-25-72   kubernetes.io/hostname=ip-172-31-25-72   Ready     1m

それぞれのホストにlabelを付与する

kubectl label nodes ip-172-31-25-70 type=app1
kubectl label nodes ip-172-31-25-72 type=app2

※上書きするときは--overwrite オプションを付ければ良い。

任意のkey=value形式で、例ではtype=app1、app2とそれぞれ付与した。

以下のようにlabelが付与されていることが確認できる。

$ kubectl get nodes
NAME              LABELS                                             STATUS    AGE
ip-172-31-25-70   kubernetes.io/hostname=ip-172-31-25-70,type=app1   Ready     31m
ip-172-31-25-72   kubernetes.io/hostname=ip-172-31-25-72,type=app2   Ready     31m

※バージョンによっては kubectl get node -L type で確認

Pod/RC(or Replica Set/Deployment)にlabelを付与する

yamlファイルのspecにて nodeSelectorを追記。
以下はReplication Controllerのyaml設定例。

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
      nodeSelector:
        type: app2

上記ではtype=app2のlabelを付与したホスト(ip-172-31-25-72)に限定することを意味する。

動作確認

RCを起動して、Podが指定したホストのみで起動されることを確認。

$ kubectl create -f test.yaml

以下の通り、type=app2のlabelを付与したホスト(ip-172-31-25-72)へのみアサインされていることがわかる。

$ kubectl get pod -o wide
NAME          READY     STATUS    RESTARTS   AGE       NODE
nginx-5w09v   1/1       Running   0          17m       ip-172-31-25-72
nginx-mf4l7   1/1       Running   0          20m       ip-172-31-25-72
nginx-p79e9   1/1       Running   0          17m       ip-172-31-25-72

試しにyamlファイルのreplicasを3から5に変更してapplyすると、新たに2つのPodが追加され、かつtype=app2のlabelを付与したホスト(ip-172-31-25-72)へのみアサインされていることがわかる。

$ kubectl get pod -o wide
NAME          READY     STATUS    RESTARTS   AGE       NODE
nginx-5w09v   1/1       Running   0          17m       ip-172-31-25-72
nginx-mf4l7   1/1       Running   0          20m       ip-172-31-25-72
nginx-p79e9   1/1       Running   0          17m       ip-172-31-25-72
nginx-r7dmc   1/1       Running   0          20m       ip-172-31-25-72
nginx-yu0r3   1/1       Running   0          20m       ip-172-31-25-72
33
32
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
33
32