k8s对资源对象的操作


node

## 增加node节点
kubeadm join 10.0.0.200:6443 --token q5uhqh.dcv21idbtej2jopc \
--discovery-token-ca-cert-hash sha256:68f830114530ab7a43d1b732eb646e0cc38df2b88f18e978d12cec01efe59db1 

#--- 忘记token
[root@master ~]#  kubeadm token create --print-join-command
W0918 14:32:06.998831   77062 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
kubeadm join 10.0.0.200:6443 --token ry4gwx.uou199gw6lokdl5u    \
--discovery-token-ca-cert-hash sha256:180b11aa0a8bf2a278573979cc95af56bb2a57205f8472ae6bdebbe9ab77904a 
## 删除node节点
kubectl delete node [主机名]
## 查询node节点
kubectl get node  						# 查看全部node
kubectl get nodes  						# 查看全部node
kebectl get node node01					 # 查看指定 node
kebectl get node node01 -o wide  		 # 查看指定node的详细信息

## 查看node的描述
kubectl describe pod nginx-565785f75c-drgsh 
kubectl describe pod -n lll centos-nginx-pod 

namespace(ns)

### 增加ns 
[root@master ~]# kubectl create ns lzd
namespace/lzd created
### 删除ns
[root@master ~]# kubectl delete ns lzd
namespace "lzd" deleted
### 查ns
[root@master ~]# kubectl get ns 
NAME              STATUS   AGE
default           Active   5d2h
kube-flannel      Active   5d2h
kube-node-lease   Active   5d2h
kube-public       Active   5d2h
kube-system       Active   5d2h
lll               Active   4h7m

## 查看指定 ns的描述
[root@master ~]# kubectl describe ns lll
Name:         lll
Labels:       <none>
Annotations:  <none>
Status:       Active

No resource quota.

No LimitRange resource

pod

### 创建一个pod
# 编写一个yaml的文件
[root@master ~]# cat nginx.yaml 
apiVersion: "v1"
kind: "Namespace"
metadata:
  name: lll

---
apiVersion: v1
kind: Pod
metadata:
  name: centos-nginx-pod
  namespace: lll
spec:
  containers:
  - image: nginx:alpine
    imagePullPolicy: IfNotPresent
    name: nginx-containers-lll

  - image: centos:7
    imagePullPolicy: IfNotPresent
    name: centos7-containers-lll
    command:
    - /bin/tail
    - -f
    - /etc/hosts
    
# 运行 apply(应用-不报错)    caeate(创建-会报错)
[root@master ~]# kubectl apply -f nginx.yaml 
namespace/lll unchanged
pod/centos-nginx-pod unchanged
[root@master ~]# kubectl create -f nginx.yaml 
Error from server (AlreadyExists): error when creating "nginx.yaml": namespaces "lll" already exists
Error from server (AlreadyExists): error when creating "nginx.yaml": pods "centos-nginx-pod" already exists
-o|--output:指定输出格式
	json:输出json格式
	yaml:输出yaml格式
	wide:输出详细信息

-n:指定名称空间
### 查看pod (默认是在default名称空间中)
[root@master ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-565785f75c-4sll4   1/1     Running   0          5h55m

### 查看指定名称空间的 pod (-n 指定名称空间)
kubectl get pod -n lll
NAME               READY   STATUS    RESTARTS   AGE
centos-nginx-pod   2/2     Running   0          4h10m

### 查看指定pod的 yaml文件
[root@master ~]# kubectl get pod nginx-565785f75c-drgsh  -n lll -o yaml

### 查看pod的详细信息
[root@master ~]# kubectl get pod -n lll centos-nginx-pod -o wide
NAME               READY   STATUS    RESTARTS   AGE     IP         NODE     NOMINATED NODE   READINESS GATES
centos-nginx-pod   2/2     Running   0          4h12m   10.2.3.6   node03   <none>           <none>
### 删除pod
[root@master ~]# kubectl delete pod nginx-565785f75c-4sll4 
pod "nginx-565785f75c-4sll4" deleted

### 利用yaml文件删除 (清楚yaml文件中的所有)
[root@master ~]# kubectl delete -f nginx.yaml 
namespace "lll" deleted
pod "centos-nginx-pod" deleted

排错

## 查看pod(容器的)的日志
[root@master ~]# kubectl logs nginx-565785f75c-drgsh 

root@master ~]# kubectl logs -n lll centos-nginx-pod nginx-containers-lll
							  ns      pod               pod中的容器		
							  

