YOKOHEI.COM

YOKOHEI.COM

›Amazon EKS

Kubernetes Basics

  • Kubernetes Basics
  • Commands
  • Static Pod
  • Drain & Uncordon
  • Cluster Upgrade
  • Backup & Restore
  • Certification
  • RBAC
  • CNI
  • CoreDNS
  • Ingress

Kubernetes the Hard Way

  • the Hard Way?
  • AWS Prerequisites
  • CA and TLS
  • Configuration Files
  • Data Encryption
  • Bootstrapping etcd
  • Bootstrapping Control Plane
  • Bootstrapping Worker Node
  • kubectl configuraton
  • Pod Network
  • RBAC for Kubelet
  • Coredns Add-On
  • Test
  • TLS Bootstrapping

Amazon EKS

  • EKS Introduction
  • Ingress with ALB
  • PersistentVolume with EBS
  • NetworkPolicy with Calico
  • Secret with KMS
  • EKS Hands-On

EKS Hands-On

Inter-Pod Affinity

特定の Pod と同じノード上で必ず起動する

sample-pod の作成。以下のようなマニフェストを利用する。

$ cat sample-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod
  labels:
    app: sample-app
spec:
  containers:
    - name: nginx-container
      image: nginx:1.12

sample-pod が稼働しているノードを確認。

$ kubectl apply -f sample-pod.yaml 
pod "sample-pod" created

$ kubectl get pods -o wide
NAME         READY     STATUS    RESTARTS   AGE       IP          NODE
sample-pod   1/1       Running   0          51s       10.0.0.98   ip-10-0-0-87.eu-west-1.compute.internal

ip-10-0-1-55.eu-west-1.compute.internal のノードで稼働中。
以下のような Affinity マニフェストを利用して、同じホストへのデプロイを試みる。

$ cat sample-pod-affinity-host.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod-affinity-host
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: metadata.name
            operator: In
            values:
            - sample-app
        topologyKey: kubernetes.io/hostname
  containers:
  - name: nginx-container
    image: nginx:1.12

$ kubectl get pods -o wide
NAME                       READY     STATUS    RESTARTS   AGE       IP          NODE
sample-pod                 1/1       Running   0          1m        10.0.0.98   ip-10-0-0-87.eu-west-1.compute.internal
sample-pod-affinity-host   1/1       Running   0          42s       10.0.0.57   ip-10-0-0-87.eu-west-1.compute.internal

同じノード (EC2 インスタンス) で Pod が起動したことを確認。

特定の Pod と必ず同じゾーン上で起動し、可能な限り同じノード上で起動する

現在ワーカーノードが稼働している AZ を確認する。

$ kubectl get nodes -o json | jq ".items[] | .metadata.labels"
{
  "beta.kubernetes.io/arch": "amd64",
  "beta.kubernetes.io/instance-type": "m4.large",
  "beta.kubernetes.io/os": "linux",
  "failure-domain.beta.kubernetes.io/region": "eu-west-1",
  "failure-domain.beta.kubernetes.io/zone": "eu-west-1a",
  "kubernetes.io/hostname": "ip-10-0-0-224.eu-west-1.compute.internal"
}
{
  "beta.kubernetes.io/arch": "amd64",
  "beta.kubernetes.io/instance-type": "m4.large",
  "beta.kubernetes.io/os": "linux",
  "failure-domain.beta.kubernetes.io/region": "eu-west-1",
  "failure-domain.beta.kubernetes.io/zone": "eu-west-1a",
  "kubernetes.io/hostname": "ip-10-0-0-87.eu-west-1.compute.internal"
}
{
  "beta.kubernetes.io/arch": "amd64",
  "beta.kubernetes.io/instance-type": "m4.large",
  "beta.kubernetes.io/os": "linux",
  "failure-domain.beta.kubernetes.io/region": "eu-west-1",
  "failure-domain.beta.kubernetes.io/zone": "eu-west-1b",
  "kubernetes.io/hostname": "ip-10-0-1-55.eu-west-1.compute.internal"
}

sample-pod が稼働する eu-west-1a には 2 台のノードが存在しており、その中でも可能な限り、同じノードを選択する状況を考える。

$ cat sample-pod-affinity-zone-host.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: sample-pod-affinity-zone-host
spec:
  affinity:
    podAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
        - labelSelector:
            matchExpressions:
            - key: app
              operator: In
              values:
              - sample-app
          topologyKey: failure-domain.beta.kubernetes.io/zone
      preferredDuringSchedulingIgnoredDuringExecution:
        - weight: 1
          podAffinityTerm:
            labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - sample-app
            topologyKey: kubernetes.io/hostname
  containers:
    - name: nginx-container
      image: nginx:1.12

上を見ればわかるが、 requiredDuringSchedulingIgnoredDuringExecution の topologyKey として zone が指定されている。
これは、 AWS の AZ に相当。
また、 preferredDuringSchedulingIgnoredDuringExecution の topologyKey のは hostname が入っているので、同じノードを希望する。

$ kubectl get pods -o wide
NAME         READY     STATUS    RESTARTS   AGE       IP          NODE
sample-pod   1/1       Running   0          9m        10.0.0.98   ip-10-0-0-87.eu-west-1.compute.internal

$ kubectl apply -f sample-pod-affinity-zone-host.yaml 
pod "sample-pod-affinity-zone-host" created

$ kubectl get pods -o wide
NAME                            READY     STATUS    RESTARTS   AGE       IP          NODE
sample-pod                      1/1       Running   0          9m        10.0.0.98   ip-10-0-0-87.eu-west-1.compute.internal
sample-pod-affinity-zone-host   1/1       Running   0          3s        10.0.0.53   ip-10-0-0-87.eu-west-1.compute.internal

必要条件「同じゾーン」、優先条件「同じノード」が満たされてデプロイされた。

← Secret with KMS
▼ Codes ▼
LeetCodeGitHub
▼ Profile ▼
LinkedInFlickr
▼ Logo made with DesignEvo ▼
DesignEvo
Copyright © 2020 Kohei Yoshida