Pod
- ๊ฐ์ฅ ์์ ๋ฐฐํฌ ๋จ์
- ์ ์ฒด ํด๋ฌ์คํฐ์์ ๊ณ ์ ํ IP ํ ๋น
- 1~N ๊ฐ์ Container ๋ฅผ ํฌํจ
- Pods usually have a one-to-one relationship with containers running the app.
- A single pod can have multiple containers, not the same kind of container but helper containers instead.
- host ํด๋ ๊ณต์ , localhost ๋คํธ์ํฌ ๊ณต์
- This means the app container and the helper container can communicate through localhost.
How to deploy pods
kubectl run nginx --image nginx
kubectl get pods
- The first line of command will pull the image from wherever K8s is configured to pull the image from such as public Docker Hub or 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
- K8s uses YAML files as inputs for creating objects and all objects contain 4 top-level fields.
apiVersion
- The version of K8s API that is used for creating the object.
- e.g. v1, apps/v1, etc
kind
- The type of the object to be created.
- e.g. Pod, Service, ReplicaSet, Deployment, etc
metadata
- A dictionary for metadata describing the object.
- e.g. name, labels, etc
spec
- A specification of the object to be created.
- For the above example,
containers
section is a list that can be listed with-
character.
A pod can be created with the command below:
kubectl run nginx --image=nginx
or using a YAML file:
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 is similar but different. The Replication Controller is the older technology that ReplicaSet replaces.
- The ReplicaSet helps to run and maintain the desired number of pods in the K8s cluster by creating and deleting pods based on the configuration. This allows the K8s cluster to achieve high availability.
- It also provides load-balancing & scaling capability by deploying additional pods in existing or additional nodes. This means the ReplicaSet spans across multiple nodes in the cluster.
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
- A template of the pod definition.
replicas
- Number of replicas of the template.
Then run the below commands to create and see the objects created.
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
- A template of the pod definition.
replicas
- Number of replicas of the template.
selector
- It helps identify pods that fall under it so that it can monitor the selected pods and maintain the number of replicas in the cluster.
Then run the below commands to create and see the objects created.
kubectl create -f replicaset-definition.yaml
kubectl get replicaset
kubectl get pods
To scale the number of replicas by file:
kubectl replace -f replicaset-definition.yaml
To scale the number of replicas by command:
kubectl scale --replicas=6 -f replicaset-definition.yaml
or
kubectl scale --replicas=6 replicaset myapp-replicaset
- Note these two commands will not change the definition file.
Deployment
- ๋ด๋ถ์ ์ผ๋ก ReplicaSet ์ ์ด์ฉํ์ฌ ๋ฐฐํฌ ๋ฒ์ ์ ๊ด๋ฆฌ
- The Deployment provides the capability of upgrading underlying instances seamlessly using rolling updates, undo changes, pauses, and resume changes as required.
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
- All contents of the Deployment are exactly similar to the ReplicaSet definition file.
Run the below commands to create and find the objects created:
kubectl create -f deployment-definition.yaml
kubectl get deployments
kubectl get replicaset
kubectl get pods
or
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
ํ๋๋ฅผ ๋น๊ตํ์ฌ ๋ณ๊ฒฝ๋ ์ฌํญ๋ง ๋ถ๋ถ์ ์ผ๋ก ํ์ธํ๊ณ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ค.
Ingress
- ๋๋ฉ์ธ ๋๋ ๊ฒฝ๋ก๋ณ ๋ผ์ฐํ
- Nginx, HAProxy, ALB, โฆ