본 포스팅은 따배쿠(따라하면서 배우는 쿠버네티스)을 보고 정리한 내용입니다.
만약 blue namespace에서 kubectl get pods 명령어를 입력하면 홈쇼핑 애플리케이션을 위해 실행 중인 애플리케이션(파드)만 출력되게 됩니다.
$ kubectl get namespaces
NAME STATUS AGE
default Active 21d
kube-node-lease Active 21d
kube-public Active 21d
kube-system Active 21d
kubectl get pod 생략하면 base namespace로 동작
$ kubectl get pod
$ kubectl get pod --namespace defalut
$ kubectl get pod -n default
No resources found in defalut namespace.
kubectl get pods -n kube-system
$ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-64897985d-fdf7f 1/1 Running 8 (6d3h ago) 21d
etcd-minikube 1/1 Running 8 (6d3h ago) 21d
kube-apiserver-minikube 1/1 Running 8 (6d3h ago) 21d
kube-controller-manager-minikube 1/1 Running 8 (6d3h ago) 21d
kube-proxy-5v277 1/1 Running 8 (6d3h ago) 21d
kube-scheduler-minikube 1/1 Running 8 (6d3h ago) 21d
storage-provisioner 1/1 Running 11 (16m ago) 21d
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- image: nginx:1.14
name: nginx
ports:
- containerPort: 80
- containerPort: 443
namespace 생성
$ kubectl create namespace blue
namespace/blue created
--dry-run(실제로 실행하지 않고 실행 가능 여부만 확인)
-o yaml(실행 결과를 yaml 파일로 출력)
$ kubectl create namespace blue --dry-run -o yaml
W0523 16:59:48.623723 8852 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1 -
kind: Namespace
metadata:
creationTimestamp: null
name: orange
spec: {}
status: {}
$ kubectl create namespace blue --dry-run -o yaml > orange-ns.yaml
W0523 16:59:48.623723 8852 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: v1 -
kind: Namespace
metadata:
creationTimestamp: null
name: orange
spec: {}
status: {}
namespace 생성
$ kubectl create -f orange-ns.yaml
namespace/orange created
$ kubectl get namespaces
NAME STATUS AGE
blue Active 9m21s
default Active 21d
kube-node-lease Active 21d
kube-public Active 21d
kube-system Active 21d
orange Active 94s
$ kubectl create -f nginx.yaml -n blue
pod/mypod created
kubectl 명령어 대신에 yaml파일에 어떤 namespace에서 돌릴지를 기입할 수 있다.
apiVersion: v1
kind: Pod
metadata:
name: mypod
namespace: orange #namespace를 생략하면 orange namespace에서 생성됨
spec:
containers:
- image: nginx:1.14
name: nginx
ports:
- containerPort: 80
- containerPort: 443
base namespace는 원래 default 지만 변경 가능하다.
$ kubectl config set-context blue@kubernetes --cluster=kubernetes--user=kubernetes-admin --namespace=blue
Context "blue@kubernetes" created
$ kubectl config view
$ kubectl config current-context
kubernetes-admin@kubernetes
$ kubectl config use-context blue@kubernetes
Switched to conext "blue@kubernetes".
오브젝트를 삭제
kubectl delete object
$ kubectl delete pods [pod-name] -n default
pod "pod-name" deleted
다시 기본 namespace switch 하려면
$ kubectl config use-context kubernetes-admin@kubernetes
Switched to context "Kubernetes-admin@kubernetes".
네임스페이스 삭제하기
namespace 안에 api들(pod, service...) 중에 가장 큰 단위
따라서 namespace를 지우면 다 지워짐..
$ kubectl delete namespace blue
namespace "blue" deleted