Skip to content

Instantly share code, notes, and snippets.

@tomotake-koike
Created January 22, 2021 09:07
Show Gist options
  • Select an option

  • Save tomotake-koike/5f569fe9122bee3d83d5b6bea47a89ca to your computer and use it in GitHub Desktop.

Select an option

Save tomotake-koike/5f569fe9122bee3d83d5b6bea47a89ca to your computer and use it in GitHub Desktop.
EKSでAZに配置されたNode間で疎通確認を行う

EKSでAZに配置されたNode間で疎通確認を行う

仕組み

EKS NodeGroupの構成がわからなくてもnodeリソースからAZのラベルを抽出し、そのラベルでaffinityしたpod間で疎通確認を行う。
疎通はClusterIPとHead Lessの2つのルートを確認する。
2AZ前提で書いている。(ちょっと直せばMulti-AZも対応可能でしょう。)

準備

AZ構成をnodeから抽出する。

AZ=($(kubectl get node -o jsonpath="{range .items[*].metadata.labels}{.topology\.kubernetes\.io/zone}{'\n'}{end}" | sort -u))

検証するために必要なリソースを生成する。PodはAZに分散され生成される。

for n in ${AZ[@]}
do cat << EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: busybox-${n}
  labels:
    app: busybox-${n}
spec:
  containers:
    - name: busybox-${n}
      image: busybox:latest
      command: ["sh"]
      args: [ "-c", "while [ ! -e /tmp/end ] ; do nc -lk -p 80 ; done"]
      lifecycle:
        preStop:
          exec:
            command: ["sh", "-c", "touch /tmp/end && pkill -TERM nc" ]
  nodeSelector:
    topology.kubernetes.io/zone: ${n}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: busybox-${n}
  name: busybox-${n}
spec:
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80
  selector:
    app: busybox-${n}
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hl-busybox-${n}
  name: hl-busybox-${n}
spec:
  clusterIP: None
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: busybox-${n}
  type: ClusterIP
EOF
done

検証

以下を実行してPod間で疎通確認を行う。

for i in 0 1
do
echo -----
src=busybox-${AZ[$i]}
dst=busybox-${AZ[$(((i+1)%2))]}
msg="from ${src} $(date)"
echo [CLIENT SEND]: $msg
echo $msg | kubectl exec ${src} -it -- nc -v ${dst}.default.svc.cluster.local 8080
echo HeadLess : $msg | kubectl exec ${src} -it -- nc -v hl-${dst}.default.svc.cluster.local 80
echo [SERVER LOG]:
kubectl logs ${dst} | tail -2
done

以下の様に2行ずつSERVER LOGとして出力されれば成功。

-----
[SEND]: from busybox-ap-northeast-1a Wed Jan 13 07:55:38 UTC 2021
Unable to use a TTY - input is not a terminal or the right kind of file
busybox-ap-northeast-1c.default.svc.cluster.local (172.20.72.217:8080) open
Unable to use a TTY - input is not a terminal or the right kind of file
hl-busybox-ap-northeast-1c.default.svc.cluster.local (10.105.240.193:80) open
[SERVER LOG]:
from busybox-ap-northeast-1a Wed Jan 13 07:55:38 UTC 2021
HeadLess : from busybox-ap-northeast-1a Wed Jan 13 07:55:38 UTC 2021
-----
[SEND]: from busybox-ap-northeast-1c Wed Jan 13 07:55:41 UTC 2021
Unable to use a TTY - input is not a terminal or the right kind of file
busybox-ap-northeast-1a.default.svc.cluster.local (172.20.47.93:8080) open
Unable to use a TTY - input is not a terminal or the right kind of file
hl-busybox-ap-northeast-1a.default.svc.cluster.local (10.105.240.4:80) open
[SERVER LOG]:
from busybox-ap-northeast-1c Wed Jan 13 07:55:41 UTC 2021
HeadLess : from busybox-ap-northeast-1c Wed Jan 13 07:55:41 UTC 2021

片付け

for n in ${AZ[@]}; do kubectl delete po/busybox-${n} svc/busybox-${n} svc/hl-busybox-${n}; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment