Skip to content

Commit 79ef636

Browse files
committed
Added namespace information
Signed-off-by: knrt10 <[email protected]>
1 parent d3c0fad commit 79ef636

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

custom-namespace.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: custom-namespace

readme.md

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ This is just a simple demonstration to get a basic understanding of how kubernet
6464
- [Looking up an objects annotations](#looking-up-an-objects-annotations)
6565
- [Adding and modifying annotations](#adding-and-modifying-annotations)
6666
- [Using namespace to group resources](#using-namespace-to-group-resources)
67+
- [Discovering other namespaces and their pods](#discovering-other-namespaces-and-their-pods)
68+
- [Creating a namespace](#creating-a-namespace)
69+
- [Managing objects in other namespaces](#managing-objects-in-other-namespaces)
70+
- [Understanding the isolation provided by namespaces](#understanding-the-isolation-provided-by-namespaces)
6771

6872
4. [Todo](#todo)
6973

@@ -725,7 +729,76 @@ You added the annotation `knrt10.github.io/someannotation` with the value `messi
725729

726730
### Using namespace to group resources
727731

728-
Previously we saw how labels organize pods and objects into groups.
732+
Previously we saw how labels organize pods and objects into groups. Because each object can have multiple labels, those groups of objects can overlap. Plus, when working with the cluster (through kubectl for example), if you don’t explicitly specify a label selector, you’ll always see all objects.
733+
734+
#### Discovering other namespaces and their pods
735+
736+
Let us first list all the namespaces in our cluster, type the following command
737+
738+
`kubectl get ns`
739+
```bash
740+
NAME STATUS AGE
741+
default Active 9h
742+
kube-public Active 9h
743+
kube-system Active 9h
744+
```
745+
746+
Up to this point, you’ve operated only in the `default` namespace. When listing resources with the `kubectl get` command, you’ve never specified the namespace explicitly, so kubectl always defaulted to the default namespace, showing you only the objects in that namespace. But as you can see from the list, the kube-public and the kube-system namespaces also exist. Let’s look at the pods that belong to the `kube-system` namespace, by telling kubectl to list pods in that namespace only:
747+
748+
`kubectl get po -n kube-system`
749+
```bash
750+
NAME READY STATUS RESTARTS AGE
751+
etcd-minikube 1/1 Running 0 4h
752+
kube-addon-manager-minikube 1/1 Running 1 9h
753+
kube-apiserver-minikube 1/1 Running 0 4h
754+
kube-controller-manager-minikube 1/1 Running 0 4h
755+
kube-dns-86f4d74b45-w8mqv 3/3 Running 4 9h
756+
kube-proxy-25t92 1/1 Running 0 4h
757+
kube-scheduler-minikube 1/1 Running 0 4h
758+
kubernetes-dashboard-5498ccf677-2zcw5 1/1 Running 2 9h
759+
storage-provisioner 1/1 Running 2 9h
760+
```
761+
762+
I will explain about these pods later (don’t worry if the pods shown here
763+
don’t match the ones on your system exactly). It’s clear from the name of the namespace that these are resources related to the Kubernetes system itself. By having them in this separate namespace, it keeps everything nicely organized. If they were all in the default namespace, mixed in with the resources you create yourself, you’d have a hard time seeing what belongs where, and you might inadvertently delete system resources.
764+
765+
Namespaces enable you to separate resources that don’t belong together into nonoverlapping groups. If several users or groups of users are using the same Kubernetes cluster, and they each manage their own distinct set of resources, they should each use their own namespace. This way, they don’t need to take any special care not to inadvertently modify or delete the other users’ resources and don’t need to concern themselves with name conflicts, because namespaces provide a scope for resource names, as has already been mentioned.
766+
767+
### Creating a namespace
768+
769+
A namespace is a Kubernetes resource like any other, so you can create it by posting a
770+
YAML file to the Kubernetes API server. Let’s see how to do this now.
771+
772+
You’re going to create a file called **custom-namespace.yml** (you can create it in any directory you want), or copy from this repo, where you’ll find the file with filename [custom-namespace.yml](https://github.com/knrt10/kubernetes-basicLearning/blob/master/custom-namespace.yml). The following listing shows the entire contents of the file.
773+
774+
```yml
775+
apiVersion: v1
776+
kind: Namespace
777+
metadata:
778+
name: custom-namespace
779+
```
780+
781+
Now type the following command
782+
783+
`kubectl create -f custom-namespace.yaml`
784+
> namespace/custom-namespace created
785+
786+
#### Managing objects in other namespaces
787+
788+
To create resources in the namespace you’ve created, either add a `namespace: customnamespace` entry to the metadata section, or specify the namespace when creating the resource with the `kubectl create` command:
789+
790+
`kubectl create -f kubia-manual.yaml -n custom-namespace`
791+
> pod/kubia-manual created
792+
793+
You now have two pods with the same name (kubia-manual). One is in the `default`
794+
namespace, and the other is in your `custom-namespace`.
795+
796+
When listing, describing, modifying, or deleting objects in other namespaces, you
797+
need to pass the `--namespace (or -n)` flag to kubectl. If you don’t specify the namespace, kubectl performs the action in the default namespace configured in the current kubectl context. The current context’s namespace and the current context itself can be changed through `kubectl config` commands.
798+
799+
#### Understanding the isolation provided by namespaces
800+
801+
To wrap up this section about namespaces, let me explain what namespaces don’t provide at least not out of the box. Although namespaces allow you to isolate objects into distinct groups, which allows you to operate only on those belonging to the specified namespace, they don’t provide any kind of isolation of running objects. For example, you may think that when different users deploy pods across different namespaces, those pods are isolated from each other and can’t communicate but that’s not necessarily the case. Whether namespaces provide network isolation depends on which networking solution is deployed with Kubernetes. When the solution doesn’t provide inter-namespace network isolation, if a pod in namespace foo knows the IP address of a pod in namespace bar, there is nothing preventing it from sending traffic, such as HTTP requests, to the other pod.
729802

730803
## Todo
731804

0 commit comments

Comments
 (0)