kubectl绠$��

Kubectl��绠$��k8s��缇ょ���戒护琛�宸ュ�凤���杩�������json�煎�浼���缁�apiserver杩�琛���寤恒���ョ����绠$������浣�

娉ㄦ��锛�姝ゅ���瑕��ㄥ�版��浠�涔����ㄧ讲��K8s澶����圭���ㄧ讲��澧�锛�濡���杩����ㄧ讲����浠ュ��������涓�绡���绔�锛�https://blog.csdn.net/JarryZho/article/details/104212822

甯哥�ㄥ�戒护琛�锛�

`�ョ��甯��╁�戒护`
[root@master1 ~]# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         浣跨�� replication controller, service, deployment ���� pod 骞舵�拈�插��浣�涓轰�涓� �扮演��
Kubernetes Service
  run            �ㄩ��缇や腑杩�琛�涓�涓���疏浚�������
  set            涓� objects 璁剧疆涓�涓���疏浚����瑰�

Basic Commands (Intermediate):
  explain        �ョ��璧�婧�����妗�
  get            �剧ず涓�涓����村� resources
  edit           �ㄦ���″�ㄤ�缂�杈�涓�涓�璧�婧�
  delete         Delete resources by filenames, stdin, resources and names, or by resources and label selector

Deploy Commands:
  rollout        Manage the rollout of a resource
  scale          涓� Deployment, ReplicaSet, Replication Controller ���� Job 璁剧疆涓�涓��扮演�������伴��
  autoscale      ���ㄨ��翠�涓� Deployment, ReplicaSet, ���� ReplicationController �������伴��

Cluster Management Commands:
  certificate    淇��� certificate 璧�婧�.
  cluster-info   �剧ず��缇や俊��
  top            Display Resource (CPU/Memory/Storage) usage.
  cordon         ��璁� node 涓� unschedulable
  uncordon       ��璁� node 涓� schedulable
  drain          Drain node in preparation for maintenance
  taint          �存�颁�涓�����澶�涓� node 涓��� taints

Troubleshooting and Debugging Commands:
  describe       �剧ず涓�涓���疏浚� resource ���� group �� resources 璇���
  logs           杈��哄�瑰�ㄥ�� pod 涓����ュ�
  attach         Attach �颁�涓�杩�琛�涓��� container
  exec           �ㄤ�涓� container 涓��ц�涓�涓��戒护
  port-forward   Forward one or more local ports to a pod
  proxy          杩�琛�涓�涓� proxy �� Kubernetes API server
  cp             澶��� files �� directories �� containers ��浠�疏浚瑰�ㄤ腑澶��� files �� directories.
  auth           Inspect authorization

Advanced Commands:
  apply          ��杩���浠跺��������杈��ユ�(stdin)瀵硅�婧�杩�琛���缃�
  patch          浣跨�� strategic merge patch �存�颁�涓�璧�婧��� field(s)
  replace        ��杩� filename ���� stdin�挎��涓�涓�璧�婧�
  wait           Experimental: Wait for a specific condition on one or many resources.
  convert        �ㄤ����� API versions 潘��㈤��缃���浠�

Settings Commands:
  label          �存�板�ㄨ�涓�璧�婧�涓��� labels
  annotate       �存�颁�涓�璧�婧���娉ㄨВ
  completion     Output shell completion code for the specified shell (bash or zsh)

Other Commands:
  alpha          Commands for features in alpha
  api-resources  Print the supported API resources on the server
  api-versions   Print the supported API versions on the server, in the form of "group/version"
  config         淇��� kubeconfig ��浠�
  plugin         Provides utilities for interacting with plugins.
  version        杈��� client �� server ������淇℃��

�ヤ��ユ��浠�浠ラ」����褰㈠�璁插�朵腑���戒护

�舵�ラ�ゅ�涓�锛���寤轰�>��甯�涓�>�存�颁�>��婊�涓�>����

1.��寤�nginx

