小言_互联网的博客

Kubernetes基础:CLI与Restful API:pod管理

240人阅读  评论(0)


使用CLI方式可以通过kubectl对Kubernetes进行操作,同时也使用Restful API直接进行操作。这篇文章介绍一下两种方式下进行pod的管理的方法。

环境准备

快速环境搭建建议使用单机版Kubernetes的安装脚本,一键安装,详情可参看:

事前准备

使用docker pull命令下载nginx:latest镜像用于试验。

[root@host132 ~]# docker pull nginx:latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete 
a17e64cfe253: Pull complete 
e1288088c7a8: Pull complete 
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
[root@host132 ~]# 

kubectl 方式

创建新的pod

  • 创建pod所用的yaml文件
[root@host132 yaml]# cat pod-demo1.yaml 
---
apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - name: nginx-container
    image: nginx:latest
    ports:
    - containerPort: 80
...
[root@host132 yaml]#

使用kubectl可以直接使用如下命令创建新的pod(名为nginx的pod)

kubectl命令:kubectl create -f pod-demo1.yaml

执行示例信息

[root@host132 yaml]# kubectl get pods
No resources found.
[root@host132 yaml]# kubectl create -f pod-demo1.yaml 
pod/nginx created
[root@host132 yaml]# kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          4s
[root@host132 yaml]# kubectl get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          10s   10.254.224.4   192.168.163.132   <none>           <none>
[root@host132 yaml]# 

获取所有pod列表信息

使用kubectl可以直接使用如下命令获取pod列表信息

kubectl命令:kubectl get pods -o wide

执行示例信息

[root@host132 ~]# kubectl get pods -o wide
No resources found.
[root@host132 ~]#

获取指定pod信息

使用kubectl可以直接使用如下命令获取指定pod信息(名为nginx的pod)

kubectl命令:kubectl get pods nginx -o wide

执行示例信息

[root@host132 yaml]# kubectl get pods nginx -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          3m26s   10.254.224.4   192.168.163.132   <none>           <none>
[root@host132 yaml]#

删除指定的pod

使用kubectl可以直接使用如下命令删除指定的pod(名为nginx的pod)

kubectl命令:kubectl delete pods nginx

执行示例信息

[root@host132 yaml]# kubectl get pods 
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          7m36s
[root@host132 yaml]# kubectl delete pods nginx
pod "nginx" deleted
[root@host132 yaml]# 
[root@host132 yaml]# kubectl get pods 
No resources found.
[root@host132 yaml]# 

从运行中的pod中获得yaml或者json的配置信息

使用kubectl可以直接使用如下命令获得运行中的pod的yaml或者json的配置详细信息>kubectl命令(获取yaml信息):kubectl get pods nginx -o yaml
kubectl命令(获取json信息):kubectl get pods nginx -o json

[root@host132 yaml]# kubectl get pods nginx -o yaml
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2019-08-31T03:38:02Z"
  name: nginx
  namespace: default
  resourceVersion: "37952"
  selfLink: /api/v1/namespaces/default/pods/nginx
  uid: 00e4d249-bf75-4506-a2dc-a3f51d0bd795
spec:
  containers:
  - image: nginx:latest
    imagePullPolicy: Always
    name: nginx-container
    ports:
    - containerPort: 80
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-sr2zb
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: 192.168.163.132
  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-sr2zb
    secret:
      defaultMode: 420
      secretName: default-token-sr2zb
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T03:38:02Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T03:38:06Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T03:38:06Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-08-31T03:38:02Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://7f9635fbe242e255f52078132ad172364b266a9c52f837f2ca0347a2583e6abd
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
    lastState: {}
    name: nginx-container
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-08-31T03:38:06Z"
  hostIP: 192.168.163.132
  phase: Running
  podIP: 10.254.224.4
  qosClass: BestEffort
  startTime: "2019-08-31T03:38:02Z"
