Kubernetes Pod的应用
基础的Pod
1 2 3 4 5 6 7 8 9 10 11
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent
|
具体镜像概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/containers/images/
多个容器组成Pod
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| apiVersion: v1 kind: Pod metadata: name: nginx-tomcat labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent - name: tomcat image: tomcat:latest imagePullPolicy: IfNotPresent
|
Pod内的容器共享存储
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| apiVersion: v1 kind: Pod metadata: name: nginx-tomcat labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent volumeMounts: - name: data mountPath: /code/ - name: tomcat image: tomcat imagePullPolicy: IfNotPresent volumeMounts: - name: data mountPath: /code/ volumes: - name: data emptyDir: {}
|
存储相关概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/storage/storage-classes/
Pod跟宿主机共享目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| apiVersion: v1 kind: Pod metadata: name: nginx-tomcat labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent volumeMounts: - name: data mountPath: /code/ - name: tomcat image: tomcat imagePullPolicy: IfNotPresent volumeMounts: - name: data mountPath: /code/
volumes: - name: data hostPath: path: /code/
|
Pod的初始化容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: initContainers: - name: busybox image: busybox imagePullPolicy: IfNotPresent args: [/bin/sh, -c, 'echo k8s >> /usr/share/nginx/html/index.html'] volumeMounts: - name: data mountPath: /usr/share/nginx/html/
containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent volumeMounts: - name: data mountPath: /usr/share/nginx/html/
volumes: - name: data emptyDir: {}
|
init容器具体概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/concepts/workloads/pods/init-containers/
配置 Pod 初始化 https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-pod-initialization/
Pod的资源限制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| apiVersion: v1 kind: Pod metadata: name: resource-demo spec: containers: - name: resource-demo images: nginx ports: - containerPort: 80 resources: requests: memory: 50Mi cpu: 1500m limits: memory: 100Mi cpu: 200m
|
1 2 3 4 5
| requests :节点所需的最⼩计算资源量,k8s调度的时候的依据值 limits :限制允许的最⼤计算资源量,真正的资源限制参数
1 CPU = 1000m,0.5 CPU = 500m,1 Mib = 1024 Kib,1 MB = 1000 KB
|
Pod配置存活、就绪和启动探针
具体概念请阅读官方文档:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
POD启动和停止钩子
PostStart(启动钩子):在容器启动创建后立刻执行,但是时间不能太长,否则容器将不会是running状态
PreStop(停止钩子):在容器停止被删除前执行,主要用于优雅的关闭应用程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: [/bin/sh, -c, 'echo k8s > /usr/share/nginx/html/index.html'] preStop: exec: command: [/bin/sh, -c, 'echo beybey > /code/stop.log'] volumeMounts: - name: data mountPath: /code/
volumes: - name: data hostPath: path: /code/
|
Pod的存活性探针
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html'] preStop: exec: command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log'] livenessProbe: exec: command: - /bin/bash - -c - cat /usr/share/nginx/html/index.html initialDelaySeconds: 3 periodSeconds: 3
volumeMounts: - name: data mountPath: /usr/share/nginx/html/
volumes: - name: data hostPath: path: /code/
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html'] preStop: exec: command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log'] livenessProbe: httpGet: host: www.opsape.com path: /index.html port: 80 initialDelaySeconds: 3 periodSeconds: 3
volumeMounts: - name: data mountPath: /usr/share/nginx/html/
volumes: - name: data hostPath: path: /code/
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html'] preStop: exec: command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log'] livenessProbe: tcpSocket: port: 80 initialDelaySeconds: 3 periodSeconds: 3
volumeMounts: - name: data mountPath: /usr/share/nginx/html/
volumes: - name: data hostPath: path: /code/
|
Pod的就绪性探针
就绪性探针和存活性探针一样有命令、http、端口三种方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| apiVersion: v1 kind: Pod metadata: name: nginx-pod labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.0 imagePullPolicy: IfNotPresent lifecycle: postStart: exec: command: [/bin/sh, -c, 'echo ok > /usr/share/nginx/html/index.html'] preStop: exec: command: [/bin/sh, -c, 'echo beybey > /usr/share/nginx/html/stop.log'] livenessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 3 periodSeconds: 3 readinessProbe: httpGet: path: /index.html port: 80 initialDelaySeconds: 30 periodSeconds: 3
volumeMounts: - name: data mountPath: /usr/share/nginx/html/
volumes: - name: data hostPath: path: /code/
|