#璇�娉�锛�kubectl run NAME --image=image [--env="key=value"����] [--port=port绔���] [--replicas=replicas������] [--dry-run=bool�舵��] [--overrides=inline-json] [--command�戒护] -- [COMMAND] [args...] [options]`

`绀轰�锛�`
[root@master1 k8s]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created
[root@master1 k8s]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-7hdfj               1/1     Running   0          4d18h
`nginx-deployment-5477945587-2dljt   1/1     Running   0          68s`
`nginx-deployment-5477945587-tt8vx   1/1     Running   0          68s`
`nginx-deployment-5477945587-wsb69   1/1     Running   0          68s`
#����涓�涓�广大辨����浠��板��寤虹��������

2.��甯�nginx service��岛�璐�潘藉��琛$������

#璇�娉�锛�kubectl expose (-f FILENAME | TYPE NAME) [--port=port缇ら��涔��村���ㄩ��淇$��绔���] [--protocol=TCP|UDP|SCTP] [--target-port瀵瑰��拈�茬��绔���=number-or-name] [--name=name��疏浚���绉�] [--external-ip=external-ip-of-service] [--type=type��疏浚�绫诲��] [options]

`绀轰�锛�`
[root@master1 k8s]# kubectl expose deployment nginx-deployment --port=80 --target-port=80 --name=nginx-deployment-service --type=NodePort
service/nginx-deployment-service exposed

`�ョ����甯�`
[root@master1 k8s]# kubectl get pods,svc        #姝ゅ�svc浣�service���$�浠剁��缂╁��
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-dbddb74b8-7hdfj               1/1     Running   0          4d19h
pod/nginx-deployment-5477945587-2dljt   1/1     Running   0          13m
pod/nginx-deployment-5477945587-tt8vx   1/1     Running   0          13m
pod/nginx-deployment-5477945587-wsb69   1/1     Running   0          13m

NAME                    TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
service/kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        6d19h
service/nginx-service   NodePort    10.0.0.242   <none>        80:40422/TCP   111s
#姝ゆ�跺�瑰��拈�茬��绔��d负40422

`�ョ��璧�婧�瀵硅薄绠���`
[root@master1 k8s]# kubectl api-resources
NAME                              SHORTNAMES   APIGROUP                       NAMESPACED 
componentstatuses                 cs                                          false       
configmaps                        cm                                          true       
endpoints                         ep                                          true       
events                            ev                                          true       
limitranges                       limits                                      true       
namespaces                        ns                                          false       
nodes                             no                                          false       
persistentvolumeclaims            pvc                                         true       
persistentvolumes                 pv                                          false       
pods                              po                                          true      
replicationcontrollers            rc                                          true       
resourcequotas                    quota                                       true       
serviceaccounts                   sa                                          true       
services                          svc                                         true       
customresourcedefinitions         crd,crds     apiextensions.k8s.io           false       
daemonsets                        ds           apps                           true       
deployments                       deploy       apps                           true       
replicasets                       rs           apps                           true       
statefulsets                      sts          apps                           true       
horizontalpodautoscalers          hpa          autoscaling                    true       
cronjobs                          cj           batch                          true       
certificatesigningrequests        csr          certificates.k8s.io            false       
events                            ev           events.k8s.io                  true       
daemonsets                        ds           extensions                     true       
deployments                       deploy       extensions                     true       
ingresses                         ing          extensions                     true       
networkpolicies                   netpol       extensions                     true       
podsecuritypolicies               psp          extensions                     false       
replicasets                       rs           extensions                     true       
networkpolicies                   netpol       networking.k8s.io              true       
poddisruptionbudgets              pdb          policy                         true       
podsecuritypolicies               psp          policy                         false       
priorityclasses                   pc           scheduling.k8s.io              false       
storageclasses                    sc           storage.k8s.io                 false       

