【K8S运维知识汇总】第3天2:kubectl详解——声明式资源管理方法

    技术2022-07-11  71

    通过陈述式创建的pod资源,获得统一配置清单

    [root@hdss7-21 ~]# kubectl get pods nginx-dp-5dfc689474-bqk8w -o yaml -n kube-public apiVersion: v1 kind: Pod metadata: creationTimestamp: "2020-07-01T12:19:34Z" generateName: nginx-dp-5dfc689474- labels: app: nginx-dp pod-template-hash: 5dfc689474 name: nginx-dp-5dfc689474-bqk8w namespace: kube-public ownerReferences: - apiVersion: apps/v1 blockOwnerDeletion: true controller: true kind: ReplicaSet name: nginx-dp-5dfc689474 uid: 109c9920-2fdc-483b-8254-eb10f3e90443 resourceVersion: "77238" selfLink: /api/v1/namespaces/kube-public/pods/nginx-dp-5dfc689474-bqk8w uid: ba5311df-0b9c-41fb-b270-56e0604a54f1 spec: containers: - image: harbor.od.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-vx5v7 readOnly: true dnsPolicy: ClusterFirst enableServiceLinks: true nodeName: hdss7-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-vx5v7 secret: defaultMode: 420 secretName: default-token-vx5v7 status: conditions: - lastProbeTime: null lastTransitionTime: "2020-07-01T12:19:35Z" status: "True" type: Initialized - lastProbeTime: null lastTransitionTime: "2020-07-01T12:19:36Z" status: "True" type: Ready - lastProbeTime: null lastTransitionTime: "2020-07-01T12:19:36Z" status: "True" type: ContainersReady - lastProbeTime: null lastTransitionTime: "2020-07-01T12:19:35Z" status: "True" type: PodScheduled containerStatuses: - containerID: docker://f5f6ea8d2bf8669efc7daf651a1404869c827a3fd2cbd6f85df8346cfd55de6d image: harbor.od.com/public/nginx:v1.7.9 imageID: docker-pullable://harbor.od.com/public/nginx@sha256:b1f5935eb2e9e2ae89c0b3e2e148c19068d91ca502e857052f14db230443e4c2 lastState: {} name: nginx ready: true restartCount: 0 state: running: startedAt: "2020-07-01T12:19:35Z" hostIP: 10.4.7.21 phase: Running podIP: 172.7.21.2 qosClass: BestEffort startTime: "2020-07-01T12:19:35Z"

    查看svc资源对应的配置清单

    [root@hdss7-21 ~]# kubectl get svc nginx-dp -o yaml -n kube-public apiVersion: v1 kind: Service metadata: creationTimestamp: "2020-07-01T04:12:24Z" labels: app: nginx-dp name: nginx-dp namespace: kube-public resourceVersion: "63896" selfLink: /api/v1/namespaces/kube-public/services/nginx-dp uid: b66f191e-0471-44ec-800a-f3ee6382b40f spec: clusterIP: 192.168.103.156 ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx-dp sessionAffinity: None type: ClusterIP status: loadBalancer: {}

    通过explain获得命令说明

    [root@hdss7-21 ~]# kubectl explain service.metadata KIND: Service VERSION: v1 RESOURCE: metadata <Object> DESCRIPTION: Standard object's metadata. More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata ObjectMeta is metadata that all persisted resources must have, which includes all objects users must create. ……

    通过声明式文件来生成svc资源

    [root@hdss7-21 ~]# vi nginx-ds-svc.yaml apiVersion: v1 kind: Service metadata: creationTimestamp: "2020-07-01T04:12:24Z" labels: app: 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-svc.yaml service/nginx-ds created [root@hdss7-21 ~]# kubectl get svc -n default NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 4d22h nginx-ds ClusterIP 192.168.68.128 <none> 80/TCP 22s # 通过查看svc资源,获得对应的资源清单信息 [root@hdss7-21 ~]# kubectl get svc nginx-ds -o yaml apiVersion: v1 kind: Service metadata: creationTimestamp: "2020-07-02T02:08:17Z" labels: app: nginx-ds name: nginx-ds namespace: default resourceVersion: "78933" selfLink: /api/v1/namespaces/default/services/nginx-ds uid: ab03e099-e113-481f-b8e2-01f56be56e66 spec: clusterIP: 192.168.68.128 ports: - port: 80 protocol: TCP targetPort: 80 selector: app: nginx-ds sessionAffinity: None type: ClusterIP status: loadBalancer: {}

    修改资源配置清单

    # 离线修改 [root@hdss7-21 ~]# vi nginx-ds-svc.yaml [root@hdss7-21 ~]# kubectl apply -f nginx-ds-svc.yaml # 在线修改 [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 4d22h nginx-ds ClusterIP 192.168.68.128 <none> 801/TCP 7m28s

    删除资源配置清单

    [root@hdss7-21 ~]# kubectl delete -f nginx-ds-svc.yaml service "nginx-ds" deleted [root@hdss7-21 ~]# kubectl get svc -n default NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 4d22h

    注: 增、删、查采用陈述式(命令行)管理K8s 改采用声明式(yaml)管理k8s

    Processed: 0.011, SQL: 9