天天看點

Kubernetes使用指令行實作副本的管理

前言:為了更好地的了解Kubernetes,我們平時在寫yaml檔案的時候可以直直接使用kubectl的一些指令直接建立,好處是能夠快速的建立我們需要的資源類型,壞處是他的可定制性沒有直接寫yaml檔案高。

step1建立deployment并建立service
[[email protected] ~]# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2
[[email protected] ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP   39d
myapp        ClusterIP   10.96.88.100    <none>        80/TCP    5s
[[email protected] ~]# curl 10.96.88.100
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
[[email protected] ~]# curl 10.96.88.100/hostname.html
myapp-7c468db58f-xrmqx
[[email protected] ~]# curl 10.96.88.100/hostname.html
myapp-7c468db58f-rdpkq
[[email protected] ~]#

           

總結:clusterIP的service類型隻能在叢集内部通路,而且通路的方式是輪詢

step2增加副本
[[email protected] ~]# kubectl scale --replicas=5 deployment myapp
deployment.apps/myapp scaled
[[email protected] ~]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
myapp-7c468db58f-84c4k       1/1     Running   0          8s
myapp-7c468db58f-dvll2       1/1     Running   0          8s
myapp-7c468db58f-qx45d       1/1     Running   0          8s
myapp-7c468db58f-rdpkq       1/1     Running   0          11m
myapp-7c468db58f-xrmqx       1/1     Running   0          11m
           
step3減少副本
[[email protected] ~]# kubectl scale --replicas=3 deployment myapp
deployment.apps/myapp scaled
[[email protected] ~]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
myapp-7c468db58f-dvll2       1/1     Running   0          60s
myapp-7c468db58f-rdpkq       1/1     Running   0          12m
myapp-7c468db58f-xrmqx       1/1     Running   0          12m
           
step4鏡像的更新
[[email protected] ~]# kubectl set image deployments.apps myapp myapp=ikubernetes/myapp:v2
deployment.apps/myapp image updated
[[email protected] ~]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
myapp-64758bffd4-7w2vc       1/1     Running   0          5s
myapp-64758bffd4-frgpq       1/1     Running   0          6s
[[email protected] ~]# kubectl describe pod myapp-64758bffd4-7w2vc |grep Image
    Image:          ikubernetes/myapp:v2
[[email protected] ~]# curl 10.96.88.100
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
           
step5復原鏡像
[[email protected] ~]# kubectl rollout undo deployment myapp
deployment.apps/myapp rolled back
[[email protected] ~]# kubectl get pod
NAME                         READY   STATUS    RESTARTS   AGE
myapp-7c468db58f-9dksr       1/1     Running   0          17s
myapp-7c468db58f-dxhxz       1/1     Running   0          20s
myapp-7c468db58f-tmfxh       1/1     Running   0          19s
[[email protected] ~]# curl 10.96.88.100
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
           
将type的類型從ClusterIP改寫成NodePort
[[email protected] ~]# kubectl edit service myapp
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-08-29T07:06:48Z"
  labels:
    run: myapp
  name: myapp
  namespace: default
  resourceVersion: "79947"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: 9f9e448b-82a0-4c7f-b8bd-46ca89ef28df
spec:
  clusterIP: 10.96.88.100
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 31051
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: myapp
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
           

檢視暴露的端口

[[email protected] ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        39d
myapp        NodePort    10.96.88.100    <none>        80:31051/TCP   27m
           
Kubernetes使用指令行實作副本的管理
Kubernetes使用指令行實作副本的管理

備注:上面的實驗是在平時不想寫yaml檔案的時候可以嘗試的使用對新手比較友好

要是平時寫的時候我們搞忘了某個字段的拼寫我們可以通過以下的方式去查找我們需要的相關資訊。

[[email protected] ~]# kubectl explain -h
List the fields for supported resources

 This command describes the fields associated with each supported API resource.
Fields are identified via a simple JSONPath identifier:

  <type>.<fieldName>[.<fieldName>]

 Add the --recursive flag to display all of the fields at once without
descriptions. Information about each field is retrieved from the server in
OpenAPI format.

Use "kubectl api-resources" for a complete list of supported resources.

Examples:
  # Get the documentation of the resource and its fields
  kubectl explain pods

  # Get the documentation of a specific field of a resource
  kubectl explain pods.spec.containers

Options:
      --api-version='': Get different explanations for particular API version
      --recursive=false: Print the fields of fields (Currently only 1 level
deep)

Usage:
  kubectl explain RESOURCE [options]

Use "kubectl options" for a list of global command-line options (applies to all
commands).
[[email protected] ~]#
           

可以簡單的将explain的字段檢視方式處理為kubectl <type>.<fieldName>[.<fieldName>]

示例

[[email protected] ~]# kubectl explain pod
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources

   kind <string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds

   metadata     <Object>
     Standard object's metadata. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata

   spec <Object>
     Specification of the desired behavior of the pod. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

   status       <Object>
     Most recently observed status of the pod. This data may not be up to date.
     Populated by the system. Read-only. More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

[[email protected] ~]#

           

通過上面的幫助文檔我們可以大緻的分析出建構一個pod的基本需要的字段,如果想檢視更多的字段的資訊加上參數。

[[email protected] ~]# kubectl explain pod --recursive
KIND:     Pod
VERSION:  v1

DESCRIPTION:
     Pod is a collection of containers that can run on a host. This resource is
     created by clients and scheduled onto hosts.

FIELDS:
   apiVersion   <string>
   kind <string>
   metadata     <Object>
      annotations       <map[string]string>
      clusterName       <string>
      creationTimestamp <string>
      deletionGracePeriodSeconds        <integer>
      deletionTimestamp <string>
      finalizers        <[]string>
      generateName      <string>
      generation        <integer>
      labels    <map[string]string>
      managedFields     <[]Object>
         apiVersion     <string>
         fieldsType     <string>
         fieldsV1       <map[string]>
         manager        <string>
         operation      <string>
         time   <string>
      name      <string>
      namespace <string>
      ownerReferences   <[]Object>
         apiVersion     <string>
         blockOwnerDeletion     <boolean>
         controller     <boolean>
         kind   <string>
         name   <string>
         uid    <string>
      resourceVersion   <string>
      selfLink  <string>
      uid       <string>
   spec <Object>
      activeDeadlineSeconds     <integer>
      affinity  <Object>
         nodeAffinity   <Object>
            preferredDuringSchedulingIgnoredDuringExecution     <[]Object>
...................
           

檢視某個字段的相信資訊.隻要字段的類型是object都可以使用這種方式檢視幫助資訊

[[email protected] ~]# kubectl explain pod.spec.containers --recursive
KIND:     Pod
VERSION:  v1

RESOURCE: containers <[]Object>

DESCRIPTION:
     List of containers belonging to the pod. Containers cannot currently be
     added or removed. There must be at least one container in a Pod. Cannot be
     updated.

     A single application container that you want to run within a pod.

FIELDS:
   args <[]string>
   command      <[]string>
   env  <[]Object>
      name      <string>
      value     <string>
      valueFrom <Object>
         configMapKeyRef        <Object>
            key <string>
            name        <string>
            optional    <boolean>
         fieldRef       <Object>
            apiVersion  <string>
            fieldPath   <string>
         resourceFieldRef       <Object>
            containerName       <string>
            divisor     <string>
            resource    <string>
         secretKeyRef   <Object>
            key <string>
            name        <string>
            optional    <boolean>
   envFrom      <[]Object>
           

繼續閱讀