๐พ 06. ๋ณผ๋ฅจ
1. EmptyDir
- Docker ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ
- ๋๋ ํ ๋ฆฌ ์์ฑ
$ mkdir -p ./fortune/docimg
$ mkdir -p ./fortune/kubetmp
์๋์ ๊ฐ์ด docker ์ด๋ฏธ์ง๋ฅผ ์์ฑํ๊ธฐ ์ํด bash ๋ก Application์ ์์ฑ ํฉ๋๋ค.
- fortuneloop.sh ๋ง๋ค๊ธฐ
#!/bin/bash
trap "exit" SIGINT
mkdir /var/htdocs
while :
do
echo $(date) Writing fortune to /var/htdocs/index.html
/usr/games/fortune > /var/htdocs/index.html
sleep 10
done
- Dockerfile ์์ฑ
# docimg/Dockerfile
FROM ubuntu:latest
RUN apt-get update; apt-get -y install fortune
ADD fortuneloop.sh /bin/fortuneloop.sh
RUN chmod 755 /bin/fortuneloop.sh
ENTRYPOINT /bin/fortuneloop.sh
- Docker ์ด๋ฏธ์ง ์์ฑ
docker build -t <DOCKER-ID>/fortune .
docker login -u <DOCKER-ID>
docker push <DOCKER-ID>/fortune
- Deployment ์์ฑ
html ๋ณผ๋ฅจ์ html-generator ๋ฐ web-seerver ์ปจํ ์ด๋์ ๋ชจ๋ ๋ง์ดํธ ํ์์ต๋๋ค.
html ๋ณผ๋ฅจ์๋ /var/htdocs ๋ฐ /usr/share/nginx/html ์ด๋ฆ ์ผ๋ก ์๋ก ๋ฐ๋ฅธ ์ปจํ ์ด๋์์ ๋ฐ๋ผ ๋ณด๊ฒ ๋ฉ๋๋ค.
๋ค๋ง, web-server ์ปจํ ์ด๋๋ ์ฝ๊ธฐ ์ ์ฉ(reeadOnly) ์ผ๋ก๋ง ์ ๊ทผ ํ๋๋ก ์ค์ ํ์์ต๋๋ค.
# kubetmp/fortune-deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: fortune-deployment
labels:
app: fortune
spec:
replicas: 3
selector:
matchLabels:
app: fortune
template:
metadata:
labels:
app: fortune
spec:
containers:
- image: dangtong/fortune
name: html-generator
volumeMounts:
- name: html
mountPath: /var/htdocs
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
emptyDir: {}
kubectl apply -f ./fortune-deploy.yml
EmptyDir ์ ์ฑ๋ฅ์ ์ํด ๋ฉ๋ชจ๋ฆฌ์ ๋ง๋ค ์ ์๋๋ฐ ์๋์ ๊ฐ์ด ์ค์ ํ๋ฉด ๋ฉ๋๋ค.
volumes:
- name: cache-volume
emptyDir:
medium: Memory
sizeLimit: 64Mi
- LoadBalancer ์์ฑ
# fortune-lb.yml
apiVersion: v1
kind: Service
metadata:
name: fortune-lb
spec:
selector:
app: fortune
ports:
- port: 80
targetPort: 80
type: LoadBalancer
externalIPs:
- 192.168.0.2
kubectl apply -f ./fortune-lb.yaml
- ์๋น์ค ํ์ธ
kubectl get svc
์๋น์ค ๋๋ฉ์ธ์ ํ์ธํด์ ๋ธ๋ผ์ฐ์ ๋ก ์ ์ ํด๋ด ๋๋ค.
2. Init Container์ ์ํ Git ๋ณผ๋ฃธ๊ตฌ์ฑ
Git ๊ณ์ ์์ฑ
GitHub.com ์ ์์ ์ ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํ์ฌ ‘k8s-web’ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ๋ง๋ญ๋๋ค.์์ค ํ์ผ ์์ฑ
mkdir -p ./gitvolume/html
mkdir -p ./gitvolume/kubetmp
<!-- gitvolume/html/index.html -->
<!DOCTYPE html>
<html>
<body>
<h1>K8s Landing Page</h1>
<p>Hello Kubernetes !!!</p>
</body>
</html>
- ๋ฆฌํฌ์งํ ๋ฆฌ ์์ฑ ๋ฐ ์ด๊ธฐํ
# gitvolume/html ๋๋ ํ ๋ฆฌ์์ ์ํ
git init
git add .
git commit -a -m "first commit"
git remote add origin https://github.com/<๊ณ์ ๋ช
>/k8s-web.git
git remote -v
git branch -M main
git push origin main
git status
- ์น์๋ฒ Deployment ์์ฑ
# givolume/kubetmp/gitvolume-deploy.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: gitvolume-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:alpine
name: web-server
volumeMounts:
- name: html
mountPath: /usr/share/nginx/html
readOnly: true
ports:
- containerPort: 80
protocol: TCP
volumes:
- name: html
gitRepo:
repository: https://github.com/<Your-Repository-ID>/k8s-web.git
revision: master
directory: .
kubectl apply -f ./gitvolume-deploy.yaml
- LoadBalancer ์์ฑ
# givolume/kubetmp/gitvolume-lb.yml
apiVersion: v1
kind: Service
metadata:
name: gitvolume-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: external
service.beta.kubernetes.io/aws-load-balancer-nlb-target-type: ip
service.beta.kubernetes.io/aws-load-balancer-scheme: internet-facing
spec:
selector:
app: nginx
ports:
- port: 80
targetPort: 80
type: LoadBalancer
kubectl apply -f ./gitvolume-lb.yaml
3. Persistent DISK ์์ฑ
- AWS EBS ์์ฑ
# AWS
$ aws ec2 create-volume --volume-type gp2 --size 80 --availability-zone ap-northeast-2a
## ์ญ์
## aws ec2 delete-volume --volume-id vol-038a54dff454064f6
## ์กฐํ
## aws ec2 describe-volumes --filters Name=status,Values=available Name=availability-zone,Values=ap-northeast-2a
- GCP ํด๋ฌ์คํฐ ์กฐํ
gcloud container clusters list
- ๋์คํฌ ์์ฑ
gcloud compute disks create --size=16GiB --zone asia-northeast1-b mongodb
- ๋์คํฌ ์ญ์
## ์ญ์
## gcloud compute disks delete mongodb --zone asia-northeast1-b
4. PV with PVC
- PV ์์ฑํ๊ธฐ
- AWS ์์ฑํ๊ธฐ
# pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodb-pv
spec:
capacity:
storage: 1Gi
csi:
driver: ebs.csi.aws.com
fsType: ext4
volumeHandle: vol-xxxxxxxxxxxxx
accessModes:
- ReadWriteOnce
- ReadOnlyMany
persistentVolumeReclaimPolicy: Retain
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: topology.ebs.csi.aws.com/zone
operator: In
values:
- ap-northeast-2a
AWS์ ๊ฒฝ์ฐ ebs csi ๋๋ผ์ด๋ฒ๋ฅผ ์ค์น ํ๋ค๋ฉด ๋
ธ๋์ ๋ค์๊ณผ ๊ฐ์ด ๋ผ๋ฒจ์ด ์๋์ผ๋ก ์ถ๊ฐ ๋ฉ๋๋ค.
topology.ebs.csi.aws.com/zone=ap-northeast-2b
์๋ ๋ช
๋ น์ด๋ก ํ์ธํด์ ebs csi ๋๋ผ์ด๋ฒ๊ฐ ์ค์น ๋์๋์ง ํ์ธ ํฉ๋๋ค.
kubectl get no
kubectl describe no <node-name>
- GCP ์์ฑํ๊ธฐ
# pv.yml
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodb-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
- ReadOnlyMany
persistentVolumeReclaimPolicy: Retain
gcePersistentDisk:
pdName: mongodb
fsType: ext4
- PVC ์์ฑํ๊ธฐ
# pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-pvc
spec:
resources:
requests:
storage: 1Gi
accessModes:
- ReadWriteOnce
storageClassName: ""
์ฌ๊ธฐ์ ์ค์ํ ๊ฒ์ storageClassName: “” ์ ๋ฐ๋์ ๋ช ๊ธฐ ํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ค. ๋ช ๊ธฐ ํ์ง ์์ผ๋ฉด Default StorageClass ์ธ gp2 ๊ฐ ์๋ ํ ๋น ๋ฉ๋๋ค.
kubectl apply -f ./pvc.yml
kubectl get pvc
- PV,PVC๋ฅผ ์ด์ฉํ Pod ์์ฑ
# pv-pvc-mongo.yml
apiVersion: v1
kind: Pod
metadata:
name: mongodb
spec:
containers:
- image: mongo
name: mongodb
volumeMounts:
- name: mongodb-data
mountPath: /data/db
ports:
- containerPort: 27017
protocol: TCP
volumes:
- name: mongodb-data
persistentVolumeClaim:
claimName: mongodb-pvc
kubectl apply -f ./pv-pvc-mongo.yml
kubectl get po,pv,pvc
- MongoDB ์ ์ ๋ฐ ๋ฐ์ดํฐ ์ธ์ํธ
kubectl exec -it mongodb -- mongo
use mystore
db.foo.insert({"first-name" : "dangtong"})
db.foo.find()
- MongoDB ์ฌ๊ฐ๋ ๋ฐ ๋ฐ์ดํฐ ํ์ธ
- MongdoDB ์ค๋จ
kubectl delete pod mongodb
- MongoDB Pod ์ฌ์์ฑ
kubectl apply -f ./pv-pvc-mongo.yml
- MongoDB ์ ์ ๋ฐ ํ์ธ
kubectl exec -it mongodb -- mongo
use mystore
db.foo.find()
- MongoDB ์ญ์
kubectl delete po mongodb
5. SC with PVC
- SC ํ์ธ
ํด๋ผ์ฐ๋์์ ์ ๊ณตํ๋ Default Storage Class ํ์ธ ํด๋ณด๊ธฐ
kubectl get sc
์์ธ๋ด์ญ ํ์ธ
kubectl describe sc gp2
- SC ์์ฑ
- AWS ์์ฑ
# sc.yml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: ebs.csi.aws.com
volumeBindingMode: WaitForFirstConsumer
# volumeBindingMode: Immediate
reclaimPolicy: Delete
parameters:
csi.storage.k8s.io/fstype: ext4
type: gp2
allowedTopologies:
- matchLabelExpressions:
- key: topology.ebs.csi.aws.com/zone
values:
- ap-northeast-2a
- ap-northeast-2b
- ap-northeast-2c
volumeBindingMode: Immediate ๋ PVC๋ฅผ ์์ฑํ๋ ์ฆ์ EBS๊ฐ ์๋ฌด AZ๋ ๋๋ค์ผ๋ก ์์ฑ๋๋ฉฐ, ์ดํ Pod์ด ํด๋น AZ์ ์๋ ๋
ธ๋์ ์ค์ผ์ค๋๋ฉด AZ mismatch ์๋ฌ ๋ฐ์ํ ์ ์์ต๋๋ค.
WaitForFirstConsumer ๋ก ์ค์ ํ๋ฉด Pod์ด ๋จผ์ ์ค์ผ์ค๋ ํ, ํด๋น ๋
ธ๋์ AZ๋ฅผ ๊ธฐ์ค์ผ๋ก EBS ์์ฑ ๋ฉ๋๋ค.
- GCP ์์ฑ
# sc.yml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: pd.csi.storage.gke.io
volumeBindingMode: WaitForFirstConsumer
reclaimPolicy: Retain
parameters:
type: pd-ssd
replication-type: none
allowedTopologies:
- matchLabelExpressions:
- key: topology.gke.io/zone
values:
- asia-northeast3-a
- asia-northeast3-b
- PVC ์์ฑํ๊ธฐ
# pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-pvc
spec:
storageClassName: fast
resources:
requests:
storage: 2Gi
accessModes:
- ReadWriteOnce
kubectl apply -f ./pvc.yaml
kubectl get pv,pvc
- PVC,SC ์ด์ฉํ Pod ์์ฑ
# pvc-sc-mongodb-pod.yml
apiVersion: v1
kind: Pod
metadata:
name: mongodb
spec:
containers:
- image: mongo
name: mongodb
volumeMounts:
- name: mongodb-data
mountPath: /data/db
ports:
- containerPort: 27017
protocol: TCP
volumes:
- name: mongodb-data
persistentVolumeClaim:
claimName: mongodb-pvc