核心概念
1.列出集群中的所有命名空间
kubectl get namespaces
kubectl get ns
2.列出所有命名空间中的所有 Pod
kubectl get po --all-namespaces
3.列出特定命名空间中的所有 Pod
kubectl get po -n <namespace name>
4.列出特定命名空间中的所有 Service
kubectl get svc -n <namespace name>
5.用 json 路径表达式列出所有显示名称和命名空间的 Pod
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"
6.在默认命名空间中创建一个 Nginx Pod,并验证 Pod 是否正在运行
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// List the pod
kubectl get po
7.使用 yaml 文件创建相同的 Nginx Pod
// get the yaml file with --dry-run flag
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml
// cat nginx-pod.yaml
// create a pod
kubectl create -f nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
8.输出刚创建的 Pod 的 yaml 文件
kubectl get po nginx -o yaml
9.输出刚创建的 Pod 的 yaml 文件,并且其中不包含特定于集群的信息
kubectl get po nginx -o yaml --export
10.获取刚刚创建的 Pod 的完整详细信息
kubectl describe pod nginx
11.删除刚创建的 Pod
kubectl delete po nginx
kubectl delete -f nginx-pod.yaml
12.强制删除刚创建的 Pod
kubectl delete po nginx --grace-period=0 --force
13.创建版本为 1.17.4 的 Nginx Pod,并将其暴露在端口 80 上
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80
14.将刚创建的容器的镜像更改为 1.15-alpine,并验证该镜像是否已更新
kubectl set image pod/nginx nginx=nginx:1.15-alpine
kubectl describe po nginx
// another way it will open vi editor and change the version
kubeclt edit po nginx
kubectl describe po nginx
15.对于刚刚更新的 Pod,将镜像版本改回 1.17.1,并观察变化
kubectl set image pod/nginx nginx=nginx:1.17.1
kubectl describe po nginx
kubectl get po nginx -w # watch it
16.在不用 describe 命令的情况下检查镜像版本
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
//方案二
kubectl get po nginx -o yaml|grep image|grep 'nginx:'|head -1|awk '{ print $3 }'
17.创建 Nginx Pod 并在 Pod 上执行简单的 shell
// creating a pod
kubectl run nginx --image=nginx --restart=Never
// exec into the pod
kubectl exec -it nginx /bin/sh
18.获取刚刚创建的 Pod 的 IP 地址
kubectl get po nginx -o wide
19.创建一个 busybox Pod,在创建它时运行命令 ls 并检查日志
kubectl run busybox --image=busybox --restart=Never -- ls
kubectl logs busybox
20.如果 Pod 崩溃了,请检查 Pod 的先前日志
kubectl logs busybox -p
21.用命令 sleep 3600 创建一个 busybox Pod
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
22.检查 busybox Pod 中 Nginx Pod 的连接
kubectl get po nginx -o wide
// check the connection
kubectl exec -it busybox -- wget -o- <IP Address>
23.创建一个能回显消息“How are you”的 busybox Pod,并手动将其删除
kubectl run busybox --image=busybox --restart=Never -it -- echo "How are you"
kubectl delete po busybox
24.创建一个能回显消息“How are you”的 busybox Pod,并立即将其删除
// notice the --rm flag
kubectl run busybox --image=busybox --restart=Never -it --rm -- echo "How are you"
25.创建一个 Nginx Pod 并列出具有不同复杂度(verbosity)的 Pod
/ create a pod
kubectl run nginx --image=nginx --restart=Never --port=80
// List the pod with different verbosity
kubectl get po nginx --v=7
kubectl get po nginx --v=8
kubectl get po nginx --v=9
26.使用自定义列 PODNAME 和 PODSTATUS 列出 Nginx Pod
kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state"
27.列出所有按名称排序的 Pod
kubectl get pods --sort-by=.metadata.name
28.列出所有按创建时间排序的 Pod
kubectl get pods --sort-by=.metadata.creationTimestamp
多容器Pod
29.用“ls; sleep 3600;”“echo Hello World; sleep 3600;”及“echo this is the third container; sleep 3600”三个命令创建一个包含三个 busybox 容器的 Pod,并观察其状态
// first create single container pod with dry run flag
kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml
// edit the pod to following yaml and create it
kubectl create -f multi-container.yaml
kubectl get po busybox
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- bin/sh
- -c
- sleep 3600; ls
image: busybox
name: busybox1
resources: {}
- args:
- bin/sh
- -c
- echo Hello World; sleep 3600
image: busybox
name: busybox2
resources: {}
- args:
- bin/sh
- -c
- echo this is the third container; sleep 3600
image: busybox
name: busybox3
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
30.检查刚创建的每个容器的日志
kubectl logs busybox -c busybox1
kubectl logs busybox -c busybox2
kubectl logs busybox -c busybox3
31.检查第二个容器 busybox2 的先前日志(如果有)
kubectl logs busybox -c busybox2 --previous
32.在上述容器的第三个容器 busybox3 中运行命令 ls
kubectl exec busybox -c busybox3 -- ls
33.显示以上容器的 metrics,将其放入 file.log 中并进行验证
kubectl top pod busybox --containers
// putting them into file
kubectl top pod busybox --containers > file.log
cat file.log
34.用主容器 busybox 创建一个 Pod,并执行“while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep 5; done”,并带有暴露在端口 80 上的 Nginx 镜像的 sidecar 容器。用 emptyDir Volume 将该卷安装在 /var/log 路径(用于 busybox)和 /usr/share/nginx/html 路径(用于nginx容器)。验证两个容器都在运行。
// create an initial yaml file with this
kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml
// edit the yml as below and create it
kubectl create -f multi-container.yaml
kubectl get po multi-cont-pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multi-cont-pod
name: multi-cont-pod
spec:
volumes:
- name: var-logs
emptyDir: {}
containers:
- image: busybox
command: ["/bin/sh"]
args: ["-c","while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep 5; done"]
name: main-container
resources: {}
volumeMounts:
- name: var-logs
mountPath: /var/log
- image: nginx
name: sidecar-container
resources: {}
ports:
- containerPort: 80
volumeMounts:
- name: var-logs
mountPath: /usr/share/nginx/html
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
35.进入两个容器并验证 index.html是否存在,并用 curl localhost 从 sidecar 容器中查询 index.html
// exec into main container
kubectl exec -it multi-cont-pod -c main-container -- sh
cat /var/log/index.html
// exec into sidecar container
kubectl exec -it multi-cont-pod -c sidecar-container -- sh
cat /usr/share/nginx/html/index.html
// install curl and get default page
kubectl exec -it multi-cont-pod -c sidecar-container -- sh
# apt-get update && apt-get install -y curl
# curl localhost
Pod设计
36.获取带有标签信息的 Pod
kubectl get pods --show-labels
37.创建 5 个 Nginx Pod,其中两个标签为 env = prod,另外三个标签为 env = dev
kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod
kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod
38.确认所有 Pod 都使用正确的标签创建
kubeclt get pods --show-labels
39.获得带有标签 env = dev 的 Pod
kubectl get pods -l env=dev
40.获得带标签 env = dev 的 Pod 并输出标签
kubectl get pods -l env=dev --show-labels
41.获得带有标签 env = prod 的 Pod
kubectl get pods -l env=prod
42.获得带标签 env = prod 的 Pod 并输出标签
kubectl get pods -l env=prod --show-labels
43.获取带有标签 env 的 Pod
kubectl get pods -l env
44.获得带标签 env = dev、env = prod 的 Pod
kubectl get pods -l 'env in (dev,prod)'
45.获取带有标签 env = dev 和 env = prod 的 Pod 并输出标签
kubectl get pods -l 'env in (dev,prod)' --show-labels
46.将其中一个容器的标签更改为 env = uat 并列出所有要验证的容器
kubectl label pod/nginx-dev3 env=uat --overwrite
kubectl get pods --show-labels
47.删除刚才创建的 Pod 标签,并确认所有标签均已删除
kubectl label pod nginx-dev{1..3} env-
kubectl label pod nginx-prod{1..2} env-
kubectl get po --show-labels
48.为所有 Pod 添加标签 app = nginx 并验证
kubectl label pod nginx-dev{1..3} app=nginx
kubectl label pod nginx-prod{1..2} app=nginx
kubectl get po --show-labels
49.获取所有带有标签的节点(如果使用 minikube,则只会获得主节点)
kubectl get nodes --show-labels
50.标记节点(如果正在使用,则为 minikube)nodeName = nginxnode
kubectl label node minikube nodeName=nginxnode
51.建一个标签为 nodeName = nginxnode 的 Pod 并将其部署在此节点上
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml
// add the nodeSelector like below and create the pod
kubectl create -f pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
nodeSelector:
nodeName: nginxnode
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}