返回总目录页

kubernetes运行应用Controller1之Deployment详解

run 或create deployment 部署一个应用

kubernetes 部署 nginx ,使用 kubectl get deployment 时出现 No resources found in default namespace

结果验证:
kubectl run mcw01_nginx --image=nginx #能get pod,但不能get deployment ,不能get replicaset
kubectl create deployment mcw01dep-nginx --image=nginx #既能get pod ,又能get deployment,又能get replicaset

kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2 #它不能 创建副本了,
kubectl create deployment mcw01dep-nginx --image=nginx #那么这个命令是否可以支持创建多个副本

首先我确保了master和nodes节点的正常运行,还有各个组件处于健康状态
使用 kubectl 命令创建监听 80 端口的 Nginx Pod(Kubernetes 运行容器的最小单元)
kubectl run mcw01_nginx --image=nginx --port=80
运行结果:

[machangwei@mcw7 ~]$ kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2
Error: unknown flag: --replicas
See 'kubectl run --help' for usage.
[machangwei@mcw7 ~]$ kubectl run mcw01_nginx --image=nginx --port=80  #不能使用下划线命名,但是可以用短横线
The Pod "mcw01_nginx" is invalid: 
* metadata.name: Invalid value: "mcw01_nginx": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*')
* spec.containers[0].name: Invalid value: "mcw01_nginx": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
[machangwei@mcw7 ~]$ 
[machangwei@mcw7 ~]$ kubectl run mcw01-nginx --image=nginx --port=80
pod/mcw01-nginx created

可以看到,这里提示只是成功创建了一个pod 叫mcw01_nginx,在以往的版本中似乎会顺便创建一个deployment,显然这里没有

使用 kubectl get deployment命令查看deployment

[machangwei@mcw7 ~]$ kubectl get deployment
No resources found in default namespace.

问题就出现了,的确没有,那我们就需要手动创建

使用 kubectl create deployment mcw01dep-nginx --image=nginx

[machangwei@mcw7 ~]$ kubectl create deployment mcw01dep-nginx --image=nginx
deployment.apps/mcw01dep-nginx created

再次使用kubectl get deployment查看,可以看到已经有了一个mcw01dep-nginx 的 deployment

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx   1/1     1            1           48s

再次使用kubectl get replicaset看副本,有mcw01dep-nginx命名的副本