`�ョ���宠����绔�������`
[root@master1 k8s]# kubectl get endpoints
NAME                       ENDPOINTS                                      AGE
kubernetes                 192.168.18.128:6443,192.168.18.132:6443        7d4h
nginx-deployment-service   172.17.32.4:80,172.17.40.2:80,172.17.40.3:80   17s

`缃�缁��舵��璇�缁�淇℃��`
[root@master1 ~]# kubectl get pods -o wide
NAME                                READY   STATUS    RESTARTS   AGE    IP            NODE             NOMINATED NODE
nginx-dbddb74b8-7hdfj               1/1     Running   0          5d5h   172.17.32.2   192.168.18.148   <none>
nginx-deployment-5477945587-2dljt   1/1     Running   0          10h    172.17.40.3   192.168.18.145   <none>
nginx-deployment-5477945587-tt8vx   1/1     Running   0          10h    172.17.40.2   192.168.18.145   <none>
nginx-deployment-5477945587-wsb69   1/1     Running   0          10h    172.17.32.4   192.168.18.148   <none>

`���℃�拈�茬��绔���`
[root@master1 ~]# kubectl get svc
NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes                 ClusterIP   10.0.0.1     <none>        443/TCP        7d5h
nginx-deployment-service   NodePort    10.0.0.50    <none>        80:48330/TCP   70m

