{"id":1562,"date":"2020-06-09T12:03:18","date_gmt":"2020-06-09T04:03:18","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1562"},"modified":"2020-06-09T12:03:18","modified_gmt":"2020-06-09T04:03:18","slug":"kubernetes-namespaces","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1562","title":{"rendered":"Kubernetes Namespaces"},"content":{"rendered":"<header class=\\\"entry-header\\\">\n<h1 class=\\\"entry-title\\\">Kubernetes Namespaces<\/h1>\n<\/header>\n<div class=\\\"entry-content\\\">\n<div id=\\\"toc_container\\\" class=\\\"no_bullets\\\">\n<ul class=\\\"toc_list\\\">\n<li><span class=\\\"toc_number toc_depth_1\\\">1<\/span>\u00a0\u4f55\u65f6\u4f7f\u7528\u591a\u4e2aNamespaces<\/li>\n<li><span class=\\\"toc_number toc_depth_1\\\">2<\/span>\u00a0\u4f7f\u7528 Namespaces\n<ul>\n<li><span class=\\\"toc_number toc_depth_2\\\">2.1<\/span>\u00a0\u521b\u5efa<\/li>\n<li><span class=\\\"toc_number toc_depth_2\\\">2.2<\/span>\u00a0\u5220\u9664<\/li>\n<li><span class=\\\"toc_number toc_depth_2\\\">2.3<\/span>\u00a0\u67e5\u770b Namespaces<\/li>\n<li><span class=\\\"toc_number toc_depth_2\\\">2.4<\/span>\u00a0Setting the namespace for a request<\/li>\n<li><span class=\\\"toc_number toc_depth_2\\\">2.5<\/span>\u00a0Setting the namespace preference<\/li>\n<\/ul>\n<\/li>\n<li><span class=\\\"toc_number toc_depth_1\\\">3<\/span>\u00a0\u6240\u6709\u5bf9\u8c61\u90fd\u5728Namespace\u4e2d?<\/li>\n<\/ul>\n<\/div>\n<p>Kubernetes\u53ef\u4ee5\u4f7f\u7528Namespaces\uff08\u547d\u540d\u7a7a\u95f4\uff09\u521b\u5efa\u591a\u4e2a\u865a\u62df\u96c6\u7fa4\u3002<\/p>\n<h2 id=\\\"when-to-use-multiple-namespaces\\\"><span id=\\\"Namespaces\\\">\u4f55\u65f6\u4f7f\u7528\u591a\u4e2aNamespaces<\/span><\/h2>\n<p>\u5f53\u56e2\u961f\u6216\u9879\u76ee\u4e2d\u5177\u6709\u8bb8\u591a\u7528\u6237\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528Namespace\u6765\u533a\u5206\uff0ca\u5982\u679c\u662f\u5c11\u91cf\u7528\u6237\u96c6\u7fa4\uff0c\u53ef\u4ee5\u4e0d\u9700\u8981\u8003\u8651\u4f7f\u7528Namespace\uff0c\u5982\u679c\u9700\u8981\u5b83\u4eec\u63d0\u4f9b\u7279\u6b8a\u6027\u8d28\u65f6\uff0c\u53ef\u4ee5\u5f00\u59cb\u4f7f\u7528Namespace\u3002<br \/>\nNamespace\u4e3a\u540d\u79f0\u63d0\u4f9b\u4e86\u4e00\u4e2a\u8303\u56f4\u3002\u8d44\u6e90\u7684Names\u5728Namespace\u4e2d\u5177\u6709\u552f\u4e00\u6027\u3002<br \/>\nNamespace\u662f\u4e00\u79cd\u5c06\u96c6\u7fa4\u8d44\u6e90\u5212\u5206\u4e3a\u591a\u4e2a\u7528\u9014(\u901a\u8fc7\u00a0resource quota)\u7684\u65b9\u6cd5\u3002<br \/>\n\u5728\u672a\u6765\u7684Kubernetes\u7248\u672c\u4e2d\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u76f8\u540cNamespace\u4e2d\u7684\u5bf9\u8c61\u5c06\u5177\u6709\u76f8\u540c\u7684\u8bbf\u95ee\u63a7\u5236\u7b56\u7565\u3002<br \/>\n\u5bf9\u4e8e\u7a0d\u5fae\u4e0d\u540c\u7684\u8d44\u6e90\u6ca1\u5fc5\u8981\u4f7f\u7528\u591a\u4e2aNamespace\u6765\u5212\u5206\uff0c\u4f8b\u5982\u540c\u610f\u8f6f\u4ef6\u7684\u4e0d\u540c\u7248\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528labels(\u6807\u7b7e)\u6765\u533a\u5206\u540c\u4e00Namespace\u4e2d\u7684\u8d44\u6e90\u3002<\/p>\n<h2><span id=\\\"_Namespaces\\\">\u4f7f\u7528 Namespaces<\/span><\/h2>\n<p>Namespace\u7684\u521b\u5efa\u3001\u5220\u9664\u548c\u67e5\u770b\u3002<\/p>\n<h3 id=\\\"\u521b\u5efa\\\"><span id=\\\"i\\\">\u521b\u5efa<\/span><\/h3>\n<pre>(1) \u547d\u4ee4\u884c\u76f4\u63a5\u521b\u5efa\n$ kubectl create namespace new-namespace\n(2) \u901a\u8fc7\u6587\u4ef6\u521b\u5efa\n$ cat my-namespace.yaml\napiVersion: v1\nkind: Namespace\nmetadata:\n  name: new-namespace\n$ kubectl create -f .\/my-namespace.yaml\n<\/pre>\n<p>\u6ce8\u610f\uff1a\u547d\u540d\u7a7a\u95f4\u540d\u79f0\u6ee1\u8db3\u6b63\u5219\u8868\u8fbe\u5f0f[a-z0-9]([-a-z0-9]*[a-z0-9])?,\u6700\u5927\u957f\u5ea6\u4e3a63\u4f4d<\/p>\n<h3 id=\\\"\u5220\u9664\\\"><span id=\\\"i-2\\\">\u5220\u9664<\/span><\/h3>\n<pre>$ kubectl delete namespaces new-namespace\n<\/pre>\n<p>\u6ce8\u610f\uff1a<\/p>\n<ol>\n<li>\u5220\u9664\u4e00\u4e2anamespace\u4f1a\u81ea\u52a8\u5220\u9664\u6240\u6709\u5c5e\u4e8e\u8be5namespace\u7684\u8d44\u6e90\u3002<\/li>\n<li>default\u548ckube-system\u547d\u540d\u7a7a\u95f4\u4e0d\u53ef\u5220\u9664\u3002<\/li>\n<li>PersistentVolumes\u662f\u4e0d\u5c5e\u4e8e\u4efb\u4f55namespace\u7684\uff0c\u4f46PersistentVolumeClaim\u662f\u5c5e\u4e8e\u67d0\u4e2a\u7279\u5b9anamespace\u7684\u3002<\/li>\n<li>Events\u662f\u5426\u5c5e\u4e8enamespace\u53d6\u51b3\u4e8e\u4ea7\u751fevents\u7684\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<h3><span id=\\\"_Namespaces-2\\\">\u67e5\u770b Namespaces<\/span><\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5217\u51fa\u7fa4\u96c6\u4e2d\u7684\u5f53\u524d\u7684Namespace\uff1a<\/p>\n<div>\n<pre>$ kubectl get namespaces\nNAME          STATUS    AGE\ndefault       Active    1d\nkube-system   Active    1d\n<\/pre>\n<\/div>\n<p>Kubernetes\u4ece\u4e24\u4e2a\u521d\u59cb\u7684Namespace\u5f00\u59cb\uff1a<\/p>\n<ul>\n<li>default<\/li>\n<li>kube-system\u00a0\u7531Kubernetes\u7cfb\u7edf\u521b\u5efa\u7684\u5bf9\u8c61\u7684Namespace<\/li>\n<\/ul>\n<h3 id=\\\"setting-the-namespace-for-a-request\\\"><span id=\\\"Setting_the_namespace_for_a_request\\\">Setting the namespace for a request<\/span><\/h3>\n<p>\u8981\u4e34\u65f6\u8bbe\u7f6eRequest\u7684Namespace\uff0c\u8bf7\u4f7f\u7528&#8211;namespace \u6807\u5fd7\u3002<br \/>\n\u4f8b\u5982\uff1a<\/p>\n<div>\n<pre>$ kubectl --namespace=&lt;insert-namespace-name-here&gt; run nginx --image=nginx\n$ kubectl --namespace=&lt;insert-namespace-name-here&gt; get pods\n<\/pre>\n<\/div>\n<h3 id=\\\"setting-the-namespace-preference\\\"><span id=\\\"Setting_the_namespace_preference\\\">Setting the namespace preference<\/span><\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528kubectl\u547d\u4ee4\u521b\u5efa\u7684Namespace\u53ef\u4ee5\u6c38\u4e45\u4fdd\u5b58\u5728context\u4e2d\u3002<\/p>\n<div>\n<pre>$ kubectl config set-context $(kubectl config current-context) --namespace=&lt;insert-namespace-name-here&gt;\n# Validate it\n$ kubectl config view | grep namespace:<\/pre>\n<\/div>\n<h2 id=\\\"not-all-objects-are-in-a-namespace\\\"><span id=\\\"Namespace\\\">\u6240\u6709\u5bf9\u8c61\u90fd\u5728Namespace\u4e2d?<\/span><\/h2>\n<p>\u5927\u591a\u6570Kubernetes\u8d44\u6e90\uff08\u4f8b\u5982pod\u3001services\u3001replication controllers\u6216\u5176\u4ed6\uff09\u90fd\u5728\u67d0\u4e9bNamespace\u4e2d\uff0c\u4f46Namespace\u8d44\u6e90\u672c\u8eab\u5e76\u4e0d\u5728Namespace\u4e2d\u3002\u800c\u4f4e\u7ea7\u522b\u8d44\u6e90\uff08\u5982Node\u548cpersistentVolumes\uff09\u4e0d\u5728\u4efb\u4f55Namespace\u4e2d\u3002Events\u662f\u4e00\u4e2a\u4f8b\u5916\uff1a\u5b83\u4eec\u53ef\u80fd\u6709\u4e5f\u53ef\u80fd\u6ca1\u6709Namespace\uff0c\u5177\u4f53\u53d6\u51b3\u4e8eEvents\u7684\u5bf9\u8c61\u3002\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Kubernetes Namespaces 1\u00a0\u4f55\u65f6\u4f7f\u7528\u591a\u4e2aNamespaces 2\u00a0\u4f7f\u7528 Namespace &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1562\"> <span class=\"screen-reader-text\">Kubernetes Namespaces<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[104,123],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"Kubernetes Namespaces 1\u00a0\u4f55\u65f6\u4f7f\u7528\u591a\u4e2aNamespaces 2\u00a0\u4f7f\u7528 Namespace&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1562"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1562"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1562\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1562"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1562"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1562"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}