2.K8S的核心资源管理方法

目录

1.1陈述式资源管理方法

1.1.1.管理名称空间资源

1.1.2.管理Deployment资源

1.1.3.管理Service资源

1.1.4.kubectl用法总结

1.2.声明式资源管理方法

 

1.1.1.管理名称空间资源

查看名称空间

[root@hdss7-21 ~]# kubectl get namespace
NAME              STATUS   AGE
default           Active   4d19h
kube-node-lease   Active   4d19h
kube-public       Active   4d19h
kube-system       Active   4d19h

  

 [root@hdss7-21 ~]# kubectl get ns
 NAME STATUS AGE
 default Active 4d19h
 kube-node-lease Active 4d19h
 kube-public Active 4d19h
 kube-system Active 4d19h

查看名称空间内的资源

[root@hdss7-21 ~]# kubectl get all -n default
NAME                 READY   STATUS    RESTARTS   AGE
pod/nginx-ds-7hg9l   1/1     Running   0          3d18h
pod/nginx-ds-tnhsg   1/1     Running   0          3d18h


NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   4d19h

NAME                      DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset.apps/nginx-ds   2         2         2       2            2           <none>          3d18h

创建名称空间

[root@hdss7-21 ~]# kubectl create namespace app
namespace/app created
[root@hdss7-21 ~]# kubectl get namespace
NAME              STATUS   AGE
app               Active   35s
default           Active   4d20h
kube-node-lease   Active   4d20h
kube-public       Active   4d20h
kube-system       Active   4d20h

删除名称空间