`��node1��浣�锛��ョ��璐�潘藉��琛$����48330`
`k8s��kube-proxy����涓�绉�妯″�锛���v1.8涔�����浠�浣跨�ㄧ����iptables浠ュ��userspace涓ょ�妯″�锛���k8s1.8涔���寮��ヤ�ipvs妯″�`
[root@node1 ~]# yum install ipvsadm -y
[root@node1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.18.148:48330 rr
  -> 172.17.32.4:80               Masq    1      0          0
  -> 172.17.40.2:80               Masq    1      0          0
  -> 172.17.40.3:80               Masq    1      0          0
#瀵瑰���岛�绔���48330锛�璋�搴�绠�娉�涓�rr潘�璇�

`��node2��浣� ���山��瑁�ipvsadmin宸ュ�锋�ョ��`
[root@node2 ~]# yum install ipvsadm -y
[root@node2 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.18.145:48330 rr
  -> 172.17.32.4:80               Masq    1      0          0
  -> 172.17.40.2:80               Masq    1      0          0
  -> 172.17.40.3:80               Masq    1      0          0
��浠��板�ㄥ�夸富�轰腑浣跨�ㄦ�瑙��ㄨ�块��192.168.18.148:48330��192.168.18.145:48330�藉��浠ヨ�块����nginx��涓婚〉锛��跺�����ョ���ュ�

�ㄨ������ュ�剧����杩�

�ㄨ������ュ�剧����杩�

`��master1��浣��ョ��璁块���ュ�锛�娉ㄦ��锛�濡���璁块���朵�node��娉�璁块��妫���proxy缁�浠讹�`
[root@master1 ~]# kubectl get pods      #姝ゆ�朵���涓�涓�������
NAME                                READY   STATUS    RESTARTS   AGE
nginx-dbddb74b8-7hdfj               1/1     Running   0          5d5h
nginx-deployment-5477945587-2dljt   1/1     Running   0          10h
nginx-deployment-5477945587-tt8vx   1/1     Running   0          10h
nginx-deployment-5477945587-wsb69   1/1     Running   0          10h

`姝ゆ�跺��澶存�ョ��璁块���ュ�锛�`
[root@master1 ~]# kubectl logs nginx-deployment-5477945587-2dljt
172.17.40.1 - - [12/Feb/2020:11:44:46 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
172.17.40.1 - - [12/Feb/2020:11:44:46 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.18.145:48330/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"

[root@master1 ~]# kubectl logs nginx-deployment-5477945587-tt8vx

[root@master1 ~]# kubectl logs nginx-deployment-5477945587-wsb69
172.17.32.1 - - [12/Feb/2020:11:47:07 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"
172.17.32.1 - - [12/Feb/2020:11:47:07 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.18.148:48330/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.131 Safari/537.36" "-"

#姝ゆ��nginx-deployment-5477945587-wsb69��nginx-deployment-5477945587-2dljt����琚�璁块��河渠�nginx��涓婚〉锛�璇存��涓��″凡缁�������甯�锛���涓�瀵规�圭�ㄧ����潘�璇㈢���瑰�璁块��澶�涓�Pod璧�婧�

3.�存��nginx 涓�1.14����

�ョ��nginx����姝ラ�わ�F12��寮�寮�������椤�--��F5�锋�拌�块��---���惧��network---���瑰��name---���惧��headers澶撮�ㄤ俊��

�ㄨ������ュ�剧����杩�

[root@master1 ~]# kubectl set image deployment/nginx-deployment nginx-deployment=nginx:1.14
deployment.extensions/nginx-deployment image updated

`澶�河渠��ㄦ�������舵��锛�姝ゆ�跺��浠ユ�ョ�����х�舵��`
[root@master1 ~]# kubectl get pods -w
NAME                                READY   STATUS              RESTARTS   AGE
nginx-6c94d899fd-8pf48              1/1     Running             0          3m54s
nginx-deployment-5477945587-2dljt   1/1     Running             0          10h
nginx-deployment-5477945587-tt8vx   1/1     Running             0          10h
nginx-deployment-5477945587-wsb69   1/1     Running             0          10h
nginx-deployment-8f66bcd89-jncdr    0/1     ContainerCreating   0          7s
#姝ゆ�跺����浠���浠ユ��Ctrl+c涓�������锛�杩��锋�存�伴��搴�蹇�
楠�璇�锛���浠���娆¢��潘藉�夸富�轰腑��nginx涓婚〉锛�姝ゆ�跺�ㄥ�������椤逛腑���扮演��澶撮�ㄤ俊����nginx�������存�颁负浜�1.14.2

�ㄨ������ュ�剧����杩�


4.��婊�nginx

`�ョ�����茬����`
[root@master1 ~]# kubectl rollout history deployment/nginx-deployment
deployment.extensions/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>    #1.17����
2         <none>    #1.14����

`�ц���婊��颁�涓�娆�`
[root@master1 ~]#  kubectl rollout undo deployment/nginx-deployment
deployment.extensions/nginx-deployment

`妫��ュ��婊��舵��`
[root@master1 ~]# kubectl rollout status deployment/nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
#��缁��剧ず������婊�
楠�璇�锛������板�夸富�雾��娴�瑙��ㄥ�锋��nginx涓婚〉锛�姝ゆ�跺�ㄩ〉�㈠��������╃��澶撮�ㄤ俊��涓����扮演��nginx����广大辨�㈠��颁�涔�����1.17.8

�ㄨ������ュ�剧����杩�


5.����nginx

`�ョ��deployment`
[root@master1 ~]# kubectl get deploy
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           11h
[root@master1 ~]# kubectl delete deployment/nginx-deployment
deployment.extensions "nginx-deployment" deleted

[root@master1 ~]# kubectl get deploy
No resources found.
[root@master1 ~]# kubectl get pods
No resources found.

`���ゆ����SVC`
[root@master1 ~]# kubectl get svc
NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes                 ClusterIP   10.0.0.1     <none>        443/TCP        7d6h
nginx-deployment-service   NodePort    10.0.0.50    <none>        80:48330/TCP   142m
[root@master1 ~]# kubectl delete svc/nginx-deployment-service
service "nginx-deployment-service" deleted  #姝ゆ�ラ�や负���ゆ�ラ��
[root@master1 ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.0.0.1     <none>        443/TCP        7d7h

6.�跺��

`�ョ���蜂�璧�婧���璇�缁�淇℃��`
[root@master1 ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created

[root@master1 ~]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-6c94d899fd-8pf48              1/1     Running   0          54m
nginx-deployment-5477945587-f5dsm   1/1     Running   0          15s
nginx-deployment-5477945587-hmgd2   1/1     Running   0          15s
nginx-deployment-5477945587-pl2hn   1/1     Running   0          15s

[root@master1 ~]# kubectl describe pod nginx-deployment-5477945587-f5dsm
Name:               nginx-deployment-5477945587-f5dsm
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               192.168.18.145/192.168.18.145
Start Time:         Wed, 12 Feb 2020 21:04:40 +0800
Labels:             pod-template-hash=5477945587
                    run=nginx-deployment
Annotations:        <none>
Status:             Running
IP:                 172.17.40.2
Controlled By:      ReplicaSet/nginx-deployment-5477945587
Containers:
  nginx-deployment:
    Container ID:   docker://670cb7230f200279b2accb344766e621ab97d279c8585cc27bd4e519dac9e677
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:ad5552c786f128e389a0263104ae39f3d3c7895579d45ae716f528185b36bc6f
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 12 Feb 2020 21:04:44 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-pbr9p (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-pbr9p:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-pbr9p
    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  19m   default-scheduler        Successfully assigned default/nginx-deployment-5477945587-f5dsm to 192.168.18.145
  Normal  Pulling    19m   kubelet, 192.168.18.145  pulling image "nginx"
  Normal  Pulled     19m   kubelet, 192.168.18.145  Successfully pulled image "nginx"
  Normal  Created    19m   kubelet, 192.168.18.145  Created container
  Normal  Started    19m   kubelet, 192.168.18.145  Started container

`�ョ��deployment璧�婧�`
[root@master1 ~]# kubectl describe deployment/nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 12 Feb 2020 21:04:40 +0800
Labels:                 run=nginx-deployment
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx-deployment
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=nginx-deployment
  Containers:
   nginx-deployment:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-5477945587 (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  21m   deployment-controller  Scaled up replica set nginx-deployment-5477945587 to 3

`杩���pod`
[root@master1 ~]# kubectl exec -it nginx-deployment-5477945587-f5dsm bash
root@nginx-deployment-5477945587-f5dsm:/#

��棰�����������锛�

`1:�舵��涓�NotReady`
[root@localhost bin]# kubectl get node
NAME              STATUS     ROLES    AGE     VERSION
192.168.195.150   NotReady   <none>   6d19h   v1.12.3
192.168.195.151   NotReady   <none>   6d18h   v1.12.3
#����: node ���规��娉�杩���apiserver
1)�����戊寅�master����  apiserver���℃����姝e父杩�琛�
systemctl restart kube-apiserver    澶辫触
cat /var/log/messages   �ュ�锛�绗�涓�娆¢�ㄧ讲锛�妫��ヨ��涔�锛�
2锛�澶����戊寅�濡���api-server杩�琛�姝e父
妫��ワ�璐�潘藉��琛′腑��VIP锛�妫���nginx璋�搴� ��灞�潘���妯″����缃�

`2锛�kubectl get pods �ヨ��podes璧�婧��舵��`
NAME                     READY   STATUS        RESTARTS   AGE
nginx-7697996758-jg47q   0/1     Pending       0          93s
nginx-7697996758-k967k   0/1     Pending       0          93s
nginx-7697996758-p5n8m   0/1     Pending       0          93s
��娉���缃�璧�婧���node���逛腑杩�琛���寤猴�澶�河渠�pending�舵��锛�
妫���node���逛腑kubelet���$�浠讹�kubeconfig��缃�

`3锛�疏浚㈡�锋��娉�璁块��pods��甯�������`
ipvsadm -L -n   璐�潘藉��琛�    瀵瑰���岛���绔���44888
#姝e父���垫��涓�node搴�璇ユ��浠ヤ��舵��锛�
TCP  192.168.195.151:44888 rr
  -> 172.17.68.2:80               Masq    1      0          0    
  -> 172.17.68.3:80               Masq    1      0          0     
  -> 172.17.86.2:80               Masq    1      0          0 
#����锛�kube-proxy缁�浠舵����