(O+P)ut

アウトプット



(O+P)ut

エンジニアのアウトプット

【Windows】skaffoldを利用してコード修正を自動的にポッドに反映する(1/2)

スポンサーリンク

はじめに

skaffoldとはKubernetes上で動作するアプリケーションの開発を省力化するコマンドラインツールです。

本記事ではskaffoldのインストールの流れとnginxのウェルカムページを編集した際のskaffoldの動きを見ていきます。

環境情報
  • Windows10(HyperV)
  • DockerDesktop

skaffoldのインストール

以下リンク先からコマンドのみをインストールすることが可能です。

以下のような実行ファイルを入手できるので

skaffold-windows-amd64.exe

こちらをskaffold.exeと名前を変えてパスが通っているディレクトリに配置します。

パスが通れば任意のディレクトリで以下のようにコマンドが利用できます。

$ skaffold version
v1.3.1

今回は以下のような状況で利用します。

  • イメージはDockerfileで作成
  • コンテナレジストリはローカル
  • Kubernetesにデプロイ

要はWindowsPC環境で全てローカルで試せます。

テストケース

以下のようなディレクトリを用意しました。

$ tree
.
├── Dockerfile
├── nginx-deployment.yaml
├── nginx-service.yaml
├── nginx-welcome.html
└── skaffold.yaml

それぞれの解説です。

Dockerfile

nginxのイメージを元に同ディレクトリにあるnginx-welcome.htmlをindex.htmlに差し替えてイメージにしています。

FROM nginx:latest
COPY --chown=1001:0 nginx-welcome.html /usr/share/nginx/html/index.html

このDockerfileを用いて以下のように新しいmynginxを用意します。

> docker build -t mynginx .

イメージが新規作成されます。

$ docker image ls
REPOSITORY TAG  IMAGE ID    CREATED SIZE
mynginx  latest bcd9d70cbd58        x seconds ago       91.7MB ...

nginx-deployment.yaml

作成したイメージを元にDeploymentを作成するYAMLです。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: mynginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

nginx-service.yaml

NodePortを30080で固定して開けるようにしています。

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  type: NodePort
  ports:
  - port: 80
    nodePort: 30080

nginx-welcome.html

nginxのウェルカムページに「Hoge!」をつけ足しています。

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx! Hoge!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>

skaffold.yaml

image欄は今回利用するmynginxを指定し、その変化があれば関連YAMLに反映させるようファイルに記載します。

apiVersion: skaffold/v1
kind: Config
build:
  artifacts:
  - image:  mynginx
deploy:
  kubectl:
    manifests:
    - nginx-deployment.yaml
    - nginx-service.yaml

後編


長くなったので別ページに分けますが、上記ファイル群を利用してskaffoldのテストを行います。