yaml--静态pod--pod生命周期--pod监控检查


//查看kubernetes-dashboard create -f xxx.yaml 拉取失败的原因:ErrImagePull、ImagePullBackOff

 kubectl describe pod kubernetes-dashboard-669f9bbd46-dph47


kubectl get nodes
kubectl get pods
kubectl get pods --all-namespaces
kubectl get pods --all-namespaces -o wide
kubectl get pods -n kube-system
kubectl get pods -n kube-system -o wide
kubectl get service -n kube-system
kubectl describe svc kubernetes-dashboard -n kube-system
kubectl describe service kubernetes-dashboard -n kube-system

kubectl get serviceaccount -n kube-system
kubectl describe serviceaccount admin -n kube-system

kubectl get secret -n kube-system
kubectl describe secret admin-token-bs9dd  -n kube-system

kubectl describe pod hrjr-web

kubectl get po # 查看目前所有的pod
kubectl get rs # 查看目前所有的replica set
kubectl get deployment # 查看目前所有的deployment
kubectl describe po my-nginx # 查看my-nginx pod的详细状态
kubectl describe rs my-nginx # 查看my-nginx replica set的详细状态
kubectl describe deployment my-nginx # 查看my-nginx deployment的详细状态

kuhbectl log init-pod




18 yaml 文件用法:
https://yaml.org

hrjr.yaml

apiVersion: v1
kind: Pod
metadata:
  name: hrjr-web
  labels:
    app: web
spec:
  containers:
    - name: front-end
      image: nginx
      ports:
        - containerPort: 80


kubectl describe pod hrjr-web


19 静态 Pod

1)修改配置文件生成静态pod
2)通过url地址来生成静态pod

systemctl status kubelet

静态pod的yaml文件存放路径:
/etc/kubernetes/manifests/

配置文件路径:
cd etc/systemd/system/kubelet.service.d
vi 10-kubeadm.conf

KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true

生成静态pod的yaml文件放置的位置: 文件在pod在,文件移走pod消失;
cat <<EOF >/etc/kubernetes/manifest/static-web.yaml
apiVersion: v1
kind: Pod
metadata:
  name: static-web
  labels:
    app: static
spec:
  containers:
    - name: web
      image: nginx
      ports:
        - name: web
          containerPort: 80
EOF


kubectl get pods
kubectl delete pod static-pod1-node01
kubectl delete pod haimaxy-pod
kubectl get pods


20 pod hook  pod的生命周期

hook  钩子函数
PostStart
PreStop

vi hook-demo1.yaml
---
apiVersion: v1
kind: Pod
metadata:
    name: hook-demo1
    labels:
        app: hook
spec:
    containers:
    - name: hook-demo1
      image: nginx
      ports:
      - name: webport
          containerPort: 80
      lifecycle:
          postStart:
              exec:
                  command: ["/bin/sh","-c","echo Hello from the postStart Handler > /usr/share/message"]



kubectl apply -f hook-demo1.yaml
kubectl describe pods hook-demo1

kubectl exec hook-demo1 -i -t /bin/bash
cd /usr/share
cat message


强制删除pod
kubectl delete pod hook-demo1 --grace-period=0 --force


preStop 优雅的退出pod

---
apiVersion: v1
kind: Pod
metadata:
  name: hook-demo2
  labels:
    app: hook
spec:
  containers:
  - name: hook-demo2
    image: nginx
    ports:
    - name: webport
      containerPort: 80
    volumeMounts:
    - name: message
      mountPath: /usr/share/
    lifecycle:
      preStop:
        exec:
          command: ['/bin/sh','-c','echo Hello from the preStop Handler > /usr/share/message']
  volumes:
  - name: message
    hostPath:
      path: /tmp


kubectl apply -f hook-demo2.yaml
kubectl get pods
kubectl describe pod hook-demo2
kubectl delete pod hook-demo2
kubectl delete -f hook-demo2.yaml

到node01 去/tmp下查看
message


21 Pod的健康检查

liveness probe(存活探针)
readiness probe(可读性探针)

配置方式:
exec         执行一段命令, 命令返回结果判定
http         检测某个 http 请求,请求结果返回判定
tcpSocket    kubectl 尝试在指定的端口上打开容器的套接字,即 端口存活判定

1) exec方式
liveness-exec.yaml

---
apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: busybox
    args:
    - /bin/sh
    - -c
    - touch /tmp/healthy;sleep 30; rm -rf /tmp/healthy; sleep 600
    livenessProbe:
      exec:
        command:
        - cat
        - /tmp/healthy
      initialDelaySeconds: 5
      periodSeconds: 5


kubectl apply -f liveness-exec.yaml
kubectl get pods
kubectl describe pod liveness-exec

强制删除pod
kubectl delete pod hook-demo1 --grace-period=0 --force

2)http 检测方式

---
apiVersion: v1
kind: Pod
metadata:
  name: liveness-http
  labels:
    app: liveness
spec:
  containers:
  - name: liveness
    image: cnych/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 3
      periodSeconds: 3


3) tcpSocket 检测方式

---
apiVersion: v1
kind: Pod
metadata:
  name: liveness-readiness
  labels:
    app: liveness-readiness
spec:
  containers:
  - name: liveness-readiness
    image: cnych/liveness
    args:
    - /server
    livenessProbe:
      tcpSocket:
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 5
    readinessProbe:
      tcpSocket:
        port:8080
      initialDelaySeconds: 3
      perioidSeconds: 3




---
apiVersion: v1
kind: Pod
metadata:
  name: liveness-readiness
  labels:
    app: liveness-readiness
spec:
  containers:
  - name: liveness-readiness
    image: cnych/liveness
    args:
    - /server
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 5
    readinessProbe:
      tcpSocket:
        port:8080
      initialDelaySeconds: 3
      perioidSeconds: 3

上一篇:SIP REG Digest认证算法分析


下一篇:linux时钟子系统-clk_register