Kubernetes

23.8.29(화) 쿠버네티스 3일차

사실 나도 모름 2023. 9. 1. 15:04

Local : pods를 특정 node로 지정해서 사용가능

 

persistant :

  • cluster의 생명주기와 동일 cluster 내 의 pod 사이에 공유가능

 

volume :

  • pod의 생명주기와 동일
  • pod내의 Container끼리 공유가능

 

PV(persistant volume)

PVC(persistant volume claim) : PV와 Pod을 매핑하고 연결해주는 역할 (중간자 역할)

 

 

#--------------------------vl-emptydir.yaml--------------------------
cat << EOF > vl-emptydir.yaml

apiVersion: v1
kind: Pod
metadata:
  name: sidecar
spec:
  containers:
    - name: app
      image: busybox
      args:
        - /bin/sh
        - -c
        - >
          while true;
          do
            echo "$(date)\n" >> /var/log/example.log;
            sleep 1;
          done
      volumeMounts:
        - name: varlog
          mountPath: /var/log
    - name: sidecar
      image: busybox
      args: [/bin/sh, -c, "tail -f /var/log/example.log"]
      volumeMounts:
        - name: varlog
          mountPath: /var/log
  volumes:
    - name: varlog
      emptyDir: {}
EOF
echo 'date' # ''이 $()를 치환
>> 뭐시기 날짜가 나옴
echo $(date) # 위의 명령과 결과가 같음
>> 뭐시기 날짜가 나옴
echo "$LOGNAME"
>> ubuntu
echo '$LOGNAME' # 홑따옴표와 쌍따옴표의 차이로 인해 결과가 달라진다. ""와 ''는 각각 변수
>> $LOGNAME

k apply -f vl-emptydir.yaml
k exec -it sidecar -- /bin/sh
k delete pods sidecar

# ----------------------------------vl-hostpath.yaml---------------------------------
cat << EOF > vl-hostpath.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: vl-webserver
spec:
  replicas: 3
  selector:
    matchLabels:
       app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: vl-webserver
        image: nginx:1.14
        volumeMounts:
        - mountPath: /var/local/data
          name: mydir
        - mountPath: /var/local/data/1.txt
          name: myfile
      volumes:
      - name: mydir
        hostPath:
         # Ensure the file directory is created.
         path: /var/local/data
         type: DirectoryOrCreate
      - name: myfile
        hostPath:
         path: /var/local/data/1.txt
         type: FileOrCreate
EOF

#--------------------------nfs 설치 및 설정----------------------------------------
apt install -y nfs-kernel-server
mkdir -m 777 {html,data}
vi /etc/exports
/data 192.168.108.0/24(rw,sync,no_root_squash)
/html 192.168.108.0/24(rw,sync,no_root_squash)
exportfs -a
systemctl restart nfs-kernel-server

showmount --exports master

#---------------------------vl-pv.yaml-------------------------------
cat << EOF > vl-pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany # ReadWriteOnce:1개 pod # ReadWriteMany:여러 pod # ReadOnlyMany:여러 pod
  storageClassName: manual # 사용자정의 이름
  persistentVolumeReclaimPolicy: Recycle # Recycle 삭제시 재활용 Retain 삭제시 보류 # Delete 삭제시 삭제
  nfs:
   server: 192.168.108.20
   path: /html
EOF

#----------------------------vl-pvc.yaml-----------------------------
cat << EOF > vl-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  volumeMode: Filesystem
  resources:
    requests:
      storage: 2Gi
  storageClassName: manual
EOF

#-----------------------------vl-pod.yaml-----------------------------
cat << EOF >  vl-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: /usr/share/nginx/html # Mount Point
      name: pvc-volume
  volumes:
  - name: pvc-volume
    persistentVolumeClaim:
      claimName: nfs-pvc
EOF

