K8s常用命令和扩缩容

1、查看pod

kubectl get pod

K8s常用命令和扩缩容

 

 

 

2、查看pod的具体信息

 kubectl describe  pod nginx-554b9c67f9-n62dr

Name:           nginx-554b9c67f9-n62dr
Namespace:      default
Priority:       0
Node:           node2/192.168.127.137
Start Time:     Mon, 30 Nov 2020 22:35:06 +0800
Labels:         app=nginx
                pod-template-hash=554b9c67f9
Annotations:    <none>
Status:         Running
IP:             10.244.1.2
Controlled By:  ReplicaSet/nginx-554b9c67f9
Containers:
  nginx:
    Container ID:   docker://47ab61ec2c3e8cb81e68c3514d695030ebf8ab6620c30e784238a5e298c7f2c8
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:6b1daa9462046581ac15be20277a7c75476283f969cb3a61c8725ec38d3b01c3
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Mon, 30 Nov 2020 22:37:46 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-l5p9n (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-l5p9n:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-l5p9n
    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:          <none>

  

3、查看pod的基本信息

kubectl get pod -o wide

K8s常用命令和扩缩容

 

 IP地址为 10.244.1.2

NODE为node2  192.168.127.137

K8s常用命令和扩缩容

 

 

4、在master节点(node1) 查看 nginx(node2)

查看nginx的根目录 

kubectl exec nginx-554b9c67f9-n62d  ls /

 

5、登录(nginx) 容器

 kubectl exec -it nginx-554b9c67f9-n62dr  /bin/bash

 

6、创建nginx-deployment,并进行弹性扩容

cd /home/tools/k8s

vi mynginx-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web_server
  template:
    metadata:
      labels:
        app: web_server
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9

kubectl apply -f mynginx-deployment.yml

K8s常用命令和扩缩容

 

 查看描述

[root@node1 k8s]# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Tue, 01 Dec 2020 22:42:08 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
                        kubectl.kubernetes.io/last-applied-configuration:
                          {"apiVersion":"apps/v1","kind":"Deployment","metadata":{"annotations":{},"name":"nginx-deployment","namespace":"default"},"spec":{"replica...
Selector:               app=web_server
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:  app=web_server
  Containers:
   nginx:
    Image:        nginx:1.7.9
    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-deployment-5d76d6897d (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  9m51s  deployment-controller  Scaled up replica set nginx-deployment-5d76d6897d to 3

 扩充了3个 Scaled up replica set nginx-deployment-5d76d6897d to 3

查看pod

 K8s常用命令和扩缩容

 

 

弹性扩容方式1

kubectl edit deployment nginx-deployment

将replicas: 3 改为 4,然后保存

K8s常用命令和扩缩容

 

 kubectl get deployment nginx-deployment 可以看到nginx-deployment有4个nginx了

K8s常用命令和扩缩容

 

 

kubectl get pod

K8s常用命令和扩缩容

 

 

弹性扩容方式2

vi mynginx-deployment.yml

K8s常用命令和扩缩容

 

 kubectl apply -f mynginx-deployment.yml 

kubectl get deployment nginx-deployment 可以看到nginx-deployment有5个nginx了

K8s常用命令和扩缩容

 

 同理,缩容只需要将数字改小即可。

 

上一篇:Deployment介绍


下一篇:Intellij IDEA Maven仓库路径配置