[root@host132 yaml]# 
[root@host132 yaml]# kubectl get pods nginx -o json
{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "creationTimestamp": "2019-08-31T03:38:02Z",
        "name": "nginx",
        "namespace": "default",
        "resourceVersion": "37952",
        "selfLink": "/api/v1/namespaces/default/pods/nginx",
        "uid": "00e4d249-bf75-4506-a2dc-a3f51d0bd795"
    },
    "spec": {
        "containers": [
            {
                "image": "nginx:latest",
                "imagePullPolicy": "Always",
                "name": "nginx-container",
                "ports": [
                    {
                        "containerPort": 80,
                        "protocol": "TCP"
                    }
                ],
                "resources": {},
                "terminationMessagePath": "/dev/termination-log",
                "terminationMessagePolicy": "File",
                "volumeMounts": [
                    {
                        "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount",
                        "name": "default-token-sr2zb",
                        "readOnly": true
                    }
                ]
            }
        ],
        "dnsPolicy": "ClusterFirst",
        "enableServiceLinks": true,
        "nodeName": "192.168.163.132",
        "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-sr2zb",
                "secret": {
                    "defaultMode": 420,
                    "secretName": "default-token-sr2zb"
                }
            }
        ]
    },
    "status": {
        "conditions": [
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-08-31T03:38:02Z",
                "status": "True",
                "type": "Initialized"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-08-31T03:38:06Z",
                "status": "True",
                "type": "Ready"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-08-31T03:38:06Z",
                "status": "True",
                "type": "ContainersReady"
            },
            {
                "lastProbeTime": null,
                "lastTransitionTime": "2019-08-31T03:38:02Z",
                "status": "True",
                "type": "PodScheduled"
            }
        ],
        "containerStatuses": [
            {
                "containerID": "docker://7f9635fbe242e255f52078132ad172364b266a9c52f837f2ca0347a2583e6abd",
                "image": "nginx:latest",
                "imageID": "docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9",
                "lastState": {},
                "name": "nginx-container",
                "ready": true,
                "restartCount": 0,
                "state": {
                    "running": {
                        "startedAt": "2019-08-31T03:38:06Z"
                    }
                }
            }
        ],
        "hostIP": "192.168.163.132",
        "phase": "Running",
        "podIP": "10.254.224.4",
        "qosClass": "BestEffort",
        "startTime": "2019-08-31T03:38:02Z"
    }
}
[root@host132 yaml]# 

curl方式

获取所有pods信息

使用curl可以直接使用如下命令获取pod信息

curl命令:curl --header “Authorization: bearer token信息” -k https://192.168.163.132:6443/api/v1/pods

执行示例信息

