フューチャー技術ブログ

Linkerdで始めるサービスメッシュ

はじめに

こんにちは。TIGの村田です。CNCF連載の第3回はLinkerdについて書いていきます。

Linkerdとは

LinkerdはCNCFのIncubating projectsに所属しているサービスメッシュプロダクトです。Twitter社における大規模なマイクロサービス運用の知見をもとに作られたという誕生エピソードが以下のブログで語られています。

Linkerd: Twitter-style Operability for Microservices

超軽量であることが特徴で、Podにinjectされるsidecar proxyはRustで実装されています。また、zero configでアプリのソースコードを全くいじることなく簡単に動作させることが可能です。

Linkerdを使ってみる

環境

$ kubectl version --short
Client Version: v1.16.6-beta.0
Server Version: v1.15.12-gke.20

今回はGKEを使います。

we need to ensure you have access to a Kubernetes cluster running 1.13 or later

Linkerdを使う際にはKubernetesは1.13以上である必要があるとのことですが問題なさそうですね。

Linkerd CLIのインストール

Mac(macOS Mojave バージョン10.14.6)を利用しているので、 brew でインストールしました。

$ brew install linkerd
$ linkerd version
Client version: stable-2.8.1
Server version: unavailable

クラスタの事前チェック

To ensure that the control plane will install correctly, the Linkerd CLI can check and validate that everything is configured correctly.

Linkerd CLIを利用して事前にインストールの準備が完了しているか確認することができるようです。

$ linkerd check --pre
kubernetes-api
--------------
√ can initialize the client
√ can query the Kubernetes API

kubernetes-version
------------------
√ is running the minimum Kubernetes API version
√ is running the minimum kubectl version

pre-kubernetes-setup
--------------------
√ control plane namespace does not already exist
√ can create non-namespaced resources
√ can create ServiceAccounts
√ can create Services
√ can create Deployments
√ can create CronJobs
√ can create ConfigMaps
√ can create Secrets
√ can read Secrets
√ can read extension-apiserver-authentication configmap
√ no clock skew detected

pre-kubernetes-capability
-------------------------
√ has NET_ADMIN capability
√ has NET_RAW capability

linkerd-version
---------------
√ can determine the latest version
√ cli is up-to-date

Status check results are √

結構丁寧にチェックされている印象です。前述した互換性のあるKubernetesバージョン等についてもこのコマンドでチェックできるので、インストール時は常に実施しておくのが良いでしょう。

LinkerdをKubernetesクラスタへインストール

Linkerdインストール時に利用されるmanifestは以下のコマンドで作成されます。

$ linkerd install
---
###
### Linkerd Namespace
###
---
kind: Namespace
apiVersion: v1
metadata:
name: linkerd
annotations:
linkerd.io/inject: disabled
labels:
linkerd.io/is-control-plane: "true"
config.linkerd.io/admission-webhooks: disabled
linkerd.io/control-plane-ns: linkerd
...
(中略)
...
- configMap:
items:
- key: grafana.ini
path: grafana.ini
- key: datasources.yaml
path: provisioning/datasources/datasources.yaml
- key: dashboards.yaml
path: provisioning/dashboards/dashboards.yaml
name: linkerd-grafana-config
name: grafana-config
- emptyDir:
medium: Memory
name: linkerd-identity-end-entity

筆者の手元では3,160行のmanifestが出力されました。これをapplyしていきます。

$ linkerd install | kubectl apply -f -

Linkerdのインストール状況をチェックできるコマンドが以下です。インストールコンポーネントが多いので、こういったステータスチェック用のコマンドが用意されているのはありがたい&安心感があります。

$ linkerd check
kubernetes-api
--------------
√ can initialize the client
√ can query the Kubernetes API

kubernetes-version
------------------
√ is running the minimum Kubernetes API version
√ is running the minimum kubectl version

linkerd-existence
-----------------
'linkerd-config' config map exists
√ heartbeat ServiceAccount exist
√ control plane replica sets are ready
√ no unschedulable pods
√ controller pod is running
√ can initialize the client
√ can query the control plane API

linkerd-config
--------------
√ control plane Namespace exists
√ control plane ClusterRoles exist
√ control plane ClusterRoleBindings exist
√ control plane ServiceAccounts exist
√ control plane CustomResourceDefinitions exist
√ control plane MutatingWebhookConfigurations exist
√ control plane ValidatingWebhookConfigurations exist
√ control plane PodSecurityPolicies exist
√ control plane PodSecurityPolicies exist