## 查看创建pod过程的故障
kubectl describe [资源] [资源名] [-n 名称空间]
[root@master ~]# kubectl describe pod -n lll centos-nginx-pod 
							   pod     ns     pod

标签的操作

## 标签都是以key=value的形势存在   并且只能存在一个key

## 查看
kubectl get [资源] [子资源] -n [名称空间] --show-labels

## 增加 
kubectl label [资源] [子资源] -n [名称空间]  [标签]+
kubectl label [资源] [子资源] -n [名称空间]  node-role.kubernetes.io/[标签]+
## 删除
kubectl label [资源] [子资源] -n [名称空间]  [标签]-
kubectl label [资源] [子资源] -n [名称空间]  node-role.kubernetes.io/[标签]-
## 根据标签删除POD
[root@master01 kubernetes]# kubectl delete pod -l run=nginx

## 连接到POD
[root@master01 kubernetes]# kubectl exec -it nginx-pod -- /bin/sh

## 当一个POD中有多个容器时,使用-c指定容器名,连接到指定的容器
[root@master01 kubernetes]# kubectl exec -it nginx-busybox -c busybox-container -- /bin/sh
node
node的标签
1.### 查看 node标签  (以逗号为分隔符号)
[root@master ~]# kubectl get node --show-labels 
NAME     STATUS   ROLES    AGE    VERSION   LABELS
master   Ready    master   6d1h   v1.19.3   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,
node01   Ready    node     6d1h   v1.19.3   beta.kubernetes.io/arch=amd64,
node02   Ready    node     6d1h   v1.19.3   beta.kubernetes.io/arch=amd64,
node03   Ready    node     6d1h   v1.19.3   beta.kubernetes.io/arch=amd64,

2.### 添加 node标签 
[root@master ~]# kubectl label node node01 web=nginx
node/node01 labeled

[root@master ~]# kubectl get node node01 --show-labels 
NAME     STATUS   ROLES   AGE    VERSION   LABELS
node01   Ready    node    6d1h   v1.19.3   web=nginx

3.#### 删除node 标签
[root@master ~]# kubectl label node node01 web-
node/node01 labeled

[root@master ~]# kubectl get node node01 --show-labels 
NAME     STATUS   ROLES   AGE    VERSION   LABELS
node01   Ready    node    6d1h   v1.19.3   
node的role
1.### 查看node的role
[root@master ~]# kubectl get node
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   6d1h   v1.19.3
node01   Ready    node     6d1h   v1.19.3
node02   Ready    node     6d1h   v1.19.3
node03   Ready    node     6d1h   v1.19.3


2.### 添加node的role
[root@master ~]# kubectl label node node01 node-role.kubernetes.io/001=
node/node01 labeled

[root@master ~]# kubectl get node
NAME     STATUS   ROLES      AGE    VERSION
master   Ready    master     6d1h   v1.19.3
node01   Ready    001,node   6d1h   v1.19.3
node02   Ready    node       6d1h   v1.19.3
node03   Ready    node       6d1h   v1.19.3


3.## 删除node的role
[root@master ~]# kubectl label node node01 node-role.kubernetes.io/001-
node/node01 labeled

[root@master ~]# kubectl get node
NAME     STATUS   ROLES    AGE    VERSION
master   Ready    master   6d1h   v1.19.3
node01   Ready    node     6d1h   v1.19.3
node02   Ready    node     6d1h   v1.19.3
node03   Ready    node     6d1h   v1.19.3
pod
1.### 查看pod的标签
[root@master ~]# kubectl get pod --show-labels 
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-565785f75c-drgsh   1/1     Running   0          23h   app=nginx,pod-template-hash=565785f75c


2.### 增加pod的标签  
[root@master ~]# kubectl label pod nginx-565785f75c-drgsh appp=web
pod/nginx-565785f75c-drgsh labeled

[root@master ~]# kubectl get pod --show-labels 
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-565785f75c-drgsh   1/1     Running   0          23h   app=nginx,appp=web,pod-template-hash=565785f75c


3.### 删除pod的标签
[root@master ~]# kubectl label pod nginx-565785f75c-drgsh app-
pod/nginx-565785f75c-drgsh labeled

[root@master ~]# kubectl get pod --show-labels
NAME                     READY   STATUS    RESTARTS   AGE   LABELS
nginx-565785f75c-drgsh   1/1     Running   0          23h   pod-template-hash=565785f75c