Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

micro serviceのサンプルデモ「sock-shop」を構築する。

はじめに

Kubernetesのクラスタを作ったので、何かしらPodをデプロイしてみる。
単にnginxとかだけをデプロイするのも面白くないので、マイクロサービスのサンプルデモである「sock-shop」を構築する。
sock-shop

デプロイするKubernetesのクラスタはこれで構築した環境
kubeadmでのk8sクラスタの構築

Gitリポジトリのクローン

手順に沿ってまずはリポジトリをクローンする。

$ sudo git clone https://github.com/microservices-demo/microservices-demo
sudo: git: コマンドが見つかりません

gitコマンドが入ってなかった・・・。

$ sudo yum install git
読み込んだプラグイン:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
・・・
依存性関連をインストールしました:
  perl-Error.noarch 1:0.17020-2.el7                                        perl-Git.noarch 0:1.8.3.1-21.el7_7                                        perl-TermReadKey.x86_64 0:2.30-20.el7

完了しました!

改めてクローン

$ sudo git clone https://github.com/microservices-demo/microservices-demo
Cloning into 'microservices-demo'...
remote: Enumerating objects: 9638, done.
remote: Total 9638 (delta 0), reused 0 (delta 0), pack-reused 9638
Receiving objects: 100% (9638/9638), 52.85 MiB | 2.07 MiB/s, done.
Resolving deltas: 100% (5796/5796), done.

$ ls -l microservices-demo/
合計 36
-rw-r--r--.  1 root root 11357  3月  1 21:24 LICENSE
-rw-r--r--.  1 root root  1674  3月  1 21:24 README.md
drwxr-xr-x. 16 root root  4096  3月  1 21:24 deploy
drwxr-xr-x.  2 root root     6  3月  1 21:24 docs
drwxr-xr-x.  2 root root   185  3月  1 21:24 graphs
drwxr-xr-x.  2 root root    46  3月  1 21:24 healthcheck
drwxr-xr-x.  3 root root    44  3月  1 21:24 install
drwxr-xr-x.  2 root root    41  3月  1 21:24 internal-docs
drwxr-xr-x.  2 root root    82  3月  1 21:24 openapi
-rwxr-xr-x.  1 root root  1526  3月  1 21:24 push.sh
-rw-r--r--.  1 root root  1668  3月  1 21:24 shippable.jobs.yml
-rw-r--r--.  1 root root  2640  3月  1 21:24 shippable.resources.yml
-rw-r--r--.  1 root root   488  3月  1 21:24 shippable.triggers.yml
drwxr-xr-x.  2 root root   196  3月  1 21:24 staging

sock-shopのデプロイ

namespaceの作成

sock-shopはnamespaceとして「sock-shop」を使用するので、あらかじめ作成する。

$ kubectl create namespace sock-shop
namespace/sock-shop created
$ kubectl get namespace
NAME              STATUS   AGE
default           Active   4d15h
kube-node-lease   Active   4d15h
kube-public       Active   4d15h
kube-system       Active   4d15h
sock-shop         Active   7s

デプロイ

$ cd microservices-demo/deploy/kubernetes/
$ kubectl apply -f complete-demo.yaml
service/carts-db created
service/carts created
service/catalogue-db created
service/catalogue created
service/front-end created
service/orders-db created
service/orders created
service/payment created
service/queue-master created
service/rabbitmq created
service/shipping created
service/user-db created
service/user created
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"

サービスはデプロイできたけど、deploymentは失敗。どうもAPIのバージョンがダメらしい。
とりあえず、削除しておく。

$ kubectl delete -f complete-demo.yaml
service "carts-db" deleted
service "carts" deleted
service "catalogue-db" deleted
service "catalogue" deleted
service "front-end" deleted
service "orders-db" deleted
service "orders" deleted
service "payment" deleted
service "queue-master" deleted
service "rabbitmq" deleted
service "shipping" deleted
service "user-db" deleted
service "user" deleted
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"
unable to recognize "complete-demo.yaml": no matches for kind "Deployment" in version "extensions/v1beta1"

yamlファイルの修正

kubernetesのバージョンとdeploymentがサポートしているAPIバージョンを確認する。

$ kubectl version --short
Client Version: v1.17.3
Server Version: v1.17.3
$ kubectl explain deployment
KIND:     Deployment
VERSION:  apps/v1
・・・

yamlファイルのAPIバージョンを確認する。

$ head complete-demo.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: carts-db
  labels:
    name: carts-db
  namespace: sock-shop
spec:
  replicas: 1
  template:

APIのバージョンがkubernetes V1.17ではサポートしていないので、書き換える必要がある。
yamlファイルの変更箇所は以下の2点

変更箇所 before after
apiVersion extensions/v1beta1 apps/v1
spec - (追記) selecter -> matchLabels

complete-demo.yamlファイルだと一つのファイルにすべて書かれてて、編集がむずかしかったので、manifestsディレクトリのバラバラのyamlファイルを編集することにする。
このシェルスクリプトで一括編集する。

convert.sh
#!/bin/bash

ls *dep* > filelist

echo "Target Files"
echo "---------"
cat filelist

mkdir org

