LoginSignup
2
1

More than 3 years have passed since last update.

Lambdaからkubectlを使う

Last updated at Posted at 2019-02-12

Serverlessを使うと簡単なので入れる


npm install -g serverless

Serverlessのプロジェクトを作る

sls create \
  --template aws-python3 \
  --name hello \
  --path hello

(pathはどちらでもいいです。)

kubectlをダウンロード

ここよりLinux版をcurlでダウンロード
https://kubernetes.io/docs/tasks/tools/install-kubectl/

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

kubernetesのconfigファイルを連れてくる

cp ~/.kube/config .

フォルダ構成

  • handler.py
  • config
  • kubectl

handler.pyを編集

def hello(event, context):
    config_filepath = '/tmp/config'
    with open(config_filepath, 'w') as f:
        f.write(config)
    shutil.copy('kubectl', '/tmp/kubectl')
    proc = subprocess.run(
        ['chmod', '+x', '/tmp/kubectl'],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    proc = subprocess.run(
        ['/tmp/kubectl', '--kubeconfig', config_filepath, 'get', 'po'],
        stdout = subprocess.PIPE,
        stderr = subprocess.PIPE
    )
    print(proc.stdout.decode('utf8'))
    print(proc.stderr.decode('utf8'))

deploy

$ sls deploy
...

確認

$ sls invoke
...
2
1
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
2
1