{"id":629,"date":"2020-12-16T12:08:30","date_gmt":"2020-12-16T04:08:30","guid":{"rendered":"https:\/\/www.yinyubo.cn\/?p=629"},"modified":"2022-05-17T09:30:19","modified_gmt":"2022-05-17T01:30:19","slug":"traefik","status":"publish","type":"post","link":"https:\/\/www.yinyubo.com\/?p=629","title":{"rendered":"traefik\u914d\u7f6e\u7528\u6237\u767b\u5f55\uff0c\u9650\u5236K8S\u7684web\u670d\u52a1\u8bbf\u95ee"},"content":{"rendered":"<h1>\u80cc\u666f<\/h1>\n<p>\u50cfElastic-APM, Traefik-Dashboard\u7b49\u9875\u9762\uff0c\u662f\u6ca1\u6709\u7528\u6237\u767b\u5f55\u9650\u5236\u7684\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b\u7ed9\u4ed6\u4eec\u52a0\u4e0a\u7528\u6237\u767b\u5f55\u9650\u5236\uff0c\u6211\u4eec\u9700\u8981\u5728traeifk\u91cc\u7ed9\u5bf9\u5e94\u7684ingress\u6dfb\u52a0\u767b\u5f55\u7528\u7684Middleware\uff0c\u90a3\u4e48\u8be5\u5982\u4f55\u6dfb\u52a0\u5462\uff1f\u672c\u6587\u4ee5\u7ed9K8S\u90e8\u7f72\u7684traefik dashboard\u4e3a\u4f8b\u8fdb\u884c\u6dfb\u52a0<\/p>\n<h1>\u7528\u6237\u540d\u5bc6\u7801\u52a0\u5bc6<\/h1>\n<p>1.\u5047\u8bbe\u6709\u5982\u4e0b3\u4e2a\u7528\u6237\u540d\u5bc6\u7801<br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">lizhenwei 123<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">zhenwei.li 456<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">hello thankyou<\/code><br \/>2.\u6211\u4eec\u901a\u8fc7htpasswd\u8fdb\u884c\u52a0\u5bc6<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">htpasswd -nb lizhenwei 123\nlizhenwei:$apr1$0wIJg4EG$RZ7wOIyIdg1R4gj4zAlzq1\nhtpasswd -nb zhenwei.li 456\nzhenwei.li:$apr1$PX8cqECj$5zvC3eB1vhLioyjVjdkkE\/\nhtpasswd -nb hello thankyou\nhello:$apr1$4nlPGEqZ$nqz2ojkuxAY4FUEy0Tp3x1<\/code><\/pre>\n<p>3.\u5c06\u52a0\u5bc6\u7684\u4fe1\u606f\u653e\u5165\u4e00\u4e2a\u53ebpolicy\u7684\u6587\u4ef6<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">vi policy<\/code><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">lizhenwei:$apr1$0wIJg4EG$RZ7wOIyIdg1R4gj4zAlzq1\nzhenwei.li:$apr1$PX8cqECj$5zvC3eB1vhLioyjVjdkkE\/\nhello:$apr1$4nlPGEqZ$nqz2ojkuxAY4FUEy0Tp3x1<\/code><\/pre>\n<p>4.\u8fdb\u884cbase64\u52a0\u5bc6\uff0c\u83b7\u5f97\u52a0\u5bc6\u540e\u7684\u5b57\u7b26<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">cat policy | openssl base64<\/code> <br \/><br \/><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">bGl6aGVud2VpOiRhcHIxJDB3SUpnNEVHJFJaN3dPSXlJZGcxUjRnajR6QWx6cTEK\nemhlbndlaS5saTokYXByMSRQWDhjcUVDaiQ1enZDM2VCMXZoTGlveWpWamRra0Uv\nCmhlbGxvOiRhcHIxJDRubFBHRXFaJG5xejJvamt1eEFZNEZVRXkwVHAzeDEK<\/code><\/pre>\n<h1>\u521b\u5efamiddleware.yaml<\/h1>\n<p>\u5c06\u52a0\u5bc6\u540e\u7684\u5b57\u7b26\uff0c\u590d\u5236\u5230data.users\u4e0b\u9762<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># Declaring the user list\napiVersion: traefik.containo.us\/v1alpha1\nkind: Middleware\nmetadata:\n  name: test-auth\nspec:\n  basicAuth:\n    secret: authsecret\n---\n# Note: in a kubernetes secret the string (e.g. generated by htpasswd) must be base64-encoded first.\n# To create an encoded user:password pair, the following command can be used:\n# htpasswd -nb user password | openssl base64\napiVersion: v1\nkind: Secret\nmetadata:\n  name: authsecret\n  namespace: default\ndata:\n  users: |2\n    bGl6aGVud2VpOiRhcHIxJDB3SUpnNEVHJFJaN3dPSXlJZGcxUjRnajR6QWx6cTEK\n    emhlbndlaS5saTokYXByMSRQWDhjcUVDaiQ1enZDM2VCMXZoTGlveWpWamRra0Uv\n    CmhlbGxvOiRhcHIxJDRubFBHRXFaJG5xejJvamt1eEFZNEZVRXkwVHAzeDEK<\/code><\/pre>\n<h1>\u521b\u5efaingress.yaml<\/h1>\n<p>\u5b9a\u4e49\u8bbf\u95ee\u8def\u5f84\uff0c\u5b9a\u4e49\u4e2d\u95f4\u4ef6<\/p>\n<pre class=\"lang:python decode:true\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\"># dashboard.yaml\napiVersion: traefik.containo.us\/v1alpha1\nkind: IngressRoute\nmetadata:\n  name: dashboard\nspec:\n  entryPoints:\n    - web\n  routes:\n    - match: Host(`traefik.test.local`) &amp;&amp; (PathPrefix(`\/dashboard`) || PathPrefix(`\/api`))\n      kind: Rule\n      services:\n        - name: api@internal\n          kind: TraefikService\n      middlewares:\n        - name: test-auth<\/code><\/pre>\n<h1>\u751f\u6548\u914d\u7f6e<\/h1>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">kubectl apply -f middleware.yaml\nkubectl apply -f ingress.yaml<\/code><\/pre>\n<h1>\u68c0\u67e5\u6253\u5f00\u7f51\u9875\u65f6\uff0c\u662f\u5426\u5f39\u51fa\u767b\u5f55\u5bf9\u8bdd\u6846<\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-630 size-full\" src=\"https:\/\/yinyubo-1257235934.cos.ap-nanjing.myqcloud.com\/content\/Image-28.png\" alt=\"\" width=\"643\" height=\"369\" \/><\/p>\n<h1>\u66f4\u65b0\u5bc6\u7801<\/h1>\n<p>\u5982\u679c\u6211\u4eec\u8981\u66f4\u65b0\u5bc6\u7801\uff0c\u53ef\u4ee5\u91cd\u65b0\u4f7f\u7528htpasswd\u751f\u6210\u5bc6\u7801\uff0c\u7136\u540e\u653e\u5728policy\u6587\u4ef6\u4e2d\uff0c\u4f7f\u7528\u547d\u4ee4\u884c\u66f4\u65b0<\/p>\n<pre class=\"lang:python decode:true \"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"golang\">kubectl create secret generic authsecret --from-file=users=.\/policy --dry-run=client -o yaml | kubectl apply -f -<\/code><\/pre>\n<p>\u00a0<\/p>\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u80cc\u666f \u50cfElastic-APM, Traefik-Dashboard\u7b49\u9875\u9762\uff0c\u662f\u6ca1\u6709\u7528\u6237\u767b\u5f55\u9650\u5236\u7684\uff0c\u5982\u679c\u6211\u4eec\u5e0c\u671b [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-k8s"],"_links":{"self":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=629"}],"version-history":[{"count":3,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":868,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions\/868"}],"wp:attachment":[{"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yinyubo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}