技术流ken

运维拯救世界

课堂记录:k8s运行deployment(二)–技术流ken

[root@ken1 ~]# kubectl get node
NAME   STATUS   ROLES    AGE   VERSION
ken1   Ready    master   23h   v1.15.2
ken2   Ready    <none>   22h   v1.15.2
ken3   Ready    <none>   22h   v1.15.2

 

那些原因造成的status出现NotReady状态

1.节点docker必须启动

2.防火墙没关

3.flannel网络没有配置

4.iptables桥配置

5.交换分区没关

6.kubelet没启动

 

k8s集群架构工作演示

 

第一步:部署nginx的pod

[root@ken1 ~]# kubectl run nginx --image=nginx -r 2 

 

-r 指定创建的pod数量

 

第二步:查看创建的pod

[root@ken1 ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-ffr5m   1/1     Running   0          41s
nginx-7bb7cd8db5-sm4l5   1/1     Running   0          41s

 

第三步:查看deployment

[root@ken1 ~]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           2m34s

 

READY 2/2 第一个2是已经启动的pod数,第二个2是需要总共启动的pod数量

UP-TO-DATE  需要的数量

AVAILABLE 可用的数量

 

第四步:出刊详细输出信息

[root@ken1 ~]# kubectl get po -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
nginx-7bb7cd8db5-ffr5m   1/1     Running   0          4m41s   10.244.1.4   ken2   <none>           <none>
nginx-7bb7cd8db5-sm4l5   1/1     Running   0          4m41s   10.244.4.4   ken3   <none>           <none>

 

分析创建过程:

1.我们使用kubectl通知api-server创建nginx deployment

2. deployment-manager创建nginx deployment

3. schelduler把两个pod调度到不用节点之上

4. 节点之上的kubelet创建两个pod

 

 

k8s运行deployment

 

第一步:查看当前deploy

[root@ken1 ~]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           11m

 

第二步:查看当前pod

[root@ken1 ~]# kubectl get po -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE   NOMINATED NODE   READINESS GATES
nginx-7bb7cd8db5-46rfm   1/1     Running   0          3m41s   10.244.1.5   ken2   <none>           <none>
nginx-7bb7cd8db5-sm4l5   1/1     Running   0          12m     10.244.4.4   ken3   <none>           <none>

 

第三步:deploy详细信息

[root@ken1 ~]# kubectl describe deploy nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Tue, 20 Aug 2019 09:37:56 +0800
Labels:                 run=nginx
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-7bb7cd8db5 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  14m   deployment-controller  Scaled up replica set nginx-7bb7cd8db5 to 2

 

第四步:查看replicaset

[root@ken1 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-7bb7cd8db5   2         2         2       18m

 

第五步:查看rs详细信息

[root@ken1 ~]# kubectl describe rs nginx-7bb7cd8db5
Name:           nginx-7bb7cd8db5
Namespace:      default
Selector:       pod-template-hash=7bb7cd8db5,run=nginx
Labels:         pod-template-hash=7bb7cd8db5
                run=nginx
Annotations:    deployment.kubernetes.io/desired-replicas: 2
                deployment.kubernetes.io/max-replicas: 3
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx
Replicas:       2 current / 2 desired
Pods Status:    2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  pod-template-hash=7bb7cd8db5
           run=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age   From                   Message
  ----    ------            ----  ----                   -------
  Normal  SuccessfulCreate  19m   replicaset-controller  Created pod: nginx-7bb7cd8db5-ffr5m
  Normal  SuccessfulCreate  19m   replicaset-controller  Created pod: nginx-7bb7cd8db5-sm4l5
  Normal  SuccessfulCreate  10m   replicaset-controller  Created pod: nginx-7bb7cd8db5-46rfm

 

 

第六步:查看pod详细信息

[root@ken1 ~]# kubectl describe po nginx-7bb7cd8db5-46rfm 
Name:           nginx-7bb7cd8db5-46rfm
Namespace:      default
Priority:       0
Node:           ken2/192.168.64.6
Start Time:     Tue, 20 Aug 2019 09:47:13 +0800
Labels:         pod-template-hash=7bb7cd8db5
                run=nginx
Annotations:    <none>
Status:         Running
IP:             10.244.1.5
Controlled By:  ReplicaSet/nginx-7bb7cd8db5
Containers:
  nginx:
    Container ID:   docker://e9620bdc9424eba06d4815cd69f8524b6f729af4beff418ab0520e43d2af3da9
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 20 Aug 2019 09:47:24 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-4vj67 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-4vj67:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-4vj67
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  11m   default-scheduler  Successfully assigned default/nginx-7bb7cd8db5-46rfm to ken2
  Normal  Pulling    11m   kubelet, ken2      Pulling image "nginx"
  Normal  Pulled     11m   kubelet, ken2      Successfully pulled image "nginx"
  Normal  Created    11m   kubelet, ken2      Created container nginx
  Normal  Started    11m   kubelet, ken2      Started container nginx

 

第七步:查看pod日志

[root@ken1 ~]# kubectl logs nginx-7bb7cd8db5-46rfm  

 

持续检测pod日志

[root@ken1 ~]# kubectl logs nginx-7bb7cd8db5-46rfm  -f

 

 

 

删除pod

 

第一步:直接删除deploy

[root@ken1 ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-46rfm   1/1     Running   0          21m
nginx-7bb7cd8db5-sm4l5   1/1     Running   0          30m
[root@ken1 ~]# kubectl delete po nginx-7bb7cd8db5-46rfm 
pod "nginx-7bb7cd8db5-46rfm" deleted
[root@ken1 ~]# kubectl get pod
NAME                     READY   STATUS    RESTARTS   AGE
nginx-7bb7cd8db5-prgr8   1/1     Running   0          8s
nginx-7bb7cd8db5-sm4l5   1/1     Running   0          30s

注意:

直接删除pod是无法进行删除的,如果说这个pod被deploy进行管理的

[root@ken1 ~]# kubectl get deploy
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   2/2     2            2           31m
[root@ken1 ~]# kubectl delete deploy nginx
deployment.extensions "nginx" deleted
[root@ken1 ~]# kubectl get deploy
No resources found.
[root@ken1 ~]# kubectl get po
No resources found.

 

 

pod命名规则

 

nginx3-77694b9997-c82dg

deploy  replicaset     随机字符串

 

 

比如:现在启动一个deploy,配置副本数为3,假如其中一个node宕机,会发生什么?

第一步:启动deploy,副本数为3

[root@ken1 ~]# kubectl run nginx3 --image=nginx -r 3

 

第二步:查看pod详细信息

[root@ken1 ~]# kubectl get po -o wide
NAME                      READY   STATUS    RESTARTS   AGE    IP            NODE   NOMINATED NODE   READINESS GATES
nginx1-7885dd9f7-vqk7t    1/1     Running   0          5m1s   10.244.1.8    ken2   <none>           <none>
nginx3-77694b9997-25cbs   1/1     Running   0          25s    10.244.4.9    ken3   <none>           <none>
nginx3-77694b9997-q5ngb   1/1     Running   0          25s    10.244.1.10   ken2   <none>           <none>
nginx3-77694b9997-qw5rq   1/1     Running   0          25s    10.244.4.8    ken3   <none>           <none>

 

第三步:模拟ken3宕机

poweroff

 

第四步:查看node节点状态

[root@ken1 ~]# kubectl get no
NAME   STATUS     ROLES    AGE   VERSION
ken1   Ready      master   23h   v1.15.2
ken2   Ready      <none>   23h   v1.15.2
ken3   NotReady   <none>   23h   v1.15.2

 

 

第五步:持续查看pod状态

[root@ken1 ~]# kubectl get po -o wide -w
NAME                      READY   STATUS    RESTARTS   AGE     IP            NODE   NOMINATED NODE   READINESS GATES
nginx1-7885dd9f7-vqk7t    1/1     Running   0          6m40s   10.244.1.8    ken2   <none>           <none>
nginx3-77694b9997-25cbs   1/1     Running   0          2m4s    10.244.4.9    ken3   <none>           <none>
nginx3-77694b9997-q5ngb   1/1     Running   0          2m4s    10.244.1.10   ken2   <none>           <none>
nginx3-77694b9997-qw5rq   1/1     Running   0          2m4s    10.244.4.8    ken3   <none>           <none>

 

 

创建资源的两种方式

1,kubectl命令行工具进行创建

2.通过yml文件

 

 

认识yml文件

 

 

① apiVersion 是当前配置格式的版本。

先执行kubectl api-resources找到所有的资源

在执行命令 kubectl explain deploy即可获取到版本和类型信息

② kind 是要创建的资源类型,这里是 Deployment。

③ metadata 是该资源的元数据,name 是必需的元数据项。

④ spec 部分是该 Deployment 的规格说明。

⑤ replicas 指明副本数量,默认为 1。

⑥ template 定义 Pod 的模板,这是配置文件的重要部分。

⑦ metadata 定义 Pod 的元数据,至少要定义一个 label。label 的 key 和 value 可以任意指定。

⑧ spec 描述 Pod 的规格,此部分定义 Pod 中每一个容器的属性,name 和 image 是必需的。

 

 

案列:启动nginxpod,副本数3

 

1.命令行工具

[root@ken1 ~]# kubectl run nginx3 --image=nginx -r 3

 

2.编写yml文件

第一步:编写yml文件

[root@ken1 ~]# cat nginx.yml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx
spec:
   replicas: 2
   template:
     metadata:
      labels:
         kk: ken
     spec:
       containers:
       - name: nginx
         image: nginx

 

第二步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx.yml

 

第三步:查看创建的资源

[root@ken1 ~]# kubectl get deploy
NAME     READY   UP-TO-DATE   AVAILABLE   AGE
nginx    2/2     2            2           59s
nginx1   1/1     1            1           51m
nginx3   3/3     3            3           47m

 

第四步:查看pod

[root@ken1 ~]# kubectl get pod
NAME                      READY   STATUS        RESTARTS   AGE
nginx-bdbfcfb4b-5jnqz     1/1     Running       0          78s
nginx-bdbfcfb4b-lmbkp     1/1     Running       0          78s
nginx1-7885dd9f7-vqk7t    1/1     Running       0          52m
nginx3-77694b9997-25cbs   1/1     Terminating   0          47m
nginx3-77694b9997-dsllp   1/1     Running       0          41m
nginx3-77694b9997-q5ngb   1/1     Running       0          47m
nginx3-77694b9997-qw5rq   1/1     Terminating   0          47m
nginx3-77694b9997-vhlnj   1/1     Running       0          41m

 

 

第五步:查看标签

[root@ken1 ~]# kubectl get pod --show-labels
NAME                      READY   STATUS        RESTARTS   AGE     LABELS
nginx-bdbfcfb4b-5jnqz     1/1     Running       0          2m29s   kk=ken,pod-template-hash=bdbfcfb4b
nginx-bdbfcfb4b-lmbkp     1/1     Running       0          2m29s   kk=ken,pod-template-hash=bdbfcfb4b

 

 

 

给node打标签

 

第一步:给node打标签

[root@ken1 ~]# kubectl label node ken3 disk=ssd

 

第二步;查看节点标签

[root@ken1 ~]# kubectl get no --show-labels
NAME   STATUS     ROLES    AGE   VERSION   LABELS
ken1   Ready      master   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
ken2   Ready      <none>   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken2,kubernetes.io/os=linux
ken3   NotReady   <none>   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disk=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken3,kubernetes.io/os=linux

 

第三步:修改yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx
spec:
   replicas: 2
   template:
     metadata:
      labels:
         kk: ken
     spec:
       containers:
       - name: nginx
         image: nginx
       nodeSelector:
         disk: ssd

 

第四步:执行yml文件

[root@ken1 ~]# kubectl apply -f nginx.yml 

 

第五步:查看pod

[root@ken1 ~]# kubectl get po -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP            NODE   NOMINATED NODE   READINESS GATES
nginx-66bdccc56-2d6m4     1/1     Running   0          7m18s   10.244.4.11   ken3   <none>           <none>
nginx-66bdccc56-bxh6z     1/1     Running   0          7m19s   10.244.4.10   ken3   <none>           <none>
nginx1-7885dd9f7-vqk7t    1/1     Running   0          66m     10.244.1.8    ken2   <none>           <none>
nginx3-77694b9997-dsllp   1/1     Running   0          55m     10.244.1.11   ken2   <none>           <none>
nginx3-77694b9997-q5ngb   1/1     Running   0          62m     10.244.1.10   ken2   <none>           <none>
nginx3-77694b9997-vhlnj   1/1     Running   0          55m     10.244.1.12   ken2   <none>           <none>

 

第六步:取消标签

[root@ken1 ~]# kubectl label no ken3 disk-
node/ken3 labeled
[root@ken1 ~]# kubectl get no --show-labels
NAME   STATUS   ROLES    AGE   VERSION   LABELS
ken1   Ready    master   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken1,kubernetes.io/os=linux,node-role.kubernetes.io/master=
ken2   Ready    <none>   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken2,kubernetes.io/os=linux
ken3   Ready    <none>   24h   v1.15.2   beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=ken3,kubernetes.io/os=linux,run=nginx

 

 

自动伸缩

 

第一步:修改yml文件

[root@ken1 ~]# cat nginx.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx 
spec:
   replicas: 5
   template:
     metadata:
      labels:
         kk: ken
     spec:
       containers:
       - name: nginx
         image: nginx

 

第二步:查看pod

[root@ken1 ~]# kubectl get po -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODE   NOMINATED NODE   READINESS GATES
nginx-bdbfcfb4b-2gbgr     1/1     Running   0          45s   10.244.4.14   ken3   <none>           <none>
nginx-bdbfcfb4b-l9w6p     1/1     Running   0          94s   10.244.4.12   ken3   <none>           <none>
nginx-bdbfcfb4b-qfnjt     1/1     Running   0          93s   10.244.1.15   ken2   <none>           <none>
nginx-bdbfcfb4b-snmcb     1/1     Running   0          45s   10.244.1.16   ken2   <none>           <none>
nginx-bdbfcfb4b-vw62d     1/1     Running   0          46s   10.244.4.13   ken3   <none>           <none>
nginx1-7885dd9f7-vqk7t    1/1     Running   0          69m   10.244.1.8    ken2   <none>           <none>
nginx3-77694b9997-dsllp   1/1     Running   0          58m   10.244.1.11   ken2   <none>           <none>
nginx3-77694b9997-q5ngb   1/1     Running   0          65m   10.244.1.10   ken2   <none>           <none>
nginx3-77694b9997-vhlnj   1/1     Running   0          58m   10.244.1.12   ken2   <none>           <none>

 

 

第三步:减少pod

[root@ken1 ~]# cat nginx.yml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
 name: nginx 
spec:
   replicas: 1
   template:
     metadata:
      labels:
         kk: ken
     spec:
       containers:
       - name: nginx
         image: nginx

 

第四步:查看pod

[root@ken1 ~]# kubectl get po -o wide
NAME                      READY   STATUS    RESTARTS   AGE     IP            NODE   NOMINATED NODE   READINESS GATES
nginx-bdbfcfb4b-qfnjt     1/1     Running   0          2m53s   10.244.1.15   ken2   <none>           <none>
nginx1-7885dd9f7-vqk7t    1/1     Running   0          71m     10.244.1.8    ken2   <none>           <none>
nginx3-77694b9997-dsllp   1/1     Running   0          60m     10.244.1.11   ken2   <none>           <none>
nginx3-77694b9997-q5ngb   1/1     Running   0          66m     10.244.1.10   ken2   <none>           <none>
nginx3-77694b9997-vhlnj   1/1     Running   0          60m     10.244.1.12   ken2   <none>           <none>

 

删除yml文件部署的pod

 

方法一:删除deploy

[root@ken1 ~]# kubectl delete deploy nginx

 

方法二:删除yml文件

[root@ken1 ~]# kubectl delete -f nginx.yml 

 

执行此命令yml文件依然存在!

 

8 thoughts on “课堂记录:k8s运行deployment(二)–技术流ken

  1. I precisely wished to thank you very much again. I’m not certain the things that I might have taken care of in the absence of the actual tactics contributed by you regarding that situation. Certainly was a frightful dilemma for me, but taking a look at the very well-written manner you handled it took me to cry for fulfillment. Now i am grateful for this support and in addition wish you know what a powerful job you were undertaking instructing the mediocre ones all through your blog. I know that you haven’t come across all of us.

  2. I must express appreciation to you just for rescuing me from this type of instance. As a result of searching through the world-wide-web and seeing ideas which were not powerful, I thought my life was well over. Living minus the strategies to the difficulties you’ve resolved by means of your entire post is a crucial case, and those which may have badly damaged my career if I had not noticed the website. Your own capability and kindness in controlling all the details was valuable. I don’t know what I would’ve done if I had not discovered such a point like this. It’s possible to now look forward to my future. Thank you so much for this high quality and sensible guide. I won’t be reluctant to endorse your blog to any individual who should get direction about this problem.

  3. I would like to express some appreciation to the writer for rescuing me from such a issue. After looking throughout the online world and meeting principles which were not powerful, I figured my entire life was over. Being alive without the answers to the issues you have resolved by means of your entire guideline is a serious case, and the ones which could have badly damaged my entire career if I had not encountered your blog. Your capability and kindness in maneuvering the whole lot was helpful. I’m not sure what I would’ve done if I had not come across such a subject like this. I can now relish my future. Thank you so much for the high quality and sensible help. I will not hesitate to propose your web site to any person who would need direction on this area.

  4. I together with my buddies appeared to be reading the best ideas on the blog then all of the sudden I had a terrible suspicion I never expressed respect to the web blog owner for them. The young men ended up certainly passionate to learn them and have in effect truly been taking pleasure in them. Thanks for truly being quite considerate and then for going for this kind of useful issues most people are really desirous to know about. Our own honest regret for not expressing appreciation to sooner.

  5. My wife and i have been so comfortable Chris managed to complete his investigations through the precious recommendations he received from your very own blog. It’s not at all simplistic just to find yourself making a gift of tactics that the others might have been making money from. So we understand we have got the website owner to thank because of that. The type of explanations you have made, the simple site menu, the friendships you aid to engender – it is most sensational, and it’s really letting our son and our family imagine that this issue is thrilling, and that’s incredibly important. Thank you for the whole lot!

  6. I am glad for writing to let you be aware of what a fabulous encounter my wife’s girl went through viewing yuor web blog. She even learned a lot of pieces, most notably how it is like to possess a great coaching heart to make the mediocre ones completely know just exactly various tortuous things. You actually did more than visitors’ expected results. I appreciate you for offering the valuable, dependable, revealing not to mention easy thoughts on your topic to Kate.

  7. I intended to write you that bit of note to say thanks a lot as before on the awesome ideas you’ve shared in this article. It has been certainly seriously open-handed of people like you to make extensively just what a number of us could have offered as an electronic book in making some bucks on their own, especially now that you might well have done it if you decided. These pointers likewise served like a fantastic way to fully grasp some people have the identical eagerness just like my personal own to see more concerning this issue. I think there are thousands of more pleasurable situations in the future for those who find out your blog.

  8. I’m commenting to make you be aware of of the awesome discovery my wife’s princess enjoyed going through yuor web blog. She came to understand plenty of things, which included how it is like to have an amazing teaching heart to get the mediocre ones without hassle know precisely some problematic topics. You actually exceeded visitors’ expected results. I appreciate you for displaying these beneficial, safe, educational and in addition easy thoughts on the topic to Jane.

发表评论

邮箱地址不会被公开。