2-Kubernetes 资源清单定义入门

Kubernetes 资源清单定义入门


创建资源的方法

  apiserver仅接收JSON格式的资源定义,yaml格式提供配置清单,apiserver可自动将其转为json格式,而后提交,每个资源引用PATH如下:

/api/GROUP/VERSION/namespces/NAMESPACE/TYPE/NAME

#例如
/api/v1/namespaces/default/pods/myapp-1d6a5d1-jgh1

查看帮助字段帮助

kubectl explain pods.spec.containers
kubectl explain pods.metadata

大部分资源配置清单格式yaml

apiVersion:group/version    #api版本
kind:                        #资源类别
metadata:                    #元数据
  nama:                        #名称
  namespce:                    #命名空间
  labels:                    #标签
   app:myapp                #标签
  annotations:
    leiyan.com/created-by:"qinzc    admin"        #资源注解  
spec:                        #期望状态
  containers:
  - name:                    #名称
    image:                    #镜像来源
    imagePullPolicy:        #pull策略,Always(总是去仓库拉最新),Never(使用本地,本地没有不去拉取),IfNotPresent(使用本地,本地没有去拉取)
    ports:                    #暴露端口
    - name:http                #暴露端口命名
      containerPort:80        #暴露80端口
    - name:https            #暴露端口命名
      containerPort:443        #暴露443端口
    command:                #启动命令如果没有指定,那么默认将使用容器里的CMD或ENTRYPOINT作为启动参数,如果容器中指定了CMD或ENTRYPOINT启动,此处指定args,将作为参数传递进容器
  nodeSelector:                #指定节点标签选择器
    disktype:ssh            #指定包含此标签的节点运行Pod
  nodeName:                    #指定节点运行Pod
  selector:                    #使用什么标签选择器选择
    matchLabels:            #直接给定键值
    matchExpressions:        #基于给定的表达式来定义使用标签选择器{key:"KEY",operator:"OPERATOR",values:[VAL1,VAL2,..]},In,NotIN:values 字段的值必须为空列表,Exists,NotExists:values字段的值必须为空列表,

例:创建一个Pod,运行两个容器(myapp、busybox)

vim pod-demo.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-demo
  namespce: default
  labels:
    app: myapp
    tier: frontend
spec: 
  contaiters: 
  - name: myapp
    image: ikubernetes/myapp:v1
  - name: busybox
    image: busybox:latest
    command: 
    - "/bin/sh"
    - "-c"
    - "sleep 3600"

创建Pod

kubectl create -f pod-demo.yaml

查看的Pod日志信息

kubectl describe pods pod-demo
kubectl logs pod-demo busybox

删除Pod

方法1:kubectl delete -f pod-demo.yaml
方法2:kubectl delete pod pod-demo

Pod控制器进阶

Pod的生命周期:

状态:Pending,Running,Failed,Succeeded,Unknown

Pod生命周期中的重要行为:

1.初始化容器:

2.启动后钩子:kubectl explain pods.spec.containers.lifecycle
    postStart:启动前
        探针类型有3种:exec、tcpSocket、httpGet
    preStop:终止前
        探针类型有3种:exec、tcpSocket、httpGet

3.容器探测:liveness(主程序,或容器存活状态),readiness(服务存活状态)

4.容器重启策略:restartPolicy:{Always(容器挂了总是重启),OnFailure(错误了重启),Never(容器挂了不重启),Default to Always}

5.结束后钩子:

存活探针类型有3种:

ExecAction、TCPSocketAction、HTTPGetAction

livenessProbe :存活状态   
readinessProbe:就绪状态

kubectl explain pods.spec.containers.livenessProbe
exec                    #探测程序
failureThreshold        #探测几次算失败,默认连续3次才是真的失败
HTTPGet                    #探测HTTP
initialDelaySeconds        #初始化延迟多久开始启动探测
periodSeconds            #探测时长周期,默认10s
tcpSocket                #探测TCP
timeoutSeconds            #探测无响应超时等多久,默认1s


kubectl explain pods.spec.containers.livenessProbe.exec
command            #如果次平明返回成功,表示存活,返回状态码,表示失败。

例1:

vim liveness-exec.yaml

apiVersion: v1
kind: Pod
metadata:
  name: liveness-exec-pod
  namespace: default
spec:
  containers:
  - name: liveness-exec-container
    image: busybox:latest
    imagePullPolicy: IfNotPresent
    command: ["/bin/sh","-c","touch /tmp/healthy; sleep 10; rm -rf /tmp/healthy; sleep 3600"]                                        
    livenessProbe:
      exec:
        command: ["test","-e","/tmp/healthy"]
      initialDelaySeconds: 1
      periodSeconds: 3

例2:

vim liveness-httpget.yaml

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

就绪状态探针

**例1:**

vim readines-httpget.yaml

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

启动后钩子

例1:

vim postStart.yaml

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","echo Home_Page >> /tmp/index.html"]
    command: ["/bin/httpd"]
    args: ["-f","-h","/tmp"]
  • blogger
版权声明:若无特殊注明,本文为《橙子柠檬》原创,转载请保留文章出处。
本文链接:https://qinzc.me/post-222.html
正文到此结束

热门推荐

发表吐槽

你肿么看?

你还可以输入 250 / 250 个字

嘻嘻 大笑 可怜 吃惊 害羞 调皮 鄙视 示爱 大哭 开心 偷笑 嘘 奸笑 委屈 抱抱 愤怒 思考 日了狗 胜利 不高兴 阴险 乖 酷 滑稽

评论信息框
可使用QQ号实时获取昵称+头像

私密评论

吃奶的力气提交吐槽中...


既然没有吐槽,那就赶紧抢沙发吧!