본문 바로가기
IT 기술/k8s

[cka] Imperative Command

by Geunny 2024. 6. 1.
반응형

 

k8s 오브젝트를 관리하는 방법

 

1. Declarative Management - 선언형

2. Imperative Management - 명령형

 

2. Deploy a pod named nginx-pod using the nginx:alpine image.Use imperative commands only.

 

$ controlplane ~ ➜  k run nginx-pod --image nginx:alpine 
pod/nginx-pod created

 

 

3. Deploy a redis pod using the redis:alpine image with the labels set to tier=db.
Either use imperative commands to create the pod with the labels. Or else use imperative commands to generate the pod definition file, then add the labels before creating the pod using the file.

 

$ controlplane ~ ➜  k run redis --image redis:alpine --dry-run=client -o yaml > redis-pod.yaml
---redis-pod.yaml----
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: redis
    tier: db   # 추가
  name: redis
spec:
  containers:
  - image: redis:alpine
    name: redis
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
--------

controlplane ~ ➜  k apply -f redis-pod.yaml 
pod/redis created

 

 

4.  Create a service redis-service to expose the redis application within the cluster on port 6379.
Use imperative commands.

 

# 서비스 생성방법

$ controlplane ~ ✖ kubectl create service --help
Create a service using a specified subcommand.

Aliases:
service, svc

Available Commands:
  clusterip      Create a ClusterIP service
  externalname   Create an ExternalName service
  loadbalancer   Create a LoadBalancer service
  nodeport       Create a NodePort service

Usage:
  kubectl create service [flags] [options]

Use "kubectl create service <command> --help" for more information about a given
command.
Use "kubectl options" for a list of global command-line options (applies to all
commands).

 

 

Imperative Command

## 1

$ controlplane ~ ✖ kubectl create service clusterip redis-service --tcp=6379:6379
service/redis-service created

## 2

$ controlplane ~ ✖ kubectl expose pod redis --port=6379 --name redis-service

 

 

5. Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas.
Try to use imperative commands only. Do not create definition files.

 

$ controlplane ~ ➜  k create deployment --help
Create a deployment with the specified name.

Aliases:
deployment, deploy

Examples:
  # Create a deployment named my-dep that runs the busybox image
  kubectl create deployment my-dep --image=busybox
  
  # Create a deployment with a command
  kubectl create deployment my-dep --image=busybox -- date
  
  # Create a deployment named my-dep that runs the nginx image with 3 replicas
  kubectl create deployment my-dep --image=nginx --replicas=3
  
  # Create a deployment named my-dep that runs the busybox image and expose port
5701
  kubectl create deployment my-dep --image=busybox --port=5701

 

Imperative Command

$ controlplane ~ ➜  k create deployment webapp --image=kodekloud/webapp-color --replicas=3
deployment.apps/webapp created

 

 

6. Create a new pod called custom-nginx using the nginx image and run it on container port 8080.

$ controlplane ~ ➜  k run custom-nginx --image nginx --help | grep port
  # Start a hazelcast pod and let the container expose port 5701
  kubectl run hazelcast --image=hazelcast/hazelcast --port=5701
        If true, create a ClusterIP service associated with the pod.  Requires `--port`.
    --port='':
        The port that this container exposes.
  kubectl run NAME --image=image [--env="key=value"] [--port=port] [--dry-run=server|client] [--overrides=inline-json] [--command] -- [COMMAND] [args...] [options]

 

 

Imperative Command

 

$ controlplane ~ ➜  k run custom-nginx --image nginx --port 8080
pod/custom-nginx create

 

 

7. Create a new namespace called dev-ns.
Use imperative commands.

 

$ controlplane ~ ➜  k create ns dev-ns
namespace/dev-ns created

$ controlplane ~ ➜  k get ns
NAME              STATUS   AGE
kube-system       Active   39m
kube-public       Active   39m
kube-node-lease   Active   39m
default           Active   39m
dev-ns            Active   2s

 

 

8. Create a new deployment called redis-deploy in the dev-ns namespace with the redis image. It should have 2 replicas.
Use imperative commands.

 

$ controlplane ~ ➜  k create deployment redis-deploy --image=redis -n dev-ns --replicas=2
deployment.apps/redis-deploy created

 

 

9. Create a pod called httpd using the image httpd:alpine in the default namespace. Next, create a service of type ClusterIP by the same name (httpd). The target port for the service should be 80.

Try to do this with as few steps as possible.

 

step1 . httpd pod 만들기

step2 httpd svc(ClusterIP) 만들기

 

# step1

$ controlplane ~ ➜  k run httpd --image httpd:alpine
pod/httpd created

# step2

$ controlplane ~ ➜  k expose pod httpd --port=80 --name httpd
service/httpd exposed

 

'IT 기술 > k8s' 카테고리의 다른 글

[cka] Labels and Selectors  (0) 2024.06.08
[cka] Manual Scheduling  (0) 2024.06.08
[cka] Services  (0) 2024.06.01
[cka] namespace  (1) 2024.05.28
[cka] Deployments  (0) 2024.05.28

댓글