관리 메뉴

피터의 개발이야기

[kubernetes] kubectl object 생성 실습문제 본문

Kubernetes/기초공부

[kubernetes] kubectl object 생성 실습문제

기록하는 백앤드개발자 2024. 1. 17. 14:05
반응형

[kubernetes] 쿠버네티스 목차

ㅁ 들어가며

 kubectl 명령문을 사용하여 pod, deployment, service를 생성 연습해 보았다.

 

ㅁ nginx pod 생성

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

 

ㅁ redis Pod 생성

$ k run redis --image=redis:alpine --labels=tier=db
pod/redis created

ㅁ redis service 생성

# 조건
- Service: redis-service
- Port: 6379
- Type: ClusterIP

$ kubectl expose pod redis --port=6379 --name redis-service
service/redis-service exposed

 

ㅁ 특정 port 연결하여 pod 생성하기

$ k run custom-nginx --image=nginx --port=8080 --dry-run=client -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: custom-nginx
  name: custom-nginx
spec:
  containers:
  - image: nginx
    name: custom-nginx
    ports:
    - containerPort: 8080
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}

ㅁ NameSpace에 따라 deployment 생성해보기

$ k create deployment redis-deploy --image=redis --replicas=2 -n dev-ns
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: redis-deploy
  name: redis-deploy
  namespace: dev-ns
spec:
  replicas: 2
  selector:
    matchLabels:
      app: redis-deploy
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: redis-deploy
    spec:
      containers:
      - image: redis
        name: redis
        resources: {}
status: {}

 

 

ㅁ httpd pod 생성하고 서비스 연결하기

# pod 생성
$ k run httpd --image=httpd:alpine --dry-run=client -o yaml > pod_httpd.yaml

$ cat pod_httpd.yaml 
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: httpd
  name: httpd
spec:
  containers:
  - image: httpd:alpine
    name: httpd
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}


# 서비스 생성
$ k expose pod httpd --name httpd --port=80 --target-port=80 --dry-run=client -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: httpd
  name: httpd
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: httpd
status:
  loadBalancer: {}

 

반응형
Comments