linkerd-identity
----------------
√ certificate config is valid
√ trust anchors are using supported crypto algorithm
√ trust anchors are within their validity period
√ trust anchors are valid for at least 60 days
√ issuer cert is using supported crypto algorithm
√ issuer cert is within its validity period
√ issuer cert is valid for at least 60 days
√ issuer cert is issued by the trust anchor

linkerd-api
-----------
√ control plane pods are ready
√ control plane self-check
√ [kubernetes] control plane can talk to Kubernetes
√ [prometheus] control plane can talk to Prometheus
√ tap api service is running

linkerd-version
---------------
√ can determine the latest version
√ cli is up-to-date

control-plane-version
---------------------
√ control plane is up-to-date
√ control plane and cli versions match

linkerd-addons
--------------
'linkerd-config-addons' config map exists

linkerd-grafana
---------------
√ grafana add-on service account exists
√ grafana add-on config map exists
√ grafana pod is running

Status check results are √

Linkerdダッシュボードの確認

check コマンドが通ったのでダッシュボードを見てみます。

$ linkerd dashboard &
http://localhost:50750
Grafana dashboard available at:
http://localhost:50750/grafana
Opening Linkerd dashboard in the default browser

画面までサクッとたどり着くことができました。 Installs in seconds with zero config と謳っているだけあって非常にスムーズです。

デモアプリを使ってLinkerdを見る

Linkerdのデモアプリとして emojivoto をインストールします。

curl -sL https://run.linkerd.io/emojivoto.yml | kubectl apply -f -

emojivotoのnamespaceにPodがデプロイされていることが確認できました。

$ kubectl get po -n emojivoto
NAME READY STATUS RESTARTS AGE
emoji-58d98d997c-vmfkq 1/1 Running 0 3m14s
vote-bot-54d5ff884-55xpf 1/1 Running 0 3m13s
voting-7d5c4f77d-rzqg4 1/1 Running 0 3m11s
web-7bd5fcb58b-85z9g 1/1 Running 0 3m10s

実際にアプリにもアクセスしてみます。

$ kubectl -n emojivoto port-forward svc/web-svc 8080:80

http://localhost:8080 を開いた結果が下の画面。

気に入った絵文字を選んで投票すると、リーダーボートの順位に反映されるアプリのようです。

ちなみにドーナツの絵文字を選ぶと404エラーが返ってくるようにあえて仕込まれているようです。このあたりはさすがサービスメッシュ向けデモアプリといったところでしょうか。

Linkerdのinjection

ちなみにこのタイミングでLinkerdのダッシュボードを見ても、 emojivoto に関してのトラフィック情報は表示されません。Linkerdのinjectionが必要であり、 Meshed というカラムを見るとそれが分かります。

以下コマンドでlinkerdのinjectionを行います。

$ kubectl get -n emojivoto deploy -o yaml \
| linkerd inject - \
| kubectl apply -f -

先程 emojivoto のPod群を参照したときは各Podでは1つのコンテナが稼働していましたが、sidecar injectionによりPodごとに2つのコンテナが稼働していることが確認できます。

$ kubectl get po -n emojivoto
NAME READY STATUS RESTARTS AGE
emoji-64784ff78d-dcmhz 2/2 Running 0 3m10s
vote-bot-779769dcfc-5krf6 2/2 Running 0 3m9s
voting-784769cf6f-4sd77 2/2 Running 0 3m9s
web-86cbd9db6c-b2sr8 2/2 Running 0 3m8s

Linkerdのダッシュボードも見てみます。 Meshed のカラムが 1/1 となっていることが確認でき、各種メトリクスの値も連携されています。

本当はGrafanaの画面にてPrometheus経由で取得されたメトリクスも参照したかったのですが、 いくつか試してみても見られず。。悔しいのでここは改めてリベンジします。

さいごに

サービスメッシュのプロダクトであるということで気になって触ってみましたが、とても簡単に始めることができました。

サービスメッシュと言えばCNCFのGraduated projectsであるEnvoyをベースとしたIstioの名前をよく聞きますが、Linkerdのほうがより軽量で簡単に導入できる印象を受けました。ただ、機能としてはIstioのほうが充実していそうで、実際に利用する際はしっかりとした比較検証が必要だなと感じました。

さて、明日はCNCF連載の第4回目です!ぜひお楽しみに。