はじめに
こんばんは。
先日こんな記事上げておいてあれですが、コチラの記事を発見しましたので、早速自分もやってみようと思いました。
まずKubernetesを入れてなかったので入れるとこからです。
やってみた
1. Kubernetesインストール
そもそも、Kubernetesってよく聞くけど何?って状況だったので調べると、コンテナ化したアプリケーションのスケーリングやら、管理やらの運用を自動化するツールということでした。
まだ??という感じですが、とりあえずは立てたコンテナアプリケーションをいい感じに管理運用できるツールということで覚えときます。
まずDockerFoMacのEdgeバージョンをインストールする必要があるので、
コチラで紹介されている方法で、インストールしました。
Preferences -> Kubernetesで、「Enable Kubernetes」をチェックしてApplyを実行します。
すると、kubectlコマンドを実行できるようになります。
$ which kubectl /usr/local/bin/kubectl
これで完了です。
2. minikubeインストール
Kubernetesのクラスタ環境をローカルで簡単に構築できるツールということです。
コチラからインストールできます。
自分はMacなので、コチラのコマンドでインストールします。
$ curl -Lo minikube https://storage.googleapis.com/minikube/releases/v0.26.1/minikube-darwin-amd64 && chmod +x minikube && sudo mv minikube /usr/local/bin/ $ which minikube /usr/local/bin/minikube
minikubeコマンドが実行できるようになってるので、minikube startで起動します。
初回は時間がかかりますが2回目からはそうでもないです。
$ minikube start Starting local Kubernetes v1.10.0 cluster... Starting VM... Downloading Minikube ISO 150.53 MB / 150.53 MB [============================================] 100.00% 0s Getting VM IP address... Moving files into cluster... Downloading kubeadm v1.10.0 Downloading kubelet v1.10.0 Finished Downloading kubelet v1.10.0 Finished Downloading kubeadm v1.10.0 Setting up certs... Connecting to cluster... Setting up kubeconfig... Starting cluster components... Kubectl is now configured to use the cluster. Loading cached images from config file.
起動出来たら Kubernetesのダッシュボード確認するため、以下のコマンドを実行してみます。
$ minikube dashboard
見えました!
かっこいい...
3. Komposeインストール
先日作ったdocker-composeの設定ファイルを使ってテストしてみたいのですが、KubernetesはKubernetesの設定ファイルの記述方法があるようで、それはdocker-composeとは違うようです。
そんなdocker-composeの遺産を、よしなにKubernetes用の設定ファイルに変換してくれるのがKomposeです。
コチラからインストールできます。
自分はMacなので以下のコマンドでインストールしました。
$ brew install kompose ... ... ==> Summary 🍺 /usr/local/Cellar/kompose/1.12.0: 8 files, 43.9MB $ which kompose /usr/local/bin/kompose
brew入ってない場合はコチラに記載されている以下のやり方でもOKだと思います。
$ curl -L https://github.com/kubernetes/kompose/releases/download/v1.12.0/kompose-darwin-amd64 -o kompose $ chmod +x kompose $ sudo mv ./kompose /usr/local/bin/kompose
さっそく変換してみます。 変換するのは先日の記事で作成した以下のdocker-compose.ymlです。
コチラにも書かれているように、
ローカルでDockerレジストリを動かす Kubernetesにはコンテナイメージをビルドする機能はありません。 従って、サンプルアプリケーションのカスタムイメージをどこかのレジストリにpushし、そこからpullするように設定する必要があります。
ということです。
docker-hubにsanple_appとしてpushしてたimageをpullしてきます。
$ docker pull XXXXXXX/sample_app Using default tag: latest latest: Pulling from XXXXXXX/sample_app Digest: sha256:f2e9fb6aaa96ffc4b03bcdeb526056c84abadf7dda38fa5bd34931533d8c4be8 Status: Image is up to date for XXXXXXX/sample_app:latest
では、以前使用したdocker-compose.ymlにimageを指定します。以下のような感じです。
version: '3' services: web: container_name: application-container image: XXXXXXX/sample_app ports: - '5260:80' volumes: - ../:/var/www/project links: - kvs kvs: container_name: kvs-container image: memcached
ちなみにdocker_file関連を置いているディレクトリは以下のようになっています。
$ tree docker_files/ docker_files/ ├── docker-compose.yml └── web ├── Dockerfile └── vhosts.conf
では、以下のコマンドで変換してみます。
$ kompose convert WARN Volume mount on the host "/path/to/projects" isn't supported - ignoring path on the host INFO Kubernetes file "web-service.yaml" created INFO Kubernetes file "kvs-deployment.yaml" created INFO Kubernetes file "web-deployment.yaml" created INFO Kubernetes file "web-claim0-persistentvolumeclaim.yaml" created $ tree . ├── docker-compose.yml ├── kvs-deployment.yaml ├── web │ ├── Dockerfile │ └── vhosts.conf ├── web-claim0-persistentvolumeclaim.yaml ├── web-deployment.yaml └── web-service.yaml
なにやらファイルがポコポコ作られました。
でもなんか warning出てます。
volumeのマウントはサポートされてない??
この指定でいけるのかな・・?
もう一回 convertしてみます。
$ kompose convert INFO Kubernetes file "web-service.yaml" created INFO Kubernetes file "kvs-deployment.yaml" created INFO Kubernetes file "web-deployment.yaml" created INFO Kubernetes file "web-claim0-persistentvolumeclaim.yaml" created
おお。Warning が消えた。
一個ファイル見てみます。
$ cat web-service.yaml apiVersion: v1 kind: Service metadata: annotations: kompose.cmd: kompose convert --volumes=hostPath kompose.version: 1.12.0 () creationTimestamp: null labels: io.kompose.service: web name: web spec: ports: - name: "5260" port: 5260 targetPort: 80 selector: io.kompose.service: web status: loadBalancer: {}
なにやら色々書いてますね。
ただ、Portとか webとか自分が設定した記述が入っているので、問題なく変換が実行されたのだと思います。
ではupで立ち上げてみます。
$ kompose up WARN Volume mount on the host "/path/to/projects" isn't supported - ignoring path on the host INFO We are going to create Kubernetes Deployments, Services and PersistentVolumeClaims for your Dockerized application. If you need different kind of resources, use the 'kompose convert' and 'kubectl create -f' commands instead. INFO Deploying application in "default" namespace INFO Successfully created Service: web INFO Successfully created Deployment: kvs INFO Successfully created Deployment: web INFO Successfully created PersistentVolumeClaim: web-claim0 of size 100Mi. If your cluster has dynamic storage provisioning, you don't have to do anything. Otherwise you have to create PersistentVolume to make PVC work Your application has been deployed to Kubernetes. You can run 'kubectl get deployment,svc,pods,pvc' for details.
お?まだ warning出てるけどなんかいったっぽい?
ダッシュボードで確認してみます!
なんかきてるー!!
成功っぽいです。
でもこれどうやってアクセスればいいんだろう・・・
要調査ということで。。
とりあえずここまで。