架構(gòu)班的小伙伴作業(yè)看這里哦:(學(xué)習(xí)杰哥視頻的作業(yè)第31-32天)
1猖辫、總結(jié)描述kubectl常用命令并用實例說明顿痪。
1. help 幫助信息
# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
? create? ? ? ? Create a resource from a file or from stdin.
? expose? ? ? ? 使用 replication controller, service, deployment 或者 pod 并暴露它作為一個新的Kubernetes Service
? run? ? ? ? ? ? 在集群中運行一個指定的鏡像
? set? ? ? ? ? ? 為 objects 設(shè)置一個指定的特征
Basic Commands (Intermediate):
? explain? ? ? ? 查看資源的文檔
? get? ? ? ? ? ? 顯示一個或更多 resources
? edit? ? ? ? ? 在服務(wù)器上編輯一個資源
? delete? ? ? ? Delete resources by filenames, stdin, resources and names, or by resources and label selector
Deploy Commands:
? rollout? ? ? ? Manage the rollout of a resource
? scale? ? ? ? ? 為 Deployment, ReplicaSet, Replication Controller 或者 Job 設(shè)置一個新的副本數(shù)量
? autoscale? ? ? 自動調(diào)整一個 Deployment, ReplicaSet, 或者 ReplicationController 的副本數(shù)量
Cluster Management Commands:
? certificate? ? 修改 certificate 資源.
? cluster-info? 顯示集群信息
? top? ? ? ? ? ? Display Resource (CPU/Memory/Storage) usage.
? cordon? ? ? ? 標(biāo)記 node 為 unschedulable
? uncordon? ? ? 標(biāo)記 node 為 schedulable
? drain? ? ? ? ? Drain node in preparation for maintenance
? taint? ? ? ? ? 更新一個或者多個 node 上的 taints
Troubleshooting and Debugging Commands:
? describe? ? ? 顯示一個指定 resource 或者 group 的 resources 詳情
? logs? ? ? ? ? 輸出容器在 pod 中的日志
? attach? ? ? ? Attach 到一個運行中的 container
? exec? ? ? ? ? 在一個 container 中執(zhí)行一個命令
? port-forward? Forward one or more local ports to a pod
? proxy? ? ? ? ? 運行一個 proxy 到 Kubernetes API server
? cp? ? ? ? ? ? 復(fù)制 files 和 directories 到 containers 和從容器中復(fù)制 files 和 directories.
? auth? ? ? ? ? Inspect authorization
Advanced Commands:
? diff? ? ? ? ? Diff live version against would-be applied version
? apply? ? ? ? ? 通過文件名或標(biāo)準(zhǔn)輸入流(stdin)對資源進行配置
? patch? ? ? ? ? 使用 strategic merge patch 更新一個資源的 field(s)
? replace? ? ? ? 通過 filename 或者 stdin替換一個資源
? wait? ? ? ? ? Experimental: Wait for a specific condition on one or many resources.
? convert? ? ? ? 在不同的 API versions 轉(zhuǎn)換配置文件
Settings Commands:
? label? ? ? ? ? 更新在這個資源上的 labels
? annotate? ? ? 更新一個資源的注解
? completion? ? Output shell completion code for the specified shell (bash or zsh)
Other Commands:
? api-resources? Print the supported API resources on the server
? api-versions? Print the supported API versions on the server, in the form of "group/version"
? config? ? ? ? 修改 kubeconfig 文件
? plugin? ? ? ? Provides utilities for interacting with plugins.
? version? ? ? ? 輸出 client 和 server 的版本信息
Usage:
? kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.Use "kubectl options" for a list of global command-line options (applies to all commands).
2. kubect create 創(chuàng)建一個資源從一個文件或標(biāo)準(zhǔn)輸入
# kubectl create deployment nginx --image=nginx:1.14
# kubectl create -f my-nginx.yaml
3. kubectl run 在集群中運行一個指定的鏡像
# kubectl run nginx --image=nginx:1.16 --port=80 --replicas=1
4. kubectl expose 創(chuàng)建Service對象以將應(yīng)用程序"暴露"于網(wǎng)絡(luò)中
# kubectl expose deployment/nginx? --type="NodePort" --port=80 --name=nginx
5.kubectl get 顯示一個或更多resources資源
# kubectl get cs? ? ? ? ? ? ? ? ? ? ? ? ? ? # 查看集群狀態(tài)
# kubectl get nodes? ? ? ? ? ? ? ? ? ? ? # 查看集群節(jié)點信息
# kubectl get ns? ? ? ? ? ? ? ? ? ? ? ? ? ? # 查看集群命名空間
# kubectl get svc -n kube-system? ? ? ? ? ? ? ?# 查看指定命名空間的服務(wù)
# kubectl get pod <pod-name> -o wide? ? ? # 查看Pod詳細(xì)信息
# kubectl get pod <pod-name> -o yaml? ? ? # 以yaml格式查看Pod詳細(xì)信息
# kubectl get pods? ? ? ? ? ? ? ? ? ? ? ? # 查看資源對象缤苫,查看所有Pod列表
# kubectl get rc,service? ? ? ? ? ? ? ? ? # 查看資源對象,查看rc和service列表
# kubectl get pod,svc,ep --show-labels? ? # 查看pod,svc,ep能及標(biāo)簽信息
# kubectl get all --all-namespaces? ? ? ? ? ?# 查看所有的命名空間
6.kubectl clster-info 顯示集群信息
# kubectl cluster-info? ? ? ? ? ? # 查看集群狀態(tài)信息
7. kubectl describe 描述資源對象
# kubectl describe nodes <node-name>? # 顯示Node的詳細(xì)信息
# kubectl describe pods/<pod-name>? ? # 顯示Pod的詳細(xì)信息
8.kubectl scale pod擴容與縮容
# kubectl scale deployment nginx --replicas 5? ? # 擴容
# kubectl scale deployment nginx --replicas 3? ? # 縮容
9.查看服務(wù)器上支持的API資源
# kubectl api-resources
2、總結(jié)k8s的常見的volume使用。
2.1 背景
? ? ? ?存儲資源在所有計算資源中扮演著十分重要的角色薇宠,大部分業(yè)務(wù)場景下都有可能使用到各類存儲資源。在Kubernetes中艰额,系統(tǒng)通過Volume對集群中的容器動態(tài)或靜態(tài)提供存儲資源澄港。通常情況下,我們可以認(rèn)為容器或者Pod的生命周期時短暫的柄沮,當(dāng)容器被銷毀時回梧,容器內(nèi)部的數(shù)據(jù)也同時被清除。為了持久化保存容器的數(shù)據(jù)祖搓,Kubernetes引入了Volume狱意,類似于Docker的Volume(Docker also has a concept of volumes, though it is somewhat looser and less managed. In Docker, a volume is simply a directory on disk or in another Container. Lifetimes are not managed and until very recently there were only local-disk-backed volumes. Docker now provides volume drivers, but the functionality is very limited for now)。這個Volume被某個Pod掛載之后拯欧,這個Pod里面的所有容器都能使用這個Volume详囤。Kubernetes目前支持的volume類型可以參考文末官方資料。
2.2 兩種Volume使用舉例
2.2.1 emptyDir:
(1)?emptyDir是最基礎(chǔ)的Volume類型镐作。每個emptyDir Volume是主機上的一個空目錄,可以被Pod中所有的容器共享藏姐。它對于容器來說是持久的,對于Pod則不是滑肉。刪除容器并不會對它造成影響包各,只有刪除整個Pod時,它才會被刪除靶庙,它的生命周期與所掛載的Pod一致问畅。簡而言之,emptyDir類型的Volume在Pod分配到Node上時被創(chuàng)建六荒,Kubernetes會在Node主機上自動分配一個目錄护姆,因此無需指定Node主機上對應(yīng)的目錄文件。 這個目錄的初始內(nèi)容為空掏击,當(dāng)Pod從Node上移除時卵皂,emptyDir中的數(shù)據(jù)會被永久刪除。emptyDir主要用于一些無需永久保留的數(shù)據(jù)砚亭,例如臨時目錄灯变,多容器共享目錄等。我們通過實際案例來理解一下捅膘,Pod gysl的yaml如下:
apiVersion: v1
kind: Pod
metadata:
??name: gysl
spec:
??containers:
??- image: busybox
????name: gysl-01
????volumeMounts:
????- mountPath: /gysl-01
??????name: gysl-volume
????args:
????- /bin/sh
????- -c
????- echo "This is a test file.">/gysl-01/test.gysl;sleep 20000
??- image: busybox
????name: gysl-02
????volumeMounts:
????- mountPath: /gysl-02
??????name: gysl-volume
????args:
????- /bin/sh
????- -c
????- cat /gysl-02/test.gysl;sleep 20000
??volumes:
??- name: gysl-volume
????emptyDir: {}
(2) 創(chuàng)建Pod gysl添祸,并查看相關(guān)信息:
[root@k8s-m k8s-volumes]# kubectl apply -f emptyDir.yaml
pod/gysl created
[root@k8s-m k8s-volumes]# kubectl get pod
NAME ??READY ??STATUS ???RESTARTS ??AGE
gysl ??2/2 ????Running ??0 ?????????10m
[root@k8s-m k8s-volumes]# kubectl logs gysl gysl-02
This is a test file.
該例中,我們創(chuàng)建了一個名為gysl的Pod寻仗,這個pod里面包含gysl-01和gysl-02兩個容器刃泌,這兩個容器同時掛載了名為gysl-volume的emptyDir,gysl-01的掛載點為/gysl-01,gysl-02的掛載點為gysl-02耙替,容器gysl-01創(chuàng)建了一個test.gysl的文件亚侠,內(nèi)容為:“This is a test file.”在容器gysl-02中,成功顯示了gysl-01創(chuàng)建的文件的內(nèi)容俗扇。
2.2.2? hostPath
(1) hostPath: hostPath的主要作用是將主機的文件或目錄掛載給Pod的容器使用硝烂,使得容器能以較為良好的性能來存儲數(shù)據(jù)。接下來我們以Pod gysl-hostpath為例來理解一下hostPath的相關(guān)概念狐援,YAML文件如下:
apiVersion: v1
kind: Pod
metadata:
? name: gysl-hostpath
spec:
? nodeSelector:
? ? role: test
? containers:
? - image: ubuntu:18.04
? ? name: gysl-hostpath-container
? ? volumeMounts:
? ? - mountPath: /etc/gysl-test-01
? ? ? name: gysl-02
? ? - mountPath: /etc/gysl-test-02
? ? ? name: gysl-01
? ? - mountPath: /gysl-test-dir
? ? ? name: gysl-dir
? ? args:
? ? - /bin/bash
? ? - -c
? ? - cat /etc/gysl-test-01 /etc/gysl-test-02;ls -l /gysl-test-dir;sleep 3600
? volumes:
? - hostPath:
? ? ? path: /root/gysl-01
? ? name: gysl-01
? - hostPath:
? ? ? path: /root/gysl-02
? ? name: gysl-02
? - hostPath:
? ? ? path: /root/gysl-dir
? ? name: gysl-dir
(2) 在Node k8s-n1的/root目錄下創(chuàng)建如下文件和目錄:
[root@k8s-n1 ~]# ll
總用量 8
-rw-r--r-- 1 root root 16 11月 21 20:31 gysl-01
-rw-r--r-- 1 root root 16 11月 21 20:31 gysl-02
drwxr-xr-x 2 root root 51 11月 21 20:32 gysl-dir
(3) 兩個文件的內(nèi)容如下:
[root@k8s-n1 ~]# cat gysl-01
This is gysl-01
[root@k8s-n1 ~]# cat gysl-02
This is gysl-02
(4) 目錄gysl-dir的內(nèi)容如下:
[root@k8s-n1 ~]# ll gysl-dir/
總用量 12
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-01
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-02
-rw-r--r-- 1 root root 16 11月 21 20:32 gysl-03
(5) 給Node k8s-n1指定一個標(biāo)簽钢坦,apply 該Pod:
[root@k8s-m k8s-Volumes]# kubectl label node k8s-n1 role=test
node/k8s-n1 labeled
[root@k8s-m k8s-Volumes]# kubectl apply -f hostPath.yaml
pod/gysl-hostpath created
[root@k8s-m k8s-Volumes]# kubectl get pod -o wide
NAME? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE? IP? ? ? ? ? ? NODE? ? NOMINATED NODE
gysl-hostpath? 1/1? ? Running? 0? ? ? ? ? 17s? 10.244.1.177? k8s-n1? <none>
[root@k8s-m k8s-Volumes]# kubectl logs gysl-hostpath
This is gysl-02
This is gysl-01
total 12
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-01
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-02
-rw-r--r-- 1 root root 16 Nov 21 12:32 gysl-03
這個例子中,我把名為gysl-02的hostPath文件掛載到了容器的文件/etc/gysl-test-01上啥酱,把名為gysl-01的hostPath文件掛載到了容器的文件/etc/gysl-test-02上,把名為gysl-dir的hostPath目錄掛載到了/gysl-test-dir下厨诸。通過logs命令镶殷,我們不難發(fā)現(xiàn),目標(biāo)已經(jīng)達成微酬。
這種掛載方式比emptyDir更為持久绘趋,除非所在Node發(fā)生故障。不過颗管,除了掛載一些配置文件/二進制文件之外陷遮,一般不采用該類掛載方式,因為這樣的掛載操作增加了Pod文件與Node主機文件的耦合垦江,不利于統(tǒng)一管理帽馋。
2.3 總結(jié)
2.3.1 在volume的配置過程中,涉及到具體掛載路徑的需要按照一定的規(guī)則來配置比吭。例如:文件或目錄需要寫絕對路徑绽族。不按照規(guī)則來配置,會出現(xiàn)以下報錯:
Warning? Failed? ? 8s (x3 over 20s)? kubelet, k8s-n1? ? Error: Error response from daemon: create ~/gysl: "~/gysl-dir" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path
2.3.2 emptyDir和hostPath都是比較常見的兩種類型的volume衩藤,在使用時需要根據(jù)具體情況進行配置吧慢。其他類型的volume可參考以上兩種類型及官方文檔進行配置,相關(guān)官方文檔會在文末給出赏表。