[root@host132 ~]# bearer_token=`cat /etc/k8s/token.csv |awk -F, '{print $1}'`
[root@host132 ~]# header_option="Authorization: bearer $bearer_token"
[root@host132 ~]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/pods
{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/pods",
    "resourceVersion": "39030"
  },
  "items": [
    {
      "metadata": {
        "name": "coredns-b7d8c5745-tws2p",
        "generateName": "coredns-b7d8c5745-",
        "namespace": "kube-system",
        "selfLink": "/api/v1/namespaces/kube-system/pods/coredns-b7d8c5745-tws2p",
        "uid": "3fb8843a-5656-4dbf-b85d-ab347b8fb647",
        "resourceVersion": "441",
        "creationTimestamp": "2019-08-30T19:03:41Z",
        "labels": {
          "k8s-app": "kube-dns",
          "pod-template-hash": "b7d8c5745"
        },
        "annotations": {
          "seccomp.security.alpha.kubernetes.io/pod": "docker/default"
        },
        "ownerReferences": [
          {
            "apiVersion": "apps/v1",
            "kind": "ReplicaSet",
            "name": "coredns-b7d8c5745",
            "uid": "cbf4908b-7013-4e66-ab09-28e8b3e7ea5d",
            "controller": true,
            "blockOwnerDeletion": true
          }
        ]
      },
      "spec": {
        "volumes": [
          {
            "name": "config-volume",
            "configMap": {
              "name": "coredns",
              "items": [
                {
                  "key": "Corefile",
                  "path": "Corefile"
                }
              ],
              "defaultMode": 420
            }
          },
          {
            "name": "coredns-token-czz8p",
            "secret": {
              "secretName": "coredns-token-czz8p",
              "defaultMode": 420
            }
          }
        ],
        "containers": [
          {
            "name": "coredns",
            "image": "k8s.gcr.io/coredns:1.2.6",
            "args": [
              "-conf",
              "/etc/coredns/Corefile"
            ],
            "ports": [
              {
                "name": "dns",
                "containerPort": 53,
                "protocol": "UDP"
              },
              {
                "name": "dns-tcp",
                "containerPort": 53,
                "protocol": "TCP"
              },
              {
                "name": "metrics",
                "containerPort": 9153,
                "protocol": "TCP"
              }
            ],
            "resources": {
              "limits": {
                "memory": "170Mi"
              },
              "requests": {
                "cpu": "100m",
                "memory": "70Mi"
              }
            },
            "volumeMounts": [
              {
                "name": "config-volume",
                "readOnly": true,
                "mountPath": "/etc/coredns"
              },
              {
                "name": "coredns-token-czz8p",
                "readOnly": true,
                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
              }
            ],
            "livenessProbe": {
              "httpGet": {
                "path": "/health",
                "port": 8080,
                "scheme": "HTTP"
              },
              "initialDelaySeconds": 60,
              "timeoutSeconds": 5,
              "periodSeconds": 10,
              "successThreshold": 1,
              "failureThreshold": 5
            },
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "IfNotPresent",
            "securityContext": {
              "capabilities": {
                "add": [
                  "NET_BIND_SERVICE"
                ],
                "drop": [
                  "all"
                ]
              },
              "readOnlyRootFilesystem": true,
              "allowPrivilegeEscalation": false
            }
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "Default",
        "serviceAccountName": "coredns",
        "serviceAccount": "coredns",
        "nodeName": "192.168.163.132",
        "securityContext": {
          
        },
        "schedulerName": "default-scheduler",
        "tolerations": [
          {
            "key": "CriticalAddonsOnly",
            "operator": "Exists"
          },
          {
            "key": "node.kubernetes.io/not-ready",
            "operator": "Exists",
            "effect": "NoExecute",
            "tolerationSeconds": 300
          },
          {
            "key": "node.kubernetes.io/unreachable",
            "operator": "Exists",
            "effect": "NoExecute",
            "tolerationSeconds": 300
          }
        ],
        "priority": 0,
        "enableServiceLinks": true
      },
      "status": {
        "phase": "Running",
        "conditions": [
          {
            "type": "Initialized",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:07Z"
          },
          {
            "type": "Ready",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:12Z"
          },
          {
            "type": "ContainersReady",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:12Z"
          },
          {
            "type": "PodScheduled",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:07Z"
          }
        ],
        "hostIP": "192.168.163.132",
        "podIP": "10.254.224.2",
        "startTime": "2019-08-30T19:04:07Z",
        "containerStatuses": [
          {
            "name": "coredns",
            "state": {
              "running": {
                "startedAt": "2019-08-30T19:04:10Z"
              }
            },
            "lastState": {
              
            },
            "ready": true,
            "restartCount": 0,
            "image": "k8s.gcr.io/coredns:1.2.6",
            "imageID": "docker://sha256:f59dcacceff45b5474d1385cd5f500d0c019ed9ca50ed5b814ac0c5fcec8699e",
            "containerID": "docker://cc7a9e9b357b1a04ad3d8820439703f79304002285d3adbfb05e3ac929ae6b05"
          }
        ],
        "qosClass": "Burstable"
      }
    },
    {
      "metadata": {
        "name": "kubernetes-dashboard-7d75c474bb-6dmt7",
        "generateName": "kubernetes-dashboard-7d75c474bb-",
        "namespace": "kube-system",
        "selfLink": "/api/v1/namespaces/kube-system/pods/kubernetes-dashboard-7d75c474bb-6dmt7",
        "uid": "e3266bce-deec-4531-b10e-33ca2e352f6d",
        "resourceVersion": "450",
        "creationTimestamp": "2019-08-30T19:04:09Z",
        "labels": {
          "k8s-app": "kubernetes-dashboard",
          "pod-template-hash": "7d75c474bb"
        },
        "ownerReferences": [
          {
            "apiVersion": "apps/v1",
            "kind": "ReplicaSet",
            "name": "kubernetes-dashboard-7d75c474bb",
            "uid": "f98118ce-7ce5-4540-a864-ab0fd22f1361",
            "controller": true,
            "blockOwnerDeletion": true
          }
        ]
      },
      "spec": {
        "volumes": [
          {
            "name": "kubernetes-dashboard-certs",
            "secret": {
              "secretName": "kubernetes-dashboard-certs",
              "defaultMode": 420
            }
          },
          {
            "name": "tmp-volume",
            "emptyDir": {
              
            }
          },
          {
            "name": "kubernetes-dashboard-token-5snr9",
            "secret": {
              "secretName": "kubernetes-dashboard-token-5snr9",
              "defaultMode": 420
            }
          }
        ],
        "containers": [
          {
            "name": "kubernetes-dashboard",
            "image": "k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1",
            "args": [
              "--auto-generate-certificates"
            ],
            "ports": [
              {
                "containerPort": 8443,
                "protocol": "TCP"
              }
            ],
            "resources": {
              
            },
            "volumeMounts": [
              {
                "name": "kubernetes-dashboard-certs",
                "mountPath": "/certs"
              },
              {
                "name": "tmp-volume",
                "mountPath": "/tmp"
              },
              {
                "name": "kubernetes-dashboard-token-5snr9",
                "readOnly": true,
                "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
              }
            ],
            "livenessProbe": {
              "httpGet": {
                "path": "/",
                "port": 8443,
                "scheme": "HTTPS"
              },
              "initialDelaySeconds": 30,
              "timeoutSeconds": 30,
              "periodSeconds": 10,
              "successThreshold": 1,
              "failureThreshold": 3
            },
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "imagePullPolicy": "IfNotPresent"
          }
        ],
        "restartPolicy": "Always",
        "terminationGracePeriodSeconds": 30,
        "dnsPolicy": "ClusterFirst",
        "serviceAccountName": "kubernetes-dashboard",
        "serviceAccount": "kubernetes-dashboard",
        "nodeName": "192.168.163.132",
        "securityContext": {
          
        },
        "schedulerName": "default-scheduler",
        "tolerations": [
          {
            "key": "node-role.kubernetes.io/master",
            "effect": "NoSchedule"
          },
          {
            "key": "node.kubernetes.io/not-ready",
            "operator": "Exists",
            "effect": "NoExecute",
            "tolerationSeconds": 300
          },
          {
            "key": "node.kubernetes.io/unreachable",
            "operator": "Exists",
            "effect": "NoExecute",
            "tolerationSeconds": 300
          }
        ],
        "priority": 0,
        "enableServiceLinks": true
      },
      "status": {
        "phase": "Running",
        "conditions": [
          {
            "type": "Initialized",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:09Z"
          },
          {
            "type": "Ready",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:13Z"
          },
          {
            "type": "ContainersReady",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:13Z"
          },
          {
            "type": "PodScheduled",
            "status": "True",
            "lastProbeTime": null,
            "lastTransitionTime": "2019-08-30T19:04:09Z"
          }
        ],
        "hostIP": "192.168.163.132",
        "podIP": "10.254.224.3",
        "startTime": "2019-08-30T19:04:09Z",
        "containerStatuses": [
          {
            "name": "kubernetes-dashboard",
            "state": {
              "running": {
                "startedAt": "2019-08-30T19:04:12Z"
              }
            },
            "lastState": {
              
            },
            "ready": true,
            "restartCount": 0,
            "image": "k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1",
            "imageID": "docker://sha256:f9aed6605b814b69e92dece6a50ed1e4e730144eb1cc971389dde9cb3820d124",
            "containerID": "docker://87593e06072b3e2db1b6e33ac00e10b5d328b72c4b6bb9e35e9ea856c4536f75"
          }
        ],
        "qosClass": "BestEffort"
      }
    }
  ]
}[root@host132 ~]# 

注意此处列出了kube-system的namespace下的pod

[root@host132 ~]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/pods 2>/dev/null |grep -w name
        "name": "coredns-b7d8c5745-tws2p",
            "name": "coredns-b7d8c5745",
            "name": "config-volume",
              "name": "coredns",
            "name": "coredns-token-czz8p",
            "name": "coredns",
                "name": "dns",
                "name": "dns-tcp",
                "name": "metrics",
                "name": "config-volume",
                "name": "coredns-token-czz8p",
            "name": "coredns",
        "name": "kubernetes-dashboard-7d75c474bb-6dmt7",
            "name": "kubernetes-dashboard-7d75c474bb",
            "name": "kubernetes-dashboard-certs",
            "name": "tmp-volume",
            "name": "kubernetes-dashboard-token-5snr9",
            "name": "kubernetes-dashboard",
                "name": "kubernetes-dashboard-certs",
                "name": "tmp-volume",
                "name": "kubernetes-dashboard-token-5snr9",
            "name": "kubernetes-dashboard",
[root@host132 ~]# kubectl get pods -n kube-system
NAME                                    READY   STATUS    RESTARTS   AGE
coredns-b7d8c5745-tws2p                 1/1     Running   0          8h
kubernetes-dashboard-7d75c474bb-6dmt7   1/1     Running   0          8h
[root@host132 ~]#

获取某特定namespace下所有pod信息

使用curl可以直接使用如下命令获取指定namespace下的pod信息(名为default的namespace下的pod信息)

kubectl命令:curl --header “Authorization: bearer token信息” -k https://192.168.163.132:6443/api/v1/namespaces/default/pods
执行示例信息

[root@host132 yaml]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/namespaces/default/pods
{
  "kind": "PodList",
  "apiVersion": "v1",
  "metadata": {
    "selfLink": "/api/v1/namespaces/default/pods",
    "resourceVersion": "42776"
  },
  "items": []
}[root@host132 yaml]# kubectl get pods -n default
No resources found.
[root@host132 yaml]# 

获取指定namespace下指定pod信息

使用curl可以直接使用如下命令获取指定namespace下的指定pod信息(kube-system的namespace下的名为coredns-b7d8c5745-tws2p的pod)

kubectl命令:curl --header “Authorization: bearer token信息” -k https://192.168.163.132:6443/api/v1/namespaces/kube-system/pods/coredns-b7d8c5745-tws2p

执行示例信息

[root@host132 yaml]# kubectl get pods -n kube-system
NAME                                    READY   STATUS    RESTARTS   AGE
coredns-b7d8c5745-tws2p                 1/1     Running   0          9h
kubernetes-dashboard-7d75c474bb-6dmt7   1/1     Running   0          9h
[root@host132 yaml]# curl --header "$header_option" -k https://192.168.163.132:6443/api/v1/namespaces/kube-system/pods/coredns-b7d8c5745-tws2p
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "coredns-b7d8c5745-tws2p",
    "generateName": "coredns-b7d8c5745-",
    "namespace": "kube-system",
    "selfLink": "/api/v1/namespaces/kube-system/pods/coredns-b7d8c5745-tws2p",
    "uid": "3fb8843a-5656-4dbf-b85d-ab347b8fb647",
    "resourceVersion": "441",
    "creationTimestamp": "2019-08-30T19:03:41Z",
    "labels": {
      "k8s-app": "kube-dns",
      "pod-template-hash": "b7d8c5745"
    },
    "annotations": {
      "seccomp.security.alpha.kubernetes.io/pod": "docker/default"
    },
    "ownerReferences": [
      {
        "apiVersion": "apps/v1",
        "kind": "ReplicaSet",
        "name": "coredns-b7d8c5745",
        "uid": "cbf4908b-7013-4e66-ab09-28e8b3e7ea5d",
        "controller": true,
        "blockOwnerDeletion": true
      }
    ]
  },
  "spec": {
    "volumes": [
      {
        "name": "config-volume",
        "configMap": {
          "name": "coredns",
          "items": [
            {
              "key": "Corefile",
              "path": "Corefile"
            }
          ],
          "defaultMode": 420
        }
      },
      {
        "name": "coredns-token-czz8p",
        "secret": {
          "secretName": "coredns-token-czz8p",
          "defaultMode": 420
        }
      }
    ],
    "containers": [
      {
        "name": "coredns",
        "image": "k8s.gcr.io/coredns:1.2.6",
        "args": [
          "-conf",
          "/etc/coredns/Corefile"
        ],
        "ports": [
          {
            "name": "dns",
            "containerPort": 53,
            "protocol": "UDP"
          },
          {
            "name": "dns-tcp",
            "containerPort": 53,
            "protocol": "TCP"
          },
          {
            "name": "metrics",
            "containerPort": 9153,
            "protocol": "TCP"
          }
        ],
        "resources": {
          "limits": {
            "memory": "170Mi"
          },
          "requests": {
            "cpu": "100m",
            "memory": "70Mi"
          }
        },
        "volumeMounts": [
          {
            "name": "config-volume",
            "readOnly": true,
            "mountPath": "/etc/coredns"
          },
          {
            "name": "coredns-token-czz8p",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "livenessProbe": {
          "httpGet": {
            "path": "/health",
            "port": 8080,
            "scheme": "HTTP"
          },
          "initialDelaySeconds": 60,
          "timeoutSeconds": 5,
          "periodSeconds": 10,
          "successThreshold": 1,
          "failureThreshold": 5
        },
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "IfNotPresent",
        "securityContext": {
          "capabilities": {
            "add": [
              "NET_BIND_SERVICE"
            ],
            "drop": [
              "all"
            ]
          },
          "readOnlyRootFilesystem": true,
          "allowPrivilegeEscalation": false
        }
      }
    ],
    "restartPolicy": "Always",
    "terminationGracePeriodSeconds": 30,
    "dnsPolicy": "Default",
    "serviceAccountName": "coredns",
    "serviceAccount": "coredns",
    "nodeName": "192.168.163.132",
    "securityContext": {
      
    },
    "schedulerName": "default-scheduler",
    "tolerations": [
      {
        "key": "CriticalAddonsOnly",
        "operator": "Exists"
      },
      {
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      },
      {
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      }
    ],
    "priority": 0,
    "enableServiceLinks": true
  },
  "status": {
    "phase": "Running",
    "conditions": [
      {
        "type": "Initialized",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-30T19:04:07Z"
      },
      {
        "type": "Ready",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-30T19:04:12Z"
      },
      {
        "type": "ContainersReady",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-30T19:04:12Z"
      },
      {
        "type": "PodScheduled",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-30T19:04:07Z"
      }
    ],
    "hostIP": "192.168.163.132",
    "podIP": "10.254.224.2",
    "startTime": "2019-08-30T19:04:07Z",
    "containerStatuses": [
      {
        "name": "coredns",
        "state": {
          "running": {
            "startedAt": "2019-08-30T19:04:10Z"
          }
        },
        "lastState": {
          
        },
        "ready": true,
        "restartCount": 0,
        "image": "k8s.gcr.io/coredns:1.2.6",
        "imageID": "docker://sha256:f59dcacceff45b5474d1385cd5f500d0c019ed9ca50ed5b814ac0c5fcec8699e",
        "containerID": "docker://cc7a9e9b357b1a04ad3d8820439703f79304002285d3adbfb05e3ac929ae6b05"
      }
    ],
    "qosClass": "Burstable"
  }
}[root@host132 yaml]# 

创建新的pod

使用curl可以直接使用如下命令在default的namespace下创建新的名为nginx的pod

curl命令:curl -k -X POST --header “Content-Type: application/json”
–header “Authorization: bearer $bearer_token”
https://192.168.163.132:6443/api/v1/namespaces/default/pods -d@pod-demo1.json

json信息如下所示

[root@host132 json]# cat pod-demo1.json 
{
    "kind": "Pod",
    "apiVersion": "v1",
    "metadata":{
        "name": "nginx",
        "namespace": "default"
    },
    "spec": {
        "containers": [{
            "name": "nginx-container",
            "image": "nginx:latest",
            "ports": [{"containerPort": 80}]
        }]
    }
}
[root@host132 json]#

执行示例信息

[root@host132 json]# kubectl get pods -n default -o wide
No resources found.
[root@host132 json]# curl -k -X POST --header "Content-Type: application/json" \
>                      --header "Authorization: bearer $bearer_token" \
>                      https://192.168.163.132:6443/api/v1/namespaces/default/pods -d@pod-demo1.json
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "nginx",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/pods/nginx",
    "uid": "888bf42d-0b8d-40a9-8295-73f17eb45272",
    "resourceVersion": "44969",
    "creationTimestamp": "2019-08-31T05:13:34Z"
  },
  "spec": {
    "volumes": [
      {
        "name": "default-token-sr2zb",
        "secret": {
          "secretName": "default-token-sr2zb",
          "defaultMode": 420
        }
      }
    ],
    "containers": [
      {
        "name": "nginx-container",
        "image": "nginx:latest",
        "ports": [
          {
            "containerPort": 80,
            "protocol": "TCP"
          }
        ],
        "resources": {
          
        },
        "volumeMounts": [
          {
            "name": "default-token-sr2zb",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "Always"
      }
    ],
    "restartPolicy": "Always",
    "terminationGracePeriodSeconds": 30,
    "dnsPolicy": "ClusterFirst",
    "serviceAccountName": "default",
    "serviceAccount": "default",
    "securityContext": {
      
    },
    "schedulerName": "default-scheduler",
    "tolerations": [
      {
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      },
      {
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      }
    ],
    "priority": 0,
    "enableServiceLinks": true
  },
  "status": {
    "phase": "Pending",
    "qosClass": "BestEffort"
  }
}[root@host132 json]# 
[root@host132 json]# kubectl get pods -n default -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          6s    10.254.224.4   192.168.163.132   <none>           <none>
[root@host132 json]# 

删除指定的pod

使用curl可以直接使用如下命令删除指定的namespace下的名为nginx的pod

curl命令:curl -k -X DELETE --header “Content-Type: application/json”
–header “Authorization: bearer $bearer_token”
https://192.168.163.132:6443/api/v1/namespaces/default/pods/nginx

执行示例信息

[root@host132 json]# kubectl get pods -n default -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          2m24s   10.254.224.4   192.168.163.132   <none>           <none>
[root@host132 json]# curl -k -X DELETE --header "Content-Type: application/json" \
>                      --header "Authorization: bearer $bearer_token" \
>                      https://192.168.163.132:6443/api/v1/namespaces/default/pods/nginx
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "nginx",
    "namespace": "default",
    "selfLink": "/api/v1/namespaces/default/pods/nginx",
    "uid": "888bf42d-0b8d-40a9-8295-73f17eb45272",
    "resourceVersion": "45178",
    "creationTimestamp": "2019-08-31T05:13:34Z",
    "deletionTimestamp": "2019-08-31T05:16:49Z",
    "deletionGracePeriodSeconds": 30
  },
  "spec": {
    "volumes": [
      {
        "name": "default-token-sr2zb",
        "secret": {
          "secretName": "default-token-sr2zb",
          "defaultMode": 420
        }
      }
    ],
    "containers": [
      {
        "name": "nginx-container",
        "image": "nginx:latest",
        "ports": [
          {
            "containerPort": 80,
            "protocol": "TCP"
          }
        ],
        "resources": {
          
        },
        "volumeMounts": [
          {
            "name": "default-token-sr2zb",
            "readOnly": true,
            "mountPath": "/var/run/secrets/kubernetes.io/serviceaccount"
          }
        ],
        "terminationMessagePath": "/dev/termination-log",
        "terminationMessagePolicy": "File",
        "imagePullPolicy": "Always"
      }
    ],
    "restartPolicy": "Always",
    "terminationGracePeriodSeconds": 30,
    "dnsPolicy": "ClusterFirst",
    "serviceAccountName": "default",
    "serviceAccount": "default",
    "nodeName": "192.168.163.132",
    "securityContext": {
      
    },
    "schedulerName": "default-scheduler",
    "tolerations": [
      {
        "key": "node.kubernetes.io/not-ready",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      },
      {
        "key": "node.kubernetes.io/unreachable",
        "operator": "Exists",
        "effect": "NoExecute",
        "tolerationSeconds": 300
      }
    ],
    "priority": 0,
    "enableServiceLinks": true
  },
  "status": {
    "phase": "Running",
    "conditions": [
      {
        "type": "Initialized",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-31T05:13:34Z"
      },
      {
        "type": "Ready",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-31T05:13:38Z"
      },
      {
        "type": "ContainersReady",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-31T05:13:38Z"
      },
      {
        "type": "PodScheduled",
        "status": "True",
        "lastProbeTime": null,
        "lastTransitionTime": "2019-08-31T05:13:34Z"
      }
    ],
    "hostIP": "192.168.163.132",
    "podIP": "10.254.224.4",
    "startTime": "2019-08-31T05:13:34Z",
    "containerStatuses": [
      {
        "name": "nginx-container",
        "state": {
          "running": {
            "startedAt": "2019-08-31T05:13:37Z"
          }
        },
        "lastState": {
          
        },
        "ready": true,
        "restartCount": 0,
        "image": "nginx:latest",
        "imageID": "docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9",
        "containerID": "docker://94ac7856d2bca02d69826bed1fccd691108cc9881d98c143906d19106c14ae84"
      }
    ],
    "qosClass": "BestEffort"
  }
}[root@host132 json]#kubectl get pods -n default -o wide
NAME    READY   STATUS        RESTARTS   AGE     IP             NODE              NOMINATED NODE   READINESS GATES
nginx   0/1     Terminating   0          2m50s   10.254.224.4   192.168.163.132   <none>           <none>
[root@host132 json]# kubectl get pods -n default -o wide
No resources found.
[root@host132 json]#

可以看到在执行之后,第一次使用kubectl get pods确认的时候显示名为nginx的pod的状态为Terminating,说明此时正在删除,稍等之后再次执行则可以看到已经成功删除了

Restful API方式常见问题

  • https://liumiaocn.blog.csdn.net/article/details/100518110

其他基础


转载:https://blog.csdn.net/liumiaocn/article/details/100528302
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场