第十一题:
解析:
多容器pod的创建,先创建一个单容器的pod,然后在此基础上修改即可
解答:
先创建单容器的pod
kubectl run kucc1 --image=nginx --dry-run=client -oyaml >11.yaml
修改11.yaml如下:
cat 11.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: kucc1
name: kucc1
spec:
containers:
- image: nginx
name: nginx
- image: memcached
name: memcached
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Always
status: {}
验证:
root@k8s-master:~# kubectl apply -f 11.yaml
pod/kucc1 created
root@k8s-master:~# kubectl get po kucc1
NAME READY STATUS RESTARTS AGE
kucc1 2/2 Running 0 43s
对应的官网文档:https://kubernetes.io/zh/docs/concepts/workloads/pods/
第十二题:
按要求创建PV
解析:
pv的创建不能使用命令直接创建,因此,需要到官网找模板,官网模板地址:配置 Pod 以使用 PersistentVolume 作为存储 | Kubernetes
解答:
拷贝官网第一个模板,模板原文如下:
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
按题意修改成下面这个样子:
去掉StorageClassName这一行,修改存储路径,存储容量
apiVersion: v1
kind: PersistentVolume
metadata:
name: app-config
labels:
type: local
spec:
capacity:
storage: 2Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/srv/app-config"
测试:
由于此pv还没有被任何pvc绑定,因此,现在的状态是available,名称,容量大小,访问模式都和题目对上即可。
root@k8s-master:~# kubectl apply -f 12-pv.yaml
persistentvolume/app-config created
root@k8s-master:~# kubectl get pv -A
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
app-config 2Gi RWO Retain Available 16s
第十三题:
解析:
仍然还是上一题的官网地址:配置 Pod 以使用 PersistentVolume 作为存储 | Kubernetes
按题意要求创建一个pvc使用storageClass,sc名称是应用到名称为web-server的pod上面。
解答:
从官方文档拷贝模板文件:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
pvc的创建-----按照题意修改容量为10Mi,修改为如下:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pv-volume
spec:
storageClassName: csi-hostpath-sc
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Mi
pod的创建----仍然是从官网文档拷贝:
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
修改成如下:
apiVersion: v1
kind: Pod
metadata:
name: web-server
spec:
volumes:
- name: pv-volume
persistentVolumeClaim:
claimName: pv-volume
containers:
- name: web-server
image: nginx
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: pv-volume
pvc的扩容:
首先查看sc是否支持扩容:
可以看到ALLOWVOLUMEEXPANSION是true,表示此sc是支持扩容的
root@k8s-master:~# kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
csi-hostpath-sc k8s-sigs.io/nfs-subdir-external-provisioner Delete Immediate true 362d
kubectl edit pvc pv-volume --record
按题目要求修改后保存即可。
测试:
查看pvc的状态,是bound绑定状态为正确:
root@k8s-master:~# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
pv-volume Bound pvc-59909415-e9c2-44c1-9f77-848baa508921 10Mi RWO csi-hostpath-sc 6m47s
查看pod的详情,可以看到正确的挂载pvc:
root@k8s-master:~# kubectl describe pod web-server
Name: web-server
Namespace: default
Priority: 0
Node: k8s-node2/192.168.123.152
Start Time: Wed, 21 Dec 2022 17:05:59 +0800
Labels: <none>
Annotations: cni.projectcalico.org/containerID: 6636d0358690a5e72617c4537ffdcb5bd22927fa90db11f6b97d074e9eab9d66
cni.projectcalico.org/podIP: 10.244.169.180/32
cni.projectcalico.org/podIPs: 10.244.169.180/32
Status: Running
IP: 10.244.169.180
IPs:
IP: 10.244.169.180
Containers:
web-server:
Container ID: docker://02fc362221efc4ebdb95b5b9c4dd74e1f6ca69f4d9cb34f553261538b8a76530
Image: nginx
Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Wed, 21 Dec 2022 17:06:17 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/usr/share/nginx/html from pv-volume (rw)
第十四题:
解析:
这道题十分简单,输出日志到指定文件内即可
解答:
kubectl logs foobar | grep unable-to-access-website >/opt/KUTR00101/foobar
第十五题:
添加 sidecar 容器并输出⽇志
解析:
验证:
kubectl logs 11-factor-app sidecar #验证⽇志输出
0: Thu Dec 23 15:15:50 UTC 2021
0: Thu Dec 23 15:15:55 UTC 2021
0: Thu Dec 23 15:16:00 UTC 2021
这个也是送分题了。
解答:
root@k8s-master:~# kubectl top pod -l name=cpu-loader -A --sort-by='cpu'
NAMESPACE NAME CPU(cores) MEMORY(bytes)
kube-system calico-node-4l4ll 42m 131Mi
可以看看这个pod的标签都有哪些:
root@k8s-master:~# kubectl get pods -n kube-system calico-node-4l4ll --show-labels
NAME READY STATUS RESTARTS AGE LABELS
calico-node-4l4ll 1/1 Running 19 (161m ago) 378d controller-revision-hash=6b57d9cd54,k8s-app=calico-node,name=cpu-loader,pod-template-generation=1
第十七题:
解析:
题目说的是一个工作节点down掉了,节点名册是wk8s-node-0 ,那么,造成节点down掉的原因有很多,必须要登录这个节点,然后查看各个服务的状态,当然,首要的服务是kubelet,如果服务正常,那么,还需要查看系统日志来进一步判断。
解答:
登陆wk8s-node-0 节点后,发现kubelet服务停止了,启动后,查看kubelet服务是否正常即可。