LoginSignup
0
1

More than 1 year has passed since last update.

kubernetesのNamespaceまとめ

Last updated at Posted at 2021-06-19

いまいちKubernetesのNamespace(名前空間)について掴めていなかったので、学習した内容をまとめました。

Namespaceとは

Kubernetesは、同一の物理クラスター上で複数の仮想クラスターの動作をサポートします。 この仮想クラスターをNamespaceと呼びます。

引用元:kubernetes.io

物理ノード達の上に作られる論理的なグループのようです。そのまま覚えておけばいい気がします。

使用する場面

Namespaceは、複数のチーム・プロジェクトにまたがる多くのユーザーがいる環境での使用を目的としています。 数人から数十人しかユーザーのいないクラスターに対して、あなたはNamespaceを作成したり、考える必要は全くありません。 Kubernetesが提供するNamespaceの機能が必要となった時に、Namespaceの使用を始めてください。

規模が大きいシステムでクラスターを分割したい際にNamespaceが活躍するようです。
それと、本番環境やステージング・開発環境を分割する際の利用でしょうか。

初期Namespace

Kubernetesの起動時には4つの初期Namespaceが作成されています。

・default 他にNamespaceを持っていないオブジェクトのためのデフォルトNamespace
・kube-system Kubernetesシステムによって作成されたオブジェクトのためのNamespace
・kube-public このNamespaceは自動的に作成され、全てのユーザーから読み取り可能です。(認証されていないユーザーも含みます。) このNamespaceは、リソースをクラスター全体を通じてパブリックに表示・読み取り可能にするため、ほとんどクラスターによって使用される用途で予約されます。 このNamespaceのパブリックな側面は単なる慣例であり、要件ではありません。
・kube-node-lease クラスターのスケールに応じたノードハートビートのパフォーマンスを向上させる各ノードに関連したLeaseオブジェクトのためのNamespace。

引用元:kubernetes.io

defaultはオブジェクトを特定のNamespaceに作成していなければ、デフォルトで割り振られるNamespaceですが、使い始めでNamespaceを使用する段階に至っていなければ、特に意識する必要はなさそうです。

関連コマンド

Namespaceの一覧を表示

kubectl get namespace

root@controlplane:~# kubectl get namespace
NAME              STATUS   AGE
default           Active   10m
dev               Active   59s
finance           Active   59s
kube-node-lease   Active   10m
kube-public       Active   10m
kube-system       Active   10m
manufacturing     Active   58s
marketing         Active   59s
prod              Active   58s
research          Active   58s
root@controlplane:~# 

特定のNamespaceに存在するPodの一覧を表示

kubectl get pod --namespace [namespace名]

root@controlplane:~# kubectl get pod --namespace research
NAME    READY   STATUS             RESTARTS   AGE
dna-1   0/1     CrashLoopBackOff   4          3m25s
dna-2   0/1     CrashLoopBackOff   4          3m25s
root@controlplane:~# 

Namespaceの作成

kubectl create namespace [namespace名]

root@controlplane:~# kubectl create namespace test  
namespace/test created
root@controlplane:~# 
root@controlplane:~# kubectl get namespace
NAME              STATUS   AGE
default           Active   33m
kube-node-lease   Active   33m
kube-public       Active   33m
kube-system       Active   33m
test              Active   13s
root@controlplane:~# 

特定のNamespaceにPodを作成

kubectl run [Pod名] --image=[イメージ名] --namespace=[namespace名]

root@controlplane:~# kubectl run redis --image=redis --namespace=finance
pod/redis created
root@controlplane:~# 
root@controlplane:~# kubectl get pod --namespace=finance
NAME      READY   STATUS    RESTARTS   AGE
redis     1/1     Running   0          58s
root@controlplane:~# 

Namespaceに属しているもの・属していないもの

ほとんどのKubernetesリソース(例えば、Pod、Service、ReplicationControllerなど)はいくつかのNamespaceにあります。 しかしNamespaceのリソースそれ自体は単一のNamespace内にありません。 そしてNodeやPersistentVolumeのような低レベルのリソースはどのNamespaceにも属していません。

引用元:kubernetes.io

以下のコマンドでNamespaceに属しているもの・属していないものが確認できます。

kubectl api-resources --namespaced=true

Namespaceに属しているもの一覧


