You must know
Kubernetes generate default
namespace when it’s installed.
And there are kube-system
and kube-pulibc
namespaces.
DNS
Connect to a service in the same namespace
If you want to connect to a database that is named db-service
in the same namespace that is default
.
1
| mysql.connect("db-service")
|
Connect to a service in the another namespace
If you want to connect to a database that is named db-service
in the another namespace that is dev
.
1
| mysql.connect("db-service.dev.svc.cluster.local")
|
cluster.local
Default domain name of the kubernetes cluster
svc
Sub domain for the service
Show PODs of the specific namespace
1
2
3
4
5
| # Show PODs of the `default` namespace
kubectl get pods
# Show PODs of the `kube-system` namespace
kubectl get pods --namespace=kube-system
|
Create a POD to the specific namespace
pod-definition.yml
1
2
3
4
5
6
7
8
9
10
11
| # To the `default` namespace
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
|
dev-pod-definition.yml
1
2
3
4
5
6
7
8
9
10
11
12
| # To the `dev` namespace
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: dev # -> Namespace
labels:
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
|
Create a POD
1
2
3
4
5
6
| # To the `default` namespace
kubectl create -f pod-definition.yml
# To the `dev` namespace
kubectl create -f pod-definition.yml --namespace=dev
kubectl create -f dev-pod-definition.yml
|
Create namespace
namespace-dev.yml
1
2
3
4
| apiVersion: v1
kind: Namespace
metadata:
name: dev
|
Create namespace
1
2
3
4
5
| # With YAML file
kubectl create -f namesapce-dev.yml
# Command
kubectl create namesapce dev
|
Switch namespace of the terminal
1
2
3
4
| kubectl config set-context $(kubectl config current-context) --namespace=dev
# Now, you can get PODs of `dev` namespace without `--namespace` option
kubectl pods
|
Show PODs of all namespaces
1
| kubectl get pods --all-namespaces
|
Resource Quota
compute-quota.yml
1
2
3
4
5
6
7
8
9
10
11
12
| apiVersion: v1
kind: ResourceQuota
metadata:
name: compute-quota
namespace: dev
spec:
hard:
pods: "10"
requests.cpu: "4"
requests.memory: 5Gi
limits.cpu: "10"
limits.memory: 10Gi
|
Create Quota
1
| kubectl create -f compute-quota.yml
|