三. k8s基本操作以及pod存活以及可用性驗證鈎子


kubectl常用命令

命令 作用
kubectl cluster-info 查看集群信息
kubectl describe pod -n kube-system kube-flannel-ds-amd64-trpqq 查看kube-system名稱空間里pod的描述信息
kubectl get pods -n NAME_SPACE #查看指定命名空間的pod
kubectl create deployment NAME --image=image [--dry-run] [options] 創建deployment, dry-run為true就是測試不執行
kubectl expose deployment nginx-deploy --name=nginx --port=80 --target-port=80 --protocol=TCP 為deployment創建service, --name為service的名字, --port為暴露端口, --target-port為目標pod端口
dig -t A nginx.default.svc.cluster.local @10.96.0.10 驗證是否能正確解析service, @后邊的ip為k8s的dns地址
kubectl describe svc nginx 查看service名字為nginx的描述信息
kubectl get pods --show-labels 查看pod的標簽
kubectl scale deployment nginx-deploy --replicas=3 擴容或縮容, --replicas為數量
kubectl run -it test --image=busybox -- sh; wget -O - -q nginx-deploy nginx-deploy是svc名字, 驗證svc是不是好用
kubectl rollout undo deployment myapp-deploy --to-revision=1 回滾到指定版本, 默認回滾到上一版本

資源清單配置

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespace: default
  labels:
    app: myapp
    tier: frontend
spec:
  containers:
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: busybox
    image: busybox
    command:
    - "/bin/sh"
    - "-c"
    - "sleep 5"
kubectl create -f test.yaml

kubectl delete -f test.yaml

kubectl describe pod pod-demo

kubectl describe pod pod-demo

kubectl exec -it pod-demo -c myapp -- /bin/sh

lable使用

過濾lable

kubectl get pods -l app --show-labels

給pod打標簽

kubectl label pods http-7f8cbdf584-dbmkn release=canary

根據label過濾pod

kubectl get pods -l release, app

kubectl get pods -l release=stable, app=myapp

kubectl get pods -l release!=canary

kubectl get pods -l "release in (canary, beta, alpha)"

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo   #name必須小寫
  namespace: default
  labels:
    app: myapp
    tier: frontend
  annotations:
    create-by: tianpei.wang
spec:
  containers:
  - name: myapp
    image: nginx
    ports:
    - name: http
      containerPort: 80
    - name: https
      containerPort: 443
  - name: busybox
    image: busybox
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh", "-c", "sleep 60"]
  nodeSelector:
    kubernetes.io/hostname: node01

Pod存活性和可用性驗證

liveness存活驗證鈎子

exec

apiVersion: v1
kind: Pod
metadata:
    name: liveness-exec-pod
    namespace: default
spec:
    containers:
    - name: liveness-exec-containers
      image: busybox:latest
      imagePullPolicy: IfNotPresent
      command: ["/bin/sh","-c","touch /tmp/healthy; sleep 30; rm -f /tmp/healthy; sleep 3600"]
      livenessProbe:
          exec:
              command: ["test", "-e", "/tmp/healthy"]
          initialDelaySeconds: 1   #延遲一秒探測
          periodSeconds: 3         #三秒為一個探測周期

httpGet

apiVersion: v1
kind: Pod
metadata:
    name: liveness-httpget-pod
    namespace: default
spec:
    containers:
    - name: liveness-httpget-containers
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80
      livenessProbe:
          httpGet:
              port: http
              path: /index.html
          initialDelaySeconds: 1
          periodSeconds: 3

liveness可用性驗證鈎子

httpGet

apiVersion: v1
kind: Pod
metadata:
    name: readiness-httpget-pod
    namespace: default
spec:
    containers:
    - name: readiness-httpget-containers
      image: ikubernetes/myapp:v1
      imagePullPolicy: IfNotPresent
      ports:
      - name: http
        containerPort: 80
      readinessProbe:
          httpGet:
              port: http
              path: /index.html
          initialDelaySeconds: 1
          periodSeconds: 3

lifecycle-poststart錯誤示例

apiVersion: v1
kind: Pod
metadata:
    name: poststart-pod
    namespace: default
spec:
    containers:
    - name: busybox-httpd
      image: busybox:latest
      imagePullPolicy: IfNotPresent
      lifecycle:
        postStart:
          exec:
            command: ["/bin/sh", "-c", "mkdir -p /data/web/html; echo 'Home Page' >>/data/web/html/index.html"]
      command: ["/bin/httpd"]
      args: ["-f", "-h /data/web/html"]

使用上述yaml創建pod會報錯

Warning  FailedPostStartHook  8s (x2 over 26s)  kubelet, node01    Exec lifecycle hook ([/bin/sh -c mkdir -p /data/web/html; echo 'Home Page' >>/data/web/html/index.html]) for Container "busybox-httpd" in Pod "poststart-pod_default(92846bc9-ca3f-11e9-9c47-0242ac110046)" failed - error: command '/bin/sh -c mkdir -p /data/web/html; echo 'Home Page' >>/data/web/html/index.html' exited with 126: , message: "cannot exec in a stopped state: unknown\r\n"

原因是由於,優先去執行containers下的command, 然后才會去執行lifecycle下的command, 所以導致/data/web/html目錄還未創建

service, deployment, replicaset和pod之間的關系

https://blog.csdn.net/ucsheep/article/details/81781509


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM