김 숨 2025. 5. 11. 22:02

 

imperative 방식 

 

--dry-run=client =>  실제로 생성하지 않고 클라이언트에서 실행 시뮬레이션

 

`kubectl run`  Pod 또는 Deployment 등을 즉시 생성

내부적으로는 YAML 리소스를 만들고 적용

kubectl run nginx --image=nginx --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

 

`kubeclt create deployment` 

지정한 이름의 Deployment 리소스 생성

해당 Deployment는 지정한 이미지로 Pod를 생성

kubectl create deployment --image=nginx nginx --dry-run=client -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

 

`kubectl scale deployment nginx --replicas=3`

replica 늘리기

 

 

`kubectl expose`

기존에 생성된 리소스(Pod, Deployment) 등을 기반으로 자동으로 selector와 포트 등을 채워서 Service를 생성

Pod redis를 클러스터 내부에서 접근 가능한 서비스로 노출(Expose)

 kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: redis
  name: redis-service
spec:
  ports:
  - port: 6379
    protocol: TCP
    targetPort: 6379
  selector:
    run: redis
status:
  loadBalancer: {}

 

Redis라는 이름의 ClusterIP 타입의 Kubernetes Service를 생성

 kubectl create service clusterip redis --tcp=6379:6379 --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: redis   # 이 label이 자동 생성됨
  name: redis
spec:
  ports:
  - name: 6379-6379
    port: 6379
    protocol: TCP
    targetPort: 6379
  selector:
    app: redis    # redis Pod의 label과 일치해야 서비스 연결 가능
  type: ClusterIP
status:
  loadBalancer: {}

서비스를 만들 때 자동으로 selector: app=redis 를 붙임( redis라는 서비스니 app=redis 라벨을 가진 Pod이 있을 거야라고 가정) => 실제로 라벨이 있는지 확인하지 않음

Pod 또는 Deployment가 metadata.labels.app: redis로 되어 있어야 함

 

*create는 nodeport 지정 expose는 nodeport 지정 불가

 

pod에 라벨 추가하기

 kubectl label pod redis app=myapp

바꾸려면 --overwrite 추가

 

'dev'이름을 가진 namespace 추가

 kubectl create ns dev

 

kubectl create deployment redis-deploy --image=redis --replicas=2 --namespace dev

 

 

 

  • httpd:alpine 이미지를 사용하여 httpd라는 이름의 Pod를 생성
  • 그 Pod와 연결된 이름이 같은 ClusterIP 타입의 서비스를 생성

kubectl run httpd --image=httpd:alpine --port=80 --expose=true

 kubectl run httpd --image=httpd:alpine --port=80 --expose=true --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  name: httpd
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: httpd
status:
  loadBalancer: {}
---
---
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: httpd
  name: httpd
spec:
  containers:
  - image: httpd:alpine
    name: httpd
    ports:
    - containerPort: 80
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}