11、列出所有的 service 为 foo ,在 namespace 为 production 的 Pod,这个文件的格式是每行一个 Pod
# kubectl -n production get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
foo ClusterIP 10.106.150.204 80/TCP 7s run=foo
# kubectl -n production get pods -l run=foo --no-headers | awk '{print $1}' > /opt/xxx/pod
12、创建 Secret 名为 mysecret,内含有 password 字段,值为 bob,然后 在 Pod1 里使用 Volume 挂载在 /data 下,pod2 里使用 ENV 进行调用,
#将密码值使用base64加密,记录在Notepad里
echo -n 'bob' | base64
secret.yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: Ym9i
# kubectl apply -f secret.yaml
pod1.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod1
spec:
containers:
- name: mypod
image: nginx
volumeMounts:
- name: mysecret
mountPath: "/data"
volumes:
- name: mysecret
secret:
secretName: mysecret
# kubectl apply -f pod1.yaml
pod2.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod2
spec:
containers:
- name: mycontainer
image: redis
env:
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
# kubectl apply -f pod2.yaml
参考:https://kubernetes.io/docs/concepts/configuration/secret/
13、创建一个 pod 名称为 test,镜像为 nginx,volume 名称 cache-volume 为挂在在 /data 目录下,且 Volume 是 non-Persistent 的
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- image: nginx
name: test-container
volumeMounts:
- mountPath: /cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {}
参考:https://kubernetes.io/docs/concepts/storage/volumes/#emptydir
14、将 deployment 为 nginx-app 的副本数从1变成6
# kubectl scale deployment nginx-app --replicas=6
15、统计 node 是ready状态,(不包含node的污点,没有调度的)
统计Ready数量N
# kubectl get node | grep -w Ready | wc -l
统计NoSchedule和Taints数量M
# kubectl describe nodes | grep Taints | grep -I NoSchedule | wc -l
# 答案填写N减去M得到的值
16、从标签为 name=cpu-utilizer的所有pod里面,找出cpu使用最高的那个pod,并写入到/opt/cpu.txt
# kubectl top pods -l name=cpu-utilizer -n kube-system
# echo '[找到的pod名]' >> /opt/cpu.txt
17、创建一个 deployment
名字为:nginx-dns
服务名为:nginx-dns
容器使用 nginx 镜像
使用 nslookup 工具来解析 service 和 pod 的记录并写入相应的/opt/service.dns 和/opt/pod.dns 文件中
#第一步:创建deployment
kubectl run nginx-dns --image=nginx
#第二步:发布服务
kubectl expose deployment nginx-dns --port=80
#第三步:查询podIP
kubectl get pods -o wide (获取pod的ip) 比如Ip是:10.244.1.37
#第四步:使用busybox1.28版本进行测试
apiVersion: v1
kind: Pod
metadata:
labels:
run: busybox28
name: busybox28
spec:
containers:
- image: busybox:1.28
name: busybox28
command:
- sleep
- "3600000"
#将svc的dns记录写入文件中
kubectl exec -ti busybox -- nslookup nginx-dns > 指定文件
#将获取的pod ip地址使用nslookup查找dns记录
kubectl exec -ti busybox -- nslookup > 指定文件
参考:https://kubernetes.io/docs/tasks/administer-cluster/dns-debugging-resolution/
18、etcd 快照
# ETCDCTL_API=3 etcdctl --help 查看帮助
# ETCDCTL_API=3 etcdctl --endpoints=https://127.0.0.1:2379
--cacert=/opt/xxx/ca.crt
--cert=/opt/xxx/etcd-client.crt
--key=/opt/xxx/etcd-client.key
snapshot save /opt/xxx/snapshot.db
参考:https://kubernetes.io/docs/tasks/administer-cluster/configure-upgrade-etcd/#backing-up-an-etcd-cluster
19、从集群中移除节点
# kubectl drain wk8s-node-1 --ignore-daemonsets --delete-local-data --force
20、修复 notready 状态 的节点
# kubectl get node 查看一个 node 是 notReady, ssh 上去
# systemctl status kubelet 发现没有启动
# systemctl start kubelet
# systemctl enable kubelet
# kubectl get node 查看node节点状态
21、创建 static pod
# 1、ssh到目标节点
# 2、在/etc/kubernetes/manifests 定义pod的yaml文件
kubectl run myservice --image=nginx --generator=run-pod/v1 --dry-run -o yaml >21.yml
# 3、配置kubelet
# 在kubelet配置(--config=/var/lib/kubelet/config.yaml)文件中
# 添加 staticPodPath: /etc/kubernetes/manifests
注意:systemctl status kubelet.service -l 可以查看kubelet的启动文件及加载的配置文件的具体路径
# 4、重启服务
systemctl daemon-reload
systemctl restart kubelet
systemctl enable kubelet
# 5、查看pod是否running
kubectl get pod
参考:https://kubernetes.io/docs/tasks/configure-pod-container/static-pod/
22、kubeadm 创建集群
23、提供了一个功能部分正常的集群,排除故障
ssh到集群节点,查看/etc/kubernetes/manifests 里有没有kube-apiserver等yaml,如果有则是以kubeadm方式安装,实现方式为静态Pod方式实现
找到kubelet服务加载的配置文件,chdwbl加上静态pod的路径即可(staticPodPath: /etc/kubernetes/manifests)。
# 重启服务
systemctl daemon-reload
systemctl restart kubelet
systemctl enable kubelet
# kubectl get node 查看node节点状态
24、创建 hostPath 持久卷
apiVersion: v1
kind: PersistentVolume
metadata:
name: app-config
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /srv/app-config