add ai sample.py

This commit is contained in:
root 2025-06-05 19:09:00 +09:00
parent dbe50f31db
commit e0f1335a6f
26 changed files with 673 additions and 0 deletions

View File

@ -0,0 +1,5 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- tf.yaml

View File

@ -0,0 +1,45 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tf-notebook
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
nodeSelector:
nodegroup: gpu
containers:
- name: tf-notebook
image: tensorflow/tensorflow:2.16.2-gpu-jupyter
resources:
limits:
nvidia.com/gpu: 1
ports:
- containerPort: 8888
name: notebook
volumeMounts:
- name: notebook-storage
mountPath: /sample
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a1-
namespace: org1
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a2-
namespace: org1
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a3-
namespace: org2
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a4-
namespace: org2
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a5-
namespace: org3
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a6-
namespace: org3
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,16 @@
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../base
namePrefix: a1-
namespace: org2
patches:
- path: patch-pvc.yaml
target:
kind: PersistentVolumeClaim
name: tf-notebook-pvc
namespace: org1

View File

@ -0,0 +1,10 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
resources:
requests:
storage: 10Gi

View File

@ -0,0 +1,46 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: a1-tf-notebook-pvc
namespace: org2
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: a1-tf-notebook
namespace: org2
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
containers:
- image: tensorflow/tensorflow:2.16.2-gpu-jupyter
name: tf-notebook
ports:
- containerPort: 8888
name: notebook
resources:
limits:
nvidia.com/gpu: 1
volumeMounts:
- mountPath: /sample
name: notebook-storage
nodeSelector:
nodegroup: gpu
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: a1-tf-notebook-pvc

View File

@ -0,0 +1,45 @@
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: tf-notebook-pvc
namespace: org1
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
storageClassName: nfs-client
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: tf-notebook
spec:
replicas: 1
selector:
matchLabels:
app: tf-notebook
template:
metadata:
labels:
app: tf-notebook
spec:
nodeSelector:
nodegroup: gpu
containers:
- name: tf-notebook
image: tensorflow/tensorflow:2.16.2-gpu-jupyter
resources:
limits:
nvidia.com/gpu: 1
ports:
- containerPort: 8888
name: notebook
volumeMounts:
- name: notebook-storage
mountPath: /sample
volumes:
- name: notebook-storage
persistentVolumeClaim:
claimName: tf-notebook-pvc

60
monitoring/sample.py Normal file
View File

@ -0,0 +1,60 @@
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
import time
# 모델 평가 시작 시간 기록
start_time = time.time()
# GPU 메모리 제한 설정 (예: 2GB)
gpus = tf.config.list_physical_devices('GPU')
memory_limit = 1024 # MB 단위
tf.config.set_logical_device_configuration(
gpus[0],
[tf.config.LogicalDeviceConfiguration(memory_limit=memory_limit)]
)
print(f"Set memory limit to {memory_limit} MB for GPU: {gpus[0].name}")
# 특정 GPU만 사용 (예: GPU 1)
tf.config.set_visible_devices(gpus[0], 'GPU')
# 데이터셋 로드 및 전처리
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# 데이터 정규화 및 차원 확장
x_train = x_train / 255.0
x_test = x_test / 255.0
x_train = np.expand_dims(x_train, -1)
x_test = np.expand_dims(x_test, -1)
# 간단한 CNN 모델 정의
model = models.Sequential([
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
layers.MaxPooling2D((2, 2)),
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
# 모델 컴파일
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 모델 요약
model.summary()
# 모델 훈련
model.fit(x_train, y_train, epochs=500, batch_size=256, validation_split=0.2)
# 모델 평가
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"Test accuracy: {test_acc:.2f}")
# 모델 평가 종료 시간 기록 및 출력
end_time = time.time()
print(f"Evaluation time: {end_time - start_time:.2f} seconds")

14
monitoring/scp.sh Executable file
View File

@ -0,0 +1,14 @@
#!/bin/bash
# 변수 정의
NAMESPACE="org1" # 예: org1
POD_NAME="a2-tf-notebook-6bd88b8884-582nr" # 예: tf-notebook-abcdef
LOCAL_FILE="./sample.py" # 예: ./config.json
TARGET_PATH="/tf" # 예: /app/config/
# 멀티컨테이너가 아닐 경우
kubectl cp "$LOCAL_FILE" "$NAMESPACE/$POD_NAME:$TARGET_PATH"
# 멀티컨테이너인 경우 (container name 지정 필요)
# kubectl cp "$LOCAL_FILE" "$NAMESPACE/$POD_NAME:$TARGET_PATH" -c "$CONTAINER_NAME"