四,如何优雅的建立pods为以后的管理工作提供一个良好的开端---pods的调度问题
还是以上面那个pod-demo 文件为例,将该pod部署到node标签设定为nodeenv=web的节点(也就是slave2)(设定node的标签并且查询):
[root@master ~]# k get nodes NAME STATUS ROLES AGE VERSION c7n.cnn Ready master 30d v1.19.4 slave1 Ready <none> 30d v1.19.4 slave2 Ready <none> 30d v1.19.4 [root@master ~]# k label node slave1 nodeenv=dev --overwrite=true node/slave1 labeled [root@master ~]# k label node slave2 nodeenv=web --overwrite=true node/slave2 labeled [root@master ~]# k get nodes slave1 --show-labels NAME STATUS ROLES AGE VERSION LABELS slave1 Ready <none> 30d v1.19.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=slave1,kubernetes.io/os=linux,nodeenv=dev [root@master ~]# k get nodes slave2 --show-labels NAME STATUS ROLES AGE VERSION LABELS slave2 Ready <none> 30d v1.19.4 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=slave2,kubernetes.io/os=linux,nodeenv=web
部署文件的内容应该修改成这样:
apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp spec: nodeSelector: nodeenv: web containers: - name: myapp-1 image: hub.escapelife.site/library/nginx-test:v1 - name: busybox-1 image: busybox:latest command: - "/bin/sh" - "-c" - "sleep 3600"
五,pods的删除
删除pod比较的简单,如果是使用的资源清单文件,kubectl delete -f 资源清单文件就可以删除了。如果是命令行生成的pod,kubectl delet pod pod名称 -n 命名空间 即可。还是以上面的那个资源清单文件为例:
[root@master ~]# k delete -f test-pod.yaml pod "pod-demo" deleted
如果资源清单文件使用了deployment控制器,那么,删除的时候必须是这样的:
以这个资源清单为例:
[root@master ~]# cat test-pod.yaml apiVersion: apps/v1 kind: Deployment metadata: name: pod-demo namespace: default spec: selector: matchLabels: app: nginx replicas: 3 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.20 imagePullPolicy: IfNotPresent ports: - containerPort: 80
deployment控制器的名称是pod-demo,因此,删除命令是:
1. [root@master ~]# k delete deployment pod-demo -n default 2. deployment.apps "pod-demo" deleted
此时如果查询pods,将不会看到那三个副本啦。
六,对于pods的具体管理工作---查----如何优雅的解决pods运行时的报错,测试pods的健康状态
查看资源里关于pods的描述:
[root@master ~]# k api-resources NAME SHORTNAMES APIGROUP NAMESPACED KIND bindings true Binding componentstatuses cs false ComponentStatus configmaps cm true ConfigMap endpoints ep true Endpoints events ev true Event limitranges limits true LimitRange namespaces ns false Namespace nodes no false Node persistentvolumeclaims pvc true PersistentVolumeClaim persistentvolumes pv false PersistentVolume pods po true Pod
可以看到,pods是有namespace隔离的,因此,查询pods的时候,如果不是default的namespace,需要-n指定namespace。
(1)
查询所有的pods
[root@master ~]# k get po -A NAMESPACE NAME READY STATUS RESTARTS AGE database mysql2-5db57c8bc8-7bwbg 1/1 Running 9 7d20h default nfs-client-provisioner-6fc484bd4f-pjxm7 1/1 Running 10 7d1h
(2)
查询某个pods的详细情况,例如上面的MySQL:
[root@master ~]# k describe pod mysql2-5db57c8bc8-7bwbg -n database Name: mysql2-5db57c8bc8-7bwbg Namespace: database Priority: 0 Node: slave1/192.168.217.17 Start Time: Wed, 06 Jul 2022 17:01:21 +0800 Labels: app=mysql2 pod-template-hash=5db57c8bc8 Annotations: <none> Status: Running IP: 10.244.1.30 IPs: IP: 10.244.1.30 Controlled By: ReplicaSet/mysql2-5db57c8bc8 Containers: mysql2: Container ID: docker://3add65793a00e60deedee16df782ec5510b7ea8450976adc1612aff0d97cf989 Image: mysql:5.7.23 Image ID: docker-pullable://mysql@sha256:953b53af26805d82eca95f28df6ae82e8e15cd1e587b4c5cd06a78be80e84050 Port: 3306/TCP Host Port: 0/TCP State: Running Started: Thu, 14 Jul 2022 12:37:21 +0800 Last State: Terminated Reason: Error Exit Code: 255 Started: Thu, 14 Jul 2022 09:15:39 +0800 Finished: Thu, 14 Jul 2022 12:36:20 +0800 Ready: True Restart Count: 9 Environment: MYSQL_ROOT_PASSWORD: 123456 Mounts: /var/lib/mysql from nfs-pvc-test (rw,path="mysql") /var/run/secrets/kubernetes.io/serviceaccount from default-token-96m7s (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: nfs-pvc-test: Type: PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace) ClaimName: nfs-pvc-test ReadOnly: false default-token-96m7s: Type: Secret (a volume populated by a Secret) SecretName: default-token-96m7s Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: <none>
以上信息表明该pod运行在slave1节点,使用了pvc ,pvc的名称是nfs-pvc-test,当然还有一些别的信息,就不详细说了。
(3)
查询这个pods运行的日志(kubectl logs pod名称 -n namespace名称):
[root@master ~]# k logs mysql2-5db57c8bc8-7bwbg -n database 2022-07-14T04:37:22.972354Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-07-14T04:37:23.006575Z 0 [Note] mysqld (mysqld 5.7.23) starting as process 1 ... 2022-07-14T04:37:23.041054Z 0 [Note] InnoDB: PUNCH HOLE support available 2022-07-14T04:37:23.041186Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
如果该pod有问题,查看日志是一个比较快速有效的方法哦。
(4)
查询所有pods的扩展信息(增加参数 -o wide):
[root@master ~]# k get po -A -o wide NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES database mysql2-5db57c8bc8-7bwbg 1/1 Running 9 7d20h 10.244.1.30 slave1 <none> <none> default nfs-client-provisioner-6fc484bd4f-pjxm7 1/1 Running 10 7d1h 10.244.2.37 slave2 <none> <none> default nginx-7b54d48599-x2zc5 1/1 Running 6 4d2h 10.244.1.32 slave1 <none> <none> kube-system coredns-6c76c8bb89-tfcrm 1/1 Running 10 31d 10.244.1.31 slave1 <none> <none>
总的来说,查询pods后,第三列READY 是1/1 ,第四列STATUS 是running 表示该pods是正常的,RESTARTS表示pod重启的次数,这个可以不用考虑,因为重启服务器它也会重启的哦。AGE这一列表示pods运行的时间,IP这一列表示pod运行时使用的IP地址,这个IP地址一般是集群内部使用,也可以不用考虑。