01 - Core Concepts - Imperative Commands

Imperative Commands

$ kubectl run --help                                   
$ kubectl create service --help                    
$ kubectl create service clusterip --help       

$ kubectl expose --help                              
[Doc] kubectl expose
kubectl expose 主要用來將 Kubernetes Pod、Deployment、ReplicaSet 或 Service 暴露成一個新的 Kubernetes Service,允許內部存取(ClusterIP)或外部存取(NodePort, LoadBalancer)。


$ kubectl run custom-nginx --image=nginx --port=8080           

$ kubectl create deployment <deployment-name> --image=<image-name> --replicas=2 -n <namespace-name>       


(1) Method 1: imperative command

$ kubectl run redis -l tier=db --image=redis:alpine                    
$ kubectl run redis --lables="tier=db" --image=redis:alpine       

(2) Method 2: declarative command

$ kubectl run redis --image=redis:alpine --dry-run=client -o yaml > redis-pod.yaml     
$ vim redis-pod.yaml                                   
$ kubectl create -f redis-pod.yaml                
```yaml
---
apiVersion: v1
kind: Pod
metadata:
  labels:
    tier: db
  name: redis
spec:
  containers:
  - image: redis:alpine
    name: redis
  dnsPolicy: ClusterFirst
  restartPolicy: Always
```



Q: Create a service redis-service to expose the redis application within the cluster on port 6379.
$ kubectl expose pod redis --port=6379 --name redis-service             
$ kubectl get svc redis-service                    
$ kubectl describe svc redis-service             


Q: Create a deployment named webapp using the image kodekloud/webapp-color with 3 replicas.
$ kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3   
$ kubectl get deploy                                   


Q: 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.
$ kubectl run httpd --image=httpd:alpine --port=80 --expose                 
$ kubectl run httpd --image=httpd:alpine --port=80 --expose=true          
$ kubectl describe svc httpd                       

留言