[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw01dep-nginx-5dd785954d   1         1         1       6m26s

使用kubectl get pod也能看到mcw01dep-nginx命名的Pod。

[machangwei@mcw7 ~]$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
httpd-app                         1/1     Running   0          3h12m
mcw01-nginx                       1/1     Running   0          3m55s
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          94s
mcw1httpd-app                     1/1     Running   0          162m
nginx-deployment                  1/1     Running   0          12m

查看刚刚部署的应用

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx   1/1     1            1           9m37s
[machangwei@mcw7 ~]$ kubectl describe deployment mcw01dep-nginx
Name:                   mcw01dep-nginx
Namespace:              default
CreationTimestamp:      Tue, 04 Jan 2022 09:49:28 +0800
Labels:                 app=mcw01dep-nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=mcw01dep-nginx
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=mcw01dep-nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   mcw01dep-nginx-5dd785954d (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  10m   deployment-controller  Scaled up replica set mcw01dep-nginx-5dd785954d to 1

查看刚刚部署的应用的副本

[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw01dep-nginx-5dd785954d   1         1         1       15m
[machangwei@mcw7 ~]$ kubectl describe replicaset mcw01dep-nginx-5dd785954d
Name:           mcw01dep-nginx-5dd785954d
Namespace:      default
Selector:       app=mcw01dep-nginx,pod-template-hash=5dd785954d
Labels:         app=mcw01dep-nginx
                pod-template-hash=5dd785954d
Annotations:    deployment.kubernetes.io/desired-replicas: 1
                deployment.kubernetes.io/max-replicas: 2
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/mcw01dep-nginx
Replicas:       1 current / 1 desired
Pods Status:    1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=mcw01dep-nginx
           pod-template-hash=5dd785954d
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  15m   replicaset-controller  Created pod: mcw01dep-nginx-5dd785954d-sb8ps

查看刚刚部署的应用的Pod信息

[machangwei@mcw7 ~]$ kubectl describe pod mcw01dep-nginx-5dd785954d-sb8ps
Name:         mcw01dep-nginx-5dd785954d-sb8ps
Namespace:    default
Priority:     0
Node:         mcw9/10.0.0.139
Start Time:   Tue, 04 Jan 2022 09:49:27 +0800
Labels:       app=mcw01dep-nginx
              pod-template-hash=5dd785954d
Annotations:  <none>
Status:       Running
IP:           10.244.0.4
IPs:
  IP:           10.244.0.4
Controlled By:  ReplicaSet/mcw01dep-nginx-5dd785954d
Containers:
  nginx:
    Container ID:   docker://374ff9c1a60e9562f7e05ec4eaa5150e3380b836a26f3b0fe565e6f9dea52cef
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 04 Jan 2022 09:49:30 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h56zh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-h56zh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  18m   default-scheduler  Successfully assigned default/mcw01dep-nginx-5dd785954d-sb8ps to mcw9
  Normal  Pulling    18m   kubelet            Pulling image "nginx"
  Normal  Pulled     18m   kubelet            Successfully pulled image "nginx" in 1.193138975s
  Normal  Created    18m   kubelet            Created container nginx
  Normal  Started    18m   kubelet            Started container nginx

指定命名空间的查询Pod详情,应该是一样的

[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                              READY   STATUS             RESTARTS          AGE
default       httpd-app                         1/1     Running            0                 3h31m
default       mcw01-nginx                       1/1     Running            0                 22m
default       mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running            0                 20m
default       mcw1httpd-app                     1/1     Running            0                 3h1m
default       nginx-deployment                  1/1     Running            0                 31m
kube-system   coredns-6d8c4cb4d-2296m           1/1     Running            1 (9h ago)        13h
kube-system   coredns-6d8c4cb4d-lphb2           1/1     Running            1 (9h ago)        13h
kube-system   etcd-mcw7                         1/1     Running            3                 13h
kube-system   kube-apiserver-mcw7               1/1     Running            2                 13h
kube-system   kube-controller-manager-mcw7      1/1     Running            4 (9h ago)        13h
kube-system   kube-flannel-ds-5pxmj             0/1     Error              102 (5m12s ago)   8h
kube-system   kube-flannel-ds-8gzfq             1/1     Running            0                 9h
kube-system   kube-flannel-ds-s4qmj             0/1     CrashLoopBackOff   95 (62s ago)      7h46m
kube-system   kube-proxy-4lmsx                  1/1     Running            0                 8h
kube-system   kube-proxy-c5zmm                  1/1     Running            0                 13h
kube-system   kube-proxy-npmtv                  1/1     Running            0                 7h46m
kube-system   kube-scheduler-mcw7               1/1     Running            3 (9h ago)        13h
[machangwei@mcw7 ~]$ kubectl describe pod mcw01dep-nginx-5dd785954d-sb8ps --namespace=default
Name:         mcw01dep-nginx-5dd785954d-sb8ps
Namespace:    default
Priority:     0
Node:         mcw9/10.0.0.139
Start Time:   Tue, 04 Jan 2022 09:49:27 +0800
Labels:       app=mcw01dep-nginx
              pod-template-hash=5dd785954d
Annotations:  <none>
Status:       Running
IP:           10.244.0.4
IPs:
  IP:           10.244.0.4
Controlled By:  ReplicaSet/mcw01dep-nginx-5dd785954d
Containers:
  nginx:
    Container ID:   docker://374ff9c1a60e9562f7e05ec4eaa5150e3380b836a26f3b0fe565e6f9dea52cef
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 04 Jan 2022 09:49:30 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h56zh (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-h56zh:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  21m   default-scheduler  Successfully assigned default/mcw01dep-nginx-5dd785954d-sb8ps to mcw9
  Normal  Pulling    21m   kubelet            Pulling image "nginx"
  Normal  Pulled     21m   kubelet            Successfully pulled image "nginx" in 1.193138975s
  Normal  Created    21m   kubelet            Created container nginx
  Normal  Started    21m   kubelet            Started container nginx
[machangwei@mcw7 ~]$ 

对刚刚部署的应用做几种资源的查询

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx   1/1     1            1           23m
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw01dep-nginx-5dd785954d   1         1         1       23m
[machangwei@mcw7 ~]$ kubectl get pod |grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          23m
[machangwei@mcw7 ~]$ kubectl get pod -o wide |grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          23m     10.244.0.4   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces |grep mcw01dep-nginx
default       mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running            0                 24m

创建同名的deployment是不可以的

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx   1/1     1            1           31m
[machangwei@mcw7 ~]$ kubectl create deployment mcw01dep-nginx --image=nginx
error: failed to create deployment: deployments.apps "mcw01dep-nginx" already exists

使用负载均衡模式发布服务,让外网访问我的Nginx服务(docker)

kubectl expose deployment mcw01dep-nginx --port=80 --type=LoadBalancer

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx   1/1     1            1           33m
[machangwei@mcw7 ~]$ kubectl expose deployment mcw01dep-nginx --port=80 --type=LoadBalancer
service/mcw01dep-nginx exposed

查看服务详情

kubectl describe service mcw01dep-nginx
输出如下:

[machangwei@mcw7 ~]$ kubectl get service  #获取服务信息
NAME             TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes       ClusterIP      10.96.0.1      <none>        443/TCP        13h
mcw01dep-nginx   LoadBalancer   10.96.184.54   <pending>     80:30102/TCP   74s
[machangwei@mcw7 ~]$ kubectl describe service mcw01dep-nginx
Name:                     mcw01dep-nginx
Namespace:                default
Labels:                   app=mcw01dep-nginx
Annotations:              <none>
Selector:                 app=mcw01dep-nginx
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.96.184.54
IPs:                      10.96.184.54
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30102/TCP
Endpoints:                10.244.0.4:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

这里我就可以利用 节点的ip+暴露出来的端口 检测服务是否访问成功

[machangwei@mcw7 ~]$ kubectl get pod -o wide |grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          43m     10.244.0.4   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl  describe pod mcw01dep-nginx-5dd785954d-sb8ps|grep Node:
Node:         mcw9/10.0.0.139

由上找到服务所在的Node ip ,找到服务暴露出来的NodePort ,然后在外部就可以使用Node ip :NodePort来访问服务
比如这里的访问:
我在master节点访问node2节点上的服务,这个服务是node2ip,端口,映射到node2的应用docker上的80服务

[machangwei@mcw7 ~]$ curl 10.0.0.139:30102
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

笔记本浏览器上访问:

进入服务所在的主机节点,查看容器的ip,与之前看的容器ip等信息一致

[root@mcw9 ~]$ docker ps|grep mcw01dep-nginx
374ff9c1a60e   nginx                                                "/docker-entrypoint.…"   49 minutes ago   Up 49 minutes             k8s_nginx_mcw01dep-nginx-5dd785954d-sb8ps_default_13d59a7a-c16f-44f1-b442-de7252ae4bac_0
388265797ee1   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 49 minutes ago   Up 49 minutes             k8s_POD_mcw01dep-nginx-5dd785954d-sb8ps_default_13d59a7a-c16f-44f1-b442-de7252ae4bac_0
[root@mcw9 ~]$ docker exec -it 374 hostname -i
10.244.0.4
[root@mcw9 ~]$ docker exec -it 388 hostname -i
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "hostname": executable file not found in $PATH: unknown
[root@mcw9 ~]$ 

在Nginx服务的主机上,是能直接通容器ip的

[root@mcw9 ~]$ docker exec -it 374 hostname -i
10.244.0.4
[root@mcw9 ~]$ docker exec -it 388 hostname -i
OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "hostname": executable file not found in $PATH: unknown
[root@mcw9 ~]$ 
[root@mcw9 ~]$ 
[root@mcw9 ~]$ 
[root@mcw9 ~]$ 
[root@mcw9 ~]$ ping 10.244.0.4
PING 10.244.0.4 (10.244.0.4) 56(84) bytes of data.
64 bytes from 10.244.0.4: icmp_seq=1 ttl=64 time=0.256 ms
64 bytes from 10.244.0.4: icmp_seq=2 ttl=64 time=0.059 ms
^C
--- 10.244.0.4 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.059/0.157/0.256/0.099 ms
[root@mcw9 ~]$ curl 10.244.0.4:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@mcw9 ~]$ 

命令行部署多个副本的应用

部署多个Nginx,然后可以用上面那个例子,去做负载均衡了

kubectl create deployment   mcw01dep-nginx   --image=nginx --replicas=3

生成应用,副本。pod里是分为三个单一的。

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           92s
mcw01dep-nginx   1/1     1            1           61m
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       101s
mcw01dep-nginx-5dd785954d   1         1         1       61m
[machangwei@mcw7 ~]$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
httpd-app                         1/1     Running   0          4h12m
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          118s
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          118s
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          118s
mcw01-nginx                       1/1     Running   0          63m
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          61m
mcw1httpd-app                     1/1     Running   0          3h42m
nginx-deployment                  1/1     Running   0          71m
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces
NAMESPACE     NAME                              READY   STATUS             RESTARTS         AGE
default       httpd-app                         1/1     Running            0                4h13m
default       mcw-depcluster-547cfdc745-258sz   1/1     Running            0                2m49s
default       mcw-depcluster-547cfdc745-q2785   1/1     Running            0                2m49s
default       mcw-depcluster-547cfdc745-wrq74   1/1     Running            0                2m49s

k8s资源删除(慎用)

1、删除一个只有Pod信息,没有deployment和replicaset信息的pod

能成功删除pod,并且去node上查看容器,已经被删除。
删除资源,delete后面接资源类型,然后接资源名称

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           13h
mcw01dep-nginx   1/1     1            1           14h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       13h
mcw01dep-nginx-5dd785954d   1         1         1       14h
[machangwei@mcw7 ~]$ kubectl get pod #httpd-app 只有pod里面看到它了,删除它
NAME                              READY   STATUS    RESTARTS   AGE
httpd-app                         1/1     Running   0          17h
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h
mcw01-nginx                       1/1     Running   0          14h
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          14h
mcw1httpd-app                     1/1     Running   0          16h
nginx-deployment                  1/1     Running   0          14h
[machangwei@mcw7 ~]$ kubectl delete httpd-app
error: the server doesn't have a resource type "httpd-app"
[machangwei@mcw7 ~]$ kubectl delete pod  httpd-app #删除这个pod成功,从node上也没有看到跟这个容器,包括退出的
pod "httpd-app" deleted
[machangwei@mcw7 ~]$ kubectl get pod  #查看httpd-app它已经不存在
NAME                              READY   STATUS    RESTARTS   AGE
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h
mcw01-nginx                       1/1     Running   0          14h
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          14h
mcw1httpd-app                     1/1     Running   0          16h
nginx-deployment                  1/1     Running   0          14h

2、第二次删除只有pod,没有deployment,replicaset的资源。

查看:kubectl get pod -o wide
删除:kubectl delete pod mcw01-nginx #指定pod name

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           13h
mcw01dep-nginx   1/1     1            1           14h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       13h
mcw01dep-nginx-5dd785954d   1         1         1       14h
[machangwei@mcw7 ~]$ kubectl get pod -o wide #查看有mcw01-nginx
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h   10.244.0.5   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h   10.244.0.4   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h   10.244.0.5   mcw9   <none>           <none>
mcw01-nginx                       1/1     Running   0          14h   10.244.0.3   mcw9   <none>           <none>
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          14h   10.244.0.4   mcw9   <none>           <none>
mcw1httpd-app                     1/1     Running   0          16h   10.244.0.2   mcw8   <none>           <none>
nginx-deployment                  1/1     Running   0          14h   10.244.0.3   mcw8   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide #查看有mcw01-nginx
NAMESPACE     NAME                              READY   STATUS             RESTARTS          AGE   IP           NODE   NOMINATED NODE   READINESS GATES
default       mcw-depcluster-547cfdc745-258sz   1/1     Running            0                 13h   10.244.0.5   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-q2785   1/1     Running            0                 13h   10.244.0.4   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-wrq74   1/1     Running            0                 13h   10.244.0.5   mcw9   <none>           <none>
default       mcw01-nginx                       1/1     Running            0                 14h   10.244.0.3   mcw9   <none>           <none>
default       mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running            0                 14h   10.244.0.4   mcw9   <none>           <none>
default       mcw1httpd-app                     1/1     Running            0                 16h   10.244.0.2   mcw8   <none>           <none>
default       nginx-deployment                  1/1     Running            0                 14h   10.244.0.3   mcw8   <none>           <none>
kube-system   coredns-6d8c4cb4d-2296m           1/1     Running            1 (23h ago)       27h   10.244.0.3   mcw7   <none>           <none>
kube-system   coredns-6d8c4cb4d-lphb2           1/1     Running            1 (23h ago)       27h   10.244.0.2   mcw7   <none>           <none>
kube-system   etcd-mcw7                         1/1     Running            3                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-apiserver-mcw7               1/1     Running            2                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-controller-manager-mcw7      1/1     Running            4 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-5pxmj             0/1     CrashLoopBackOff   119 (3m25s ago)   22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-flannel-ds-8gzfq             1/1     Running            0                 23h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-s4qmj             0/1     CrashLoopBackOff   112 (2m25s ago)   21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-proxy-4lmsx                  1/1     Running            0                 22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-proxy-c5zmm                  1/1     Running            0                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-proxy-npmtv                  1/1     Running            0                 21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-scheduler-mcw7               1/1     Running            3 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>

[root@mcw9 ~]$ docker ps -a|grep mcw01-nginx  #node上查看到这个mcw01-nginx容器资源,占两个进程,其中一个是POD
f9aa914cff31   nginx                                                "/docker-entrypoint.…"   14 hours ago    Up 14 hours                          k8s_mcw01-nginx_mcw01-nginx_default_8e8154f0-40d2-412a-8fc6-7a0fdb4106d9_0
894c97f6cb91   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago    Up 14 hours                          k8s_POD_mcw01-nginx_default_8e8154f0-40d2-412a-8fc6-7a0fdb4106d9_0

[machangwei@mcw7 ~]$  kubectl delete pod mcw01-nginx #删除pod mcw01-nginx
pod "mcw01-nginx" deleted
[machangwei@mcw7 ~]$ kubectl get pod -o wide #查看已删除了mcw01-nginx
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h   10.244.0.5   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h   10.244.0.4   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h   10.244.0.5   mcw9   <none>           <none>
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          14h   10.244.0.4   mcw9   <none>           <none>
mcw1httpd-app                     1/1     Running   0          16h   10.244.0.2   mcw8   <none>           <none>
nginx-deployment                  1/1     Running   0          14h   10.244.0.3   mcw8   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide #查看已删除了mcw01-nginx
NAMESPACE     NAME                              READY   STATUS             RESTARTS          AGE   IP           NODE   NOMINATED NODE   READINESS GATES
default       mcw-depcluster-547cfdc745-258sz   1/1     Running            0                 13h   10.244.0.5   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-q2785   1/1     Running            0                 13h   10.244.0.4   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-wrq74   1/1     Running            0                 13h   10.244.0.5   mcw9   <none>           <none>
default       mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running            0                 14h   10.244.0.4   mcw9   <none>           <none>
default       mcw1httpd-app                     1/1     Running            0                 16h   10.244.0.2   mcw8   <none>           <none>
default       nginx-deployment                  1/1     Running            0                 14h   10.244.0.3   mcw8   <none>           <none>
kube-system   coredns-6d8c4cb4d-2296m           1/1     Running            1 (23h ago)       27h   10.244.0.3   mcw7   <none>           <none>
kube-system   coredns-6d8c4cb4d-lphb2           1/1     Running            1 (23h ago)       27h   10.244.0.2   mcw7   <none>           <none>
kube-system   etcd-mcw7                         1/1     Running            3                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-apiserver-mcw7               1/1     Running            2                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-controller-manager-mcw7      1/1     Running            4 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-5pxmj             0/1     CrashLoopBackOff   120 (30s ago)     22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-flannel-ds-8gzfq             1/1     Running            0                 23h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-s4qmj             0/1     CrashLoopBackOff   112 (4m44s ago)   21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-proxy-4lmsx                  1/1     Running            0                 22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-proxy-c5zmm                  1/1     Running            0                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-proxy-npmtv                  1/1     Running            0                 21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-scheduler-mcw7               1/1     Running            3 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>
[machangwei@mcw7 ~]$ 

[root@mcw9 ~]$ docker ps -a|grep mcw01-nginx  #node 节点上查看已经删除掉了容器mcw01-nginx,不是停止容器,而是删除了容器
[root@mcw9 ~]$ 

3、删除带有一个deployment,和一个副本的资源的pod,只做删除pod操作。以及POD等资源如何命名详解

删除后会重新生成pod,NODE上重新生成容器。pod name被修改
由下命名可知。当创建deployment指定名字为mcw01dep-nginx时,生成的副本命名为mcw01dep-nginx-5dd785954d ,副本名字为deployment-随机字符串。而pod名字是副本名字-随机字符串,也就是deployment-radomstr-radomstr
master获取到的pod name,node上查看对应容器的name,可以看到容器name包含pod name字符串,如下:
pod name :mcw01dep-nginx-5dd785954d-rxwbj
容器 name:k8s_nginx_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
k8s_POD_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
如上: 容器命名是 k8s_nginx(可能跟镜像名有关)_POD名字_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
容器命名是 k8s_POD_POD名字__default_随机字符串_0
每个容器运行后,都有一个与之对应的POD容器,这个容器还进不去

当deployment有多个副本(比如三个)时,get deployment和replicaset是单行显示多个副本资源,显示个数是3;get pod时(包括加命名空间的参数),每个副本就是显示一行,它们的副本 名字是一样的,pod名字是不同的,pod名字是副本名字带了不同的后缀字符串。命名空间是一样的,可以在不通的node上。只删除副本的话,副本名字不变,pod名字改变,每个副本的副本名字一致,POD名字不一致。 多个副本重新分配在不同的node上部署,一个副本就是一个docker容器加上它对应的pod容器,它们的pod是一致的,包含在容器名称上。如果调度的话,估计可以根据这个去做如下区分:
可以通过副本名字确认docker是同一个副本,可以根据POD名字确认是同一个副本集合体名字下的某个副本,可以根据POD名字一致,确认docker容器和它对应的POD容器是一组,是同一个副本资源。


据目前所知,只要不是删除了deployment,容器会自动重新部署

get pod --all-namespaces 获取的名字就是pod的name

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           13h
mcw01dep-nginx   1/1     1            1           14h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       13h
mcw01dep-nginx-5dd785954d   1         1         1       14h
[machangwei@mcw7 ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h   10.244.0.5   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h   10.244.0.4   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h   10.244.0.5   mcw9   <none>           <none>
mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running   0          14h   10.244.0.4   mcw9   <none>           <none>
mcw1httpd-app                     1/1     Running   0          17h   10.244.0.2   mcw8   <none>           <none>
nginx-deployment                  1/1     Running   0          14h   10.244.0.3   mcw8   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                              READY   STATUS             RESTARTS          AGE   IP           NODE   NOMINATED NODE   READINESS GATES
default       mcw-depcluster-547cfdc745-258sz   1/1     Running            0                 13h   10.244.0.5   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-q2785   1/1     Running            0                 13h   10.244.0.4   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-wrq74   1/1     Running            0                 13h   10.244.0.5   mcw9   <none>           <none>
default       mcw01dep-nginx-5dd785954d-sb8ps   1/1     Running            0                 14h   10.244.0.4   mcw9   <none>           <none>
default       mcw1httpd-app                     1/1     Running            0                 17h   10.244.0.2   mcw8   <none>           <none>
default       nginx-deployment                  1/1     Running            0                 14h   10.244.0.3   mcw8   <none>           <none>
kube-system   coredns-6d8c4cb4d-2296m           1/1     Running            1 (23h ago)       27h   10.244.0.3   mcw7   <none>           <none>
kube-system   coredns-6d8c4cb4d-lphb2           1/1     Running            1 (23h ago)       27h   10.244.0.2   mcw7   <none>           <none>
kube-system   etcd-mcw7                         1/1     Running            3                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-apiserver-mcw7               1/1     Running            2                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-controller-manager-mcw7      1/1     Running            4 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-5pxmj             0/1     CrashLoopBackOff   121 (3m39s ago)   22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-flannel-ds-8gzfq             1/1     Running            0                 23h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-flannel-ds-s4qmj             0/1     CrashLoopBackOff   114 (2m54s ago)   21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-proxy-4lmsx                  1/1     Running            0                 22h   10.0.0.138   mcw8   <none>           <none>
kube-system   kube-proxy-c5zmm                  1/1     Running            0                 27h   10.0.0.137   mcw7   <none>           <none>
kube-system   kube-proxy-npmtv                  1/1     Running            0                 21h   10.0.0.139   mcw9   <none>           <none>
kube-system   kube-scheduler-mcw7               1/1     Running            3 (23h ago)       27h   10.0.0.137   mcw7   <none>           <none>
[machangwei@mcw7 ~]$  kubectl delete pod mcw01dep-nginx-5dd785954d-sb8ps #只删除pod
pod "mcw01dep-nginx-5dd785954d-sb8ps" deleted
[machangwei@mcw7 ~]$ kubectl get pod -o wide  #查看pod的后缀随机字符串换了,只换了最后一个随机字符串
NAME                              READY   STATUS    RESTARTS   AGE    IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          13h    10.244.0.5   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          13h    10.244.0.4   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          13h    10.244.0.5   mcw9   <none>           <none>
mcw01dep-nginx-5dd785954d-rxwbj   1/1     Running   0          2m8s   10.244.0.6   mcw9   <none>           <none>
mcw1httpd-app                     1/1     Running   0          17h    10.244.0.2   mcw8   <none>           <none>
nginx-deployment                  1/1     Running   0          14h    10.244.0.3   mcw8   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw01dep-nginx #这里查看也是换成新的了
default       mcw01dep-nginx-5dd785954d-rxwbj   1/1     Running            0               4m10s   10.244.0.6   mcw9   <none>           <none>


[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx #去node上查看容器,是新起的一个容器
ea798eb82a0a   nginx                                                "/docker-entrypoint.…"   About a minute ago   Up About a minute                    k8s_nginx_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
fad1d6c48689   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 About a minute ago   Up About a minute                    k8s_POD_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
[root@mcw9 ~]$ 

4、删除带有一个deployment,和一个副本的资源的pod,只做删除replicaset操作

副本名字没有改变,但是pod的名字(后缀字符串)改变了,说明删除的时候,副本没变化但是pod是重新生成,对应容器也是重新生成的

[machangwei@mcw7 ~]$ kubectl get deployment #删除mcw01dep-nginx的replicaset前查看deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           13h
mcw01dep-nginx   1/1     1            1           14h
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx #删除mcw01dep-nginx的replicaset前查看pod
mcw01dep-nginx-5dd785954d-rxwbj   1/1     Running   0          8m5s   10.244.0.6   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get replicaset|grep mcw01dep-nginx #删除mcw01dep-nginx的replicaset前查看replicaset
mcw01dep-nginx-5dd785954d   1         1         1       14h
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx 
mcw01dep-nginx-5dd785954d-rxwbj   1/1     Running   0          8m42s   10.244.0.6   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw01dep-nginx #删除mcw01dep-nginx的replicaset前查看pod和namespace
default       mcw01dep-nginx-5dd785954d-rxwbj   1/1     Running            0                 8m51s   10.244.0.6   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl delete replicaset mcw01dep-nginx-5dd785954d ##删除mcw01dep-nginx的replicaset
replicaset.apps "mcw01dep-nginx-5dd785954d" deleted
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx #查看删除mcw01dep-nginx的replicaset后pod名字改变
mcw01dep-nginx-5dd785954d-tf7lg   0/1     ContainerCreating   0          3s    <none>       mcw8   <none>           <none>
[machangwei@mcw7 ~]$ kubectl delete replicaset mcw01dep-nginx-5dd785954d
replicaset.apps "mcw01dep-nginx-5dd785954d" deleted
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-d94vk   1/1     Running   0          16s   10.244.0.7   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get replicaset|grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d   1         1         1       27s
[machangwei@mcw7 ~]$ 
[machangwei@mcw7 ~]$ kubectl get replicaset|grep mcw01dep-nginx  #查看删除mcw01dep-nginx的replicaset后replicaset名字没变
mcw01dep-nginx-5dd785954d   1         1         1       99s
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-d94vk   1/1     Running   0          3m31s   10.244.0.7   mcw9   <none>           <none>


删除前容器查看
[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx
ea798eb82a0a   nginx                                                "/docker-entrypoint.…"   8 minutes ago   Up 8 minutes                         k8s_nginx_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
fad1d6c48689   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 8 minutes ago   Up 8 minutes                         k8s_POD_mcw01dep-nginx-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
删除后容器好像是重新生成了新的,
[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx
3ffa7162b89e   nginx                                                "/docker-entrypoint.…"   40 seconds ago       Up 39 seconds                             k8s_nginx_mcw01dep-nginx-5dd785954d-d94vk_default_19b12206-9571-4d97-b9eb-dd34cd197642_0
9970f71f7f8f   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 46 seconds ago       Up 41 seconds                             k8s_POD_mcw01dep-ngin-5dd785954d-d94vk_default_19b12206-9571-4d97-b9eb-dd34cd197642_0
fad1d6c48689   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 10 minutes ago       Exited (0) 58 seconds ago                 k8s_POD_mcw01dep-ngin-5dd785954d-rxwbj_default_9822de3f-c2d8-4ca2-b221-cd47b360b725_0
[root@mcw9 ~]$ 

pod的容器应该就一个,但是发现容器还有一个pod是停掉的,怀疑是系统没有删除干净,手动删除一下,过了很长一段时间,并没有重新多次容器
[root@mcw9 ~]$ docker rm fad1
fad1
[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx
3ffa7162b89e   nginx                                                "/docker-entrypoint.…"   13 minutes ago   Up 13 minutes                        k8s_nginx_mcw01dep-nginx-5dd785954d-d94vk_default_19b12206-9571-4d97-b9eb-dd34cd197642_0
9970f71f7f8f   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 13 minutes ago   Up 13 minutes                        k8s_POD_mcw01dep-nginx-5dd785954d-d94vk_default_19b12206-9571-4d97-b9eb-dd34cd197642_0

5、删除带有一个deployment,和一个副本的资源的pod,只做删除deployment操作

会将deployment,副本信息,pod都删除掉,容器也慢慢都被删除
[machangwei@mcw7 ~]$ kubectl get deployment|grep mcw01dep-nginx
mcw01dep-nginx   1/1     1            1           15h
[machangwei@mcw7 ~]$ kubectl get replicaset|grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d   1         1         1       18m
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx
mcw01dep-nginx-5dd785954d-d94vk   1/1     Running   0          19m   10.244.0.7   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw01dep-nginx
default       mcw01dep-nginx-5dd785954d-d94vk   1/1     Running            0                 19m   10.244.0.7   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl delete deployment mcw01dep-nginx  #制作删除deployment,其它资源信息都被删除
deployment.apps "mcw01dep-nginx" deleted
[machangwei@mcw7 ~]$ kubectl get deployment|grep mcw01dep-nginx
[machangwei@mcw7 ~]$ kubectl get replicaset|grep mcw01dep-nginx
[machangwei@mcw7 ~]$ kubectl get pod -o wide|grep mcw01dep-nginx
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw01dep-nginx
[machangwei@mcw7 ~]$ 


[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx #查看容器,还有一个pod的已退出未删除,慢慢就被删除了
9970f71f7f8f   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 20 minutes ago       Exited (0) 51 seconds ago                 k8s_POD_mcw01dep-ngin-5dd785954d-d94vk_default_19b12206-9571-4d97-b9eb-dd34cd197642_0
[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx
[root@mcw9 ~]$ docker ps -a|grep mcw01dep-nginx
[root@mcw9 ~]$ 

6、删除有deployment,replicaset,pod的,有多个(三个)副本的,只删除replicaset

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           14h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       14h
[machangwei@mcw7 ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-258sz   1/1     Running   0          14h   10.244.0.5   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-q2785   1/1     Running   0          14h   10.244.0.4   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-wrq74   1/1     Running   0          14h   10.244.0.5   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw-depcluster
default       mcw-depcluster-547cfdc745-258sz   1/1     Running            0                 14h   10.244.0.5   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-q2785   1/1     Running            0                 14h   10.244.0.4   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-wrq74   1/1     Running            0                 14h   10.244.0.5   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ 
[machangwei@mcw7 ~]$ 
[machangwei@mcw7 ~]$ kubectl delete replicaset mcw-depcluster-547cfdc745  #删除副本
replicaset.apps "mcw-depcluster-547cfdc745" deleted
[machangwei@mcw7 ~]$ kubectl get deployment #删除副本后deployment节点并不是立即都ready了,有个过程
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   1/3     3            1           14h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         1       22s
[machangwei@mcw7 ~]$ kubectl get pod -o wide  #删除副本后,POD并不是都立即ready了,有个过程
NAME                              READY   STATUS              RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-2pxg9   1/1     Running             0          28s   10.244.0.7   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-42pzk   0/1     ContainerCreating   0          28s   <none>       mcw9   <none>           <none>
mcw-depcluster-547cfdc745-zhbqd   0/1     ContainerCreating   0          28s   <none>       mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw-depcluster
default       mcw-depcluster-547cfdc745-2pxg9   1/1     Running            0                 39s   10.244.0.7   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-42pzk   1/1     Running            0                 39s   10.244.0.8   mcw9   <none>           <none>
default       mcw-depcluster-547cfdc745-zhbqd   1/1     Running            0                 39s   10.244.0.9   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ dock



查看node1上的副本变化
容器被停退出,然后被删除,同时新的容器运行起来。这并不是立即就好的,要经历一个过程的时间。由上面的kubectl get pod -o wide可以看出副本的分布情况发生了改变
由mcw8 mcw8 mcw9 改为了mcw8 mcw9 mcw9的分布情况。
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
3fce98f64e54   nginx                                                "/docker-entrypoint.…"   14 hours ago    Up 14 hours                          k8s_nginx_mcw-depcluster-547cfdc745-q2785_default_46a70038-4e48-4776-bddb-3a29a82b6536_0
5157d0d4bf9e   nginx                                                "/docker-entrypoint.…"   14 hours ago    Up 14 hours                          k8s_nginx_mcw-depcluster-547cfdc745-258sz_default_a53916c0-7263-4761-9e34-8cdc46e7d3aa_0
19ab47a1e439   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago    Up 14 hours                          k8s_POD_mcw-depcluster-547cfdc745-q2785_default_46a70038-4e48-4776-bddb-3a29a82b6536_0
45830e9cf7e0   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago    Up 14 hours                          k8s_POD_mcw-depcluster-547cfdc745-258sz_default_a53916c0-7263-4761-9e34-8cdc46e7d3aa_0
[root@mcw8 ~]$ 
[root@mcw8 ~]$ 
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 6 seconds ago    Created                               k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
3fce98f64e54   nginx                                                "/docker-entrypoint.…"   14 hours ago     Exited (0) 6 seconds ago              k8s_nginx_mcw-depcluster-547cfdc745-q2785_default_46a70038-4e48-4776-bddb-3a29a82b6536_0
5157d0d4bf9e   nginx                                                "/docker-entrypoint.…"   14 hours ago     Exited (0) 6 seconds ago              k8s_nginx_mcw-depcluster-547cfdc745-258sz_default_a53916c0-7263-4761-9e34-8cdc46e7d3aa_0
19ab47a1e439   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago     Exited (0) 6 seconds ago              k8s_POD_mcw-depcluster-547cfdc745-q2785_default_46a70038-4e48-4776-bddb-3a29a82b6536_0
45830e9cf7e0   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago     Exited (0) 6 seconds ago              k8s_POD_mcw-depcluster-547cfdc745-258sz_default_a53916c0-7263-4761-9e34-8cdc46e7d3aa_0
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
1ff7e5277adb   nginx                                                "/docker-entrypoint.…"   40 seconds ago       Up 39 seconds                             k8s_nginx_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 48 seconds ago       Up 42 seconds                             k8s_POD_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
19ab47a1e439   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago         Exited (0) 48 seconds ago                 k8s_POD_mcw-depcluste-547cfdc745-q2785_default_46a70038-4e48-4776-bddb-3a29a82b6536_0
45830e9cf7e0   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 14 hours ago         Exited (0) 48 seconds ago                 k8s_POD_mcw-depcluste-547cfdc745-258sz_default_a53916c0-7263-4761-9e34-8cdc46e7d3aa_0
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
1ff7e5277adb   nginx                                                "/docker-entrypoint.…"   22 minutes ago   Up 22 minutes                         k8s_nginx_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 22 minutes ago   Up 22 minutes                         k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
[root@mcw8 ~]$ 

7、检验手动删除某个node上有deployment资源的容器,是否重新启动一个容器(有时间再验证只有pod资源的情况)

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           15h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       62m
[machangwei@mcw7 ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-2pxg9   1/1     Running   0          62m   10.244.0.7   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-42pzk   1/1     Running   0          62m   10.244.0.8   mcw9   <none>           <none>
mcw-depcluster-547cfdc745-zhbqd   1/1     Running   0          62m   10.244.0.9   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw-depcluster
default       mcw-depcluster-547cfdc745-2pxg9   1/1     Running            0               62m   10.244.0.7   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-42pzk   1/1     Running            0               62m   10.244.0.8   mcw9   <none>           <none>
default       mcw-depcluster-547cfdc745-zhbqd   1/1     Running            0               62m   10.244.0.9   mcw9   <none>           <none>


手动停掉mcw8 即node1上的docker容器,不停它对应的pod容器。结果是重新运行一个容器,POD容器不影响,起自己手动停掉的容器不删除。POD名字不改变
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
1ff7e5277adb   nginx                                                "/docker-entrypoint.…"   About an hour ago   Up About an hour                         k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 About an hour ago   Up About an hour                         k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
[root@mcw8 ~]$ docker stop 1ff7  #
1ff7
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
ab82eb57be69   nginx                                                "/docker-entrypoint.…"   2 minutes ago       Up 2 minutes                             k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1
1ff7e5277adb   nginx                                                "/docker-entrypoint.…"   About an hour ago   Exited (0) 2 minutes ago                 k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 About an hour ago   Up About an hour                         k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
[root@mcw8 ~]$ 


手动停掉mcw8上POD容器,与之对应的应用docker容器两个都重新启动一个容器进程,POD名字不改变,停掉的两个貌似也是不自动删除
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
ab82eb57be69   nginx                                                "/docker-entrypoint.…"   7 minutes ago       Up 7 minutes                             k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 About an hour ago   Up About an hour                         k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
[root@mcw8 ~]$ docker stop 1d95
1d95
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
8af312811838   nginx                                                "/docker-entrypoint.…"   3 minutes ago       Up 3 minutes                             k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_2
36b53506aa83   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 3 minutes ago       Up 3 minutes                             k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1
ab82eb57be69   nginx                                                "/docker-entrypoint.…"   11 minutes ago      Exited (0) 3 minutes ago                 k8s_nginx_mcw-depcluste-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1
1d95132abef6   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 About an hour ago   Exited (0) 3 minutes ago                 k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_0
[root@mcw8 ~]$ 


手动停掉容器和它对应的POD容器,马上生成新的容器。重新生成的容器,末尾数字就不是0了,是在叠加,这应该是初始重新生成次数为0,后面的数字代表重新生成容器次数。手动停掉的容器应该会成为垃圾数据,经检查,重新生成的容器的名字,貌似前面的信息不变,只修改末尾重新生成次数,这样除了可以看出第一次重新生成的次数,如果有退出未被清理的垃圾容器数据也能找出来,决定是否需要手动清除呢
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
8af312811838   nginx                                                "/docker-entrypoint.…"   44 minutes ago   Up 44 minutes                        k8s_nginx_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_2
36b53506aa83   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 44 minutes ago   Up 44 minutes                        k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1
[root@mcw8 ~]$ 
[root@mcw8 ~]$ 
[root@mcw8 ~]$ 
[root@mcw8 ~]$ 
[root@mcw8 ~]$ docker stop 36b535 8af312
36b535
8af312
[root@mcw8 ~]$ 
[root@mcw8 ~]$ 
[root@mcw8 ~]$  #重新生成的容器,末尾数字就不是0了,是在叠加,这应该是初始重新生成次数为0,后面的数字代表重新生成容器次数
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
ce51a4df7453   nginx                                                "/docker-entrypoint.…"   2 seconds ago    Up 1 second                          k8s_nginx_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_3
82aad84cf703   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 3 seconds ago    Up 2 seconds                         k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_2
8af312811838   nginx                                                "/docker-entrypoint.…"   44 minutes ago   Exited (0) 3 seconds ago             k8s_nginx_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_2
36b53506aa83   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 44 minutes ago   Exited (0) 3 seconds ago             k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_1


在主节点上查看容器的最高重新生成次数RESTARTS ,就是这个节点上的docker重启的
[machangwei@mcw7 ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS        AGE    IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-2pxg9   1/1     Running   3 (7m47s ago)   123m   10.244.0.9   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-42pzk   1/1     Running   0               123m   10.244.0.8   mcw9   <none>           <none>
mcw-depcluster-547cfdc745-zhbqd   1/1     Running   0               123m   10.244.0.9   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ 

 

8、删除有deployment,replicaset,pod的,有多个(三个)副本的,只删除deployment,相关资源就都被删除

[machangwei@mcw7 ~]$ kubectl get deployment
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
mcw-depcluster   3/3     3            3           16h
[machangwei@mcw7 ~]$ kubectl get replicaset
NAME                        DESIRED   CURRENT   READY   AGE
mcw-depcluster-547cfdc745   3         3         3       125m
[machangwei@mcw7 ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP           NODE   NOMINATED NODE   READINESS GATES
mcw-depcluster-547cfdc745-2pxg9   1/1     Running   3          125m   10.244.0.9   mcw8   <none>           <none>
mcw-depcluster-547cfdc745-42pzk   1/1     Running   0          125m   10.244.0.8   mcw9   <none>           <none>
mcw-depcluster-547cfdc745-zhbqd   1/1     Running   0          125m   10.244.0.9   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw-depcluster
default       mcw-depcluster-547cfdc745-2pxg9   1/1     Running            3                 125m   10.244.0.9   mcw8   <none>           <none>
default       mcw-depcluster-547cfdc745-42pzk   1/1     Running            0                 125m   10.244.0.8   mcw9   <none>           <none>
default       mcw-depcluster-547cfdc745-zhbqd   1/1     Running            0                 125m   10.244.0.9   mcw9   <none>           <none>
[machangwei@mcw7 ~]$ kubectl delete deployment  mcw-depcluster
deployment.apps "mcw-depcluster" deleted
[machangwei@mcw7 ~]$ kubectl get deployment
No resources found in default namespace.
[machangwei@mcw7 ~]$ kubectl get replicaset
No resources found in default namespace.
[machangwei@mcw7 ~]$ kubectl get pod -o wide
No resources found in default namespace.
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces -o wide|grep mcw-depcluster
[machangwei@mcw7 ~]$ 


[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
82aad84cf703   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 10 minutes ago   Exited (0) 43 seconds ago             k8s_POD_mcw-depcluster-547cfdc745-2pxg9_default_0519fe8f-aa37-4901-8f46-027acc89e57a_2
[root@mcw8 ~]$ docker ps -a|grep mcw-depcluster
[root@mcw8 ~]$ 


[root@mcw9 ~]$ docker ps -a|grep mcw-depcluster
06a0b4eee404   nginx                                                "/docker-entrypoint.…"   2 hours ago     Up 2 hours                           k8s_nginx_mcw-depcluster-547cfdc745-zhbqd_default_a1cc5f0f-788e-4a62-9766-6e1b4d3ea97e_0
614aa9ddae1f   nginx                                                "/docker-entrypoint.…"   2 hours ago     Up 2 hours                           k8s_nginx_mcw-depcluster-547cfdc745-42pzk_default_66aef584-c41e-448d-993d-af2868002af8_0
a6b0828a21b2   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 2 hours ago     Up 2 hours                           k8s_POD_mcw-depcluster-547cfdc745-zhbqd_default_a1cc5f0f-788e-4a62-9766-6e1b4d3ea97e_0
32cb8d125ed7   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 2 hours ago     Up 2 hours                           k8s_POD_mcw-depcluster-547cfdc745-42pzk_default_66aef584-c41e-448d-993d-af2868002af8_0
[root@mcw9 ~]$ 
[root@mcw9 ~]$ docker ps -a|grep mcw-depcluster
[root@mcw9 ~]$ 

 

查看node上的镜像情况,

[root@mcw8 ~]$ docker images
REPOSITORY                                           TAG       IMAGE ID       CREATED        SIZE
nginx                                                latest    605c77e624dd   5 days ago     141MB
httpd                                                latest    dabbfbe0c57b   2 weeks ago    144MB
registry.aliyuncs.com/google_containers/kube-proxy   v1.23.1   b46c42588d51   2 weeks ago    112MB
quay.io/coreos/flannel                               v0.15.1   e6ea68648f0c   7 weeks ago    69.5MB
rancher/mirrored-flannelcni-flannel-cni-plugin       v1.0.0    cd5235cd7dc2   2 months ago   9.03MB
registry.aliyuncs.com/google_containers/pause        3.6       6270bb605e12   4 months ago   683kB
nginx                                                1.7.9     84581e99d807   6 years ago    91.7MB
[root@mcw8 ~]$ 

 

@bigtitle@vs配置文件

原部署内容,存在问题的yml文件

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: web_server
      spec:
        containers:
          - name: mcwnginx
            image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ 

报错unable to recognize "mcwnginx.yml": no matches for kind "Deployment" in version "extensions/v1beta1"
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml #版本不对
error: unable to recognize "mcwnginx.yml": no matches for kind "Deployment" in version "extensions/v1beta1"

查看api版本

[machangwei@mcwk8s-master ~]$ kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta2
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
[machangwei@mcwk8s-master ~]$ 

[machangwei@mcwk8s-master ~]$ kubectl api-versions|grep events.k8s.io/v1beta1
events.k8s.io/v1beta1
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ head -1 mcwnginx.yml  #修改为其它的v1beta1也是不行
apiVersion: events.k8s.io/v1beta1
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
error: unable to recognize "mcwnginx.yml": no matches for kind "Deployment" in version "events.k8s.io/v1beta1"
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
error: unable to recognize "mcwnginx.yml": no matches for kind "Deployment" in version "discovery.k8s.io/v1beta1"

修改yaml文件,extensions/v1beta1修改为apps/v1

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
修改如下:

---
apiVersion: apps/v1
kind: Deployment

[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ head -1 mcwnginx.yml  #修改为apps/v1 ,再次部署成为其它方面存在问题了
apiVersion: apps/v1
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
error: error validating "mcwnginx.yml": error validating data: [ValidationError(Deployment.spec.template.metadata): unknown field "spec" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta, ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec]; if you choose to ignore these errors, turn validation off with --validate=false

报错missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec]

结果,根据提示:Deployment.spec下添加selector,selector下加了共三行,然后错误已经变成别的了。
将:
spec:
  replicas: 2
  template:

修改为:
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcwkube-site
  template:
  

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcwkube-site
  template:
    metadata:
      labels:
        app: web_server
      spec:
        containers:
          - name: mcwnginx
            image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
error: error validating "mcwnginx.yml": error validating data: ValidationError(Deployment.spec.template.metadata): unknown field "spec" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta; if you choose to ignore these errors, turn validation off with --validate=false

错误信息:error validating data: ValidationError(Deployment.spec.template.metadata): unknown field "spec" in io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta

格式不对。spec 是和metadata同级别的,我给弄到metadata下了,spec和metadata同是template下的
将:
  template:
    metadata:
      labels:
        app: web_server
      spec:
        containers:
          - name: mcwnginx
            image: nginx:1.7.9
修改为:
  template:
    metadata:
      labels:
        app: web_server
    spec:
      containers:
        - name: mcwnginx
          image: nginx:1.7.9

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcwkube-site
  template:
    metadata:
      labels:
        app: web_server
    spec:
      containers:
        - name: mcwnginx
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
The Deployment "mcwnginx-depoyment" is invalid: spec.template.metadata.labels: Invalid value: map[string]string{"app":"web_server"}: `selector` does not match template `labels`
[machangwei@mcwk8s-master ~]$ 

错误信息:spec.template.metadata.labels: Invalid value: map[string]string{"app":"web_server"}: `selector` does not match template `labels`

选择器不能匹配模板标签
将spec.selector.matchLabels.app和spec.template.labels.app改为同名字,即mcw_nginxServer

将:
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcwkube-site
  template:
    metadata:
      labels:
        app: web_server

修改为:
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer


[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcwNginxContainer
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
The Deployment "mcwnginx-depoyment" is invalid: spec.template.spec.containers[0].name: Invalid value: "mcwNginxContainer": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
[machangwei@mcwk8s-master ~]$ 

小写RFC 1123标签必须由小写字母数字字符或“-”组成,并且必须以字母数字字符开头和结尾(例如,“my-name”或“123- abc”,用于验证的正则表达式为“[a-z0-9]”([-a-z0-9]*[a-z0-9])?)

这里意思应该是容器[0]名字mcwNginxContainer的标签是无效的,即spec.template.labels.app,将两处都修改一下名字,不用下划线,可以用-

报错信息;Invalid value: "mcwNginxContainer": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character

将spec.selector.matchLabels.app.mcw_nginxServer和spec.template.labels.app.mcw_nginxServer 修改为mcw-nginxServer,依然报错,那还有其它的原因
将:
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer

修改为:
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw-nginxServer
  template:
    metadata:
      labels:
        app: mcw-nginxServer

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw-nginxServer
  template:
    metadata:
      labels:
        app: mcw-nginxServer
    spec:
      containers:
        - name: mcwNginxContainer
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
The Deployment "mcwnginx-depoyment" is invalid: spec.template.spec.containers[0].name: Invalid value: "mcwNginxContainer": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name',  or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?')
[machangwei@mcwk8s-master ~]$ 



再看报错信息:是不是因为spec.template.spec.containers.containers.name.mcwNginxContainer,容器名字包含大写的原因呢
小写RFC 1123标签必须由小写字母数字字符或“-”组成,并且必须以字母数字字符开头和结尾(例如,“my-name”或“123- abc”,用于验证的正则表达式为“[a-z0-9]”([-a-z0-9]*[a-z0-9])?)

修改成小写的试试。果然是成功的。也就是这里spec.template.spec.containers[0].name名字是不能大写,不能_线。也就是必须由小写字母数字字符或“-”组成,并且必须以字母数字字符开头和结尾
将:
    spec:
      containers:
        - name: mcwNginxContainer
修改为:
    spec:
      containers:
        - name: mcw-nginx-container

[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
deployment.apps/mcwnginx-depoyment created
[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw-nginxServer
  template:
    metadata:
      labels:
        app: mcw-nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ 


配置成功执行:
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   0/2     2            0           3m29s


7分钟才成功一个,我以为拉取镜像要失败呢,拉取镜像那里花时间不少
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                READY   STATUS              RESTARTS   AGE
mcwnginx-depoyment-fd9c85d4-bg5wm   0/1     ContainerCreating   0          7m43s
mcwnginx-depoyment-fd9c85d4-dx4wc   1/1     Running             0          7m43s

两个副本都成功了 
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-fd9c85d4-bg5wm   1/1     Running   0          8m43s
mcwnginx-depoyment-fd9c85d4-dx4wc   1/1     Running   0          8m43s
[machangwei@mcwk8s-master ~]$ 

部署好后查看信息:

[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/2     2            2           9m17s
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                          DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-fd9c85d4   2         2         2       9m43s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-fd9c85d4-bg5wm   1/1     Running   0          9m57s
mcwnginx-depoyment-fd9c85d4-dx4wc   1/1     Running   0          9m57s
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-fd9c85d4-bg5wm   1/1     Running   0          10m   10.244.0.5   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-fd9c85d4-dx4wc   1/1     Running   0          10m   10.244.0.4   mcwk8s-node2   <none>           <none>

节点上去查看容器
[root@mcwk8s-node1 ~]$ docker ps|grep  nginx
8bf7d48a56aa   nginx                                                "nginx -g 'daemon of…"   2 minutes ago    Up 2 minutes             k8s_mcw-nginx-container_mcwnginx-depoyment-fd9c85d4-bg5wm_default_fc7b1656-bbed-4212-875d-0cd71db9c9cc_0
e87f6b6be617   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 11 minutes ago   Up 9 minutes             k8s_POD_mcwnginx-depoyment-fd9c85d4-bg5wm_default_fc7b1656-bbed-4212-875d-0cd71db9c9cc_0
[root@mcwk8s-node1 ~]$ 

使用部署的yml文件删除容器。不太清楚删除的原理

不过kubectl delete -f mcwnginx.yml等同于执行kubectl delete deployment [deployment name]
可以猜测,是否是读取yml文件中的[deployment name],然后执行kubectl delete deployment [deployment name]呢?
[machangwei@mcwk8s-master ~]$ kubectl delete -f mcwnginx.yml 
deployment.apps "mcwnginx-depoyment" deleted
[machangwei@mcwk8s-master ~]$ kubectl get deployment
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl get pod
No resources found in default namespace.



去节点上查看docker已经没了
[root@mcwk8s-node1 ~]$ docker ps|grep  nginx
[root@mcwk8s-node1 ~]$ docker ps -a|grep nginx
e87f6b6be617   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 13 minutes ago   Exited (0) 46 seconds ago             k8s_POD_mcwnginx-depoyment-fd9c85d4-bg5wm_default_fc7b1656-bbed-4212-875d-0cd71db9c9cc_0
[root@mcwk8s-node1 ~]$ docker ps -a|grep nginx
[root@mcwk8s-node1 ~]$ 

验证spec.selector.matchLabels和spec.template.labels下的app使用_被限制嘛?结果是app命名不受大写字母和_的限制,containers.name受限制

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw-nginxServer
  template:
    metadata:
      labels:
        app: mcw-nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
deployment.apps/mcwnginx-depoyment created
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/2     2            2           23s
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   2         2         2       29s
[machangwei@mcwk8s-master ~]$  kubectl get pod
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-qdz26   1/1     Running   0          35s
mcwnginx-depoyment-7bb9d7f846-xrrjb   1/1     Running   0          35s
[machangwei@mcwk8s-master ~]$ 

查看两个pod详情描述,以及yml文件中name的命名在哪里使用

可知两个都有相同的lables,就是我在yml文件中设置的labels标签名字,
文字中定义的metadata.name.mcwnginx-depoyment是Deployment的名字。
文件中定义的spec.selector.matchLabels.app和spec.template.labels.app就是labels的app名字,k8s根据相同的标签名字,将请求调度到同组的POD(同组pod标签名一致)
[machangwei@mcwk8s-master ~]$ kubectl describe pod mcwnginx-depoyment-7bb9d7f846-qdz26 
Name:         mcwnginx-depoyment-7bb9d7f846-qdz26
Namespace:    default
Priority:     0
Node:         mcwk8s-node2/10.0.0.142
Start Time:   Sun, 09 Jan 2022 18:59:05 +0800
Labels:       app=mcw_nginxServer
              pod-template-hash=7bb9d7f846
Annotations:  <none>
Status:       Running
IP:           10.244.0.5
IPs:
  IP:           10.244.0.5
Controlled By:  ReplicaSet/mcwnginx-depoyment-7bb9d7f846
Containers:


[machangwei@mcwk8s-master ~]$ kubectl describe pod mcwnginx-depoyment-7bb9d7f846-xrrjb
Name:         mcwnginx-depoyment-7bb9d7f846-xrrjb
Namespace:    default
Priority:     0
Node:         mcwk8s-node1/10.0.0.141
Start Time:   Sun, 09 Jan 2022 18:59:03 +0800
Labels:       app=mcw_nginxServer
              pod-template-hash=7bb9d7f846
Annotations:  <none>
Status:       Running
IP:           10.244.0.6
IPs:
  IP:           10.244.0.6
Controlled By:  ReplicaSet/mcwnginx-depoyment-7bb9d7f846
Containers:

k8s部署Nginx的yml文件,经过校验成功运行的

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9

k8s部署Nginx的yml文件的配置文件讲解:

apiVersion: apps/v1 #当前配置合适的版本
kind: Deployment #要创建的资源类型,这里是Deployment
metadata: #该资源的元数据,name是必须的元数据项,就是定义的Deployment名字
  name: mcwnginx-depoyment
spec:  #该Deployment的规格说明
  replicas: 2  #指明副本数量
  selector:  #指明选择器,下面模板下元数据标签下有标签名字,应该是可以设置多个标签把,反正这里就需要指定使用哪个标签了,
    matchLabels: #不指明的话会报错。不知道如果下面不用标签,那么这里也用不上选择器,这样是否能成功创建POD呢?
     app: mcw_nginxServer #两个标签名字要一致,不一致报错了。两个标签名字可以使用_和大写字符命名
  template:   #定义Pod的模板,这是配置文件的重要部分
    metadata: #定义Pod的元数据,至少要定义一个label,label的key和value可以任意指定
      labels:
        app: mcw_nginxServer
    spec:  #描述Pod的规格,此部分定义Pod中每一个容器的属性,name和image是必须的
      containers: #配置容器
        - name: mcw-nginx-container #指定生成的容器的名字(一般都是生成包含这个名字的容器名,因为容器名生成会添加固定前缀和指定格式的随机字符串的后缀)
          image: nginx:1.7.9  #指定创建容器使用的镜像



一级下有
apiVersion api版本
kind 资源类型
metadata 资源元数据(比如应用名字)
spec 资源的规格


spec资源的规格下的一级有
replicas 副本数量
selector 使用哪个标签来调度用
template Pod的模板

template Pod的模板一级下有:
metadata 包含容器含有的标签
spec 描述Pod的规格,之前是描述资源的规格,级别上不同

template下 spec的下有
containers 可以指定容器可以包含的容器名字符串,容器使用的镜像

Specification 美[ˌspesɪfɪˈkeɪʃn]
名词 n.
1.载明;详述[U][(+of)]
2.规格;明细单;详细计划书[P][(+for)]
3.(产品等的)说明书;说明书中载明的项目[C]
网络释义
1.规格 规格、规范
2.规范
3.详细说明,产品说明书

是否能将两个labels去掉的 验证?答案是不能

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  template:
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
error: error validating "mcwnginx.yml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false
[machangwei@mcwk8s-master ~]$ 

一级下有
apiVersion
kind
metadata
spec

 

伸缩

 修改前檢查,初始两个副本,node1和node2分别有一个

[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/2     2            2           3h40m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   2         2         2       3h41m
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          3h41m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          3h41m
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                                  READY   STATUS    RESTARTS   AGE     IP           NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          3h42m   10.244.0.6   mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          3h42m   10.244.0.7   mcwk8s-node1   <none>           <none>
[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml #修改前配置
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 2
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ grep replicas mcwnginx.yml   #由2修改为5
  replicas: 5
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml  #还是指定这个yml文件部署
deployment.apps/mcwnginx-depoyment configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment  #首先应用资源已经改为5了,三个没有用ready
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/5     2            2           3h44m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset  #其它两个过挺长一段时间才有反应,也有可能 电脑太卡影响创建速度了吧
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   2         2         2       3h44m
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          3h44m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          3h44m

过一段时间查看,其它两个资源也变成5了,新的ready一个都没有

[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/5     5            2           3h49m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         2       3h49m
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                  READY   STATUS              RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running             0          3h49m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running             0          3h49m
mcwnginx-depoyment-7bb9d7f846-n67cw   0/1     ContainerCreating   0          3m30s
mcwnginx-depoyment-7bb9d7f846-nj4dz   0/1     ContainerCreating   0          3m30s
mcwnginx-depoyment-7bb9d7f846-v88gg   0/1     ContainerCreating   0          3m30s

再过一段时间,首先是pod开始有新的ready的了,但是deployment和replicaset都没有ready的

[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   2/5     5            2           3h55m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         2       3h55m
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                  READY   STATUS             RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running            0          3h56m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running            0          3h56m
mcwnginx-depoyment-7bb9d7f846-n67cw   1/1     Running            0          9m41s
mcwnginx-depoyment-7bb9d7f846-nj4dz   1/1     Running            0          9m41s
mcwnginx-depoyment-7bb9d7f846-v88gg   0/1     ImagePullBackOff   0          9m4

再过一段时间,所有的pod都ready了,deployment和replicaset的ready才开始变多。这个过程真得是极其缓慢啊,不知到慢在哪里了。可能是电脑不行

[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   4/5     5            4           4h
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         4       4h
[machangwei@mcwk8s-master ~]$ kubectl get pod 
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          4h
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          4h
mcwnginx-depoyment-7bb9d7f846-n67cw   1/1     Running   0          13m
mcwnginx-depoyment-7bb9d7f846-nj4dz   1/1     Running   0          13m
mcwnginx-depoyment-7bb9d7f846-v88gg   1/1     Running   0          13m

新增副本查看deployment和replicaset详情

[machangwei@mcwk8s-master ~]$ kubectl describe deployment  mcwnginx-depoyment
Name:                   mcwnginx-depoyment
Namespace:              default
CreationTimestamp:      Sun, 09 Jan 2022 21:01:41 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=mcw_nginxServer
Replicas:               5 desired | 5 updated | 5 total | 4 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=mcw_nginxServer
  Containers:
   mcw-nginx-container:
    Image:        nginx:1.7.9
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   mcwnginx-depoyment-7bb9d7f846 (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  17m   deployment-controller  Scaled up replica set mcwnginx-depoyment-7bb9d7f846 to 5
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   4/5     5            4           4h4m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         4       4h4m
[machangwei@mcwk8s-master ~]$ kubectl describe replicaset
Name:           mcwnginx-depoyment-7bb9d7f846
Namespace:      default
Selector:       app=mcw_nginxServer,pod-template-hash=7bb9d7f846
Labels:         app=mcw_nginxServer
                pod-template-hash=7bb9d7f846
Annotations:    deployment.kubernetes.io/desired-replicas: 5
                deployment.kubernetes.io/max-replicas: 7
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/mcwnginx-depoyment
Replicas:       5 current / 5 desired
Pods Status:    5 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=mcw_nginxServer
           pod-template-hash=7bb9d7f846
  Containers:
   mcw-nginx-container:
    Image:        nginx:1.7.9
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  17m   replicaset-controller  Created pod: mcwnginx-depoyment-7bb9d7f846-v88gg
  Normal  SuccessfulCreate  17m   replicaset-controller  Created pod: mcwnginx-depoyment-7bb9d7f846-n67cw
  Normal  SuccessfulCreate  17m   replicaset-controller  Created pod: mcwnginx-depoyment-7bb9d7f846-nj4dz

很久之后,新增的三个都好了

[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         5       4h4m
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   5/5     5            5           4h4m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   5         5         5       4h5m
[machangwei@mcwk8s-master ~]$ kubectl get pod 
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          4h5m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          4h5m
mcwnginx-depoyment-7bb9d7f846-n67cw   1/1     Running   0          18m
mcwnginx-depoyment-7bb9d7f846-nj4dz   1/1     Running   0          18m
mcwnginx-depoyment-7bb9d7f846-v88gg   1/1     Running   0          18m

其中有一个分到master上了,是否默认是不会将pod调度到master上暂时没有验证过,这个k8s集群我部署后是开启了master也是可以当做NODE来使用的

[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide|grep master
mcwnginx-depoyment-7bb9d7f846-v88gg   1/1     Running   0          21m    10.244.0.5   mcwk8s-master   <none>           <none>

master可以当做node使用命令:

kubectl taint node k8s-master node-role.kubernetes.io/master-
恢复Master Only状态,执行如下命令
kubectl  taint node k8s-master node-role.kubernetes.io/master="":NoSchedule

修改副本数量,为减少数量,副本数由5改为3再执行部署,发现三种资源都很快少了两个,貌似比创建快很多。剩三个,其中一个是在master节点上

[machangwei@mcwk8s-master ~]$ cat mcwnginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 5
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ vim mcwnginx.yml 
[machangwei@mcwk8s-master ~]$ grep replicas mcwnginx.yml 
  replicas: 3
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwnginx.yml 
deployment.apps/mcwnginx-depoyment configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   3/3     3            3           4h18m
[machangwei@mcwk8s-master ~]$ kubectl get replicaset
NAME                            DESIRED   CURRENT   READY   AGE
mcwnginx-depoyment-7bb9d7f846   3         3         3       4h18m
[machangwei@mcwk8s-master ~]$ kubectl get pod 
NAME                                  READY   STATUS    RESTARTS   AGE
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          4h18m
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          4h18m
mcwnginx-depoyment-7bb9d7f846-v88gg   1/1     Running   0          32m
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                                  READY   STATUS    RESTARTS   AGE     IP           NODE            NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-7bb9d7f846-5q6n9   1/1     Running   0          4h18m   10.244.0.6   mcwk8s-node2    <none>           <none>
mcwnginx-depoyment-7bb9d7f846-hcz45   1/1     Running   0          4h18m   10.244.0.7   mcwk8s-node1    <none>           <none>
mcwnginx-depoyment-7bb9d7f846-v88gg   1/1     Running   0          32m     10.244.0.5   mcwk8s-master   <none>           <none>
[machangwei@mcwk8s-master ~]$ 

回头再去验证主节点能与不能作为node节点的问题

failover

主节点上查看,三个running的pod,其中两个在node2上

[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                              READY   STATUS    RESTARTS   AGE    IP           NODE           NOMINATED NODE   READINESS GATES
mcw01dep-nginx-5dd785954d-6wddp   1/1     Running   0          8m9s   10.244.0.4   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-kh46h   1/1     Running   0          8m9s   10.244.0.3   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-r9ps2   1/1     Running   0          8m9s   10.244.0.5   mcwk8s-node1   <none>           <none>

将node2关机,模拟node2故障。这里使用命令没有关闭虚拟机成功,而是直接把虚拟机挂起

[root@mcwk8s-node2 ~]$ halt -h
PolicyKit daemon disconnected from the bus.
We are no longer a registered authentication agent.

Connection closed by foreign host.

Disconnected from remote host(mcw06) at 10:18:28.

Type `help' to learn how to use Xshell prompt.
[c:\~]$ 

master上查看node2已经notready了,但是pod还是在running

[machangwei@mcwk8s-master ~]$ kubectl get nodes
NAME            STATUS     ROLES                  AGE     VERSION
mcwk8s-master   Ready      control-plane,master   4d11h   v1.23.1
mcwk8s-node1    Ready      <none>                 4d10h   v1.23.1
mcwk8s-node2    NotReady   <none>                 4d10h   v1.23.1

过了一两分钟查看pod,发现node2上的两个pod已经terminating了,在其它结点(node1)上重新创建新的pod

[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                              READY   STATUS              RESTARTS   AGE   IP           NODE           NOMINATED NODE   READINESS GATES
mcw01dep-nginx-5dd785954d-65rh2   0/1     ContainerCreating   0          15s   <none>       mcwk8s-node1   <none>           <none>
mcw01dep-nginx-5dd785954d-6wddp   1/1     Terminating         0          14m   10.244.0.4   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-kh46h   1/1     Terminating         0          14m   10.244.0.3   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-nk2tt   0/1     ContainerCreating   0          15s   <none>       mcwk8s-node1   <none>           <none>
mcw01dep-nginx-5dd785954d-r9ps2   1/1     Running   

现在三个pod都已经在运行了。但是node2的两个没有被删除,还是在终止状态

[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                              READY   STATUS        RESTARTS   AGE     IP           NODE           NOMINATED NODE   READINESS GATES
mcw01dep-nginx-5dd785954d-65rh2   1/1     Running       0          2m20s   10.244.0.7   mcwk8s-node1   <none>           <none>
mcw01dep-nginx-5dd785954d-6wddp   1/1     Terminating   0          16m     10.244.0.4   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-kh46h   1/1     Terminating   0          16m     10.244.0.3   mcwk8s-node2   <none>           <none>
mcw01dep-nginx-5dd785954d-nk2tt   1/1     Running       0          2m20s   10.244.0.6   mcwk8s-node1   <none>           <none>
mcw01dep-nginx-5dd785954d-r9ps2   1/1     Running       0          16m     10.244.0.5   mcwk8s-node1   <none>           <none>

node2貌似出了点问题。取消挂机不行,重启之后,容器没有自动起来,手动把结点必备的容器起来,在主节点上查看,还是notready ,把容器都删掉也没有看到新的容器起来

过很长一段时间还是没有成功加入到集群。

[machangwei@mcwk8s-master ~]$ kubectl get nodes
NAME            STATUS     ROLES                  AGE     VERSION
mcwk8s-master   Ready      control-plane,master   4d11h   v1.23.1
mcwk8s-node1    Ready      <none>                 4d11h   v1.23.1
mcwk8s-node2    NotReady   <none>                 4d11h   v1.23.1

那我直接重新加入集群把,让node2

[root@mcwk8s-node2 ~]$ docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@mcwk8s-node2 ~]$ kubeadm join 10.0.0.4:6443 --token zxnhj1.tholf3rugaekesfm \
> --discovery-token-ca-cert-hash sha256:06d4fd66aaaadf5f28d0d521f5538d8b6e68170cfe87db07f72d377c3dc31ccf 
[preflight] Running pre-flight checks
    [WARNING Swap]: swap is enabled; production deployments should disable swap unless testing the NodeSwap feature gate of the kubelet
    [WARNING Hostname]: hostname "mcwk8s-node2" could not be reached
    [WARNING Hostname]: hostname "mcwk8s-node2": lookup mcwk8s-node2 on 223.5.5.5:53: no such host
error execution phase preflight: [preflight] Some fatal errors occurred:
    [ERROR FileAvailable--etc-kubernetes-kubelet.conf]: /etc/kubernetes/kubelet.conf already exists
    [ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`
To see the stack trace of this error execute with --v=5 or higher
[root@mcwk8s-node2 ~]$ 

卡在无法重新加入集群

[root@mcwk8s-node2 ~]$ kubeadm join 10.0.0.4:6443 --token zxnhj1.tholf3rugaekesfm --discovery-token-ca-cert-hash sha256:06d4fd66aaaadf5f28d0d521f5538d8b6e68170cfe87db07f72d377c3dc31ccf 
[preflight] Running pre-flight checks
    [WARNING Hostname]: hostname "mcwk8s-node2" could not be reached
    [WARNING Hostname]: hostname "mcwk8s-node2": lookup mcwk8s-node2 on 223.5.5.5:53: no such host

主节点那两个node2上的pod自己没了,但是node2还是没有成功加入集群,卡住不动
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
mcw01dep-nginx-5dd785954d-65rh2   1/1     Running   0          33m
mcw01dep-nginx-5dd785954d-nk2tt   1/1     Running   0          33m
mcw01dep-nginx-5dd785954d-r9ps2   1/1     Running   0          48m

无法加入集群,token失效的问题

[root@mcwk8s-node2 ~]$ kubeadm join 10.0.0.4:6443 --token zxnhj1.tholf3rugaekesfm --discovery-token-ca-cert-hash sha256:06d4fd66aaaadf5f28d0d521f5538d8b6e68170cfe87db07f72d377c3dc31ccf  --v=5
I0117 11:12:15.632910    5736 token.go:80] [discovery] Created cluster-info discovery client, requesting info from "10.0.0.4:6443"
I0117 11:12:15.660800    5736 token.go:223] [discovery] The cluster-info ConfigMap does not yet contain a JWS signature for token ID "zxnhj1", will try again

 

去主节点上查看token,查看是否是令牌超时问题。
下面命令没有显示任何内容,则问题是令牌超时。
[root@mcwk8s-master ~]$ kubeadm token list
[root@mcwk8s-master ~]$ 


为了解决问题,可以执行以下操作
[root@mcwk8s-master ~]$ kubeadm token create
myf6nc.tab4y18yd7ykiwsf

如果kubeadm token list再次运行,可以看到以下结果

[root@mcwk8s-master ~]$ kubeadm token list
TOKEN                     TTL         EXPIRES                USAGES                   DESCRIPTION                                                EXTRA GROUPS
myf6nc.tab4y18yd7ykiwsf   23h         2022-01-18T03:18:11Z   authentication,signing   <none>                                                     system:bootstrappers:kubeadm:default-node-token

如上所见,此令牌具有 TTL,因此 23 小时后此令牌已过期。

因此,在 TTL 到来之前,就可以使用上述命令的结果令牌加入另一个节点。

编辑命名,将令牌替换一下就行。就成功加入集群了
kubeadm join 10.0.0.4:6443 --token myf6nc.tab4y18yd7ykiwsf --discovery-token-ca-cert-hash sha256:06d4fd66aaaadf5f28d0d521f5538d8b6e68170cfe87db07f72d377c3dc31ccf

查看结点,已经准备好了
[machangwei@mcwk8s-master ~]$ kubectl get nodes
NAME            STATUS   ROLES                  AGE     VERSION
mcwk8s-master   Ready    control-plane,master   4d12h   v1.23.1
mcwk8s-node1    Ready    <none>                 4d11h   v1.23.1
mcwk8s-node2    Ready    <none>                 116s    v1.23.1

 

由上可知,如果结点出现问题,结点上的pod会在其它结点上重新创建。

 

用label控制Pod的位置

 

给节点添加标签,然后在模板规格里,与容器平行,添加节点选择器,指定添加标签的节点,那么部署的pod都会部署到这个结点上。

疑问,以后验证:
1、是否标签的键值可以随意指定,然后yml添加对应的标签键值对都行?
2、是否可以给多个节点添加同样的标签,这样部署多个副本时,是不是可以对有同样标签的节点都分配部署呢?

kubectl label node mcwk8s-node1 disktype=ssd #添加标签
kubectl label node mcwk8s-node1 disktype- #删除标签disktype,成功删除

操作前的环境

[machangwei@mcwk8s-master ~]$ kubectl get pod  --show-labels #
NAME                               READY   STATUS    RESTARTS   AGE   LABELS
mcw01dep-nginx1-69bc6f5957-7s7zx   1/1     Running   0          10h   app=mcw01dep-nginx1,pod-template-hash=69bc6f5957
mcw01dep-nginx1-69bc6f5957-lzpdv   1/1     Running   0          10h   app=mcw01dep-nginx1,pod-template-hash=69bc6f5957
mcw01dep-nginx1-69bc6f5957-md2jk   1/1     Running   0          10h   app=mcw01dep-nginx1,pod-template-hash=69bc6f5957
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get node --show-labels #没有标签
NAME            STATUS   ROLES                  AGE     VERSION   LABELS
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
mcwk8s-node1    Ready    <none>                 4d22h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node1,kubernetes.io/os=linux
mcwk8s-node2    Ready    <none>                 10h     v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node2,kubernetes.io/os=linux
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get nodes

NAME            STATUS   ROLES                  AGE     VERSION
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1
mcwk8s-node1    Ready    <none>                 4d22h   v1.23.1
mcwk8s-node2    Ready    <none>                 10h     v1.23.1
[machangwei@mcwk8s-master ~]$ 

给node添加标签label

给node添加标签label
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node1 disktype=ssd  #添加标签,disktype=ssd 
node/mcwk8s-node1 labeled



查看标签
[machangwei@mcwk8s-master ~]$ kubectl get node
NAME            STATUS   ROLES                  AGE     VERSION
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1
mcwk8s-node1    Ready    <none>                 4d22h   v1.23.1
mcwk8s-node2    Ready    <none>                 10h     v1.23.1
[machangwei@mcwk8s-master ~]$ kubectl get node --show-labels  #可以看到刚刚给node1添加的disktype=ssd标签,成功添加
NAME            STATUS   ROLES                  AGE     VERSION   LABELS
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
mcwk8s-node1    Ready    <none>                 4d22h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node1,kubernetes.io/os=linux
mcwk8s-node2    Ready    <none>                 10h     v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node2,kubernetes.io/os=linux


删除之前的环境
[machangwei@mcwk8s-master ~]$ kubectl get deployment
k^HNAME              READY   UP-TO-DATE   AVAILABLE   AGE
mcw01dep-nginx1   3/3     3            3           10h
[machangwei@mcwk8s-master ~]$ kubectl delete deployment mcw01dep-nginx1
deployment.apps "mcw01dep-nginx1" deleted
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get deployment
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl get pod
No resources found in default namespace.


以前写的yml文件,修改前的文件
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 3
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
          

修改后的文件,如下。修改内容为:
1、将副本数增加到6
2、在模板规则下,与容器平行,添加如下结点选择器,指定标签键值对,这个键值对是给node1添加的标签
      nodeSelector:
          disktype: ssd

[machangwei@mcwk8s-master ~]$ vim mcwNginx.yml
[machangwei@mcwk8s-master ~]$ cat mcwNginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 6
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
      nodeSelector:
          disktype: ssd

          
查看添加标签后,部署情况
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwNginx.yml 
deployment.apps/mcwnginx-depoyment created
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   0/6     6            0           34s
[machangwei@mcwk8s-master ~]$ kubectl get pod
NAME                                  READY   STATUS              RESTARTS   AGE
mcwnginx-depoyment-6c74986b7b-74j82   0/1     ContainerCreating   0          37s
mcwnginx-depoyment-6c74986b7b-nbv4p   0/1     ContainerCreating   0          38s
mcwnginx-depoyment-6c74986b7b-tzl6t   0/1     ContainerCreating   0          38s
mcwnginx-depoyment-6c74986b7b-v8m6r   0/1     ContainerCreating   0          38s
mcwnginx-depoyment-6c74986b7b-vm54t   0/1     ContainerCreating   0          37s
mcwnginx-depoyment-6c74986b7b-vn6nr   0/1     ContainerCreating   0          37s

过会儿再次查看
[machangwei@mcwk8s-master ~]$ kubectl get deployment #6个副本都ready了
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   6/6     6            6           10m
[machangwei@mcwk8s-master ~]$ kubectl get node --show-labels  #查看节点1上是有disktype=ssd标签的
NAME            STATUS   ROLES                  AGE     VERSION   LABELS
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
mcwk8s-node1    Ready    <none>                 4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node1,kubernetes.io/os=linux
mcwk8s-node2    Ready    <none>                 11h     v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node2,kubernetes.io/os=linux
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #查看所有的pod都部署到节点1上,符合预期
NAME                                  READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-6c74986b7b-74j82   1/1     Running   0          11m   10.244.0.10   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-nbv4p   1/1     Running   0          11m   10.244.0.13   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-tzl6t   1/1     Running   0          11m   10.244.0.9    mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-v8m6r   1/1     Running   0          11m   10.244.0.12   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vm54t   1/1     Running   0          11m   10.244.0.11   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vn6nr   1/1     Running   0          11m   10.244.0.14   mcwk8s-node1   <none>           <none>
[machangwei@mcwk8s-master ~]$ 

综上,给节点添加标签,然后在模板规格里,与容器平行,添加节点选择器,指定添加标签的节点,那么部署的pod都会部署到这个结点上。

疑问,以后验证:
1、是否标签的键值可以随意指定,然后yml添加对应的标签键值对都行?
2、是否可以给多个节点添加同样的标签,这样部署多个副本时,是不是可以对有同样标签的节点都分配部署呢?

 

删除label 

[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node1 disktype-  #删除标签disktype,成功删除
node/mcwk8s-node1 unlabeled
[machangwei@mcwk8s-master ~]$ kubectl get node --show-labels  #查看已经删除标签
NAME            STATUS   ROLES                  AGE     VERSION   LABELS
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
mcwk8s-node1    Ready    <none>                 4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node1,kubernetes.io/os=linux
mcwk8s-node2    Ready    <none>                 11h     v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node2,kubernetes.io/os=linux

[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  #删除标签后,pod并没有改变,还在原来的节点,不会自动重新部署
NAME                                  READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-6c74986b7b-74j82   1/1     Running   0          20m   10.244.0.10   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-nbv4p   1/1     Running   0          20m   10.244.0.13   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-tzl6t   1/1     Running   0          20m   10.244.0.9    mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-v8m6r   1/1     Running   0          20m   10.244.0.12   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vm54t   1/1     Running   0          20m   10.244.0.11   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vn6nr   1/1     Running   0          20m   10.244.0.14   mcwk8s-node1   <none>           <none>
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ vim mcwNginx.yml 
[machangwei@mcwk8s-master ~]$ cat mcwNginx.yml #要想重新分布pod,需要把最后两行带有节点选择器的配置去掉,然后重新手动部署
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 6
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwNginx.yml 
deployment.apps/mcwnginx-depoyment configured
[machangwei@mcwk8s-master ~]$ kubectl get deployment  
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-depoyment   5/6     5            5           22m
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide #重新手动部署后,会删除之前的Pod并调度和运行新的Pod。
NAME                                  READY   STATUS              RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-6c74986b7b-nbv4p   1/1     Running             0          22m   10.244.0.13   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vm54t   1/1     Running             0          22m   10.244.0.11   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vn6nr   1/1     Running             0          22m   10.244.0.14   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-8hgxs   0/1     ContainerCreating   0          11s   <none>        mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-9wj28   1/1     Running             0          13s   10.244.0.16   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b2cb4   0/1     ContainerCreating   0          19s   <none>        mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b9d2k   1/1     Running             0          19s   10.244.0.15   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-p86r4   0/1     ContainerCreating   0          19s   <none>        mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                                  READY   STATUS              RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-6c74986b7b-nbv4p   1/1     Running             0          22m   10.244.0.13   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vm54t   1/1     Running             0          22m   10.244.0.11   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-6c74986b7b-vn6nr   1/1     Running             0          22m   10.244.0.14   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-8hgxs   0/1     ContainerCreating   0          54s   <none>        mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-9wj28   1/1     Running             0          56s   10.244.0.16   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b2cb4   0/1     ContainerCreating   0          62s   <none>        mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b9d2k   1/1     Running             0          62s   10.244.0.15   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-p86r4   0/1     ContainerCreating   0          62s   <none>        mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 


[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  #Pod重新均匀分布
NAME                                  READY   STATUS    RESTARTS   AGE     IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-depoyment-7bb9d7f846-6rfbk   1/1     Running   0          5m27s   10.244.0.17   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-8hgxs   1/1     Running   0          7m24s   10.244.0.4    mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-9wj28   1/1     Running   0          7m26s   10.244.0.16   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b2cb4   1/1     Running   0          7m32s   10.244.0.6    mcwk8s-node2   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-b9d2k   1/1     Running   0          7m32s   10.244.0.15   mcwk8s-node1   <none>           <none>
mcwnginx-depoyment-7bb9d7f846-p86r4   1/1     Running   0          7m32s   10.244.0.5    mcwk8s-node2   <none>           <none>

验证标签是随意指定键值对的。

[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl delete deployment mcwnginx-depoyment
deployment.apps "mcwnginx-depoyment" deleted
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ cat mcwNginx.yml #查看操作前的配置
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-depoyment
spec:
  replicas: 6
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
[machangwei@mcwk8s-master ~]$ vim mcwNginx.yml 
[machangwei@mcwk8s-master ~]$ cat mcwNginx.yml #查看修改后的配置,添加了我名字为标签值
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-deployment
spec:
  replicas: 6
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
      nodeSelector:
          myName: machangwei
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node2 myName=machangwei  #给节点2添加标签myName=machangwei 
node/mcwk8s-node2 labeled
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwNginx.yml  #部署
deployment.apps/mcwnginx-deployment created
[machangwei@mcwk8s-master ~]$ kubectl get node --show-labels  #查看标签,节点2已经添加了键值对
NAME            STATUS   ROLES                  AGE     VERSION   LABELS
mcwk8s-master   Ready    control-plane,master   4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/control-plane=,node-role.kubernetes.io/master=,node.kubernetes.io/exclude-from-external-load-balancers=
mcwk8s-node1    Ready    <none>                 4d23h   v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node1,kubernetes.io/os=linux
mcwk8s-node2    Ready    <none>                 11h     v1.23.1   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=mcwk8s-node2,kubernetes.io/os=linux,myName=machangwei
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  #查看部署结果,结果如预期都部署到了节点2上
NAME                                   READY   STATUS              RESTARTS   AGE   IP       NODE           NOMINATED NODE   READINESS GATES
mcwnginx-deployment-6577dffddd-8gq82   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-cvpl7   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-gq85b   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-ll5xg   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-lxdk6   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-rmb24   0/1     ContainerCreating   0          30s   <none>   mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node2 myName- 
node/mcwk8s-node2 unlabeled

验证多个节点含有相同标签,

验证多个节点含有相同标签,貌似应用部署没有分布到多个节点,这里莫非跟主节点有关系,它没有部署到主节点上,后面看的确是主节点的问题
[machangwei@mcwk8s-master ~]$ kubectl get deployment
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
mcwnginx-deployment   6/6     6            6           5m59s
[machangwei@mcwk8s-master ~]$ kubectl delete  deployment mcwnginx-deployment
deployment.apps "mcwnginx-deployment" deleted
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get nodes
NAME            STATUS   ROLES                  AGE     VERSION
mcwk8s-master   Ready    control-plane,master   5d      v1.23.1
mcwk8s-node1    Ready    <none>                 4d23h   v1.23.1
mcwk8s-node2    Ready    <none>                 11h     v1.23.1
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node2 myName=machangwei
node/mcwk8s-node2 labeled
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-master myName=machangwei
node/mcwk8s-master labeled
[machangwei@mcwk8s-master ~]$ kubectl get pod
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ cat mcwNginx.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mcwnginx-deployment
spec:
  replicas: 6
  selector:
    matchLabels:
     app: mcw_nginxServer
  template:
    metadata:
      labels:
        app: mcw_nginxServer
    spec:
      containers:
        - name: mcw-nginx-container
          image: nginx:1.7.9
      nodeSelector:
          myName: machangwei
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwNginx.yml 
deployment.apps/mcwnginx-deployment created
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
NAME                                   READY   STATUS    RESTARTS   AGE   IP            NODE           NOMINATED NODE   READINESS GATES
mcwnginx-deployment-6577dffddd-26z5n   1/1     Running   0          15s   10.244.0.14   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-2rjkf   1/1     Running   0          15s   10.244.0.18   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-d256t   1/1     Running   0          15s   10.244.0.17   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-hwqlj   1/1     Running   0          15s   10.244.0.16   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-kqkh2   1/1     Running   0          15s   10.244.0.13   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-z2hrd   1/1     Running   0          15s   10.244.0.15   mcwk8s-node2   <none>           <none>
[machangwei@mcwk8s-master ~]$ 


当将node1也加上标签后,就可以了,节点1也部署了pod,主节点上还是没有pod。当前是三个节点都有同样的标签,但是只部署到了两个node节点上,主节点是没有部署上。也就是,多个主机都有相同的标签的时候,部署的yml文件中选择这个标签,那么这几个主机上都会部署pod
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide
No resources found in default namespace.
[machangwei@mcwk8s-master ~]$ kubectl label node mcwk8s-node1 myName=machangwei
node/mcwk8s-node1 labeled
[machangwei@mcwk8s-master ~]$ kubectl apply -f mcwNginx.yml 
deployment.apps/mcwnginx-deployment created
[machangwei@mcwk8s-master ~]$ 
[machangwei@mcwk8s-master ~]$ kubectl get pod -o wide  
NAME                                   READY   STATUS              RESTARTS   AGE   IP       NODE           NOMINATED NODE   READINESS GATES
mcwnginx-deployment-6577dffddd-48xzm   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-hpz9w   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node1   <none>           <none>
mcwnginx-deployment-6577dffddd-nntkr   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node1   <none>           <none>
mcwnginx-deployment-6577dffddd-qj54q   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node2   <none>           <none>
mcwnginx-deployment-6577dffddd-xmxsf   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node1   <none>           <none>
mcwnginx-deployment-6577dffddd-zgdtl   0/1     ContainerCreating   0          4s    <none>   mcwk8s-node2   <none>           <none>

 

posted @ 2022-01-04 10:48  马昌伟  阅读(2478)  评论(0编辑  收藏  举报
博主链接地址:https://www.cnblogs.com/machangwei-8/