gashirar's blog

ウイスキーがすき/美味しいものがすき/k8sがすき

GitHub ActionsでPushされたらMinikubeを立ててテストする

はじめに

GitHub Actionsのジョブ内でMinikubeを起動してテストすることが可能なことを知ったので、その疎通記事となります。
ネタ元はこちら↓

Setup minikube as CI step in github actions | minikube

リポジトリ作成

ディレクトリ構成はサンプルにならい下記のようにしています。 (.gitなどは省略)

# tree -a
.
├── .github
│   └── workflows
│       └── pr.yml          # GitHub Actionsの設定ファイル
├── Dockerfile              # サンプルで利用するDockerfile
├── README.md
└── deploy-to-minikube.yaml # Kubernetes Manifestファイル

下記に今回利用したリポジトリを配置しています。 github.com

pr.yml

name: CI
on:
  - push    # minikube.sigs.k8s.ioではPull Request時に発火となっていますが、今回はPushに変更
jobs:
  job1:
    runs-on: ubuntu-latest
    name: build example and deploy to minikbue
    steps:
    - uses: actions/checkout@v2
    - name: Start minikube
      uses: medyagh/setup-minikube@master
    - name: Try the cluster !
      run: kubectl get pods -A
    - name: Build image
      run: |
        export SHELL=/bin/bash
        eval $(minikube -p minikube docker-env)
        docker build -f ./Dockerfile -t local/example .
        echo -n "verifying images:"
        docker images
    - name: Deploy to minikube
      run:
        kubectl apply -f deploy-to-minikube.yaml
    - name: Test service URLs
      run: |
        minikube service list
        minikube service example --url
        echo "------------------opening the service------------------"
        curl $(minikube service example --url)

Dockerfile

例としてnginxを使います。

FROM nginx

deploy-to-minikube.yaml

nginxを利用するためDeploymentのcontainerPortを80に変更し、名前をついでにつけておきます。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: example
spec:
  selector:
    matchLabels:
      app: example
  replicas: 2
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: example-api
          imagePullPolicy: Never
          image: local/example:latest
          resources:
            limits:
              cpu: 50m
              memory: 100Mi
            requests:
              cpu: 25m
              memory: 10Mi
          ports:
            - name: example
              containerPort: 80 # Nginxが80番ポートで公開されるため
---
apiVersion: v1
kind: Service
metadata:
  name: example
spec:
  type: NodePort
  selector:
    app: example
  ports:
    - port: 8080
      targetPort: example # 名前で指定

GithubにPush

上記作成出来たのでGitHubにpushして確認してみましょう。

GitHub Actionsの確認

f:id:gashirar:20200607094209p:plain
GitHub Actionsの実行結果画面

エラーとなっているTest service URLsをみてみます。

# Test service URLs 4s
##[error]Process completed with exit code 7.
Run minikube service list
|-------------|------------|--------------|-------------------------|
|  NAMESPACE  |    NAME    | TARGET PORT  |           URL           |
|-------------|------------|--------------|-------------------------|
| default     | example    |         8080 | http://172.17.0.3:31808 |
| default     | kubernetes | No node port |
| kube-system | kube-dns   | No node port |
|-------------|------------|--------------|-------------------------|
http://172.17.0.3:31808
------------------opening the service------------------
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed to connect to 172.17.0.3 port 31808: Connection refused
##[error]Process completed with exit code 7.

どうやらNodePortへの接続に失敗しているようです。
ローカルで確認してみたところnginxからのレスポンスが見られるので、環境起因によるものと思われます。

gashirar:~/minikube-in-github-actions-example$ curl http://172.17.0.3:30500
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</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>

まとめ

締まらない感じになってしまいましたが、GitHub ActionsでMinikubeを立ててテストすることが可能になりました。
CIテスト用のクラスタを立てる手間が省けるため、いろいろと楽が出来そうな感じですね。