[root@hdss7-21 ~]# kubectl delete ns app
namespace "app" deleted
[root@hdss7-21 ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   4d20h
kube-node-lease   Active   4d20h
kube-public       Active   4d20h
kube-system       Active   4d20h

1.1.2.管理Deployment资源

创建deployment

 [root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.fx.com/public/nginx:v1.7.9 -n kube-public
 deployment.apps/nginx-dp created

查看deployment

  • 简单查看
[root@hdss7-21 ~]# kubectl get deployment -n kube-public
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dp   1/1     1            1           56s
  • 扩展查看
[root@hdss7-21 ~]# kubectl get deployment -n kube-public -o wide
NAME       READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                              SELECTOR
nginx-dp   1/1     1            1           20m   nginx        harbor.fx.com/public/nginx:v1.7.9   app=nginx-dp
  • 详细查看
[root@hdss7-21 ~]# kubectl describe deployment nginx-dp -n kube-public
Name:                   nginx-dp
Namespace:              kube-public
CreationTimestamp:      Mon, 08 Jun 2020 11:40:54 +0800
Labels:                 app=nginx-dp
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx-dp
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=nginx-dp
  Containers:
   nginx:
    Image:        harbor.fx.com/public/nginx:v1.7.9
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-dp-6f4ddd775 (1/1 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  6m31s  deployment-controller  Scaled up replica set nginx-dp-6f4ddd775 to 1

查看pod资源

  • 简单查找
[root@hdss7-21 ~]# kubectl get pods -n kube-public
NAME                       READY   STATUS    RESTARTS   AGE
nginx-dp-6f4ddd775-98dbq   1/1     Running   0          107m
  • 扩展查找
[root@hdss7-21 ~]# kubectl get pods -n kube-public -o wide
NAME                       READY   STATUS    RESTARTS   AGE    IP           NODE                NOMINATED NODE   READINESS GATES
nginx-dp-6f4ddd775-98dbq   1/1     Running   0          108m   172.7.21.3   hdss7-21.host.com   <none>           <none>
  • 扩展查找
[root@hdss7-21 ~]# kubectl describe pods nginx-dp-6f4ddd775-98dbq -n kube-public
Name:           nginx-dp-6f4ddd775-98dbq
Namespace:      kube-public
Priority:       0
Node:           hdss7-21.host.com/10.4.7.21
Start Time:     Mon, 08 Jun 2020 11:40:54 +0800
Labels:         app=nginx-dp
                pod-template-hash=6f4ddd775
Annotations:    <none>
Status:         Running
IP:             172.7.21.3
Controlled By:  ReplicaSet/nginx-dp-6f4ddd775
Containers:
  nginx:
    Container ID:   docker://93213abdbfb595ebe213c6764f0af4932a40c2ceaa5efc562fac9f2cde5b69fe
    Image:          harbor.fx.com/public/nginx:v1.7.9
    Image ID:       docker-pullable://harbor.fx.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 08 Jun 2020 11:40:56 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zld8g (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-zld8g:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zld8g
    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:          <none>

进入pod资源

[root@hdss7-21 ~]# kubectl exec -it nginx-dp-6f4ddd775-98dbq /bin/bash -n kube-public
root@nginx-dp-6f4ddd775-98dbq:/# 

注:也可以使用docker exec进入容器

删除pod资源(重启)

[root@hdss7-21 ~]# kubectl delete pods nginx-dp-6f4ddd775-98dbq -n kube-public
pod "nginx-dp-6f4ddd775-98dbq" deleted

强制删除参数: --force-grace-period=0

删除deployment 

[root@hdss7-21 ~]# kubectl delete deployment nginx-dp -n kube-public
deployment.extensions "nginx-dp" deleted
[root@hdss7-21 ~]# kubectl get deployment -n kube-public No resources found.
[root@hdss7-21 ~]# kubectl get pods -n kube-public No resources found.

1.1.3 管理Service资源

 创建Service

[root@hdss7-21 ~]# kubectl create deployment nginx-dp --image=harbor.fx.com/public/nginx:v1.7.9 -n kube-public
deployment.apps/nginx-dp created
[root@hdss7-21 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[root@hdss7-21 ~]# kubectl scale deployment nginx-dp --replicas=2 -n kube-public
deployment.extensions/nginx-dp scaled
[root@hdss7-21 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.1:443 nq
  -> 10.4.7.21:6443               Masq    1      0          0         
  -> 10.4.7.22:6443               Masq    1      0          0         
TCP  192.168.164.107:80 nq
  -> 172.7.21.3:80                Masq    1      0          0         
  -> 172.7.22.3:80                Masq    1      0          0
[root@hdss7-21 ~]# kubectl get service -n kube-public
NAME       TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
nginx-dp   ClusterIP   192.168.164.107   <none>        80/TCP    6m54s

查看service

[root@hdss7-21 ~]# kubectl describe service nginx-dp -n kube-public
Name:              nginx-dp
Namespace:         kube-public
Labels:            app=nginx-dp
Annotations:       <none>
Selector:          app=nginx-dp
Type:              ClusterIP
IP:                192.168.164.107
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         172.7.22.3:80
Session Affinity:  None
Events:            <none>
1.1.4.kubectl用法总结

陈述式资源管理方法小结:

  • kubernetes集群管理集群的唯一入口是通过相应的方法调用apiserver的接口。
  • kubectl是官方的CLI命令行工具,用于与apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理K8S各种资源的一种有效途径。
  • kubectl的命令大全
  • 陈述式资源管理方法可以满足90%以上的资源管理需求,但它的缺点也很明显
    • 命令冗长、复杂、难以记忆。
    • 特定场景下,无法实现管理需求。
    • 对资源的增、删、查操作比较容易,改就很痛苦。
1.2.声明式资源管理方法

 声明式资源管理方法依赖于一资源配置清单(yaml/json)

查看资源配置清单的方法

[root@hdss7-21 ~]# kubectl get pods nginx-dp-6f4ddd775-x9hjg -o yaml -n kube-public
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-06-08T05:56:16Z"
  generateName: nginx-dp-6f4ddd775-
  labels:
    app: nginx-dp
    pod-template-hash: 6f4ddd775
  name: nginx-dp-6f4ddd775-x9hjg
  namespace: kube-public
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-dp-6f4ddd775
    uid: 872e6838-b8ef-4a6a-8cb2-98a94dca0c91
  resourceVersion: "522272"
  selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-6f4ddd775-x9hjg
  uid: 7a04423c-add2-45ee-8211-c6e80545cb9a
spec:
  containers:
  - image: harbor.fx.com/public/nginx:v1.7.9
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-zld8g
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: hdss7-22.host.com
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-zld8g
    secret:
      defaultMode: 420
      secretName: default-token-zld8g
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-06-08T05:56:16Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2020-06-08T05:56:17Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2020-06-08T05:56:17Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2020-06-08T05:56:16Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://f5e7ac3cef3c1439df242cc2519ca22c7b942ec44099ffdd0258fed05a075dea
    image: harbor.fx.com/public/nginx:v1.7.9
    imageID: docker-pullable://harbor.fx.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2
    lastState: {}
    name: nginx
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2020-06-08T05:56:17Z"
  hostIP: 10.4.7.22
  phase: Running
  podIP: 172.7.22.3
  qosClass: BestEffort
  startTime: "2020-06-08T05:56:16Z"

[root@hdss7-21 ~]# kubectl get svc nginx-dp -o yaml -n kube-public
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-06-08T05:58:22Z"
  labels:
    app: nginx-dp
  name: nginx-dp
  namespace: kube-public
  resourceVersion: "522457"
  selfLink: /api/v1/namespaces/kube-public/services/nginx-dp
  uid: df2f2bd9-bfa7-4cdb-b25b-1488e63944a9
spec:
  clusterIP: 192.168.164.107
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-dp
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

解释资源配置清单

 [root@hdss7-21 ~]# kubectl explain service

创建资源配置清单

[root@hdss7-21 ~]# vim nginx-ds.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    nginx: nginx-ds
  name: nginx-ds
  namespace: default
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-ds
  type: ClusterIP

应用资源配置清单

[root@hdss7-21 ~]# kubectl create -f nginx-ds.yaml 
service/nginx-ds configured
[root@hdss7-21 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)    AGE
kubernetes   ClusterIP   192.168.0.1       <none>        443/TCP    5d1h
nginx-ds     ClusterIP   192.168.100.178   <none>        80/TCP   8m54s

修改资源配置清单

  • 离线修改
[root@hdss7-21 ~]# docker login docker.io
[root@hdss7-21 ~]# docker pull fangxing1001/nginx:curl
[root@hdss7-21 ~]# vim nginx-ds.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: nginx-test
spec:
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: mynginx
        image: harbor.fx.com/public/nginx:v1.7.9
        ports:
        - containerPort: 80
[root@hdss7-21 ~]# kubectl apply -f nginx-ds.yaml 
deployment.extensions/nginx-test created
[root@hdss7-21 ~]# vim nginx-ds.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
    name: nginx-test
spec:
  template:
    metadata:
      labels:
        app: nginx-test
    spec:
      containers:
      - name: mynginx
        image: harbor.fx.com/public/nginx:curl
        ports:
        - containerPort: 80
[root@hdss7-21 ~]# kubectl apply -f nginx-ds.yaml 
deployment.extensions/nginx-test configured
[root@hdss7-21 ~]# kubectl describe pods nginx-test-655f555749-wwbnp
Name:           nginx-test-655f555749-wwbnp
Namespace:      default
Priority:       0
Node:           hdss7-22.host.com/10.4.7.22
Start Time:     Mon, 08 Jun 2020 17:40:13 +0800
Labels:         app=nginx-test
                pod-template-hash=655f555749
Annotations:    <none>
Status:         Running
IP:             172.7.22.4
Controlled By:  ReplicaSet/nginx-test-655f555749
Containers:
  mynginx:
    Container ID:   docker://35a16c22463b9ff05adb69bb2f3665c316c58de657ab01f9c9f63896d0d1c3f0
    Image:          harbor.fx.com/public/nginx:curl
    Image ID:       docker-pullable://harbor.fx.com/public/nginx@sha256:75f7e3c69bb839f8400adbca228bce8b7e5bc3e9bce2ff1bb543094b82337e16
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 08 Jun 2020 17:40:22 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zkqff (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-zkqff:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zkqff
    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  3m13s  default-scheduler           Successfully assigned default/nginx-test-655f555749-wwbnp to hdss7-22.host.com
  Normal  Pulling    3m11s  kubelet, hdss7-22.host.com  Pulling image "harbor.fx.com/public/nginx:curl"
  Normal  Pulled     3m4s   kubelet, hdss7-22.host.com  Successfully pulled image "harbor.fx.com/public/nginx:curl"
  Normal  Created    3m4s   kubelet, hdss7-22.host.com  Created container mynginx
  Normal  Started    3m4s   kubelet, hdss7-22.host.com  Started container mynginx
  • 在线修改
[root@hdss7-21 ~]# kubectl edit svc nginx-ds
service/nginx-ds edited
[root@hdss7-21 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   192.168.0.1       <none>        443/TCP   5d1h
nginx-ds     ClusterIP   192.168.100.178   <none>        80/TCP    16m

删除资源

[root@hdss7-21 ~]# kubectl delete -f nginx-ds.yaml 
deployment.extensions "nginx-test" deleted
[root@hdss7-21 ~]# kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx-ds-7hg9l   1/1     Running   0          4d1h
nginx-ds-tnhsg   1/1     Running   0          4d1h

声明资源管理方法小结:

  • 声明式资源管理方法,依赖于统一资源配置清单文件对资源进行管理。
  • 对资源的管理,是通过事先定义在统一资源配置清单内,再通过陈述式命令应用到K8S集群里。
  • 语法格式:kubectl create/apply/delete -f /path/to/yaml

返回顶部

posted @ 2020-06-08 11:04  XingFang  阅读(588)  评论(0编辑  收藏  举报