{"id":9508,"date":"2025-11-23T17:12:05","date_gmt":"2025-11-23T09:12:05","guid":{"rendered":"\/?p=9508"},"modified":"2025-11-23T17:12:05","modified_gmt":"2025-11-23T09:12:05","slug":"%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%85%a5%e9%97%a8-k8s%ef%bc%9a%e7%90%86%e8%a7%a3-etcd","status":"publish","type":"post","link":"\/?p=9508","title":{"rendered":"\u4ece\u96f6\u5f00\u59cb\u5165\u95e8 K8s\uff1a\u7406\u89e3 etcd"},"content":{"rendered":"<h2><strong>1. \u5f15\u8a00<\/strong><\/h2>\n<p>Kubernetes\uff08\u7b80\u79f0 K8s\uff09\u4f5c\u4e3a\u5bb9\u5668\u7f16\u6392\u9886\u57df\u7684\u6807\u51c6\uff0c\u5176\u6838\u5fc3\u4f9d\u8d56\u4e8e\u5206\u5e03\u5f0f\u952e\u503c\u5b58\u50a8\u7cfb\u7edf <strong>etcd<\/strong> \u5b9e\u73b0\u96c6\u7fa4\u72b6\u6001\u7ba1\u7406\u3002etcd \u7684\u9ad8\u53ef\u7528\u6027\u3001\u5f3a\u4e00\u81f4\u6027\u548c\u9ad8\u6027\u80fd\u7279\u6027\uff0c\u4f7f\u5176\u6210\u4e3a K8s \u63a7\u5236\u5e73\u9762\u7684\u201c\u5927\u8111\u201d\u3002\u672c\u6587\u5c06\u4ece\u96f6\u5f00\u59cb\uff0c\u6df1\u5165\u89e3\u6790 etcd \u7684\u539f\u7406\u3001\u5728 K8s \u4e2d\u7684\u6838\u5fc3\u4f5c\u7528\uff0c\u5e76\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u4e0e\u573a\u666f\u5b9e\u8df5\u5e2e\u52a9\u8bfb\u8005\u638c\u63e1\u5176\u5e94\u7528\u65b9\u6cd5\u3002<\/p>\n<p>\u7b80\u5355\u6765\u8bf4\uff0c\u4f60\u53ef\u4ee5\u628a etcd \u901a\u4fd7\u5730\u7406\u89e3\u4e3a<strong>\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u201c\u5171\u4eab\u8bb0\u4e8b\u672c\u201d\u6216\u201c\u914d\u7f6e\u5927\u7ba1\u5bb6\u201d<\/strong>\u3002\u5b83\u7684\u540d\u5b57\u7cbe\u51c6\u5730\u6982\u62ec\u4e86\u5176\u6838\u5fc3\u4ef7\u503c\uff1a\u4e00\u4e2a\u9ad8\u53ef\u7528\u3001\u5f3a\u4e00\u81f4\u6027\u7684\u5206\u5e03\u5f0f\u952e\u503c\u5b58\u50a8\u6570\u636e\u5e93\uff0c\u4e13\u95e8\u7528\u4e8e\u5b88\u62a4\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u6700\u5173\u952e\u7684\u6570\u636e<\/p>\n<hr \/>\n<h2><strong>2. \u6280\u672f\u80cc\u666f<\/strong><\/h2>\n<h3><strong>2.1 etcd \u7684\u8bde\u751f\u4e0e\u5b9a\u4f4d<\/strong><\/h3>\n<ul>\n<li>\n<p><strong>\u8d77\u6e90<\/strong>\uff1a\u7531 CoreOS \u56e2\u961f\u5f00\u53d1\uff0c\u6700\u521d\u4e3a\u89e3\u51b3\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u4e00\u81f4\u6027\u95ee\u9898\u3002<\/p>\n<\/li>\n<li>\n<p>\u8bbe\u8ba1\u76ee\u6807\uff1a<\/p>\n<ul>\n<li><strong>\u5f3a\u4e00\u81f4\u6027<\/strong>\uff1a\u57fa\u4e8e Raft \u5171\u8bc6\u7b97\u6cd5\u4fdd\u8bc1\u6570\u636e\u4e00\u81f4\u6027\u3002<\/li>\n<li><strong>\u9ad8\u53ef\u7528\u6027<\/strong>\uff1a\u652f\u6301\u591a\u8282\u70b9\u96c6\u7fa4\u90e8\u7f72\uff0c\u5bb9\u5fcd\u8282\u70b9\u6545\u969c\u3002<\/li>\n<li><strong>\u8f7b\u91cf\u7ea7<\/strong>\uff1a\u4f4e\u8d44\u6e90\u6d88\u8017\uff0c\u9002\u5408\u5d4c\u5165\u5230 Kubernetes \u7b49\u7cfb\u7edf\u4e2d\u3002<\/li>\n<\/ul>\n<p>etcd \u8fd9\u4e2a\u540d\u5b57\u7684\u7531\u6765\u786e\u5b9e\u633a\u6709\u610f\u601d\u7684\uff0c\u5b83\u5de7\u5999\u5730\u5c06\u4e24\u4e2a\u6982\u5ff5\u7ec4\u5408\u5728\u4e86\u4e00\u8d77\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">\u7ec4\u6210\u5143\u7d20<\/th>\n<th style=\"text-align: left;\">\u542b\u4e49\u89e3\u6790<\/th>\n<th style=\"text-align: left;\">\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\"><strong><code>etc<\/code><\/strong><\/td>\n<td style=\"text-align: left;\">\u6e90\u4e8e Unix\/Linux \u7cfb\u7edf\u4e2d\u7684 <code>\/etc<\/code>\u76ee\u5f55<\/td>\n<td style=\"text-align: left;\">\u8be5\u76ee\u5f55\u7528\u4e8e\u5b58\u653e<strong>\u7cfb\u7edf\u914d\u7f6e<\/strong>\u6587\u4ef6\uff0c\u5982\u7f51\u7edc\u914d\u7f6e\u3001\u7528\u6237\u4fe1\u606f\u7b49 \u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong><code>d<\/code><\/strong><\/td>\n<td style=\"text-align: left;\">\u4ee3\u8868 <strong>D<\/strong>istributed\uff08\u5206\u5e03\u5f0f\uff09<\/td>\n<td style=\"text-align: left;\">\u8868\u660e\u5b83\u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u7cfb\u7edf\uff0c\u800c\u975e\u5355\u673a\u670d\u52a1 \u3002<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><strong>\u5168\u79f0\u542b\u4e49<\/strong><\/td>\n<td style=\"text-align: left;\"><strong>\u5206\u5e03\u5f0f\u914d\u7f6e\u76ee\u5f55<\/strong><\/td>\n<td style=\"text-align: left;\">\u5408\u8d77\u6765\uff0cetcd \u7684\u5b9a\u4f4d\u5c31\u662f\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406<strong>\u5206\u5e03\u5f0f\u7cfb\u7edf<\/strong>\u5173\u952e<strong>\u914d\u7f6e<\/strong>\u4fe1\u606f\u7684\u670d\u52a1<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/li>\n<\/ul>\n<p>etcd \u7684\u540d\u5b57\u76f4\u63a5\u53cd\u6620\u4e86\u5b83\u7684\u6838\u5fc3\u8bbe\u8ba1\u76ee\u6807\uff1a\u6210\u4e3a\u5206\u5e03\u5f0f\u7cfb\u7edf\u7684\u201c\u795e\u7ecf\u4e2d\u67a2\u201d\u6216\u201c\u914d\u7f6e\u4ed3\u5e93\u201d\u3002\u5728\u4e00\u4e2a\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u901a\u5e38\u6709\u591a\u4e2a\u670d\u52a1\u5b9e\u4f8b\u9700\u8981\u8bbf\u95ee\u76f8\u540c\u7684\u914d\u7f6e\u4fe1\u606f\uff08\u4f8b\u5982\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32\u3001\u529f\u80fd\u5f00\u5173\u3001\u670d\u52a1\u5730\u5740\u7b49\uff09\u3002etcd \u5c31\u50cf\u4e00\u4e2a\u5206\u5e03\u5f0f\u7684 <code>\/etc<\/code>\u76ee\u5f55\uff0c\u4e3a\u6574\u4e2a\u96c6\u7fa4\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7edf\u4e00\u3001\u53ef\u9760\u7684\u5730\u65b9\u6765\u5b58\u50a8\u548c\u83b7\u53d6\u8fd9\u4e9b\u5173\u952e\u6570\u636e<\/p>\n<h3><strong>2.2 etcd \u5728 K8s \u4e2d\u7684\u89d2\u8272<\/strong><\/h3>\n<ul>\n<li><strong>\u96c6\u7fa4\u72b6\u6001\u5b58\u50a8<\/strong>\uff1a\u4fdd\u5b58 K8s \u7684\u6240\u6709\u914d\u7f6e\u6570\u636e\uff08\u5982 Pod\u3001Service\u3001Deployment \u72b6\u6001\uff09\u3002<\/li>\n<li><strong>\u914d\u7f6e\u7ba1\u7406<\/strong>\uff1a\u5b58\u50a8\u96c6\u7fa4\u7ea7\u522b\u7684\u914d\u7f6e\u4fe1\u606f\uff08\u5982\u8ba4\u8bc1\u8bc1\u4e66\u3001\u7f51\u7edc\u7b56\u7565\uff09\u3002<\/li>\n<li><strong>\u9886\u5bfc\u8005\u9009\u4e3e<\/strong>\uff1a\u652f\u6301 K8s \u7ec4\u4ef6\uff08\u5982 Controller Manager\uff09\u7684\u9ad8\u53ef\u7528\u9009\u4e3e\u3002<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u8bf4\uff0cetcd \u5145\u5f53\u4e86 Kubernetes \u96c6\u7fa4\u7684\u201c\u8bb0\u5fc6\u4f53\u201d\uff0c\u6240\u6709\u7ec4\u4ef6\uff08\u5982 kube-apiserver\u3001kube-controller-manager\uff09\u90fd\u901a\u8fc7\u5b83\u4e0e\u8fd9\u4efd\u552f\u4e00\u7684\u6570\u636e\u8bb0\u5f55\u8fdb\u884c\u4ea4\u4e92\uff0c\u4ece\u800c\u534f\u540c\u5de5\u4f5c\uff0c\u786e\u4fdd\u96c6\u7fa4\u7684\u671f\u671b\u72b6\u6001\u4e0e\u5b9e\u9645\u72b6\u6001\u4e00\u81f4<\/p>\n<h3><strong>2.3 \u6280\u672f\u6311\u6218<\/strong><\/h3>\n<ul>\n<li><strong>\u6570\u636e\u4e00\u81f4\u6027<\/strong>\uff1a\u5982\u4f55\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e0b\u4fdd\u8bc1\u8bfb\u5199\u64cd\u4f5c\u7684\u5f3a\u4e00\u81f4\u6027\uff1f<\/li>\n<li><strong>\u6027\u80fd\u4f18\u5316<\/strong>\uff1a\u5927\u89c4\u6a21\u96c6\u7fa4\u4e0b\u5982\u4f55\u5e73\u8861\u8bfb\u5199\u5ef6\u8fdf\u4e0e\u541e\u5410\u91cf\uff1f<\/li>\n<li><strong>\u5b89\u5168\u9632\u62a4<\/strong>\uff1a\u5982\u4f55\u9632\u6b62\u672a\u6388\u6743\u8bbf\u95ee\u548c\u6570\u636e\u6cc4\u9732\uff1f<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>3. \u5e94\u7528\u4f7f\u7528\u573a\u666f<\/strong><\/h2>\n<h3><strong>3.1 \u573a\u666f1\uff1aK8s \u96c6\u7fa4\u72b6\u6001\u5b58\u50a8<\/strong><\/h3>\n<ul>\n<li><strong>\u76ee\u6807<\/strong>\uff1a\u901a\u8fc7 etcd \u5b58\u50a8\u548c\u7ba1\u7406 K8s \u7684 Pod\u3001Deployment \u7b49\u8d44\u6e90\u72b6\u6001\u3002<\/li>\n<\/ul>\n<h3><strong>3.2 \u573a\u666f2\uff1a\u5206\u5e03\u5f0f\u9501\u4e0e\u9886\u5bfc\u8005\u9009\u4e3e<\/strong><\/h3>\n<ul>\n<li><strong>\u76ee\u6807<\/strong>\uff1a\u5229\u7528 etcd \u7684\u79df\u7ea6\uff08Lease\uff09\u673a\u5236\u5b9e\u73b0 K8s \u7ec4\u4ef6\u7684\u6545\u969c\u8f6c\u79fb\u3002<\/li>\n<\/ul>\n<h3><strong>3.3 \u573a\u666f3\uff1a\u914d\u7f6e\u4e2d\u5fc3<\/strong><\/h3>\n<ul>\n<li><strong>\u76ee\u6807<\/strong>\uff1a\u5c06\u96c6\u7fa4\u914d\u7f6e\uff08\u5982\u6570\u636e\u5e93\u8fde\u63a5\u4e32\uff09\u5b58\u50a8\u5728 etcd \u4e2d\uff0c\u4f9b\u5404\u8282\u70b9\u52a8\u6001\u8bfb\u53d6\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>4. \u4e0d\u540c\u573a\u666f\u4e0b\u8be6\u7ec6\u4ee3\u7801\u5b9e\u73b0<\/strong><\/h2>\n<h3><strong>4.1 \u73af\u5883\u51c6\u5907<\/strong><\/h3>\n<h4><strong>4.1.1 \u5f00\u53d1\u73af\u5883\u914d\u7f6e<\/strong><\/h4>\n<ul>\n<li>\n<p>\u5de5\u5177\u94fe\uff1a<\/p>\n<ul>\n<li><code>etcdctl<\/code>\uff1aetcd \u7684\u547d\u4ee4\u884c\u5ba2\u6237\u7aef\uff08\u7248\u672c\u9700\u4e0e\u96c6\u7fa4\u5339\u914d\uff09\u3002<\/li>\n<li><code>Go<\/code> \u6216 <code>Python<\/code> SDK\uff1a\u7528\u4e8e\u7f16\u7a0b\u4ea4\u4e92\uff08\u672c\u6587\u4ee5 Go \u4e3a\u4f8b\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u672c\u5730\u90e8\u7f72 etcd<\/p>\n<p>\uff08\u5355\u673a\u6a21\u5f0f\uff09\uff1a<\/p>\n<pre><code class=\"language-javascript\"># \u4e0b\u8f7d\u5e76\u542f\u52a8 etcd\uff08Linux\/macOS\uff09\nwget https:\/\/github.com\/etcd-io\/etcd\/releases\/download\/v3.5.0\/etcd-v3.5.0-linux-amd64.tar.gz\ntar xvf etcd-v3.5.0-linux-amd64.tar.gz\ncd etcd-v3.5.0-linux-amd64\n.\/etcd  # \u9ed8\u8ba4\u76d1\u542c 127.0.0.1:2379<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong>4.1.2 \u9a8c\u8bc1 etcd \u670d\u52a1<\/strong><\/h4>\n<pre><code class=\"language-javascript\"># \u5199\u5165\u952e\u503c\u5bf9\n.\/etcdctl put mykey &quot;Hello etcd&quot;\n# \u8bfb\u53d6\u952e\u503c\u5bf9\n.\/etcdctl get mykey\n# \u8f93\u51fa\uff1amykey Hello etcd<\/code><\/pre>\n<hr \/>\n<h3><strong>4.2 \u573a\u666f1\uff1aK8s \u96c6\u7fa4\u72b6\u6001\u5b58\u50a8\u6a21\u62df<\/strong><\/h3>\n<h4><strong>4.2.1 \u4f7f\u7528 etcdctl \u7ba1\u7406 K8s \u8d44\u6e90\u72b6\u6001<\/strong><\/h4>\n<pre><code class=\"language-javascript\"># \u6a21\u62df\u5b58\u50a8\u4e00\u4e2a Deployment \u72b6\u6001\n.\/etcdctl put \/k8s\/deployments\/nginx &quot;replicas=3,image=nginx:1.23&quot;\n\n# \u8bfb\u53d6\u72b6\u6001\n.\/etcdctl get \/k8s\/deployments\/nginx\n# \u8f93\u51fa\uff1a\/k8s\/deployments\/nginx replicas=3,image=nginx:1.23<\/code><\/pre>\n<h4><strong>4.2.2 Go \u4ee3\u7801\u5b9e\u73b0\u72b6\u6001\u8bfb\u5199<\/strong><\/h4>\n<pre><code class=\"language-javascript\">package main\n\nimport (\n    &quot;context&quot;\n    &quot;fmt&quot;\n    &quot;go.etcd.io\/etcd\/clientv3&quot;\n    &quot;time&quot;\n)\n\nfunc main() {\n    \/\/ \u521b\u5efa etcd \u5ba2\u6237\u7aef\n    cli, err := clientv3.New(clientv3.Config{\n        Endpoints:   []string{&quot;127.0.0.1:2379&quot;}, \/\/ etcd \u5730\u5740\n        DialTimeout: 5 * time.Second,\n    })\n    if err != nil {\n        panic(err)\n    }\n    defer cli.Close()\n\n    \/\/ \u5199\u5165\u72b6\u6001\n    _, err = cli.Put(context.Background(), &quot;\/k8s\/deployments\/nginx&quot;, &quot;replicas=3,image=nginx:1.23&quot;)\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ \u8bfb\u53d6\u72b6\u6001\n    resp, err := cli.Get(context.Background(), &quot;\/k8s\/deployments\/nginx&quot;)\n    if err != nil {\n        panic(err)\n    }\n    for _, kv := range resp.Kvs {\n        fmt.Printf(&quot;Key: %s, Value: %s\\n&quot;, kv.Key, kv.Value)\n    }\n}<\/code><\/pre>\n<hr \/>\n<h3><strong>4.3 \u573a\u666f2\uff1a\u5206\u5e03\u5f0f\u9501\u5b9e\u73b0<\/strong><\/h3>\n<h4><strong>4.3.1 \u4f7f\u7528 etcd \u7684\u79df\u7ea6\u673a\u5236<\/strong><\/h4>\n<pre><code class=\"language-javascript\">package main\n\nimport (\n    &quot;context&quot;\n    &quot;fmt&quot;\n    &quot;go.etcd.io\/etcd\/clientv3&quot;\n    &quot;time&quot;\n)\n\nfunc main() {\n    cli, err := clientv3.New(clientv3.Config{Endpoints: []string{&quot;127.0.0.1:2379&quot;}})\n    if err != nil {\n        panic(err)\n    }\n    defer cli.Close()\n\n    \/\/ \u521b\u5efa\u79df\u7ea6\uff0810\u79d2\u8fc7\u671f\uff09\n    leaseResp, err := cli.Grant(context.Background(), 10)\n    if err != nil {\n        panic(err)\n    }\n\n    \/\/ \u5c1d\u8bd5\u83b7\u53d6\u9501\uff08Key: \/locks\/mylock\uff09\n    _, err = cli.Put(context.Background(), &quot;\/locks\/mylock&quot;, &quot;locked&quot;, clientv3.WithLease(leaseResp.ID))\n    if err != nil {\n        panic(err)\n    }\n    fmt.Println(&quot;\u6210\u529f\u83b7\u53d6\u9501&quot;)\n\n    \/\/ \u6a21\u62df\u4e1a\u52a1\u5904\u7406\n    time.Sleep(5 * time.Second)\n\n    \/\/ \u91ca\u653e\u9501\uff08\u79df\u7ea6\u5230\u671f\u540e\u81ea\u52a8\u91ca\u653e\uff09\n    fmt.Println(&quot;\u9501\u5df2\u91ca\u653e\uff08\u6216\u7b49\u5f85\u79df\u7ea6\u8fc7\u671f\uff09&quot;)\n}<\/code><\/pre>\n<hr \/>\n<h3><strong>4.4 \u573a\u666f3\uff1a\u914d\u7f6e\u4e2d\u5fc3\u5b9e\u73b0<\/strong><\/h3>\n<h4><strong>4.4.1 \u52a8\u6001\u8bfb\u53d6\u914d\u7f6e<\/strong><\/h4>\n<pre><code class=\"language-javascript\">package main\n\nimport (\n    &quot;context&quot;\n    &quot;fmt&quot;\n    &quot;go.etcd.io\/etcd\/clientv3&quot;\n    &quot;time&quot;\n)\n\nfunc watchConfig(cli *clientv3.Client, key string) {\n    \/\/ \u76d1\u542c\u914d\u7f6e\u53d8\u5316\n    watcher := clientv3.NewWatcher(cli)\n    watchChan := watcher.Watch(context.Background(), key)\n\n    for resp := range watchChan {\n        for _, ev := range resp.Events {\n            fmt.Printf(&quot;\u914d\u7f6e\u53d8\u66f4: \u7c7b\u578b=%s, Key=%s, Value=%s\\n&quot;, ev.Type, ev.Kv.Key, ev.Kv.Value)\n        }\n    }\n}\n\nfunc main() {\n    cli, err := clientv3.New(clientv3.Config{Endpoints: []string{&quot;127.0.0.1:2379&quot;}})\n    if err != nil {\n        panic(err)\n    }\n    defer cli.Close()\n\n    \/\/ \u521d\u59cb\u8bfb\u53d6\u914d\u7f6e\n    resp, err := cli.Get(context.Background(), &quot;\/config\/db_url&quot;)\n    if err != nil {\n        panic(err)\n    }\n    if len(resp.Kvs) &gt; 0 {\n        fmt.Printf(&quot;\u5f53\u524d\u914d\u7f6e: %s\\n&quot;, resp.Kvs[0].Value)\n    }\n\n    \/\/ \u542f\u52a8\u76d1\u542c\u534f\u7a0b\n    go watchConfig(cli, &quot;\/config\/db_url&quot;)\n\n    \/\/ \u6a21\u62df\u914d\u7f6e\u66f4\u65b0\uff08\u53e6\u4e00\u7ec8\u7aef\u6267\u884c\uff1aetcdctl put \/config\/db_url &quot;mysql:\/\/new:pass@10.0.0.1:3306\/db&quot;\uff09\n    time.Sleep(60 * time.Second)\n}<\/code><\/pre>\n<hr \/>\n<h2><strong>5. \u539f\u7406\u89e3\u91ca\u4e0e\u539f\u7406\u6d41\u7a0b\u56fe<\/strong><\/h2>\n<h3><strong>5.1 etcd \u6838\u5fc3\u539f\u7406<\/strong><\/h3>\n<ul>\n<li>Raft \u5171\u8bc6\u7b97\u6cd5\uff1a\n<ul>\n<li>\u6240\u6709\u5199\u64cd\u4f5c\u9700\u901a\u8fc7\u591a\u6570\u8282\u70b9\uff08Quorum\uff09\u786e\u8ba4\uff0c\u4fdd\u8bc1\u5f3a\u4e00\u81f4\u6027\u3002<\/li>\n<li>Leader \u8282\u70b9\u8d1f\u8d23\u5904\u7406\u5199\u8bf7\u6c42\uff0cFollower \u8282\u70b9\u540c\u6b65\u6570\u636e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u952e\u503c\u5b58\u50a8\u6a21\u578b\uff1a\n<ul>\n<li>\u6570\u636e\u6309 Key \u7684\u5b57\u5178\u5e8f\u5b58\u50a8\uff0c\u652f\u6301\u8303\u56f4\u67e5\u8be2\uff08\u5982 <code>etcdctl get \/k8s\/deployments --prefix<\/code>\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><strong>5.2 \u539f\u7406\u6d41\u7a0b\u56fe<\/strong><\/h3>\n<pre><code class=\"language-javascript\">[\u5ba2\u6237\u7aef\u5199\u5165\u8bf7\u6c42]\n  \u2192 [Leader \u8282\u70b9\u63a5\u6536\u8bf7\u6c42]\n    \u2192 [Leader \u5c06\u65e5\u5fd7\u590d\u5236\u5230 Follower \u8282\u70b9]\n      \u2192 [\u591a\u6570\u8282\u70b9\u786e\u8ba4\u540e\u63d0\u4ea4\u65e5\u5fd7]\n        \u2192 [Leader \u8fd4\u56de\u6210\u529f\u54cd\u5e94]\n          \u2192 [\u5ba2\u6237\u7aef\u6536\u5230\u786e\u8ba4]<\/code><\/pre>\n<hr \/>\n<h2><strong>6. \u73af\u5883\u51c6\u5907\u4e0e\u90e8\u7f72<\/strong><\/h2>\n<h3><strong>6.1 \u751f\u4ea7\u73af\u5883\u90e8\u7f72\u5efa\u8bae<\/strong><\/h3>\n<ul>\n<li><strong>\u96c6\u7fa4\u89c4\u6a21<\/strong>\uff1a\u81f3\u5c11 3 \u8282\u70b9\uff08\u5bb9\u5fcd 1 \u8282\u70b9\u6545\u969c\uff09\u3002<\/li>\n<li><strong>\u6301\u4e45\u5316\u5b58\u50a8<\/strong>\uff1a\u4f7f\u7528 SSD \u78c1\u76d8\u5e76\u914d\u7f6e\u5b9a\u671f\u5feb\u7167\u3002<\/li>\n<li><strong>\u7f51\u7edc\u9694\u79bb<\/strong>\uff1a\u901a\u8fc7\u9632\u706b\u5899\u9650\u5236 etcd \u7aef\u53e3\uff082379\/2380\uff09\u7684\u8bbf\u95ee\u6743\u9650\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>7. \u8fd0\u884c\u7ed3\u679c<\/strong><\/h2>\n<h3><strong>7.1 \u6d4b\u8bd5\u7528\u4f8b1\uff1a\u72b6\u6001\u5b58\u50a8\u9a8c\u8bc1<\/strong><\/h3>\n<ul>\n<li><strong>\u64cd\u4f5c<\/strong>\uff1a\u901a\u8fc7 <code>etcdctl<\/code> \u548c Go \u4ee3\u7801\u5199\u5165\/\u8bfb\u53d6\u952e\u503c\u5bf9\u3002<\/li>\n<li><strong>\u9884\u671f\u7ed3\u679c<\/strong>\uff1a\u6570\u636e\u4e00\u81f4\u4e14\u65e0\u4e22\u5931\u3002<\/li>\n<\/ul>\n<h3><strong>7.2 \u6d4b\u8bd5\u7528\u4f8b2\uff1a\u5206\u5e03\u5f0f\u9501\u6709\u6548\u6027<\/strong><\/h3>\n<ul>\n<li><strong>\u64cd\u4f5c<\/strong>\uff1a\u542f\u52a8\u591a\u4e2a\u5b9e\u4f8b\u7ade\u4e89\u540c\u4e00\u9501\u3002<\/li>\n<li><strong>\u9884\u671f\u7ed3\u679c<\/strong>\uff1a\u4ec5\u4e00\u4e2a\u5b9e\u4f8b\u6210\u529f\u83b7\u53d6\u9501\uff0c\u5176\u4ed6\u5b9e\u4f8b\u963b\u585e\u6216\u8d85\u65f6\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>8. \u6d4b\u8bd5\u6b65\u9aa4\u4e0e\u8be6\u7ec6\u4ee3\u7801<\/strong><\/h2>\n<h3><strong>8.1 \u96c6\u6210\u6d4b\u8bd5\u811a\u672c<\/strong><\/h3>\n<pre><code class=\"language-javascript\">\/\/ \u6587\u4ef6\uff1aetcd_test.go\npackage main\n\nimport (\n    &quot;context&quot;\n    &quot;testing&quot;\n    &quot;go.etcd.io\/etcd\/clientv3&quot;\n)\n\nfunc TestPutAndGet(t *testing.T) {\n    cli, err := clientv3.New(clientv3.Config{Endpoints: []string{&quot;127.0.0.1:2379&quot;}})\n    if err != nil {\n        t.Fatal(err)\n    }\n    defer cli.Close()\n\n    _, err = cli.Put(context.Background(), &quot;test_key&quot;, &quot;test_value&quot;)\n    if err != nil {\n        t.Fatal(err)\n    }\n\n    resp, err := cli.Get(context.Background(), &quot;test_key&quot;)\n    if err != nil || len(resp.Kvs) == 0 {\n        t.Fatal(&quot;\u8bfb\u53d6\u5931\u8d25&quot;)\n    }\n    if string(resp.Kvs[0].Value) != &quot;test_value&quot; {\n        t.Fatal(&quot;\u503c\u4e0d\u5339\u914d&quot;)\n    }\n}<\/code><\/pre>\n<hr \/>\n<h2><strong>9. \u90e8\u7f72\u573a\u666f<\/strong><\/h2>\n<h3><strong>9.1 K8s \u96c6\u7fa4\u5185\u90e8\u7f72 etcd<\/strong><\/h3>\n<pre><code class=\"language-javascript\"># \u6587\u4ef6\uff1aetcd-deployment.yaml\napiVersion: apps\/v1\nkind: StatefulSet\nmetadata:\n  name: etcd\nspec:\n  serviceName: etcd\n  replicas: 3\n  selector:\n    matchLabels:\n      app: etcd\n  template:\n    metadata:\n      labels:\n        app: etcd\n    spec:\n      containers:\n      - name: etcd\n        image: quay.io\/coreos\/etcd:v3.5.0\n        command: [&quot;etcd&quot;]\n        args: [\n          &quot;--name&quot;, &quot;$(POD_NAME)&quot;,\n          &quot;--listen-client-urls&quot;, &quot;http:\/\/0.0.0.0:2379&quot;,\n          &quot;--advertise-client-urls&quot;, &quot;http:\/\/$(POD_NAME).etcd:2379&quot;,\n          &quot;--initial-cluster&quot;, &quot;etcd-0=http:\/\/etcd-0.etcd:2380,etcd-1=http:\/\/etcd-1.etcd:2380,etcd-2=http:\/\/etcd-2.etcd:2380&quot;,\n          &quot;--initial-cluster-token&quot;, &quot;etcd-cluster&quot;,\n          &quot;--initial-cluster-state&quot;, &quot;new&quot;\n        ]\n        env:\n        - name: POD_NAME\n          valueFrom:\n            fieldRef:\n              fieldPath: metadata.name\n        ports:\n        - containerPort: 2379\n          name: client\n        - containerPort: 2380\n          name: peer<\/code><\/pre>\n<hr \/>\n<h2><strong>10. \u7591\u96be\u89e3\u7b54<\/strong><\/h2>\n<h3><strong>\u5e38\u89c1\u95ee\u98981\uff1aetcd \u96c6\u7fa4\u65e0\u6cd5\u9009\u4e3e Leader<\/strong><\/h3>\n<ul>\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u7f51\u7edc\u5206\u533a\u6216\u8282\u70b9\u914d\u7f6e\u9519\u8bef\u3002<\/li>\n<li><strong>\u89e3\u51b3<\/strong>\uff1a\u68c0\u67e5\u8282\u70b9\u65e5\u5fd7\uff08<code>journalctl -u etcd<\/code>\uff09\uff0c\u786e\u8ba4 <code>--initial-cluster<\/code> \u53c2\u6570\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<h3><strong>\u5e38\u89c1\u95ee\u98982\uff1a\u8bfb\u5199\u5ef6\u8fdf\u8fc7\u9ad8<\/strong><\/h3>\n<ul>\n<li><strong>\u539f\u56e0<\/strong>\uff1a\u78c1\u76d8 I\/O \u74f6\u9888\u6216\u7f51\u7edc\u62e5\u585e\u3002<\/li>\n<li><strong>\u89e3\u51b3<\/strong>\uff1a\u4f7f\u7528 SSD \u5b58\u50a8\uff0c\u4f18\u5316\u7f51\u7edc\u5e26\u5bbd\uff08\u5982\u542f\u7528 gRPC \u538b\u7f29\uff09\u3002<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>11. \u672a\u6765\u5c55\u671b\u4e0e\u6280\u672f\u8d8b\u52bf<\/strong><\/h2>\n<h3><strong>11.1 \u6280\u672f\u8d8b\u52bf<\/strong><\/h3>\n<ul>\n<li><strong>etcd v3.6+ \u65b0\u7279\u6027<\/strong>\uff1a\u652f\u6301\u66f4\u7ec6\u7c92\u5ea6\u7684\u6743\u9650\u63a7\u5236\uff08RBAC\uff09\u3001\u6539\u8fdb\u7684 Watch \u6027\u80fd\u3002<\/li>\n<li><strong>\u4e0e WASM \u96c6\u6210<\/strong>\uff1a\u901a\u8fc7 WebAssembly \u6269\u5c55 etcd \u7684\u6570\u636e\u5904\u7406\u80fd\u529b\u3002<\/li>\n<\/ul>\n<h3><strong>11.2 \u6311\u6218<\/strong><\/h3>\n<ul>\n<li><strong>\u591a\u79df\u6237\u9694\u79bb<\/strong>\uff1a\u5982\u4f55\u5728\u5171\u4eab\u96c6\u7fa4\u4e2d\u4fdd\u969c\u4e0d\u540c\u79df\u6237\u7684\u6570\u636e\u5b89\u5168\uff1f<\/li>\n<li><strong>\u4e91\u539f\u751f\u6269\u5c55<\/strong>\uff1a\u5728 Serverless \u73af\u5883\u4e0b\u5982\u4f55\u9002\u914d etcd \u7684\u957f\u8fde\u63a5\u6a21\u578b\uff1f<\/li>\n<\/ul>\n<hr \/>\n<h2><strong>12. \u603b\u7ed3<\/strong><\/h2>\n<p>\u672c\u6587\u4ece etcd \u7684\u6838\u5fc3\u539f\u7406\u51fa\u53d1\uff0c\u7ed3\u5408 K8s \u7684\u5b9e\u9645\u5e94\u7528\u573a\u666f\uff0c\u901a\u8fc7\u4ee3\u7801\u793a\u4f8b\u4e0e\u6d4b\u8bd5\u9a8c\u8bc1\uff0c\u5e2e\u52a9\u8bfb\u8005\u6df1\u5165\u7406\u89e3 etcd \u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u7684\u5173\u952e\u4f5c\u7528\u3002\u638c\u63e1 etcd \u4e0d\u4ec5\u80fd\u63d0\u5347\u5bf9 K8s \u5e95\u5c42\u7684\u8ba4\u77e5\uff0c\u4e5f\u4e3a\u6784\u5efa\u9ad8\u53ef\u9760\u3001\u53ef\u6269\u5c55\u7684\u5206\u5e03\u5f0f\u7cfb\u7edf\u5960\u5b9a\u4e86\u57fa\u7840\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u5f15\u8a00 Kubernetes\uff08\u7b80\u79f0 K8s\uff09\u4f5c\u4e3a\u5bb9\u5668\u7f16\u6392\u9886\u57df\u7684\u6807\u51c6\uff0c\u5176\u6838\u5fc3\u4f9d\u8d56\u4e8e\u5206\u5e03\u5f0f\u952e\u503c\u5b58\u50a8\u7cfb\u7edf etcd \u5b9e\u73b0\u96c6\u7fa4\u72b6\u6001\u7ba1\u7406\u3002et   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[291],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9508"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9508"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9508\/revisions"}],"predecessor-version":[{"id":9509,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9508\/revisions\/9509"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9508"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}