root@controlplane:~# kubectl api-resources --namespaced=true
NAME                        SHORTNAMES   APIVERSION                     NAMESPACED   KIND
bindings                                 v1                             true         Binding
configmaps                  cm           v1                             true         ConfigMap
endpoints                   ep           v1                             true         Endpoints
events                      ev           v1                             true         Event
limitranges                 limits       v1                             true         LimitRange
persistentvolumeclaims      pvc          v1                             true         PersistentVolumeClaim
pods                        po           v1                             true         Pod
podtemplates                             v1                             true         PodTemplate
replicationcontrollers      rc           v1                             true         ReplicationController
resourcequotas              quota        v1                             true         ResourceQuota
secrets                                  v1                             true         Secret
serviceaccounts             sa           v1                             true         ServiceAccount
services                    svc          v1                             true         Service
controllerrevisions                      apps/v1                        true         ControllerRevision
daemonsets                  ds           apps/v1                        true         DaemonSet
deployments                 deploy       apps/v1                        true         Deployment
replicasets                 rs           apps/v1                        true         ReplicaSet
statefulsets                sts          apps/v1                        true         StatefulSet
localsubjectaccessreviews                authorization.k8s.io/v1        true         LocalSubjectAccessReview
horizontalpodautoscalers    hpa          autoscaling/v1                 true         HorizontalPodAutoscaler
cronjobs                    cj           batch/v1beta1                  true         CronJob
jobs                                     batch/v1                       true         Job
leases                                   coordination.k8s.io/v1         true         Lease
endpointslices                           discovery.k8s.io/v1beta1       true         EndpointSlice
events                      ev           events.k8s.io/v1               true         Event
ingresses                   ing          extensions/v1beta1             true         Ingress
ingresses                   ing          networking.k8s.io/v1           true         Ingress
networkpolicies             netpol       networking.k8s.io/v1           true         NetworkPolicy
poddisruptionbudgets        pdb          policy/v1beta1                 true         PodDisruptionBudget
rolebindings                             rbac.authorization.k8s.io/v1   true         RoleBinding
roles                                    rbac.authorization.k8s.io/v1   true         Role
root@controlplane:~# 

kubectl api-resources --namespaced=false

Namespaceに属していないもの一覧

root@controlplane:~# kubectl api-resources --namespaced=false
NAME                              SHORTNAMES   APIVERSION                             NAMESPACED   KIND
componentstatuses                 cs           v1                                     false        ComponentStatus
namespaces                        ns           v1                                     false        Namespace
nodes                             no           v1                                     false        Node
persistentvolumes                 pv           v1                                     false        PersistentVolume
mutatingwebhookconfigurations                  admissionregistration.k8s.io/v1        false        MutatingWebhookConfiguration
validatingwebhookconfigurations                admissionregistration.k8s.io/v1        false        ValidatingWebhookConfiguration
customresourcedefinitions         crd,crds     apiextensions.k8s.io/v1                false        CustomResourceDefinition
apiservices                                    apiregistration.k8s.io/v1              false        APIService
tokenreviews                                   authentication.k8s.io/v1               false        TokenReview
selfsubjectaccessreviews                       authorization.k8s.io/v1                false        SelfSubjectAccessReview
selfsubjectrulesreviews                        authorization.k8s.io/v1                false        SelfSubjectRulesReview
subjectaccessreviews                           authorization.k8s.io/v1                false        SubjectAccessReview
certificatesigningrequests        csr          certificates.k8s.io/v1                 false        CertificateSigningRequest
flowschemas                                    flowcontrol.apiserver.k8s.io/v1beta1   false        FlowSchema
prioritylevelconfigurations                    flowcontrol.apiserver.k8s.io/v1beta1   false        PriorityLevelConfiguration
ingressclasses                                 networking.k8s.io/v1                   false        IngressClass
runtimeclasses                                 node.k8s.io/v1                         false        RuntimeClass
podsecuritypolicies               psp          policy/v1beta1                         false        PodSecurityPolicy
clusterrolebindings                            rbac.authorization.k8s.io/v1           false        ClusterRoleBinding
clusterroles                                   rbac.authorization.k8s.io/v1           false        ClusterRole
priorityclasses                   pc           scheduling.k8s.io/v1                   false        PriorityClass
csidrivers                                     storage.k8s.io/v1                      false        CSIDriver
csinodes                                       storage.k8s.io/v1                      false        CSINode
storageclasses                    sc           storage.k8s.io/v1                      false        StorageClass
volumeattachments                              storage.k8s.io/v1                      false        VolumeAttachment
root@controlplane:~# 
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1