# RustでService Mesh RustのLT会 Shinjuku.rs #4 @FORCIA --- # About Me * @11Takanori * 興味 * Rust * Microservices * Service Mesh --- # linkerd2-proxy * Service Mesh用のSidecar Proxy * Rustで実装されている * RustやTokioのコミッター数名が開発に参加している ![](https://i.imgur.com/jrArSp4.png) --- # Service Mesh アプリケーションの代わりにネットワーク層の仕事をするレイヤ。PodにSidecar ProxyをinjectしてService Meshを実現する。 * ロードバランシング * メトリクスの取得/送信 * リトライ/タイムアウト * Observabilityの向上 --- # Linkerd ![](https://i.imgur.com/yuKhpFo.png) https://linkerd.io/2/reference/architecture/ --- - 内向きのトラフィックも外向きのトラフィックもすべてlinkerd2-proxyを通る - 課題1: proxyを通ることによるレイテンシをできるだけ小さくする必要がある - Microservicesだと、linkerd2-proxyがinjectされたPod自体がたくさんある - 課題2:Podが数千個あってもメモリを使いすぎないようする必要がある 高速でメモリ使用量が少ないRustが適している --- # Proxyの内部 * Tokio、Towerなどが使われている * Event-drivenで非同期 * 大きく分けるとoutbound用、inbound用、admin用のスレッドが起動されている --- ## 実装の一部(1) New Type Patternで外部のクレートをwrapし、疎結合にする ``` use tower::builder::ServiceBuilder; use tower::layer::util::{Identity, Stack}; #[derive(Clone, Debug)] pub struct Builder<L>(ServiceBuilder<L>); pub fn builder() -> Builder<Identity> { Builder(ServiceBuilder::new()) } impl<L> Builder<L> { ... } ``` --- ## 実装の一部(2) 別のスレッドのある処理が完了しているかどうかで、処理を分岐させる ``` // 別のスレッドのある処理が終わったかを気にするスレッドに渡す #[derive(Clone, Debug)] pub struct Readiness(Weak<()>); // Readinessが気にする処理を行うスレッドに渡す #[derive(Clone, Debug)] pub struct Latch(Arc<()>); impl Readiness { pub fn new() -> (Readiness, Latch) { let r = Arc::new(()); (Readiness(Arc::downgrade(&r)), Latch(r)) } pub fn is_ready(&self) -> bool { // upgrade()はOption<Arc<T>>を返す。解放済みならNoneを返す。 self.0.upgrade().is_none() } } impl Latch { pub fn release(self) { drop(self); } } ``` --- # まとめ * Rustで実装されたlinkerd2-proxyというProxyがある * RustやTokioのコミッターが自分で作ったものを使ってlinkerd2-proxyを開発している --- # 参考 * https://linkerd.io/ * https://www.infoq.com/articles/linkerd-v2-production-adoption * https://github.com/linkerd/linkerd2-proxy --- # Linkerdを試す * LinkerdをinstallしたKubernetesの環境構築 * おそらく1番手数が少なくて手軽 ``` $ snap install microk8s --classic --channel=1.14/edge $ microk8s.enable linkerd:proxy-auto-inject ```
{"metaMigratedAt":"2023-06-14T21:45:03.303Z","metaMigratedFrom":"Content","title":"RustでService Mesh","breaks":true,"contributors":"[{\"id\":\"b71f687b-9123-4307-927d-766358321e7c\",\"add\":4608,\"del\":2436}]"}
    3300 views