管理K8S核心资源

2020-09-27  本文已影响0人  草莓_Ops

管理K8S核心资源的三种基本方法:

image.png
~]# kubectl get namespace/ns
NAME              STATUS   AGE
default           Active   21h
kube-node-lease   Active   21h
kube-public       Active   21h
kube-system       Active   21h

~]# kubectl get all [-n default]
NAME                             READY   STATUS    RESTARTS   AGE
pod/nginx-ds-4tt4m               1/1     Running   0          16h
pod/nginx-ds-9jcqb               1/1     Running   0          16h
pod/nginx-web-799b86bc9c-9b8mg   1/1     Running   0          16h


NAME                 TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   192.168.0.1       <none>        443/TCP   21h
service/nginx-web    ClusterIP   192.168.131.242   <none>        80/TCP    16h

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

NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-web   1/1     1            1           16h

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-web-799b86bc9c   1         1         1       16h

~]# kubectl create ns app
namespace/app created

~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:curl -n kube-public
deployment.apps/nginx-dp created
[root@k8s7-21.host.com ~]# kubectl get deploy -n kube-public
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dp   1/1     1            1           23s

[root@k8s7-21.host.com ~]# kubectl get pods -n kube-public -o wide
NAME                       READY   STATUS    RESTARTS   AGE   IP           NODE               NOMINATED NODE   READINESS GATES
nginx-dp-9c86cfc59-n9vfr   1/1     Running   0          51s   172.7.21.5   k8s7-21.host.com   <none>           <none>

[root@k8s7-21.host.com ~]# kubectl describe deployment nginx-dp -n kube-public
Name:                   nginx-dp
Namespace:              kube-public
CreationTimestamp:      Sun, 27 Sep 2020 16:24:42 +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.od.com/public/nginx:curl
    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-9c86cfc59 (1/1 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  5m35s  deployment-controller  Scaled up replica set nginx-dp-9c86cfc59 to 1

[root@k8s7-21.host.com ~]# kubectl exec -it nginx-dp-9c86cfc59-n9vfr /bin/bash -n kube-public

[root@k8s7-21.host.com ~]# watch -n 1 'kubectl describe deployment nginx-dp -n kube-public|grep -C 5 Event'
~]# kubectl delete pod nginx-dp-9c86cfc59-n9vfr -n kube-public [--force --grace-period=0]
使用watch观察pod重建状态变化
强制删除参数:--force --grace-period=0

[root@k8s7-21.host.com ~]# kubectl delete deploy nginx-dp -n kube-public
deployment.extensions "nginx-dp" deleted

[root@k8s7-21.host.com ~]# kubectl create deployment nginx-dp --image=harbor.od.com/public/nginx:curl -n kube-public
deployment.apps/nginx-dp created
[root@k8s7-21.host.com ~]# kubectl get all -n kube-public

[root@k8s7-21.host.com ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[root@k8s7-21.host.com ~]# kubectl get all -n kube-public
NAME                           READY   STATUS    RESTARTS   AGE
pod/nginx-dp-9c86cfc59-84n55   1/1     Running   0          3m41s


NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/nginx-dp   ClusterIP   192.168.204.85   <none>        80/TCP    18s


NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   1/1     1            1           3m41s

NAME                                 DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-9c86cfc59   1         1         1       3m41s

~]# curl 192.168.204.85
[root@k8s7-21.host.com ~]# 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.204.85:80 nq
  -> 172.7.22.7:80                Masq    1      0          1  

[root@k8s7-21.host.com ~]# kubectl scale deployment nginx-dp --replicas=2 -n kube-public
deployment.extensions/nginx-dp scaled
[root@k8s7-21.host.com ~]# kubectl get pods -n kube-public
NAME                       READY   STATUS    RESTARTS   AGE
nginx-dp-9c86cfc59-84n55   1/1     Running   0          7m21s
nginx-dp-9c86cfc59-ghgg8   1/1     Running   0          14s

TCP  192.168.204.85:80 nq
  -> 172.7.21.5:80                Masq    1      0          0         
  -> 172.7.22.7:80                Masq    1      0          0        

[root@k8s7-21.host.com ~]# kubectl describe svc 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.204.85
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         172.7.21.5:80,172.7.22.7:80
Session Affinity:  None
Events:            <none>

image.png
[root@k8s7-21.host.com ~]# kubectl get daemonset -n default
NAME       DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
nginx-ds   2         2         2       2            2           <none>          17h

