본문 바로가기

Cloud

(Kubernetes) - resouce 생성 예시 file

반응형

🍳머리말

k8s resouce를 code 복붙으로 최대한 쉽게 만들어 test해보기 위한 예시들입니다. {}로 감싸진 부분은 직접 입력해야합니다. 계속 갱신 예정


📕storageclassname으로 pvc, pv, pod생성 

storage가 이미 있는 경우 해당 storage를 dynamic volume이 이용하기 위해 설정된 file을 이용하는 예제입니다. {차례대로 아래로 적용해줍니다.

📔 PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: task-pv-claim
spec:
  storageClassName: {storageclass명}
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi

📔 PV

kind: PersistentVolume
apiVersion: v1
metadata:
  name: task-pv
spec:
  storageClassName: {storageclass명}
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"

📔 pod

apiVersion: v1
kind: Pod
metadata:
  name: task-pv-pod
spec:
  volumes:
    - name: task-pv-storage
      persistentVolumeClaim:
        claimName: task-pv-claim
  containers:
    - name: task-pv-container
      image: nginx
      ports:
        - containerPort: 80
          name: "http-server"
      volumeMounts:
        - mountPath: "/usr/share/nginx/html"
          name: task-pv-storage

📕Deployment

📔 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

📕Service

📔 Service

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
  	#app: MyApp
    {선택할 pod label의 key}: {선택할 pod label의 value}
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

📕StatefulSet

📔 Service + StatefulSet

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # .spec.template.metadata.labels 와 일치해야 한다
  serviceName: "nginx"
  replicas: 3 # 기본값은 1
  minReadySeconds: 10 # 기본값은 0
  template:
    metadata:
      labels:
        app: nginx # .spec.selector.matchLabels 와 일치해야 한다
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

📕Secret

📔 Secret

apiVersion: v1
kind: Secret
metadata:
  name: {생성할 secret명}
  # annotations:
  #   kubernetes.io/service-account.name: "sa-name"
# type: kubernetes.io/service-account-token
data:
  # 사용자는 불투명 시크릿을 사용하므로 추가적인 키 값 쌍을 포함할 수 있다. 다음은 예시
  password: {base64로 encoding된 값} #ex) YmFy => decoding 결과 bar