• kubernetes集群系列资料06--pod介绍


    一、pod介绍

    二、pod全生命周期

    三、pod案例

    ##########K8S案例1---pod#############
    #####init容器使用案例---视频19有讲解。
    vim myapp.yaml  #包含init C的POD模板,该initC会解析2个域名。
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1          #内容格式为group/apiversion,如果没有给定group名称则默认为core;
    kind: Pod               #资源类别;
    metadata:               #资源元数据;
        name: myapp-pod
        labels:
            app: myapp
        # annotations:      #主要目的是方便用户阅读查找。
    spec:                   #期望的状态;
        containers:
        - name: myapp-container     #mainC容器,主要目的时输出“The app is running”,然后等待3600s;
          image: busybox            #mainC容器的使用镜像busybox:是一个封装很多小工具的镜像。
          command: ['sh','-c','echo The app is running! && sleep 3600'] #mainC容器中的执行命令。
        initContainers:             #为myapp-container创建初始化容器。
        - name: init-myservice
          image: busybox
          command: ['sh','-c','until nslookup myservice;do echo waiting for myservice;sleep 2;done;']  #运行命令:解析myservice直至其成功为止。
        - name: init-mydb
          image: busybox
          command: ['sh','-c','until nslookup mydb;do echo waiting for mydb;sleep 2;done;']
    # status:             #当前状态,本字段有k8s自身维护,用户不能去定义;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f myapp.yaml
    kubectl get pod                 #myapp-pod状态为Init:0/2;其故障原因:解析myservice、mydb不成功,导致执行initC一直在重启而不能进入下一步;
    kubectl logs myapp-pod          #查看myapp-pod的日志;
    kubectl describe pod myapp-pod  #查看myapp-pod的详细信息,定位错误点;
    kubectl edit pod myapp-pod      #修改pod的内容;该yaml文件有些内容时不可更改,有些内容更改了也不能触发initC的重新执行,但可通过修改image字段会触发initC的重新执行。
    kubectl logs myapp-pod -c init-myservice  #查看pod下定义的init-myservice服务的日志。
    
    
    # ##在node节点上传busybox至harbor
    # docker tag busybox:latest hub.atguigu.com/library/busybox:latest
    # docker push hub.atguigu.com/library/busybox
    
    vim myservice.yaml  #将myservice域名写入coreDNS服务器的POD;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Service
    metadata:
        name: myservice
    spec:
        ports:
        - protocol: TCP
          port: 80
          targetPort: 9376
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f myservice.yaml
    ubectl get svc          #查看创建的服务;
    kubectl get pod -w      #一直监测pod的执行过程;
    
    
    vim mydb.yaml  #将mydb域名写入coreDNS服务器的POD;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Service
    metadata:
        name: mydb
    spec:
        ports:
        - protocol: TCP
          port: 80
          targetPort: 9377
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f mydb.yaml
    
    
    
    ####检测探针---就绪检测
    docker pull mylandmarktech/myapp
    docker tag mylandmarktech/myapp hub.atguigu.com/library/mylandmarktech/myapp:v1
    
    
    vim readinessProbe-httpget.yaml  #通过确认容器是否能访问index1.html来判断容器nginx是否就绪。
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Pod
    metadata:
        name: readiness-httpget-pod
        namespace: default
    spec:
        containers:
        - name: readiness-httpget-container
          image: hub.atguigu.com/library/nginx
          imagePullPolicy: IfNotPresent   #若本地存在image则不需下载;
          readinessProbe:
            httpGet:
                port: 80
                path: /index1.html
            initialDelaySeconds: 1
            periodSeconds: 3
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f readinessProbe-httpget.yaml
    kubectl exec -it readiness-httpget-pod -- /bin/sh   #进入pod的单一容器内部,进入交互式操作。
    kubectl exec -it readiness-httpget-pod  -c k8s_readiness-httpget-container_readiness-httpget-pod_default_0c0c5c17-895c-4353-8930-fe4730aecaa7_0 -- /bin/sh  #若pod中只有一个容器则可不需使用“-c ”参数指定容器。
    kubectl exec -it readiness-httpget-pod ls /usr/share/nginx/html  #无参数可省略--。
    kubectl exec -it readiness-httpget-pod -- ls -l /usr/share/nginx/html  #要加-- 号,不然shell命令中的参数,不能识别;
    kubectl exec -it readiness-httpget-pod -- echo "123" >/usr/share/nginx/html/index1.html  #无法实现直接创建文件???
    
    
    
    ####检测探针---存活检测
    vim livenessProbe-exec.yaml  #容器不断创建文件后删除,通过检测该文件是否存在来确认容器为正常存活状态,若不存在则重新创建pod;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Pod
    metadata:
        name: liveness-exec-pod
        namespace: default
    spec:
        containers:
        - name: liveness-exec-container
          image: hub.atguigu.com/library/busybox
          imagePullPolicy: IfNotPresent
          command: ["/bin/sh","-c","touch /tmp/live;sleep 60;rm -rf /tmp/live;sleep 3600"]
          livenessProbe:
            exec:
                command: ["test","-e","/tmp/live"]
            initialDelaySeconds: 1
            periodSeconds: 3
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f livenessProbe-exec.yaml
    kubectl exec -it liveness-exec-pod -- ls -l /tmp/live  #查看文件是否存活来判断pod是否正常运行;
    kubectl get pod -w #监控pod是否被反复重启,若pod退出会自动重启。
    
    vim livenessProbe-httpget.yaml  #
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Pod
    metadata:
        name: liveness-httpget-pod
        namespace: default
    spec:
        containers:
        - name: liveness-httpget-container
          image: hub.atguigu.com/library/nginx
          imagePullPolicy: IfNotPresent
          ports:
          - name: http
            containerPort: 80
          livenessProbe:
            httpGet:
                port: http
                path: /index.html
            initialDelaySeconds: 1
            periodSeconds: 3
            timeoutSeconds: 10
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f livenessProbe-httpget.yaml
    kubectl exec -it liveness-httpget-pod -- rm -f /usr/share/nginx/html/index.html  #查看文件是否存活来判断pod是否正常运行;
    kubectl get pod -w #监控pod是否被重启1次,若pod退出会自动重启。
    
    
    
    
    
    vim livenessProbe-tcp.yaml
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Pod
    metadata:
        name: liveness-tcp-pod
        namespace: default
    spec:
        containers:
        - name: liveness-tcp-container
          image: hub.atguigu.com/library/nginx
          imagePullPolicy: IfNotPresent
          livenessProbe:
            tcpSocket:
                port: 80
            initialDelaySeconds: 5  #等5秒后开始存活检测;
            periodSeconds: 3        #未成功检测再过3秒后检测;
            timeoutSeconds: 1       #存活检测超时时间为1s,若仍不能检测到端口,则重启POD;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f livenessProbe-tcp.yaml
    docker stop k8s_liveness-tcp-container_liveness-tcp-pod_default_4794b8bf-4ddb-4e0b-b07b-cec04dfa99f0_0;docker ps -a #在工作节点停止容器后进行查看容器是否被重启;
    kubectl get pod -w #监控pod是否被重启1次。
    
    ####启动、退出动作
    
    vim start-stop.yaml
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    apiVersion: v1
    kind: Pod
    metadata:
        name: lifecycle-demo
        namespace: default
    spec:
        containers:
        - name: lifecycle-demo-container
          image: hub.atguigu.com/library/nginx:latest
          lifecycle:
            postStart:
                exec:
                    command: ["/bin/sh","-c","echo Hello from the postStart handler > /usr/share/message"]
            preStop:
                exec:
                    command: ["/bin/sh","-c","echo Hello from the poststop handler > /usr/share/message"]
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    kubectl apply -f start-stop.yaml
    kubectl exec -it lifecycle-demo -- exit && cat /usr/share/message #查看容器启动成功后输出的日志信息。
    
    ##########K8S案例1---pod#############
    

      

  • 相关阅读:
    MySql中启用InnoDB数据引擎的方法
    云说的到底对不对,京东到底行不行?
    hibernate HQL查询的参数绑定
    MySQL到底能支持多大的数据量?
    C# RSA和Java RSA互通
    Log4j 2使用教程
    Log4j.properties配置详解
    JMX 基础Demo
    iBatis缓存实现源码分析-FIFO,LUR实现方法
    SqlMapClient 创建过程之SqlMapConfigParser源码走读
  • 原文地址:https://www.cnblogs.com/chalon/p/14897600.html
Copyright © 2020-2023  润新知