K8s (2)-kubernetes resource list writing rules


Preface

What is a resource?
All content in k8s is abstracted as resources, and after the resources are instantiated, they are called objects.

What is a resource list?
In k8s, yaml format files are generally used to create pods that meet our expectations. Such yaml files are generally called resource lists

1. Writing the resource list

https://kubernetes.io/docs/setup/

View the API provided by k8s kubectl api-versions

Insert picture description here

Create xxx.yml manifest file

The main parameters:

Insert picture description here
Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here

2. Method One

Use the kubectl explaincommand to help find the necessary documents in the list and write step by step

Required parameters are required
Insert picture description here

Sample pod.yml file

[root@server2 ~]# cat pod.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      #nodeSelector:
      #  kubernetes.io/hostname: server4
      #nodeName: server3
      #hostNetwork: true
      containers:
      - name: nginx
        image: myapp:v1
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 0.5
            memory: 512Mi
      #- name: busyboxplus
      #  image: busyboxplus
      #  imagePullPolicy: IfNotPresent
      #  stdin: true
      #  tty: true
kubectl apply   -f  xxx.yml # 运行清单
kubectl delete  -f  xxx.yml # 删除清单(删除清单部署的内容)--很方便

3. Method two

Use of existing pod .yml output file is modified (more convenient, but chose to study a method)
Export command kubectl get pod nginx -o yaml
Insert picture description here
example export:

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2021-02-21T08:30:16Z"
  generateName: nginx-67f9d9c97f-
  labels:
    app: nginx
    pod-template-hash: 67f9d9c97f
  managedFields:
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:generateName: {
    
    }
        f:labels:
          .: {
    
    }
          f:app: {
    
    }
          f:pod-template-hash: {
    
    }
        f:ownerReferences:
          .: {
    
    }
          k:{
    
    "uid":"9a6ef1c2-dc68-4f79-8437-9a9b81cbd004"}:
            .: {
    
    }
            f:apiVersion: {
    
    }
            f:blockOwnerDeletion: {
    
    }
            f:controller: {
    
    }
            f:kind: {
    
    }
            f:name: {
    
    }
            f:uid: {
    
    }
      f:spec:
        f:containers:
          k:{
    
    "name":"myapp"}:
            .: {
    
    }
            f:image: {
    
    }
            f:imagePullPolicy: {
    
    }
            f:name: {
    
    }
            f:resources: {
    
    }
            f:terminationMessagePath: {
    
    }
            f:terminationMessagePolicy: {
    
    }
        f:dnsPolicy: {
    
    }
        f:enableServiceLinks: {
    
    }
        f:restartPolicy: {
    
    }
        f:schedulerName: {
    
    }
        f:securityContext: {
    
    }
        f:terminationGracePeriodSeconds: {
    
    }
    manager: kube-controller-manager
    operation: Update
    time: "2021-02-21T08:30:16Z"
  - apiVersion: v1
    fieldsType: FieldsV1
    fieldsV1:
      f:status:
        f:conditions:
          k:{
    
    "type":"ContainersReady"}:
            .: {
    
    }
            f:lastProbeTime: {
    
    }
            f:lastTransitionTime: {
    
    }
            f:message: {
    
    }
            f:reason: {
    
    }
            f:status: {
    
    }
            f:type: {
    
    }
          k:{
    
    "type":"Initialized"}:
            .: {
    
    }
            f:lastProbeTime: {
    
    }
            f:lastTransitionTime: {
    
    }
            f:status: {
    
    }
            f:type: {
    
    }
          k:{
    
    "type":"Ready"}:
            .: {
    
    }
            f:lastProbeTime: {
    
    }
            f:lastTransitionTime: {
    
    }
            f:message: {
    
    }
            f:reason: {
    
    }
            f:status: {
    
    }
            f:type: {
    
    }
        f:containerStatuses: {
    
    }
        f:hostIP: {
    
    }
        f:podIP: {
    
    }
        f:podIPs:
          .: {
    
    }
          k:{
    
    "ip":"10.244.2.6"}:
            .: {
    
    }
            f:ip: {
    
    }
        f:startTime: {
    
    }
    manager: kubelet
    operation: Update
    time: "2021-02-21T08:30:22Z"
  name: nginx-67f9d9c97f-mz5v4
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-67f9d9c97f
    uid: 9a6ef1c2-dc68-4f79-8437-9a9b81cbd004
  resourceVersion: "115314"
  uid: aa9d8806-0ca1-4296-be67-57f0ecaabded
spec:
  containers:
  - image: myapp:v1
    imagePullPolicy: IfNotPresent
    name: myapp
    resources: {
    
    }
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-96sws
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: server4
  preemptionPolicy: PreemptLowerPriority
  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: default-token-96sws
    secret:
      defaultMode: 420
      secretName: default-token-96sws
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2021-02-21T08:30:16Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2021-02-21T08:30:16Z"
    message: 'containers with unready status: [myapp]'
    reason: ContainersNotReady
    status: "False"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2021-02-21T08:30:16Z"
    message: 'containers with unready status: [myapp]'
    reason: ContainersNotReady
    status: "False"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2021-02-21T08:30:16Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - image: myapp:v1
    imageID: ""
    lastState: {
    
    }
    name: myapp
    ready: false
    restartCount: 0
    started: false
    state:
      waiting:
        message: 'rpc error: code = Unknown desc = Error response from daemon: pull
          access denied for myapp, repository does not exist or may require ''docker
          login'': denied: requested access to the resource is denied'
        reason: ErrImagePull
  hostIP: 172.25.200.4
  phase: Pending
  podIP: 10.244.2.6
  podIPs:
  - ip: 10.244.2.6
  qosClass: BestEffort
  startTime: "2021-02-21T08:30:16Z"

Guess you like

Origin blog.csdn.net/weixin_41191813/article/details/113921750