k8s 操作

1、查看Kubernetes API所有版本

[root@linux-node1 ~]# kubectl api-versions
admissionregistration.k8s.io/v1beta1
apiextensions.k8s.io/v1beta1
apiregistration.k8s.io/v1
apiregistration.k8s.io/v1beta1
apps/v1
apps/v1beta1
apps/v1beta2
authentication.k8s.io/v1
authentication.k8s.io/v1beta1
authorization.k8s.io/v1
authorization.k8s.io/v1beta1
autoscaling/v1
autoscaling/v2beta1
batch/v1
batch/v1beta1
certificates.k8s.io/v1beta1
events.k8s.io/v1beta1
extensions/v1beta1
networking.k8s.io/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
rbac.authorization.k8s.io/v1beta1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1

2、kubectl explain 来查找 kind(kubernetes对象资源类型) 对应的 API 版本信息

[root@linux-node1 ~]# kubectl explain deployment
KIND:     Deployment
VERSION:  extensions/v1beta1
DESCRIPTION:
     DEPRECATED - This group version of Deployment is deprecated by
     apps/v1beta2/Deployment. See the release notes for more information.
     Deployment enables declarative updates for Pods and ReplicaSets.
FIELDS:
   apiVersion    <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources
   kind    <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds
   metadata    <Object>
     Standard object metadata.
   spec    <Object>
     Specification of the desired behavior of the Deployment.
   status    <Object>
     Most recently observed status of the Deployment.

3、使用标签选择器列出pod

yaml文件示例:

# cat nginx-daemonset.yam
apiVersion: apps/v1 kind: DaemonSet metadata: name: nginx-daemonset labels: app: nginx spec: selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.13.12 ports: - containerPort: 80 nodeSelector: nginx_yaml
[root@linux-node1 ~]# kubectl get pod   -l app=nginx -o wide 
NAME                    READY     STATUS    RESTARTS   AGE       IP          NODE
nginx-daemonset-lr8rk   1/1       Running   0          1d        10.2.76.4   192.168.56.13

4、查看lable下key为app的pod标签

yaml文件示例:

# cat nginx-daemonset.yam
apiVersion: apps/v1             
  kind: DaemonSet               
  metadata:                     
    name: nginx-daemonset       
    labels:                     
      app: nginx                
  spec:                         
    selector:                   
      matchLabels:              
        app: nginx              
    template:                   
      metadata:                 
        labels:                 
          app: nginx            
      spec:                     
        containers:             
        - name: nginx           
          image: nginx:1.13.12  
          ports:                
          - containerPort: 80   
        nodeSelector:           
          nginx_yaml
[root@linux-node1 ~]# kubectl get pod   -L app 
NAME                    READY     STATUS    RESTARTS   AGE       APP
nginx-daemonset-lr8rk   1/1       Running   0          1d        nginx

5、为node(服务器)打标签

[root@linux-node1 ~]# kubectl label nodes 192.168.56.13 nginx_yaml=true
node "192.168.56.13" labeled

6、查看node(服务器)所有标签

[root@linux-node1 ~]# kubectl get node --show-labels
NAME            STATUS    ROLES     AGE       VERSION   LABELS
192.168.56.12   Ready     <none>    2d        v1.10.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,edgenode=true,kubernetes.io/hostname=192.168.56.12
192.168.56.13   Ready     <none>    2d        v1.10.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/hostname=192.168.56.13,nginx_yaml=true

7、删除node(服务器)标签

[root@linux-node1 ~]# kubectl label nodes 192.168.56.13 nginx_yaml-
node "192.168.56.13" labeled

8、查看当前命名空间

[root@linux-node1 ~]# kubectl get namespace
NAME          STATUS    AGE
default       Active    2d
kube-public   Active    2d
kube-system   Active    2d

9、创建命名空间

[root@linux-node1 ~]# kubectl create namespace test-namespace
namespace "test-namespace" created

[root@linux-node1 ~]# kubectl get namespace
NAME             STATUS    AGE
default          Active    2d
kube-public      Active    2d
kube-system      Active    2d
test-namespace   Active    3s

10、删除命名空间

[root@linux-node1 ~]# kubectl delete namespace test-namespace
namespace "test-namespace" deleted

[root@linux-node1 ~]# kubectl get namespace
NAME             STATUS        AGE
default          Active        2d
kube-public      Active        2d
kube-system      Active        2d
test-namespace   Terminating   1m

[root@linux-node1 ~]# kubectl get namespace
NAME          STATUS    AGE
default       Active    2d
kube-public   Active    2d
kube-system   Active    2d

11、在运行的容器中远程执行命令

[root@linux-node1 ~]# kubectl get pod
NAME                    READY     STATUS    RESTARTS   AGE
nginx-daemonset-kkkb6   1/1       Running   0          19h

[root@linux-node1 ~]# kubectl exec nginx-daemonset-kkkb6 -- cat /etc/resolv.conf
nameserver 10.1.0.2
search default.svc.cluster.local. svc.cluster.local. cluster.local.
options ndots:5

 12、强制删除pod 和namespace命令

# 删除 Pod
[root@linux-node1 ~]# kubectl delete pod podName(pod名称) -n NAMESPACENAME(命名空间名称) --force --grace-period=0

# 删除 Namespace 
kubectl delete namespace  NAMESPACENAME(命名空间名称) --force --grace-period=0


#######
原文地址:https://www.cnblogs.com/faithH/p/12171983.html