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?

More than 1 year has passed since last update.

Podの通信をtcコマンドで遅延させる

Posted at

Podを使ったテストの際、遅延を入れてテストしたいことがある。
Istioなどがあれば簡単に実現できるが、逆にちょっとしたテストだけのためにIstioを入れるのは流石に面倒である。
なので、tcコマンドをサイドカーとして差し込んで遅延を起こしてみる。

tcコンテナの作成&動作確認

tcのコンテナを作成する。確認用にpingも入れている。

cat << EOF > ./Dockerfile
FROM ubuntu:latest
RUN apt-get update && apt-get install -y iproute2
RUN apt-get install iputils-ping net-tools
CMD ["/bin/bash"]
EOF

イメージをビルドする。IMAGEは自環境のリポジトリ等に書き換えること。

IMAGE=imuratashared/tc
docker build -t $IMAGE --platform linux/x86_64 .
docker push $IMAGE

次に動作確認する。
tcは強めの権限がないと動かないので、ここではPrivilegedをつけて起動する。

kubectl run --privileged=true --image $IMAGE tc -- sleep 3d

何もしていない状態でのpingの応答は以下のような感じ。

$ kubectl exec -it tc -- ping vmware.com
PING vmware.com (10.113.63.149) 56(84) bytes of data.
64 bytes from cloudsolutions.vmware.com (10.113.63.149): icmp_seq=1 ttl=51 time=34.6 ms

遅延を入れてみる。

kubectl exec -it tc -- tc qdisc add dev eth0 root netem delay 100ms

pingの応答は以下のようになり、遅延が効いていることが分かる。(なんか接続先は微妙に変わっているが。。。)

$ kubectl exec -it tc -- ping vmware.com
PING vmware.com (10.113.63.149) 56(84) bytes of data.
64 bytes from passwordreset.vmware.com (10.113.63.149): icmp_seq=1 ttl=51 time=135 ms

任意のPodへの組み込み

先程のtcコマンドを持ったコンテナをinitContainerとして組み込み、tcコマンドを実行するようにする。
ここではサンプルとしてCentOSのイメージで確認する。
以下のようなManifestを作成する(先程使ったIMAGEという環境変数が設定されているものとする)。

cat << EOF > ./delay-centos.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: centos
  name: centos
spec:
  initContainers:
  - command: ["tc","qdisc","add","dev","eth0","root","netem","delay","100ms"]
    name: tc
    image: $IMAGE
    securityContext:
      capabilities:
        add:
        - NET_ADMIN
  containers:
  - args:
    - sleep
    - 1d
    image: centos
    name: centos
EOF
kubectl apply -f delay-centos.yaml

作成したPodでpingを実行する。

$ kubectl exec -it centos -c centos -- ping vmware.com
PING vmware.com (10.113.63.149) 56(84) bytes of data.
64 bytes from www.vmwareblogs.com (10.113.63.149): icmp_seq=1 ttl=51 time=125 ms

なお、何も入れていない状態だと以下のような数値となっているため、遅延出来ていることが分かる。

$ kubectl exec -it centos -- ping vmware.com
PING vmware.com (10.113.63.149) 56(84) bytes of data.
64 bytes from www.vmwareblogs.com (10.113.63.149): icmp_seq=1 ttl=51 time=24.2 ms
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?