---
# System prepended metadata

title: Kubectl commnad
tags: [' kubectl', Kubernetes]

---

---
title: Kubectl commnad
tags: Kubernetes, kubectl
description: Kubectl common commnad
---

# Kubectl command

Kubctl在建立pod時以deployment為單位建置

---
## Kubectl create deployment
以deployment的單位建立container
```
$ kubectl create deployment NAME --image=image [--dry-run] [option]
```

---
## Kubectl create deployment with yaml
使用yaml file的configuration建立deployment
```
$ kubectl apply -f nginx-deployment.yaml
```
yaml file 最簡單格式如下，往下看第一個 spec is for deployment，template 以下稱為 Blueprint，重要為 metadata 下的 label 後續再建立 service 時 label 會取代 ip 進行綁定(詳細後續文章會介紹)，再往下第二個 spec is for pod，container image 開啟的 ports 在此 config

**nginx-deployment.yaml**
```
apiVersion: apps/v1
kind: Deployment # crate format is deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 8080
```
---
## Kubectl get deployment status
獲得運行中deployment的狀態資訊
```
$ kubectl get deployment
```
---
## Kubectl get pod status
獲得運行中pod的狀態資訊
```
$ kubectl get pod
```
---
## Kubectl get service detail
獲得運行中service資訊
```
$ kubectl get service -o wide
```
---
## Kubectl get replicaset status
獲得運行中pod的replicaset狀態資訊
```
$ kubectl get replicaset
```
---
## Kubectl get logs (Debug)
獲得運行中pod的logs資訊
```
$ kubectl logs [pod name]
```
---

## Kubectl describe pod
獲得運行中pod中運行資源的詳細狀態
```
$ kubectl describe pod [pod name]
```
---

## Kubectl enter executing container shell
進入運行中container的shell
```
$ kubectl exec -it [pod name] bash
```
---
## Kubectl delete deployment
刪除運行中deployment的pod
```
$ kubectl delete deployment [deployment name]
```

## Thank you! :dash: 

You can find me on
- GitHub: https://github.com/shaung08
- Email: a2369875@gmail.com