Kubernetes --(k8s)yml 文件

认识yml文件

yaml文件语法

  • 大小写敏感
  • 使用缩进表示层级关系
  • 缩进时不允许使用Tab键,只允许使用空格。
  • 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可
  • # 表示注释,从这个字符一直到行尾,都会被解析器忽略。
apiVersion: extensions/v1beta1      # 当前配置格式的版本 
kind: Deployment      # 要创建的资源的类型
metadata:       # 资源的元素据
   name: nginx1
spec:     # 资源的说明书
   replicas: 2      # 副本数量
   template:       # 定义pod模板
      metadata:    # pod 元数据
         labels:    # 标签
           name: web
      spec:     #  pod 说明  
         containers:   #定义每个容器的属性 name和image 是必选项
         - name: nginx      # " - " 不要忘记因为containers节元素为列表格式
           image: nginx

查找资源时用到的命令

╭─root@node1 ~  
╰─➤  kubectl explain deploy                              
KIND:     Deployment
VERSION:  extensions/v1beta1
...
# kubectl api-resources
# kubectl explain deploy.metadata
# kubectl explain deploy.spec
# kubectl explain deploy.spec.template
# kubectl explain deploy.spec.template.spec 
# kubectl explain deploy.spec.template.metadata

使用yml文件部署

第一步:编写yml文件

╭─root@node1 ~  
╰─➤  vim nginx.yml
...
apiVersion: extensions/v1beta1  
kind: Deployment
metadata:
   name: nginx1
spec:
   replicas: 2
   template:
      metadata:
         labels:
           name: web
      spec:
         containers:
         - name: nginx
           image: nginx
...

第二步:依靠yml文件运行/删除

╭─root@node1 ~  
╰─➤  kubectl apply -f nginx.yml        
deployment.extensions/nginx1 created



#  kubectl delete -f nginx.yml    #  删除

第三步:查看pods

╭─root@node1 ~  
╰─➤  kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx1-99f7df68c-7v5pb   1/1     Running   0          77s
nginx1-99f7df68c-lcvrk   1/1     Running   0          77s


k8s command & args

命令和参数说明:

command、args两项实现覆盖Dockerfile中ENTRYPOINT的功能,具体的command命令代替ENTRYPOINT的命令行,args代表集体的参数。

  • 如果command和args均没有写,那么用Dockerfile的配置。
  • 如果command写了,但args没有写,那么Dockerfile默认的配置会被忽略,执行输入的command(不带任何参数,当然command中可自带参数)。
  • 如果command没写,但args写了,那么Dockerfile中配置的ENTRYPOINT的命令行会被执行,并且将args中填写的参数追加到ENTRYPOINT中。
  • 如果command和args都写了,那么Dockerfile的配置被忽略,执行command并追加上args参数。比如:
  • 另:多命令执行使用sh,-c,[command;command,...]的形式,单条命令的参数填写在具体的command里面

摘自:https://www.cnblogs.com/qiang-cnblog/p/7641423.html


k8s 查看pod的详细yaml文件

╭─root@node1 ~  
╰─➤  kubectl get po
NAME         READY   STATUS              RESTARTS   AGE
pod-secret   0/1     ContainerCreating   0          9m35s

╭─root@node1 ~  
╰─➤  kubectl get pod  pod-secret -o yaml   
apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"pod-secret","namespace":"default"},"spec":{"containers":[{"command":["/bin/sh","-c","touch test;sleep 60000"],"image":"busybox","imagePullPolicy":"IfNotPresent","name":"busybox","volumeMounts":[{"mountPath":"/tmp","name":"du"}]}],"volumes":[{"name":"du","secret":{"secretName":"mysecret"}}]}}
  creationTimestamp: "2019-08-31T01:20:35Z"
  name: pod-secret
  namespace: default
  resourceVersion: "267900"
  selfLink: /api/v1/namespaces/default/pods/pod-secret
  uid: a222ab7e-cafc-46b3-8a82-8ab1b4fc0599
spec:
  containers:
  - command:
    - /bin/sh
    - -c
    - touch test;sleep 60000
    image: busybox
    imagePullPolicy: IfNotPresent
    name: busybox
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /tmp
      name: du
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-ngn4n
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: node2
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: du
    secret:
      defaultMode: 420
      secretName: mysecret
  - name: default-token-ngn4n
    secret:
      defaultMode: 420
      secretName: default-token-ngn4n
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T01:26:25Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T01:26:25Z"
    message: 'containers with unready status: [busybox]'
    reason: ContainersNotReady
    status: "False"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T01:26:25Z"
    message: 'containers with unready status: [busybox]'
    reason: ContainersNotReady
    status: "False"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T01:26:24Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - image: busybox
    imageID: ""
    lastState: {}
    name: busybox
    ready: false
    restartCount: 0
    state:
      waiting:
        reason: ContainerCreating
  hostIP: 192.168.137.4
  phase: Pending
  qosClass: BestEffort
  startTime: "2019-08-31T01:26:25Z"

原文地址:https://www.cnblogs.com/du-z/p/11383982.html