这部分主要的练习目的
- 了解kubernetes API的基本操作
- 创建、查看配置Pod的基本操作
- 查看集群中的所有命名空间
kubectl get namespaces
kubectl get ns #缩写
- 查看所有命令空间的Pod
kubectl get po --all-namespaces
- 查看指定命名空间里所有pod
使用-n参数指定命名空间
kubectl get po -n <namespace name>
示例
root@test:~# kubectl get po -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-drl99 0/1 Completed 0 5d19h
ingress-nginx-admission-patch-bwt9g 0/1 Completed 0 5d19h
ingress-nginx-controller-6f454c9b47-w26nj 1/1 Running 1 (8h ago) 5d19h
- 查看指定命名空间的Service
root@test:~# kubectl get svc -n ingress-nginx
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller NodePort 10.96.228.240 <none> 80:31767/TCP,443:31499/TCP 5d19h
ingress-nginx-controller-admission ClusterIP 10.96.130.226 <none> 443/TCP 5d19h
- 列出所有Pod,基于json path 表达式只展示name和namespace
# default空的所以需要加上命名空间
kubectl get pods -o=jsonpath="{.items[*]['metadata.name','metadata.namespace']}" -n <namespace name>
jsonpath格式输出(不好看)
ingress-nginx-admission-create-drl99 ingress-nginx-admission-patch-bwt9g ingress-nginx-controller-6f454c9b47-w26nj ingress-nginx ingress-nginx ingress-nginx
所以可以选择custom-columns格式输出
关于 -o 参数输出选项的设置,可以参考官方文档 Output options
- 使用yaml文件创建并查看运行Nginx的Pod
// 创建Pod
kubectl run nginx --image=nginx --restart=Never
// 列出pod
kubectl get po
- 使用yaml文件创建运行nginx的pod
// run 运行一个 Nginx Pod,然后通过 --dry-run=client 参数指定参数 -o 将 Pod 的定义描述写入文件
kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml > nginx-pod.yaml
// 通过yaml文件创建Pod
kubectl create -f nginx-pod.yaml
- 输出Pod的yaml文件描述
kubectl get po nginx -o yaml
可以看下第五题对于-o输出的选项
- 输出Pod的yaml文件描述,但不包含 集群特定信息(--export 选项已经被废弃了)
kubectl get po nginx -o yaml --export
- 获取Pod的详细信息
kubectl describe pod nginx
- 删除刚创建的Nginx Pod
kubectl delete po nginx
kubectl delete -f nginx-pod.yaml
- 强制删除Pod
kubectl delete po nginx --grace-period=0 --force
--grace-period 表示删除 Pod 前可以有几秒的处理时间,设置为负数时会被忽略,设置为 1 说明要立即删除。只有加上 --force 参数时才可以设置为 0,表示立刻强制删除。
- 创建Nginx Pod,并指定Nginx的版本和开放端口
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80
- 修改Pod的镜像版本并验证
# 通过命令的方式修改
kubectl set image pod/nginx nginx=nginx:1.15-alpine
# 通过edit方式,修改后保存
kubectl edit po nginx
# 修改后查看pod
kubectl describe pod nginx
- 将nginx镜像版本换回1.17.1
kubectl set image pod/nginx nginx=nginx:1.17.1
kubectl describe po nginx
kubectl get po nginx -w # watch it
- 不使用describe查看Pod的容器镜像版本
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
- 创建nginx pod 并执行shell 命令
// 创建pod
kubectl run nginx --image=nginx --restart=Never
// exec 执行
kubectl exec -it nginx /bin/sh
上面命令已经不推荐使用,推荐下面写法
kubectl exec [POD] -- [COMMAND]
kubectl exec -it nginx -- /bin/sh
- 查看Pod列表,并展示IP地址
kubectl get pods -o wide
依然是-o 命令输出
- 创建运行busybox容器的Pod并执行ls命令,查看其日志
# 创建Pod并执行命令
kubectl run busybox --image=busybox --restart=Never -- ls
# 查看日志
kubectl logs busybox
- 查看Pod中上一个容器实例的日志
kubectl logs busybox -p
- 创建运行busybox容器的Pod并执行命令 sleep 3600
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
- 从busybox容器的pod访问nginx pod
# 查看nginx pod的IP地址
kubectl get po nginx -o wide
# 在busybox 中执行命令,请求nginx pod
kubectl exec -it busybox -- wget -o- 10.244.0.14
- 创建运行busybox容器的pod并打印信息,然后手动删除
# 创建pod,打印详细
kubectl run busybox --image=busybox --restart=Never -it -- echo "How are you"
# 手动删除
kubectl delete po busybox
- 创建运行busybox容器的pod并打印信息后自动删除
kubectl run busybox --image=busybox --restart=Never -it --rm -- echo "How are you"
- 基于不同的等级查看Pod
# 创建容器
kubectl run nginx --image=nginx --restart=Never --port=80
# 通过 --v 指定查看的等级
kubectl get po nginx --v=7
kubectl get po nginx --v=8
kubectl get po nginx --v=9
- 查看Pod列表,使用自定义的POD_NAME和POD_STATUS展示名称和状态
kubectl get po -o=custom-columns="POD_NAME:.metadata.name,POD_STATUS:.status.containerStatuses[].state"
还是-o输出选项的使用
- 查看所有的Pod,根据名称排名
kubectl get pods --sort-by=.metadata.name
- 查看所有的Pod,根据创建时间排序
kubectl get pods --sort-by=.metadata.creationTimestamp
这部分理解Pod多容器设计模式
- 创建运行三个busybox容器的Pod并执行命令
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- bin/sh
- -c
- ls; sleep 3600
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 third container; sleep 3600
image: busybox
name: busybox3
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
创建Pod并查看日志(要注意缩进)
kubectl create -f multi-container.yaml
- 查看上面Pod中三个容器的日志 -c 参数指定容器
kubectl logs busybox -c busybox1
- 查看Pod中busybox2先前的容器日志
kubectl logs busybox -c busybox2 --previous
--previous 参数展示的上一个容器实例的日志,如果我们的 busybox2 容器没有重建过,那就是第一个实例,这时候是没法查看日志的,运行命令会报错
- 在Pod中的指定容器中运行命令
kubectl exec busybox -c busybox3 -- ls
- 查看Pod里容器的指标并存入文件中查看
kubectl top pod busybox --containers
kubectl top pod busybox --containers > file.log
# 通过 top 命令可以查看 node 和 pod 的 CPU 与内存使用,如下
kubectl top nodes
kubectl top pods
top 命令的使用需要安装 metric-server 并修改 TLS 相关的配置,否则会报错
- 创建一个多容器pod
要求
- 主容器:运行busybox容器,不断写入文本到index.html
- sideCar容器:运行nginx,读取主容器写入的文件index.html,对外提供访问
- 两个容器通过Volume实现文件的共享
下面是Pod的yaml文件
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: {}
- 查看Pod中主副容器的文件内容
# 查看主容器的文件
$ kubectl exec -it multi-cont-pod -c main-container -- sh
/ # cat /var/log/index.html
Hi I am from Main container
# 查看副容器的内容
$ kubectl exec -it multi-cont-pod -c sidecar-container -- sh
# cat /usr/share/nginx/html/index.html
Hi I am from Main container
Hi I am from Main container
Hi I am from Main container
# 在副容器中通过请求访问
# 安装 curl
# apt-get update && apt-get install -y curl
# 请求访问
# curl localhost
Hi I am from Main container
Hi I am from Main container
Hi I am from Main container
Hi I am from Main container
``