2
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 5 years have passed since last update.

ローカルのプログラムやコマンドを k8s クラスタ内で実行する方法

Last updated at Posted at 2019-07-11

はじめに

  • 以下の様なモチベーションから、同僚に聞いた方法をまとめました
    • k8s クラスタ上の Pod に対して、自作のプログラムでリクエストを投げたい
    • kubectl port-forward でもできるけど、いろんな Pod に投げたいので、何個も port-forward するのはだるい

想定

  • GKE 上のクラスタで一時的にプログラムを動かしたい(クラスタ上のイメージには使えるものがない)
  • GCR HostName: asia.gcr.io
  • GCP ProjectName: gcp-project-hoge

Docker イメージを作る

  • Dockerfile
# 動かしたいプログラムのバイナリと以下のDockerfile を同じ場所に置く
FROM ubuntu:latest

COPY ./app .
  • build docker image
# sample-image-name は適当
$ docker build . -t asia.gcr.io/gcp-project-hoge/sample-image-name

GCR に push する

$ docker push asia.gcr.io/gcp-project-hoge/sample-image-name:latest

k8s クラスタにデプロイ

# sample-pod は適当な pod 名
# デプロイしたい k8s の context に切り替えておくこと 
# 実行後、Docker コンテナにログインした状態になり(-it)、 exit すると Pod は削除される(--rm)
$ kubectl run sample-pod --restart=Never -it --rm --image asia.gcr.io/gcp-project-hoge/sample-image-name:latest
2
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?