while read FILE
do
        NAME=`grep name: $FILE | head -1 |awk -F':' '{print $2}'`
        mv $FILE org/$FILE.org
        sed -e 's/extensions\/v1beta1/apps\/v1/g' ./org/$FILE.org > ./$FILE-hogehogehoge
        sed -e '/template/i \ \ selector:' ./$FILE-hogehogehoge > ./$FILE-hogehoge
        sed -e '/template/i \ \ \ \ matchLabels:' ./$FILE-hogehoge > ./$FILE-hoge
        sed -e "/template/i \ \ \ \ \ \ name:$NAME" ./$FILE-hoge > ./$FILE
done < filelist

rm filelist
rm *hoge

もっとうまい書き方ありそうな気もするけど・・・
変換後のファイルとオリジナルをdiffで確認しておく。

$ diff carts-db-dep.yaml org/carts-db-dep.yaml.org
2c2
< apiVersion: apps/v1
---
> apiVersion: extensions/v1beta1
11,13d10
<   selector:
<     matchLabels:
<       name: carts-db

構築しているKubernetesのクラスタはPC上にVMを切って作ってるので、ロードバランサはない。
sock-shopのfront-end-svcはロードバランサになっているので、NodePortに変更しておく。

front-end-svc.yaml
---
apiVersion: v1
kind: Service
metadata:
  name: front-end
  labels:
    name: front-end
  namespace: sock-shop
spec:
 # type: LoadBalancer
  type: NodePort
  ports:
  - port: 80
    targetPort: 8079
    nodePort: 30001
  selector:
    name: front-end

デプロイ(2回目)

$ kubectl apply -f manifests/
deployment.apps/carts-db created
service/carts-db created
deployment.apps/carts created
service/carts created
deployment.apps/catalogue-db created
service/catalogue-db created
deployment.apps/catalogue created
service/catalogue created
deployment.apps/front-end created
service/front-end created
namespace/loadtest created
deployment.apps/orders-db created
service/orders-db created
deployment.apps/orders created
service/orders created
deployment.apps/payment created
service/payment created
deployment.apps/queue-master created
service/queue-master created
deployment.apps/rabbitmq created
service/rabbitmq created
deployment.apps/session-db created
service/session-db created
deployment.apps/shipping created
service/shipping created
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
namespace/sock-shop configured
deployment.apps/user-db created
service/user-db created
deployment.apps/user created
service/user created

一つWarrningが出ているが、最初のデプロイしたときの何かが残ってたのかな?あとで調べよう。

デプロイされたPodとServiceを確認する。

$ kubectl get pods -n sock-shop
NAME                            READY   STATUS    RESTARTS   AGE
carts-5fc45568c4-r4fqk          1/1     Running   0          9m33s
carts-db-64ff6c747f-vlghp       1/1     Running   0          9m33s
catalogue-69b7ff5bb4-pm988      0/1     Running   0          9m32s
catalogue-db-69cf48ff8-5sztl    1/1     Running   0          9m33s
front-end-6fc8dff6ff-4lxmn      1/1     Running   0          9m32s
orders-74c6b45f4f-42rns         1/1     Running   0          9m32s
orders-db-df75f545f-vvth6       1/1     Running   0          9m32s
payment-7f95f9f77-n5v7p         0/1     Running   0          9m32s
queue-master-85f5644bf5-pgdkt   0/1     Running   0          9m32s
rabbitmq-c7b9db6cc-bb5hn        2/2     Running   0          9m32s
session-db-67fd8d6d7-f2s96      1/1     Running   0          9m32s
shipping-745b9d8755-nvqng       0/1     Running   0          9m31s
user-559d7f794f-65sxs           1/1     Running   0          9m31s
user-db-87f548457-ldqvh         1/1     Running   0          9m31s
$ kubectl get svc -n sock-shop
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)             AGE
carts          ClusterIP   10.110.120.109   <none>        80/TCP              10m
carts-db       ClusterIP   10.109.174.164   <none>        27017/TCP           10m
catalogue      ClusterIP   10.108.116.129   <none>        80/TCP              10m
catalogue-db   ClusterIP   10.110.226.123   <none>        3306/TCP            10m
front-end      NodePort    10.107.92.139    <none>        80:30001/TCP        10m
orders         ClusterIP   10.107.232.34    <none>        80/TCP              10m
orders-db      ClusterIP   10.103.162.21    <none>        27017/TCP           10m
payment        ClusterIP   10.108.6.76      <none>        80/TCP              10m
queue-master   ClusterIP   10.97.129.100    <none>        80/TCP              10m
rabbitmq       ClusterIP   10.104.194.103   <none>        5672/TCP,9090/TCP   10m
session-db     ClusterIP   10.108.6.216     <none>        6379/TCP            10m
shipping       ClusterIP   10.105.98.147    <none>        80/TCP              10m
user           ClusterIP   10.100.7.156     <none>        80/TCP              10m
user-db        ClusterIP   10.108.238.79    <none>        27017/TCP           10m

動作確認

gatewayのブラウザから画面を確認する。
 IPアドレス:workerノードのIPアドレス
 ポート:front-end Serviceのポート

なお、IPアドレスはどちらのworkerノードのIPアドレスでも表示された。Serviceの作りとか後で確認しよう。

image.png

まとめ

手順に沿ってデプロイしただけだけど、マイクロサービスのデモが簡単にできた。
今はとりあえず作っただけなので、マイクロサービスとは何かみたいなことはよくわからないけど、この環境を使って色々と調べていこう。

dingtianhongjie
青いベンダーでインフラのアーキテクトやってます。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away