#----------------------------vl-pod-1.yaml-------------------------------
cat << EOF >  vl-pod-1.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-1
spec:
  containers:
  - name: nginx
    image: nginx:1.14
    ports:
    - containerPort: 80
    volumeMounts:
    - mountPath: /usr/share/nginx/html # Mount Point
      name: pvc-volume
  volumes:
  - name: pvc-volume
    persistentVolumeClaim:
      claimName: nfs-pvc
EOF

#-----------------------------vl-st-nginx.yaml-----------------------------
cat << EOF > vl-st-nginx.yaml

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: st-nginx
spec:
  selector:
    matchLabels:
      app: st-nginx
  replicas: 3
  serviceName: st-nginx
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: st-nginx
    spec:
      containers:
      - name: st-nginx
        image: nginx:latest
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: pvc-volume
      volumes:
      - name: pvc-volume
        persistentVolumeClaim:
          claimName: nfs-pvc
EOF



k create -f . # 디렉토리안에 있는 모든 파일을 실행

k get pv,pvc,pods
k get pv,pvc,pods -o wide
#-----------------------------------customization.yaml--------------------------------
cat << EOF > kustomization.yaml
secretGenerator:
- name: mysql-pass
  literals:
  - password=dkagh1.
resources:
  - pv-mysql.yaml
  - pv-wordpress.yaml
  - mysql-deployment.yaml
  - wordpress-deployment.yaml
EOF

#-------------------------------pv-mysql.yaml--------------------------------------
cat << EOF > pv-mysql.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-mysql
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  storageClassName: pv-mysql
  persistentVolumeReclaimPolicy: Recycle
  nfs:
   server: 192.168.108.20
   path: /data
EOF

#-------------------------------pv-wordpress.yaml-----------------------------------
cat << EOF > pv-wordpress.yaml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-wordpress
spec:
  capacity:
    storage: 10Gi
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  storageClassName: pv-wordpress
  persistentVolumeReclaimPolicy: Recycle
  nfs:
   server: 192.168.108.20
   path: /html
EOF

#-------------------------------mysql-deployment.yaml------------------------------------
cat << EOF > mysql-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  ports:
    - port: 3306
  selector:
    app: wordpress
    tier: mysql
#  clusterIP: None
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: pv-mysql
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress-mysql
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: mysql
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: mysql
    spec:
      containers:
              #- image: mysql:8.0
      - image: mysql
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        - name: MYSQL_DATABASE
          value: wordpress
        - name: MYSQL_USER
          value: wordpress
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc
EOF

#---------------------------wordpress-deployment.yaml--------------------------------
cat << EOF > wordpress-deployment.yaml
apiVersion: v1
kind: Service
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  ports:
    - port: 80
  selector:
    app: wordpress
    tier: frontend
#  type: LoadBalancer
  type: NodePort
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: wp-pvc
  labels:
    app: wordpress
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: pv-wordpress
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: wordpress
  labels:
    app: wordpress
spec:
  selector:
    matchLabels:
      app: wordpress
      tier: frontend
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: wordpress
        tier: frontend
    spec:
      containers:
      #- image: wordpress:6.2.1-apache
      - image: wordpress
        name: wordpress
        env:
        - name: WORDPRESS_DB_HOST
          value: wordpress-mysql
        - name: WORDPRESS_DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-pass
              key: password
        - name: WORDPRESS_DB_USER
          value: wordpress
        ports:
        - containerPort: 80
          name: wordpress
        volumeMounts:
        - name: wordpress-persistent-storage
          mountPath: /var/www/html
      volumes:
      - name: wordpress-persistent-storage
        persistentVolumeClaim:
          claimName: wp-pvc
EOF

 

'Kubernetes' 카테고리의 다른 글

23.9.1(금) 쿠버네티스 6일차  (0) 2023.09.01
23.8.25(금) 쿠버네티스 1일차  (0) 2023.09.01
23.8.28(월) 쿠버네티스 2일차  (0) 2023.09.01
23.8.30(수) 쿠버네티스 4일차  (0) 2023.09.01
23.8.31(목) 쿠버네티스 5일차  (0) 2023.09.01