k8s常用命令记录

kubectl 相关

kubectl get - 列出资源
kubectl describe - 显示有关资源的详细信息
kubectl logs - 打印 pod 和其中容器的日志
kubectl logs $POD_NAME
kubectl exec - 在 pod 中的容器上执行命令

kubectl version

获取客户端版本与服务端版本
Client Version: version.Info{Major:“1”, Minor:“17”, GitVersion:“v1.17.3”, GitCommit:“06ad960bfd03b39c8310aaf92d1e7c12ce618213”, GitTreeState:“clean”, BuildDate:“2020-02-11T18:14:22Z”, GoVersion:“go1.13.6”, Compiler:“gc”, Platform:“linux/amd64”}
Server Version: version.Info{Major:“1”, Minor:“17”, GitVersion:“v1.17.3”, GitCommit:“06ad960bfd03b39c8310aaf92d1e7c12ce618213”, GitTreeState:“clean”, BuildDate:“2020-02-11T18:07:13Z”, GoVersion:“go1.13.6”, Compiler:“gc”, Platform:“linux/amd64”}

kubectl cluster-info

获取集群信息
Kubernetes master is running at https://172.17.0.11:8443
KubeDNS is running at https://172.17.0.11:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

kubectl get nodes

获取所有的节点
NAME STATUS ROLES AGE VERSION
minikube Ready master 9m15s v1.17.3

kubectl get pods

获取pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-765bf4c7b4-p54md 1/1 Running 0 9m52s

kubectl create deployment

创建deployment

kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

kubectl get deployments

获取deployment
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 14m

kubectl get services

列出集群当中的所有服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 16m

kubectl expose deployment/kubernetes-bootcamp --type=“NodePort” --port 8080

暴露服务公开给外部流量,应该使用带NodePort的参数
service/kubernetes-bootcamp exposed

现在使用get services应该能看到有两个服务,
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 21m
kubernetes-bootcamp NodePort 10.99.211.75 8080:30447/TCP 102s
一个TYPE是ClusterIP ,只能是集群内部使用,
另一个就是刚创建的 TYPE是NodePort 的服务

kubectl describe services/kubernetes-bootcamp

可以使用describe 查看 资源的详细信息
NodePort选项可以看见哪一个端口被公开给外部流量
To find out what port was opened externally (by the NodePort option) we’ll run the describe service command

kubectl describe services/kubernetes-bootcamp
Name:                     kubernetes-bootcamp
Namespace:                default
Labels:                   run=kubernetes-bootcamp
Annotations:              <none>
Selector:                 run=kubernetes-bootcamp
Type:                     NodePort
IP:                       10.99.211.75
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  30447/TCP
Endpoints:                172.18.0.3:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

kubectl get rs

查看deployment创建的副本集(ReplicaSet)

Notice that the name of the ReplicaSet is always formatted as [DEPLOYMENT-NAME]-[RANDOM-STRING]. The random string is randomly generated and uses the pod-template-hash as a seed.
副本集的名字总是被格式化为【deployment的名字】-【随机字符串】,这个随机字符串使用pod-template-hash作为种子随机生成

NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-765bf4c7b4 1 1 1 3m20s

kubectl scale deployments/kubernetes-bootcamp --replicas=4

deployment.apps/kubernetes-bootcamp scaled

use the kubectl scale command, followed by the deployment type, name and desired number of instances:
使用扩展scale 命令,后面跟上deployment类型,deployment的名字和想要(desired )的实例副本数量

这个时候可以看看扩展后的deployment的状态了

kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   4/4     4            4           9m52s

也可以看到pods的数量也发生了变化

kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE     IP          NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-765bf4c7b4-8w6cb   1/1     Running   0          4m51s   172.18.0.8   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-dxgsb   1/1     Running   0          4m51s   172.18.0.7   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-ftmjf   1/1     Running   0          4m51s   172.18.0.9   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-jmzrj   1/1     Running   0          12m     172.18.0.2   minikube   <none>           <none>
$ kubectl get nodes
NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   13m   v1.17.3

There are 4 Pods now, with different IP addresses. The change was registered in the Deployment events log. To check that, use the describe command:
现在有着4个不同id的pod,这次改变已经被记录在了Deployment的事件log中。

