三. 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

posted @ 2019-08-26 18:00  培天王  阅读(1236)  评论(0编辑  收藏  举报