daemonset这种控制器会让每个运算节点都要运行一份
[root@k8s7-21.host.com ~]# kubectl expose daemonset -n default nginx-ds --port=888
error: cannot expose a DaemonSet.extensions
不允许给daemonset控制器创建svc
image.png
1.获取统一资源配置清单
[root@k8s7-21.host.com ~]# kubectl get pods -n kube-public
NAME                       READY   STATUS    RESTARTS   AGE
nginx-dp-9c86cfc59-84n55   1/1     Running   0          23m
nginx-dp-9c86cfc59-ghgg8   1/1     Running   0          16m
[root@k8s7-21.host.com ~]# kubectl get pods nginx-dp-9c86cfc59-ghgg8 -o yaml -n kube-public
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-09-27T09:27:00Z"
  generateName: nginx-dp-9c86cfc59-
  labels:
    app: nginx-dp
    pod-template-hash: 9c86cfc59
  name: nginx-dp-9c86cfc59-ghgg8
  namespace: kube-public
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: nginx-dp-9c86cfc59
    uid: a68e29e0-98ba-4270-93ae-8d14c0b6de64
  resourceVersion: "109194"
  selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-9c86cfc59-ghgg8
  uid: 76438ce3-058e-4b48-b15d-9b68731ddd37
spec:
  containers:
  - image: harbor.od.com/public/nginx:curl
    imagePullPolicy: IfNotPresent
    name: nginx
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-zqxm4
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: k8s7-21.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-zqxm4
    secret:
      defaultMode: 420
      secretName: default-token-zqxm4
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-09-27T09:27:00Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2020-09-27T09:27:02Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2020-09-27T09:27:02Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2020-09-27T09:27:00Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://1c2f1e9f15b0450f9bd84b7478550d04479d4f48d94e2b43c1b2e48f542a4bcf
    image: harbor.od.com/public/nginx:curl
    imageID: docker-pullable://harbor.od.com/public/nginx@sha256:9fb052e40b572765bca65cf43cbe6a8d62053361afcbac0e69999f61ab72c6d5
    lastState: {}
    name: nginx
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2020-09-27T09:27:01Z"
  hostIP: 10.4.7.21
  phase: Running
  podIP: 172.7.21.5
  qosClass: BestEffort
  startTime: "2020-09-27T09:27:00Z"


查看SVC
[root@k8s7-21.host.com ~]# kubectl get svc -n kube-public
NAME       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
nginx-dp   ClusterIP   192.168.204.85   <none>        80/TCP    22m
[root@k8s7-21.host.com ~]# kubectl get svc nginx-dp -o yaml -n kube-public
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-09-27T09:23:16Z"
  labels:
    app: nginx-dp
  name: nginx-dp
  namespace: kube-public
  resourceVersion: "108854"
  selfLink: /api/v1/namespaces/kube-public/services/nginx-dp
  uid: a8cd589a-98b8-4ac4-9bbe-50af51e06f85
spec:
  clusterIP: 192.168.204.85
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: nginx-dp
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

点读机专用命令:
[root@k8s7-21.host.com ~]# kubectl explain service.metadata

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

[root@k8s7-21.host.com ~]# kubectl create/apply -f nginx-ds-svc.yaml 
service/nginx-ds created
[root@k8s7-21.host.com ~]# kubectl get svc 
NAME         TYPE        CLUSTER-IP        EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   192.168.0.1       <none>        443/TCP   23h
nginx-ds     ClusterIP   192.168.90.55     <none>        80/TCP    22s
nginx-web    ClusterIP   192.168.131.242   <none>        80/TCP    18h

[root@k8s7-21.host.com ~]# kubectl get svc nginx-ds -o yaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-09-27T10:03:46Z"
  labels:
    app: nginx-ds
  name: nginx-ds
  namespace: default
  resourceVersion: "112350"
  selfLink: /api/v1/namespaces/default/services/nginx-ds
  uid: a69781d2-6d92-4f39-87a6-19ddb454cef5
spec:
  clusterIP: 192.168.90.55
  ports:
  - port: 80    #对外暴露的端口
    protocol: TCP
    targetPort: 80    #容器内的端口
  selector:
    app: nginx-ds
  sessionAffinity: None
  type: ClusterIP
status:
  loadBalancer: {}

在线修改资源配置清单:
[root@k8s7-21.host.com ~]# kubectl edit svc nginx-ds

离线修改:
vim nginx-ds-svc.yaml
kubectl apply -f ....

[root@k8s7-21.host.com ~]# kubectl delete svc nginx-ds
service "nginx-ds" deleted

[root@k8s7-21.host.com ~]# kubectl delete -f nginx-ds-svc.yaml 

上一篇下一篇

猜你喜欢

热点阅读