kubectl describe deployments/kubernetes-bootcamp
Name:                   kubernetes-bootcamp
Namespace:              default
CreationTimestamp:      Tue, 11 Aug 2020 05:57:10 +0000
Labels:                 run=kubernetes-bootcamp
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=kubernetes-bootcamp
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  run=kubernetes-bootcamp
  Containers:
   kubernetes-bootcamp:
    Image:        gcr.io/google-samples/kubernetes-bootcamp:v1
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   kubernetes-bootcamp-765bf4c7b4 (4/4 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  16m    deployment-controller  Scaled up replica set kubernetes-bootcamp-765bf4c7b4 to 1
  Normal  ScalingReplicaSet  8m56s  deployment-controller  Scaled up replica set kubernetes-bootcamp-765bf4c7b4 to 4

可以再使用kubectl describe services/kubernetes-bootcamp命令查看暴露出去的服务,
现在可以发现服务端点从一个变成了多个

Endpoints:                172.18.0.2:8080,172.18.0.7:8080,172.18.0.8:8080 + 1 more...

Note:
kubectl scale deployments/kubernetes-bootcamp --replicas=2
命令也可以缩放我们的副本数量,可以看见有2个pod正在被中断中

$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   2/2     2            2           31m
$ kubectl get pods -o wide
NAME                                   READY   STATUS        RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-765bf4c7b4-8w6cb   1/1     Terminating   0          24m   172.18.0.8   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-dxgsb   1/1     Running       0          24m   172.18.0.7   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-ftmjf   1/1     Terminating   0          24m   172.18.0.9   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-jmzrj   1/1     Running       0          31m   172.18.0.2   minikube   <none>           <none>
$ kubectl get pods -o wide
NAME                                   READY   STATUS    RESTARTS   AGE   IP        NODE       NOMINATED NODE   READINESS GATES
kubernetes-bootcamp-765bf4c7b4-dxgsb   1/1     Running   0          25m   172.18.0.7   minikube   <none>           <none>
kubernetes-bootcamp-765bf4c7b4-jmzrj   1/1     Running   0          32m   172.18.0.2   minikube   <none>           <none>

kubectl set image

滚动更新app实例

$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-765bf4c7b4-bhggc   1/1     Running   0          108s
kubernetes-bootcamp-765bf4c7b4-cfwx6   1/1     Running   0          108s
kubernetes-bootcamp-765bf4c7b4-tbxjw   1/1     Running   0          108s
kubernetes-bootcamp-765bf4c7b4-v5d8f   1/1     Running   0          108s

$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated

$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-7d6f8694b6-7cxlf   1/1     Running   0          2m13s
kubernetes-bootcamp-7d6f8694b6-8jrzq   1/1     Running   0          2m13s
kubernetes-bootcamp-7d6f8694b6-cz65w   1/1     Running   0          2m17s
kubernetes-bootcamp-7d6f8694b6-f74tc   1/1     Running   0          2m17s

这个命令通知了deployment为应用程序使用其他镜像,并且启动了滚动更新
注意,新的4个pod实例已经改变了,它们使用了新的不同的ip。
查看一下deployment的event log。

kubectl rollout status deployments/kubernetes-bootcamp

可以使用rollout 命令来确认是否更新成功

$ kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out

kubectl rollout undo

当更新镜像失败时,可以使用rollout命令回滚到以前任何已知的部署状态

现在发布一个不存在的镜像v10

$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.apps/kubernetes-bootcamp image updated

发现期望数量的pod实例没有满足,

$ kubectl get deployments
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-bootcamp   3/4     2            3           22m

查看pods,发现886577c5d的状态为ErrImagePull

$ kubectl get pods
NAME                                   READY   STATUS         RESTARTS   AGE
kubernetes-bootcamp-7d6f8694b6-7cxlf   0/1     Terminating    0          18m
kubernetes-bootcamp-7d6f8694b6-8jrzq   1/1     Running        0          18m
kubernetes-bootcamp-7d6f8694b6-cz65w   1/1     Running        0          18m
kubernetes-bootcamp-7d6f8694b6-f74tc   1/1     Running        0          18m
kubernetes-bootcamp-886577c5d-g82nc    0/1     ErrImagePull   0          36s
kubernetes-bootcamp-886577c5d-grtgv    0/1     ErrImagePull   0          36s

使用更详细的命令来查看每个pod,这里我只截取了其中的一个pod,在Events:可以看见在拉取镜像的时候发生了错误

kubectl describe pods
Name:         kubernetes-bootcamp-886577c5d-g82nc
Namespace:    default
Priority:     0
Node:         minikube/172.17.0.12
Start Time:   Tue, 11 Aug 2020 07:00:39 +0000
Labels:       pod-template-hash=886577c5d
              run=kubernetes-bootcamp
Annotations:  <none>
Status:       Pending
IP:           172.18.0.5
IPs:
  IP:           172.18.0.5
Controlled By:  ReplicaSet/kubernetes-bootcamp-886577c5d
Containers:
  kubernetes-bootcamp:
    Container ID:
    Image:          gcr.io/google-samples/kubernetes-bootcamp:v10
    Image ID:
    Port:           8080/TCP
    Host Port:      0/TCP
    State:          Waiting
      Reason:       ErrImagePull
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-rgfjf (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  default-token-rgfjf:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-rgfjf
    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  103s                default-scheduler  Successfully assigned default/kubernetes-bootcamp-886577c5d-g82nc to minikube
  Normal   Pulling    17s (x4 over 102s)  kubelet, minikube  Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
  Warning  Failed     17s (x4 over 101s)  kubelet, minikube  Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found: manifest unknown: Failed to fetch "v10" from request "/v2/google-samples/kubernetes-bootcamp/manifests/v10".
  Warning  Failed     17s (x4 over 101s)  kubelet, minikube  Error: ErrImagePull
  Normal   BackOff    4s (x5 over 100s)   kubelet, minikube  Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
  Warning  Failed     4s (x5 over 100s)   kubelet, minikube  Error: ImagePullBackOff

执行命令开始回滚,然后执行describe pods查看详细的每个pod的镜像id,已经全部回复到了v2

$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
$ kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
kubernetes-bootcamp-7d6f8694b6-4s4kd   1/1     Running   0          5s
kubernetes-bootcamp-7d6f8694b6-8jrzq   1/1     Running   0          27m
kubernetes-bootcamp-7d6f8694b6-cz65w   1/1     Running   0          27m
kubernetes-bootcamp-7d6f8694b6-f74tc   1/1     Running   0          27m
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值