2020-10-13 架構(gòu)師第16周作業(yè)

架構(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)官方文檔會在文末給出赏表。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末检诗,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子瓢剿,更是在濱河造成了極大的恐慌逢慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件跋选,死亡現(xiàn)場離奇詭異涕癣,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門坠韩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來距潘,“玉大人,你說我怎么就攤上這事只搁∫舯龋” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵氢惋,是天一觀的道長洞翩。 經(jīng)常有香客問我,道長焰望,這世上最難降的妖魔是什么骚亿? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮熊赖,結(jié)果婚禮上来屠,老公的妹妹穿的比我還像新娘。我一直安慰自己震鹉,他們只是感情好俱笛,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著传趾,像睡著了一般迎膜。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上浆兰,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天磕仅,我揣著相機與錄音,去河邊找鬼镊讼。 笑死宽涌,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的蝶棋。 我是一名探鬼主播卸亮,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼玩裙!你這毒婦竟也來了兼贸?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤吃溅,失蹤者是張志新(化名)和其女友劉穎溶诞,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體决侈,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡螺垢,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片枉圃。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡功茴,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出孽亲,到底是詐尸還是另有隱情坎穿,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布返劲,位于F島的核電站玲昧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏篮绿。R本人自食惡果不足惜孵延,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望搔耕。 院中可真熱鬧隙袁,春花似錦、人聲如沸弃榨。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鲸睛。三九已至,卻和暖如春坡贺,著一層夾襖步出監(jiān)牢的瞬間官辈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工遍坟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拳亿,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓愿伴,卻偏偏與公主長得像肺魁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子隔节,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355