{"id":1399,"date":"2024-01-08T11:34:33","date_gmt":"2024-01-08T03:34:33","guid":{"rendered":"https:\/\/ext4.cn\/?p=1399"},"modified":"2024-01-08T11:34:34","modified_gmt":"2024-01-08T03:34:34","slug":"kubernetes%e5%a4%9a%e4%b8%aaconfig%e5%90%88%e5%b9%b6","status":"publish","type":"post","link":"https:\/\/ext4.cn\/?p=1399","title":{"rendered":"kubernetes\u591a\u4e2aconfig\u5408\u5e76"},"content":{"rendered":"\n<p>\u60f3\u8981\u5728\u4e00\u53f0\u7ec8\u7aef\u63a7\u5236\u591a\u4e2ak8s\u96c6\u7fa4\uff0c\u4f1a\u9700\u8981\u591a\u4e2akubeconfig\u6587\u4ef6\u3002kubectl\u547d\u4ee4\u4f1a\u5728\u9ed8\u8ba4\u8def\u5f84 $HOME\/.kube \u4e0b\u627e config \u4f5c\u4e3akubeconfig\u6765\u8bbf\u95eeKubernetes\u96c6\u7fa4\uff0c\u6240\u4ee5\u9700\u8981\u5c06\u591a\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u7528\u6765\u63a7\u5236\u591a\u4e2a\u96c6\u7fa4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5408\u5e76<\/h2>\n\n\n\n<p>1\u3001\u5148\u5907\u4efd\u73b0\u6709\u7684config\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> ~\/.kube  cp ~\/.kube\/config ~\/.kube\/config.bak<\/code><\/pre>\n\n\n\n<p>2\u3001\u8bbe\u7f6ekubeconfig\u73af\u5883\u53d8\u91cf<\/p>\n\n\n\n<ul>\n<li>\u901a\u8fc7\u6b64\u73af\u5883\u53d8\u91cf\u6765\u6307\u5b9a\u591a\u4e2akubeconfig\uff0c\u5982\u4e0b\uff1a(\u786e\u4fdd<code>clusters<\/code>, <code>contexts<\/code>, \u548c <code>users<\/code>\u90e8\u5206\u6ca1\u6709\u5185\u5bb9\u91cd\u590d)<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" loading=\"lazy\" width=\"527\" height=\"88\" data-original=\"https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1400\"  sizes=\"(max-width: 527px) 100vw, 527px\" \/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>export KUBECONFIG=~\/.kube\/ack-prod-config:~\/.kube\/ack-test-config\n# \u5982\u679c\u5f53\u524d\u76ee\u5f55\u6709\u591a\u4e2akubeconfig\u6587\u4ef6\uff0c\u53ef\u4ee5\u7528\u5982\u4e0b\u547d\u4ee4\u5feb\u901f\u8bbe\u7f6e\uff1a\nexport KUBECONFIG=~\/.kube\/config:$(find . -type f | tr '\\n' ':')<\/code><\/pre>\n\n\n\n<p>3\u3001\u901a\u8fc7kbuectl\u547d\u4ee4\u5408\u5e76\u591a\u4e2akubeconfig\u5230\u4e00\u4e2a\u6587\u4ef6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl config view --flatten > all-config\n# \u5c06\u5408\u5e76\u540e\u7684\u6587\u4ef6\u91cd\u547d\u540d\u4e3aconfig\nmv all-config ~\/.kube\/config<\/code><\/pre>\n\n\n\n<p>4\u3001\u9a8c\u8bc1<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>kubectl config get-contexts<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-1.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" loading=\"lazy\" width=\"902\" height=\"107\" data-original=\"https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-1.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1401\"  sizes=\"(max-width: 902px) 100vw, 902px\" \/><\/div><\/figure>\n\n\n\n<ul>\n<li>\u5207\u6362\u96c6\u7fa4<\/li>\n<\/ul>\n\n\n\n<p>\u4e00\u4e2a\u5b8c\u6574\u7684\u914d\u7f6e\u6587\u4ef6\u5305\u542b\u8fd9\u4e9b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>clusters \u96c6\u7fa4\ncontexts \u4e0a\u4e0b\u6587\nusers \u7528\u6237<\/code><\/pre>\n\n\n\n<p>\u276f kubectl config use-context kubernetes-admin@kubernetes<\/p>\n\n\n\n<p>Switched to context &#8220;kubernetes-admin@kubernetes&#8221;.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-2.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" loading=\"lazy\" width=\"922\" height=\"213\" data-original=\"https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1402\"  sizes=\"(max-width: 922px) 100vw, 922px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u5207\u6362\u96c6\u7fa4<\/h2>\n\n\n\n<ul>\n<li>\u628akubecluster\u8bbe\u7f6e\u53ef\u6267\u884c\uff0c\u5e76\u4e14\u653e\u5728\/usr\/bin\u4e0b\u9762<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>chmod +x kubecluster\nmv kubecluster \/usr\/local\/bin<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-3.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" loading=\"lazy\" width=\"926\" height=\"367\" data-original=\"https:\/\/ext4.cn\/wp-content\/uploads\/2024\/01\/image-3.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1404\"  sizes=\"(max-width: 926px) 100vw, 926px\" \/><\/div><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/bash\n\n# \u51fd\u6570\uff1a\u663e\u793a\u5f53\u524d\u96c6\u7fa4\nfunction show_current_cluster {\n    current_context=$(kubectl config current-context)\n    echo \"\u5f53\u524d\u96c6\u7fa4: $current_context\"\n}\n\n# \u51fd\u6570\uff1a\u5217\u51fa\u6240\u6709\u96c6\u7fa4\u5e76\u8d4b\u4e88\u6570\u5b57\u7f16\u53f7\nfunction list_clusters {\n    echo \"\u53ef\u5207\u6362\u7684Kubernetes\u96c6\u7fa4:\"\n    kubectl config get-contexts --no-headers | awk '$1 != \"*\" {print NR \")\\t\" $1 \"\\t\" $3}'\n}\n\n# \u663e\u793a\u5f53\u524d\u96c6\u7fa4\nshow_current_cluster\n\n# \u663e\u793a\u6240\u6709\u53ef\u7528\u96c6\u7fa4\nlist_clusters\n\n# \u63d0\u793a\u7528\u6237\u8f93\u5165\necho \"\u8f93\u5165\u60a8\u60f3\u8981\u5207\u6362\u5230\u7684\u96c6\u7fa4\u7684\u7f16\u53f7\uff0c\u6216\"no\"\u9000\u51fa:\"\nread input\n\n# \u68c0\u67e5\u7528\u6237\u8f93\u5165\u662f\u5426\u4e3a 'no'\nif &#91; \"$input\" = \"no\" ]; then\n    echo \"\u9000\u51fa\u811a\u672c\"\n    exit 0\nfi\n\n# \u83b7\u53d6\u7528\u6237\u9009\u62e9\u7684\u96c6\u7fa4\u540d\u79f0\ncontext=$(kubectl config get-contexts --no-headers | awk -v row=$input 'NR==row {print $1}')\n\n# \u68c0\u67e5\u662f\u5426\u5f97\u5230\u6709\u6548\u7684\u96c6\u7fa4\u540d\u79f0\nif &#91; -z \"$context\" ]; then\n    echo \"\u9009\u62e9\u65e0\u6548\"\n    exit 1\nfi\n\n# \u5207\u6362\u5230\u7528\u6237\u9009\u62e9\u7684\u96c6\u7fa4\nif kubectl config use-context \"$context\"; then\n    echo \"\u6210\u529f\u5207\u6362\u5230\u4e0a\u4e0b\u6587: $context\"\nelse\n    echo \"\u65e0\u6cd5\u5207\u6362\u5230\u4e0a\u4e0b\u6587: $context\"\nfi<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60f3\u8981\u5728\u4e00\u53f0\u7ec8\u7aef\u63a7\u5236\u591a\u4e2ak8s\u96c6\u7fa4\uff0c\u4f1a\u9700\u8981\u591a\u4e2akubeconfig\u6587\u4ef6\u3002kubectl\u547d\u4ee4\u4f1a\u5728\u9ed8\u8ba4\u8def\u5f84 $HOM [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[92,71],"tags":[93],"_links":{"self":[{"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/posts\/1399"}],"collection":[{"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ext4.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1399"}],"version-history":[{"count":2,"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/posts\/1399\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/ext4.cn\/index.php?rest_route=\/wp\/v2\/posts\/1399\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/ext4.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ext4.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ext4.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}