1. 列出環境內所有的pv 並以 name字段排序(使用kubectl自帶排序功能)
kubectl get pv --sort-by=.metadata.name
2. 列出指定pod的日志中狀態為Error的行,並記錄在指定的文件上
kubectl logs <podname> | grep bash > /opt/KUCC000xxx/KUCC000xxx.txt
3.列出k8s可用的節點,不包含不可調度的 和 NoReachable的節點,並把數字寫入到文件里
#笨方法,人工數
kubectl get nodes
參考:kubernetes備忘:https://kubernetes.io/docs/reference/kubectl/cheatsheet/
4.創建一個pod名稱為nginx,並將其調度到節點為 disk=stat上
apiVersion: v1 kind: Pod metadata: name: nginx labels: env: test spec: containers: - name: nginx image: nginx imagePullPolicy: IfNotPresent nodeSelector: disktype: ssd
參考:將pod分配給節點,https://kubernetes.io/docs/tasks/configure-pod-container/assign-pods-nodes/
5. 提供一個pod的yaml,要求添加Init Container,Init Container的作用是創建一個空文件,pod的Containers判斷文件是否存在,不存在則退出
apiVersion: v1 kind: Pod metadata: name: init-demo spec: containers: - name: nginx image: busybox:1.28 ports: - containerPort: 80 command:['sh', '-c', 'if [ ! -e "/opt/myfile"]; then exit;fi;'] volumeMounts: - name: workdir mountPath: /opt/ # These containers are run during pod initialization initContainers: - name: install image: busybox command: ['sh', '-c', 'touch -p /opt/myfile'] volumeMounts: - name: workdir mountPath: /opt/ volumes: - name: workdir emptyDir: {}
參考:Init Container https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/#creating-a-pod-that-has-an-init-container
https://kubernetes.io/docs/concepts/workloads/pods/init-containers/
6. 指定在命名空間內創建一個pod名稱為test,內含四個指定的鏡像nginx、redis、memcached、busybox
必須自己寫 yaml
7.創建一個pod名稱為test,鏡像為nginx,Volume名稱cache-volume為掛在在/data目錄下,且Volume是non-Persistent的
apiVersion: v1 kind: Pod metadata: name: test-pod spec: containers: - name: test image: nginx volumeMounts: - mountPath: /data name: cache-volume volumes: - name: cache-volume emptyDir: {}
參考:volume : https://kubernetes.io/docs/concepts/storage/volumes/#local
8. 列出Service名為test下的pod 並找出使用CPU使用率最高的一個,將pod名稱寫入文件中
#使用-o wide 獲取service test的SELECTOR
kubectl get svc test -o wide
##獲取結果我就隨便造了
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
test ClusterIP None <none> 3306/TCP 50d app=wordpress,tier=mysql
#獲取對應SELECTOR的pod使用率,找到最大那個寫入文件中
kubectl top pods -l 'app=wordpress,tier=mysql'
9.創建一個Pod名稱為nginx-app,鏡像為nginx,並根據pod創建名為nginx-app的Service,type為NodePort
kubectl run nginx-app --image=nginx
之后創建service
apiVersion: v1 kind: Service metadata: name: nginx-app spec: selector: run: nginx-app ports: - name: http protocol: TCP port: 80 targetPort: 9376 - name: https protocol: TCP port: 443 targetPort: 9377 type: NodePort
參考:service https://kubernetes.io/docs/concepts/services-networking/service/
10.創建一個nginx的Workload,保證其在每個節點上運行,注意不要覆蓋節點原有的Tolerations
這道題直接復制文檔的yaml太長了,由於damonSet的格式和Deployment格式差不多,我用旁門左道的方法 先創建Deploy,再修改,這樣速度會快一點
kubectl run nginx --image=nginx:1.17.1 -oyaml > nginx-daemonset.yaml
# 修改yaml文件
vi nginx-daemonset.yaml
#修改apiVersion和kind #apiVersion: extensions/v1beta1 #kind: Deployment apiVersion:apps/v1 kind: DaemonSet metadata: creationTimestamp: null labels: run: nginx name: nginx spec: #去掉replicas # replicas: 1 selector: matchLabels: run: nginx strategy: {} template: metadata: creationTimestamp: null labels: run: nginx spec: containers: - image: nginx name: nginx resources: {} status: {}
11. 將deployment為nginx-app的副本數從1變成4。
#方法1 kubectl scale --replicas=4 deployment nginx-app #方法2,使用edit命令將replicas改成4 kubectl edit deploy nginx-app
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
12. 創建nginx-app的deployment ,使用鏡像為nginx:1.11.0-alpine ,修改鏡像為1.11.3-alpine,並記錄升級,再使用回滾,將鏡像回滾至nginx:1.11.0-alpine
# 創建nginx-app的deployment kubectl run nginx-app --image=nginx:1.11.0-alpine --record # 修改鏡像,nginx-app為container的名字 kubectl set image deployment nginx-app nginx-app=nginx:1.11.3-alipne # 回滾 kubectl rollout undo deployment nginx-app
參考:https://kubernetes.io/docs/reference/kubectl/cheatsheet/
13. 根據已有的一個nginx的pod、創建名為nginx的svc、並使用nslookup查找出service dns記錄,pod的dns記錄並分別寫入到指定的文件中
#創建一個服務 kubectl create svc nodeport nginx --tcp=80:80 #創建一個指定版本的busybox,用於執行nslookup kubectl create -f https://k8s.io/examples/admin/dns/busybox.yaml #將svc的dns記錄寫入文件中 kubectl exec -ti busybox -- nslookup nginx > 指定文件 #獲取pod的ip地址 kubectl get pod nginx -o yaml #將獲取的pod ip地址使用nslookup查找dns記錄 kubectl exec -ti busybox -- nslookup <Pod ip>
考點:網絡相關,DNS解析
參考:https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/
14. 創建Secret 名為mysecret,內含有password字段,值為bob,然后 在pod1里 使用ENV進行調用,Pod2里使用Volume掛載在/data 下
#將密碼值使用base64加密,記錄在Notepad里 echo -n 'bob' | base64
secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: Ym9i
pod1.yaml 使用env進行調用
apiVersion: v1 kind: Pod metadata: name: pod2 spec: containers: - name: mycontainer image: redis env: - name: SECRET_PASSWORD valueFrom: secretKeyRef: name: mysecret key: password
pod2.yaml 掛載到data目錄下
apiVersion: v1 kind: Pod metadata: name: pod1 spec: containers: - name: mypod image: nginx volumeMounts: - name: mysecret mountPath: "/data" readOnly: true volumes: - name: mysecret secret: secretName: mysecret
參考:https://kubernetes.io/docs/concepts/configuration/secret/
15. 使node1節點不可調度,並重新分配該節點上的pod
#直接drain會出錯,需要添加--ignore-daemonsets --delete-local-data參數
kubectl drain node node1 --ignore-daemonsets --delete-local-data
參考:https://kubernetes.io/docs/tasks/administer-cluster/safely-drain-node/
16. 使用etcd 備份功能備份etcd(提供enpoints,ca、cert、key)
ETCDCTL_API=3 etcdctl --endpoints https://127.0.0.1:2379 \ --cacert=ca.pem --cert=cert.pem --key=key.pem \ snapshot save snapshotdb
17. 給出一個失聯節點的集群,排查節點故障,要保證改動是永久的。
#查看集群狀態 kubectl get nodes #查看故障節點信息 kubectl describe node node1 #Message顯示kubelet無法訪問(記不清了) #進入故障節點 ssh node1 #查看節點中的kubelet進程 ps -aux | grep kubelete #沒找到kubelet進程,查看kubelet服務狀態 systemctl status kubelet.service #kubelet服務沒啟動,啟動服務並觀察 systemctl start kubelet.service #啟動正常,enable服務 systemctl enable kubelet.service #回到考試節點並查看狀態 exit kubectl get nodes #正常
參考:https://kubernetes.io/docs/tasks/debug-application-cluster/debug-cluster/
18. 創建一個pv,類型是hostPath,位於/data中,大小1G,模式ReadOnlyMany
apiVersion: v1 kind: PersistentVolume metadata: name: pv-host spec: capacity: storage: 1Gi volumeMode: Filesystem accessModes: - ReadWriteMany persistentVolumeReclaimPolicy: Recycle storageClassName: slow hostPath: path: /data
參考: https://kubernetes.io/docs/concepts/storage/persistent-volumes/
19. 給出一個集群,將節點node1添加到集群中,並使用TLS bootstrapping
https://blog.fanfengqiang.com/2019/03/11/kubernetes-TLS-Bootstrapping%E9%85%8D%E7%BD%AE/