Pod
- ๊ฐ์ฅ ์์ ๋ฐฐํฌ ๋จ์
- ์ ์ฒด ํด๋ฌ์คํฐ์์ ๊ณ ์ ํ IP ํ ๋น
- 1~N ๊ฐ์ Container ๋ฅผ ํฌํจ
- ์ผ๋ฐ์ ์ผ๋ก ํ๋๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์คํํ๋ ์ปจํ ์ด๋์ 1:1 ๊ด๊ณ๋ฅผ ๊ฐ๋๋ค.
- ํ๋์ ํ๋๋ ์ฌ๋ฌ ์ปจํ ์ด๋๋ฅผ ํฌํจํ ์ ์์ผ๋ฉฐ, ๋์ผํ ์ข ๋ฅ์ ์ปจํ ์ด๋๋ฅผ ๋ณต์ ํ๊ธฐ๋ณด๋ค๋ ํฌํผ(๋ณด์กฐ) ์ปจํ ์ด๋๋ฅผ ํจ๊ป ๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง๋ค.
- host ํด๋ ๊ณต์ , localhost ๋คํธ์ํฌ ๊ณต์
- ์ด๋ ์ฑ ์ปจํ ์ด๋์ ํฌํผ ์ปจํ ์ด๋๊ฐ localhost๋ฅผ ํตํด ํต์ ํ ์ ์์์ ์๋ฏธํ๋ค.
How to deploy pods
kubectl run nginx --image nginx
kubectl get pods
- ์ฒซ ๋ฒ์งธ ๋ช ๋ น์ Kubernetes๊ฐ ๊ตฌ์ฑ๋ ๋ ์ง์คํธ๋ฆฌ(๊ณต๊ฐ Docker Hub ๋๋ private registries ๋ฑ)์์ ์ด๋ฏธ์ง๋ฅผ ๊ฐ์ ธ์จ๋ค.
YAML in K8s
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
- Kubernetes๋ ์ค๋ธ์ ํธ ์์ฑ์ ์ํด YAML ํ์ผ์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉํ๋ฉฐ, ๋ชจ๋ ์ค๋ธ์ ํธ๋ 4๊ฐ์ ์ต์์ ํ๋๋ฅผ ๊ฐ๋๋ค.
apiVersion
- ์ค๋ธ์ ํธ ์์ฑ์ ์ํด ์ฌ์ฉํ๋ Kubernetes API ๋ฒ์
- e.g. v1, apps/v1 ๋ฑ
kind
- ์์ฑํ ์ค๋ธ์ ํธ์ ์ข ๋ฅ
- e.g. Pod, Service, ReplicaSet, Deployment ๋ฑ
metadata
- ์ค๋ธ์ ํธ๋ฅผ ์ค๋ช ํ๋ ๋ฉํ๋ฐ์ดํฐ(์ฌ์ ํํ)
- e.g. name, labels ๋ฑ
spec
- ์์ฑํ ์ค๋ธ์ ํธ์ ๊ตฌ์ฒด์ ์ธ ์ ์
- ์ ์์์์
containers
์น์ ์ ํ์ดํ(-)์ผ๋ก ๋์ด๋๋ ๋ฆฌ์คํธ๋ค.
์๋ ๋ช ๋ น์ผ๋ก ํ๋๋ฅผ ์์ฑํ ์ ์๋ค:
kubectl run nginx --image=nginx
๋๋ YAML ํ์ผ์ ์ฌ์ฉํ๋ค:
kubectl create -f pod-definition.yaml
kubectl get pods
kubectl describe pod myapp-pod
kubectl run redis --image=redis --dry-run=client -o yaml > redis.yaml
kubectl ๋ช
๋ น์ด๋ก template yaml file ์ ์์ฑํ๊ณ ์ถ์ ๊ฒฝ์ฐ --dry-run
๊ณผ -o yaml
์ต์
์ ํตํด yaml file ์ ์์ฑํ ์ ์๋ค.
ReplicaSet
- Replication Controller์ ReplicaSet์ ์ ์ฌํ์ง๋ง ๋ค๋ฅด๋ค. ReplicaSet์ ๊ธฐ์กด์ Replication Controller๋ฅผ ๋์ฒดํ๋ ๋ ์ต์ ๊ธฐ์ ์ด๋ค.
- ReplicaSet์ ์ค์ ์ ๋ฐ๋ผ ํ๋๋ฅผ ์์ฑ/์ญ์ ํ์ฌ ํด๋ฌ์คํฐ ๋ด ์ํ๋ ๊ฐ์์ ํ๋๋ฅผ ์ ์งํจ์ผ๋ก์จ ๊ณ ๊ฐ์ฉ์ฑ์ ์ ๊ณตํ๋ค.
- ๋ํ ๊ธฐ์กด ๋๋ ์ถ๊ฐ ๋ ธ๋์ ํ๋๋ฅผ ์ฆ์คํ์ฌ ๋ก๋๋ฐธ๋ฐ์ฑ๊ณผ ์ค์ผ์ผ๋ง์ ์ง์ํ๋ฉฐ, ReplicaSet์ ํด๋ฌ์คํฐ์ ์ฌ๋ฌ ๋ ธ๋์ ๊ฑธ์ณ ๋์ํ๋ค.
Creating Replication Controller with YAML
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp-rc
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
replicas: 3
template
- ํ๋ ์ ์์ ํ ํ๋ฆฟ.
replicas
- ํ ํ๋ฆฟ์ผ๋ก ์์ฑํ ํ๋์ ๋ณต์ ์.
์๋ ๋ช ๋ น์ผ๋ก ์์ฑํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๋ค.
kubectl create -f rc-definition.yaml
kubectl get replicationcontroller
kubectl get pods
Creating ReplicaSet with YAML
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
replicas: 3
selector:
matchLabels:
type: front-end
template
- ํ๋ ์ ์์ ํ ํ๋ฆฟ.
replicas
- ํ ํ๋ฆฟ์ผ๋ก ์์ฑํ ํ๋์ ๋ณต์ ์.
selector
- ReplicaSet์ด ๊ด๋ฆฌํ ํ๋๋ฅผ ์๋ณํ๊ธฐ ์ํ ์ ํ์์ด๋ฉฐ, ์ด๋ฅผ ํตํด ์ง์ ๋ ํ๋๋ค์ ๋ชจ๋ํฐ๋งํ๊ณ ์ํ๋ ๋ณต์ ์๋ฅผ ์ ์งํ๋ค.
์๋ ๋ช ๋ น์ผ๋ก ์์ฑํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๋ค.
kubectl create -f replicaset-definition.yaml
kubectl get replicaset
kubectl get pods
ํ์ผ์ ์์ ํด ์ค์ผ์ผ๋ง:
kubectl replace -f replicaset-definition.yaml
๋ช ๋ น์ผ๋ก ์ค์ผ์ผ๋ง:
kubectl scale --replicas=6 -f replicaset-definition.yaml
๋๋
kubectl scale --replicas=6 replicaset myapp-replicaset
- ์ด ๋ ๋ช ๋ น์ ์ ์ ํ์ผ ์์ฒด๋ฅผ ๋ณ๊ฒฝํ์ง ์๋๋ค.
Deployment
- ๋ด๋ถ์ ์ผ๋ก ReplicaSet ์ ์ด์ฉํ์ฌ ๋ฐฐํฌ ๋ฒ์ ์ ๊ด๋ฆฌ
- Deployment๋ ๋กค๋ง ์ ๋ฐ์ดํธ, ๋๋๋ฆฌ๊ธฐ(undo), ์ผ์ ์ค์ง/์ฌ๊ฐ ๋ฑ์ ํตํด ํ์ ์ธ์คํด์ค(ํ๋/ReplicaSet)์ ์ ๊ทธ๋ ์ด๋๋ฅผ ๋ฌด์ค๋จ์ผ๋ก ์ํํ ์ ์๊ฒ ํด์ค๋ค.
Creating Deployment with YAML
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-replicaset
labels:
app: myapp
type: front-end
spec:
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
replicas: 3
selector:
matchLabels:
type: front-end
- Deployment ์ ์๋ ReplicaSet ์ ์์ ์ฌ์ค์ ๋์ผํ๋ค.
์๋ ๋ช ๋ น์ผ๋ก ์์ฑํ๊ณ ์์ฑ๋ ์ค๋ธ์ ํธ๋ฅผ ํ์ธํ๋ค:
kubectl create -f deployment-definition.yaml
kubectl get deployments
kubectl get replicaset
kubectl get pods
๋๋
kubectl get all
Service
NodePort
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: NodePort
ports:
- targetPort: 80
- port: 80
- nodePort: 30008
selector:
app: myapp
type: front-end
- Node(host) ์ ๋ ธ์ถ๋์ด ์ธ๋ถ์์ ์ ๊ทผ ๊ฐ๋ฅํ ์๋น์ค
- ๋ชจ๋ Node ์ ๋์ผํ ํฌํธ๋ก ์์ฑ
- targetPort ๋ target pod ์ port ๋ฅผ ์ง์ , ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ port ์ ๊ฐ์ ๊ฐ์ ๊ฐ์ง
- port ๋ Service Object ์ port ๋ฅผ ์ง์ (required)
- nodePort ๋ 30,000 ~ 32,767 ๊น์ง์ ๋ฒ์ ์์ ์ํด์ผ ํ๋ฉฐ ์ง์ ํ์ง ์์ ๊ฒฝ์ฐ ์๋์ผ๋ก ํ ๋น๋จ
- selector ๋ฅผ ํตํด target pod ๋ฅผ ์ง์
- ํ๋์ Node ์ ๊ฐ์ ์ข ๋ฅ์ Pod ๊ฐ ์ฌ๋ฌ๊ฐ ์์ ๊ฒฝ์ฐ Service ๊ฐ ์์์ selector ์ ํด๋นํ๋ Pod ๋ฅผ ์ฐพ๊ณ ๋ถํ๋ฅผ ๋ถ์ฐํ์ฌ ๊ฐ๊ฐ Pod ์ Random ํ๊ฒ ๋ณด๋ด์ค.
- ์ฌ๋ฌ Node ์ Pod ๊ฐ ๋ฐฐํฌ๋์ด ์์ ๊ฒฝ์ฐ์๋ Service ๋ ๋ชจ๋ Node ์ ๊ฑธ์ณ ์ ์ฉ๋์ด ์๋ฌด Node ์ ์์ฒญ์ ๋ณด๋ด๋ ์์์ ์๋ฌด Node ์ ๋ฐฐํฌ๋ Pod ์ ํธ๋ํฝ์ ๋ณด๋.
ClusterIP
apiVersion: v1
kind: Service
metadata:
name: back-end
spec:
type: ClusterIP
ports:
- targetPort: 80
- port: 80
selector:
app: myapp
type: back-end
- ํด๋ฌ์คํฐ ๋ด๋ถ์์ ์ฌ์ฉํ๋ ํ๋ก์
- Pod ์ ๋์ ์ด์ง๋ง ์๋น์ค๋ ๊ณ ์ IP ๋ฅผ ๊ฐ์ง
- ํด๋ฌ์คํฐ ๋ด๋ถ์์ ์๋น์ค ์ฐ๊ฒฐ์ DNS ๋ฅผ ์ด์ฉ
- ๊ฐ๋ น 3-tier-architecture ์์ backend app ์ ์์ฒญ์ ๋ณด๋ด๊ณ ์ถ์ ๋ backend ๋ฅผ ๋ด๋นํ๋ ์ฌ๋ฌ Pod ๊ฐ cluster ์ ์ฒด์ ํผ์ ธ์์ผ๋ ClusterIP ๋ฅผ ํตํด ์ํ๋ layer ๋ฅผ ์ง์
kubectl run httpd --image=httpd --port=80 --expose
- kubectl run ์ผ๋ก Pod ๋ฅผ ์์ฑํ ๋ โexpose ์ต์ ์ ์ฃผ๋ฉด Pod ์ ์ฐ๊ฒฐ๋๋ ClusterIP ๋ฅผ ์๋์ผ๋ก ์์ฑํด์ค๋ค. ClusterIP ์ ์ฐ๊ฒฐ๋์ด์ผ ํ๊ธฐ ๋๋ฌธ์ โport ์ต์ ์ด ํ์์ ์ผ๋ก ํฌํจ๋์ด์ผ ํ๋ค.
LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: myapp-service
spec:
type: LoadBalancer
ports:
- targetPort: 80
- port: 80
- nodePort: 30008
- ํ๋์ IP ์ฃผ์๋ฅผ ์ธ๋ถ์ ๋ ธ์ถํ์ฌ ์ง์ ๋ Pod ์ ์์ฒญ์ด ๋๋ฌํ ์ ์๋๋ก ๋์์ค
- NodePort ๋์ AWS, Azure, GCP ๋ฑ์์ ์ ๊ณตํ๋ Native Load Balancer ๋ก ๋ถํ๋ฅผ ๋ถ์ฐํ์ฌ ๋ณด๋ด๋ ์ญํ ์ ์ํํจ
Namespaces
K8s Cluster ๋ฅผ ๊ตฌ์ถํ๊ฒ๋๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก Default, kube-system, kube-public Namespace ๋ค์ด ์๋์ผ๋ก ์์ฑ๋๋ค. ์ฌ์ฉ์๊ฐ ์์ฑํ Pod, Deployment, Service ๋ฑ K8s Object ๋ค์ ๊ธฐ๋ณธ์ ์ผ๋ก Default Namespace ์ ์ํ๊ฒ ๋๋ค.
kubectl get pods --namespace=kube-system
kube-system Namespace ์ ๊ฒฝ์ฐ, coredns, etcd-master, kube-apiserver-master, kube-controller-manager-master ๋ฑ K8s ์ ๊ธฐ๋ณธ์ ์ธ Component ๋ค์ ํ์ธํ ์ ์๋ค.
kubectl create -f pod-definition.yml --namespace=dev
K8s Object ๋ฅผ ํน์ Namespace ์ ๋ฐฐํฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ต์ ์ผ๋ก namespace ๋ฅผ ์ง์ ํด์ฃผ๊ฑฐ๋,
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
namespace: dev
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
์ ์ฒ๋ผ metadata ํ๋์ namespace ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด๋๋ค.
apiVersion: v1
kind: Namespace
metadata:
name: dev
Namespace ๋ฅผ ์์ฑํ ๋ ์์ฒ๋ผ Namespace yaml ์ ์์ฑํ ๋ค,
kubectl create -f namespace-dev.yml
์ ๋ช ๋ น์ด๋ก ์์ฑํ๊ฑฐ๋,
kubectl create namespace dev
yaml ํ์ผ ์์ด ๋ช ๋ น์ด๋ก ์์ฑํ ์ ์๋ค.
kubectl config set-context $(kubectl config current-context) --namespace=dev
์ ๋ช ๋ น์ด๋ฅผ ํตํด cli ์์ ์์น๋ฅผ ํน์ Namespace ๋ก ์ด๋์ํฌ ์๋ ์๋ค.
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
ํน์ Namespace ์ ํ๋์จ์ด ๋ฆฌ์์ค๋ฅผ ํ ๋นํ ๋์ ResourceQuota yaml ์ ์์ฑํ ๋ค,
kubectl create -f compute-quota.yml
์์ฑํ์ฌ ์์์ ํ ๋นํด์ค ์ ์๋ค.
Imperative vs Declarative
IaC ์์ ๋ช ๋ นํ๊ณผ ์ ์ธํ ๋ฐฉ์์ผ๋ก ์ธํ๋ผ๋ฅผ ๊ตฌ์ถํ ์ ์๋๋ฐ,
kubectl run --image=nginx nginx
kubectl create deployment --image=nginx nginx
kubectl expose deployment nginx --port 80
์ ๊ฐ์ ๋ช ๋ น์ด๋ค์ ํตํด Object ๋ฅผ ์์ฑํ๊ณ ,
kubectl edit deployment nginx
kubectl scale deployment nginx --replicas=5
kubectl set image deployment nginx nginx=nginx:1.18
์์ฑ๋ Object ๋ฅผ ์์ ํ ์ ์๋ค. ์ด๋ ๋ช ๋ นํ ๋ฐฉ์์ผ๋ก K8s ๋ฅผ ๊ตฌ์ถํ๋ ๋ฐฉ๋ฒ์ด๋ค.
kubectl create -f nginx.yaml
kubectl edit deployment nginx
K8s configuration file ์ ํตํด Object ๋ฅผ ์์ฑํ ๊ฒฝ์ฐ ์ ์ธํ ๋ฐฉ์์ผ๋ก ๊ตฌ์ถํ๋ค๊ณ ์๊ฐํ ์ ์์ง๋ง, edit ๋ช ๋ น์ด๋ฅผ ํตํด ์ ๊ทผํ file ์ K8s memory ์ ๋์ ์ผ๋ก ์์ฑ๋ yaml ์ธ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๋๋ฌธ์ edit ์ผ๋ก ์ค์ ์ ๋ฐ๊พผ๋ค๊ณ ๊ธฐ์กด์ ์์ฑํ nginx.yaml ์ ์์ ๋์ง ์๊ธฐ ๋๋ฌธ์ ์ถํ์ replace ๋ฑ์ ํ ๊ฒฝ์ฐ ์์ํ์ง ๋ชปํ ๋ณ๊ฒฝ์ด ์๊ธธ ์ ์๋ค. ์ด๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์ edit ๋์ configuration file ์์ฒด๋ฅผ ๋ณ๊ฒฝํ ํ replace ๋ฅผ ์คํํ๋ ๊ฒ์ด ๋ฐ๋์งํ๋ค.
kubectl apply -f nginx.yaml
K8s ๋ apply ๋ฅผ ํตํด ์ ์ธํ ๋ฐฉ์์ผ๋ก Object ๋ฅผ ์ ์ดํ ์ ์๋ค. apply ๋ ์ด๋ฏธ ๋ง๋ค์ด์ง Object ๋ฅผ ํ์ ํ๋ฉฐ ๋ถ๋ถ์ ์ผ๋ก ์ค์ ์ด ์์ ๋ ๊ฒฝ์ฐ ํด๋น ๋ถ๋ถ๋ง ์ ์ฉํ๋ ๊ธฐ๋ฅ ์ญ์ ์ง์ํ๋ค.
# Live object configuration
apiVersion: v1
kind: Pod
metadata:
name: myapp-pod
annotations:
kubectl.kubernetes.io/last-applied-configuration: {"apiVersion": "v1", ...}
labels:
app: myapp
type: front-end
spec:
containers:
- name: nginx-container
image: nginx
status:
conditions:
- lastProbeTime: null
...
kubectl apply ๋ฅผ ์คํํ๋ฉด K8s ๋ ์ฌ์ฉ์๊ฐ ์์ฑํ yaml file ๊ณผ K8s memory ์ ์ ์ฅ๋ Live object configuration ๊ณผ kubectl.kubernetes.io/last-applied-configuration
ํ๋๋ฅผ ๋น๊ตํ์ฌ ๋ณ๊ฒฝ๋ ์ฌํญ๋ง ๋ถ๋ถ์ ์ผ๋ก ํ์ธํ๊ณ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ค.