我是靠谱客的博主 开放马里奥,最近开发中收集的这篇文章主要介绍Kubernetes集群常用命令,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

Kubernetes集群常用命令

      • Namespace
          • 查看
          • 创建
          • 删除
          • 配置文件方式
      • Pod
          • 创建并运行Pod
          • 查看Pod
          • 访问Pod
          • 删除指定Pod
          • 配置方式操作
      • Label
          • 命令方式
          • 配置方式
      • Deployment
          • 命令操作
          • 配置操作
      • Service
          • 创建集群内部可访问的Service
          • 创建集群外部也可访问的Service
          • 删除Service
          • 配置方式

Namespace

查看
# 1 查看所有的ns  命令:kubectl get ns
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   20d
kube-node-lease   Active   20d
kube-public       Active   20d
kube-system       Active   20d
# 2 查看指定的ns   命令:kubectl get ns ns名称
[root@master ~]# kubectl get ns kube-system
NAME          STATUS   AGE
kube-system   Active   20d
# 3 指定输出格式  命令:kubectl get ns ns名称  -o 格式参数
# kubernetes支持的格式有很多,比较常见的是wide、json、yaml
[root@master ~]# kubectl get ns kube-system -o yaml
apiVersion: v1
kind: Namespace
metadata:
  creationTimestamp: "2021-11-30T12:07:17Z"
  name: kube-system
  resourceVersion: "8"
  selfLink: /api/v1/namespaces/kube-system
  uid: 500ba364-475f-4b38-ac87-6694d8e500d9
spec:
  finalizers:
  - kubernetes
status:
  phase: Active
# 4 查看ns详情  命令:kubectl describe ns ns名称
[root@master ~]# kubectl describe ns kube-system
Name:         kube-system
Labels:       <none>
Annotations:  <none>
Status:       Active # Active 命名空间正在使用中  Terminating 正在删除命名空间
# ResourceQuota针对namespace做的资源限制
# LimitRange针对namespace中的每个组件做的资源限制
No resource quota.
No LimitRange resource.
创建
# 创建namespace
[root@master ~]# kubectl create ns dev
namespace/dev created
删除
[root@master ~]# kubectl delete ns dev
namespace "dev" deleted
配置文件方式

准备一个yaml文件:ns-dev.yaml

apiVersion: v1
kind: Namespace
metadata:
  name: dev

执行对应的创建和删除命令了:

# 创建
[root@master data]# kubectl create -f ns-dev.yaml 
namespace/dev created
# 删除
[root@master data]# kubectl delete -f ns-dev.yaml 
namespace "dev" deleted

Pod

查看kubernetes集群中的各个组件Pod

[root@master data]# kubectl get pod -n kube-system
NAME                             READY   STATUS    RESTARTS   AGE
coredns-6955765f44-nkm8n         1/1     Running   5          20d
coredns-6955765f44-sj6zb         1/1     Running   5          20d
etcd-master                      1/1     Running   15         20d
kube-apiserver-master            1/1     Running   17         20d
kube-controller-manager-master   1/1     Running   21         20d
kube-flannel-ds-4zld7            1/1     Running   7          19d
kube-flannel-ds-96hzv            1/1     Running   2          19d
kube-flannel-ds-q9fkv            1/1     Running   9          19d
kube-proxy-ggtln                 1/1     Running   3          19d
kube-proxy-s8rbr                 1/1     Running   14         20d
kube-proxy-t2vzq                 1/1     Running   11         19d
kube-scheduler-master            1/1     Running   20         20d
创建并运行Pod

kubernetes没有提供单独运行Pod的命令,都是通过Pod控制器来实现的

# 命令格式: kubectl run (pod控制器名称) [参数] 
# --image  指定Pod的镜像
# --port   指定端口
# --namespace/-n  指定namespace
[root@master data]# kubectl run nginx-dev --image=nginx:latest --port=80 --namespace dev
deployment.apps/nginx-dev created
查看Pod
# 查看Pod基本信息
[root@master home]# kubectl get pod -n dev
NAME                               READY   STATUS    RESTARTS   AGE
nginx-dev-58476b668f-m9z6r         1/1     Running   0          23h
nginx-dev-ectype-985c586cc-4dsmh   1/1     Running   0          23h

# 查看Pod的详细信息
[root@master home]# kubectl describe pod nginx-dev-ectype-985c586cc-4dsmh -n dev
Name:         nginx-dev-ectype-985c586cc-4dsmh
Namespace:    dev
Priority:     0
Node:         node2/192.168.6.130
Start Time:   Tue, 21 Dec 2021 11:41:33 +0800
Labels:       pod-template-hash=985c586cc
              run=nginx-dev-ectype
Annotations:  <none>
Status:       Running
IP:           10.244.2.11
IPs:
  IP:           10.244.2.11
Controlled By:  ReplicaSet/nginx-dev-ectype-985c586cc
Containers:
  nginx-dev-ectype:
    Container ID:   docker://49f6547fd6e3fd258b458de5ed6ca6e190fee1409af7d79ad1db74be838ca0f8
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:d13dca1855de09e2fe392c58a66dd73d4ff4b71da4d1720bcf3f47b48c53ca1d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Tue, 21 Dec 2021 11:42:17 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-6rz8v (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  default-token-6rz8v:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-6rz8v
    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>
访问Pod
# 获取PodIP
[root@master home]# kubectl get pod -n dev -o wide
NAME                               READY   STATUS    RESTARTS   AGE   IP            NODE    NOMINATED NODE   READINESS GATES
nginx-dev-58476b668f-m9z6r         1/1     Running   0          23h   10.244.1.2    node1   <none>           <none>
nginx-dev-ectype-985c586cc-4dsmh   1/1     Running   0          23h   10.244.2.11   node2   <none>           <none>

# 访问Pod
[root@master home]# curl 10.244.2.11
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
删除指定Pod
# 删除指定Pod
[root@master home]# kubectl delete pod nginx-dev-ectype-985c586cc-4dsmh -n dev
pod "nginx-dev-ectype-985c586cc-4dsmh" deleted
# 此时,显示删除Pod成功,但是再查询,发现又新产生了一个 
[root@master home]# kubectl get pod -n dev
NAME                               READY   STATUS    RESTARTS   AGE
nginx-dev-58476b668f-m9z6r         1/1     Running   0          23h
nginx-dev-ectype-985c586cc-tqlpq   1/1     Running   0          84s

# 这是因为当前Pod是由Pod控制器创建的,控制器会监控Pod状况,一旦发现Pod死亡,会立即重建
# 此时要想删除Pod,必须删除Pod控制器

# 查询当前namespace下的Pod控制器
[root@master home]# kubectl get deploy -n dev
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dev          1/1     1            1           23h
nginx-dev-ectype   1/1     1            1           23h

# 删除此PodPod控制器
[root@master home]# kubectl delete deploy nginx-dev-ectype -n dev
deployment.apps "nginx-dev-ectype" deleted

# 稍等片刻,再查询Pod,发现Pod被删除了
[root@master home]# kubectl get pod -n dev
NAME                         READY   STATUS    RESTARTS   AGE
nginx-dev-58476b668f-m9z6r   1/1     Running   0          23h
配置方式操作

创建一个pod-nginx.yaml,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:latest
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP

执行对应的创建和删除命令

# 创建
[root@master home]# kubectl create -f pod-nginx.yaml 
pod/nginx created
# 删除
[root@master home]# kubectl delete -f pod-nginx.yaml 
pod "nginx--dev" deleted

Label

一些常用的Label 示例如下:

  • 版本标签:“version”:“release”, “version”:“stable”…
  • 环境标签:“environment”:“dev”,“environment”:“test”,“environment”:“pro”
  • 架构标签:“tier”:“frontend”,“tier”:“backend”

有两种Label Selector:

  • 基于等式的Label Selector

    name = slave: 选择所有包含Label中key="name"且value="slave"的对象

    env != production: 选择所有包括Label中的key="env"且value不等于"production"的对象

  • 基于集合的Label Selector

    name in (master, slave): 选择所有包含Label中的key="name"且value="master"或"slave"的对象

    name not in (frontend): 选择所有包含Label中的key="name"且value不等于"frontend"的对象

标签的选择条件可以使用多个,此时将多个Label Selector进行组合,使用逗号","进行分隔即可。例如:

name=slave,env!=production

name not in (frontend),env!=production

命令方式
# 为Pod资源打标签
[root@master home]# kubectl label pod nginx version=1.0.0 -n dev
pod/nginx labeled

# 查看Pod标签
[root@master home]# kubectl get pod nginx -ndev --show-labels
NAME    READY   STATUS    RESTARTS   AGE    LABELS
nginx   1/1     Running   0          143m   version=1.0.0

# 为pod资源更新标签
[root@master home]# kubectl label pod nginx version=2.0.0 -n dev --overwrite
pod/nginx labeled

# 查看Pod标签
[root@master home]# kubectl get  pod nginx -ndev --show-labels
NAME    READY   STATUS    RESTARTS   AGE    LABELS
nginx   1/1     Running   0          4h7m   version=2.0.0

# 筛选标签
[root@master home]# kubectl get pod -ndev -l version=2.0.0 --show-labels
NAME    READY   STATUS    RESTARTS   AGE     LABELS
nginx   1/1     Running   0          4h10m   version=2.0.0
[root@master home]# kubectl get pod -ndev -l version!=2.0.0 --show-labels
NAME                         READY   STATUS    RESTARTS   AGE   LABELS
nginx-dev-58476b668f-m9z6r   1/1     Running   0          28h   pod-template-hash=58476b668f,run=nginx-dev

# 删除标签
[root@master home]# kubectl label pod nginx version- -ndev
pod/nginx labeled
配置方式
apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
  labels:
    version: "3.0" 
    env: "test"
spec:
  containers:
  - image: nginx:latest
    name: pod
    ports:
    - name: nginx-port
      containerPort: 80
      protocol: TCP
# 执行对应的更新命令
[root@master data]# kubectl apply -f pod-nginx.yaml
pod/nginx configured

Deployment

image-20200408193950807

命令操作
# 命令格式: kubectl create deployment 名称  [参数] 
# --image  指定pod的镜像
# --port   指定端口
# --replicas  指定创建pod数量
# --namespace  指定namespace
[root@master data]# kubectl run nginx-test --image=nginx:latest --port=80 --replicas=3 -ndev
deployment.apps/nginx-test created

# 查看创建的Pod
[root@master data]# kubectl get pod -ndev
NAME                          READY   STATUS    RESTARTS   AGE
nginx                         1/1     Running   0          4h48m
nginx-dev-58476b668f-m9z6r    1/1     Running   0          28h
nginx-test-5687b54bfc-kc9mw   1/1     Running   0          2m2s
nginx-test-5687b54bfc-rrbgb   1/1     Running   0          2m2s
nginx-test-5687b54bfc-wc9md   1/1     Running   0          2m2s

# 查看deployment的信息
# UP-TO-DATE:成功升级的副本数量
# AVAILABLE:可用副本的数量
[root@master data]# kubectl get deploy -ndev
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
nginx-dev    1/1     1            1           29h
nginx-test   3/3     3            3           26m

# 查看选择器
[root@master data]# kubectl get deploy -ndev -owide
NAME         READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-dev    1/1     1            1           29h   nginx-dev    nginx:latest   run=nginx-dev
nginx-test   3/3     3            3           33m   nginx-test   nginx:latest   run=nginx-test

# 查看deployment的详细信息
[root@master data]# kubectl describe deploy nginx-test -ndev
Name:                   nginx-test
Namespace:              dev
CreationTimestamp:      Wed, 22 Dec 2021 16:19:26 +0800
Labels:                 run=nginx-test
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               run=nginx-test
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-test
  Containers:
   nginx-test:
    Image:        nginx:latest
    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-test-5687b54bfc (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  38m   deployment-controller  Scaled up replica set nginx-test-5687b54bfc to 3
  
# 删除 
[root@master data]# kubectl delete deploy nginx-test -ndev
deployment.apps "nginx-test" deleted
配置操作

创建一个deploy-nginx.yaml,内容如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx:latest
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
# 创建:
[root@master data]# kubectl create -f deploy-nginx.yaml 
deployment.apps/nginx-deploy created
# 删除:
[root@master data]# kubectl delete -f deploy-nginx.yaml 
deployment.apps "nginx-deploy" deleted

Service

Service可以看作是一组同类Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。

image-20200408194716912

创建集群内部可访问的Service
# 暴露Service
[root@master data]# kubectl expose deploy nginx-deploy --name svc-nginx-deploy-test --type=ClusterIP -ndev
service/svc-nginx-deploy-test exposed

# 查看service
[root@master data]# kubectl get svc svc-nginx-deploy-test -ndev -owide
NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE    SELECTOR
svc-nginx-deploy-test   ClusterIP   10.109.82.64   <none>        80/TCP    109s   run=nginx
# 这里产生了一个CLUSTER-IP,这就是service的IP,在Service的生命周期中,这个地址是不会变动的
# 可以通过这个IP访问当前service对应的Pod
[root@master data]# curl 10.109.82.64:80
<!DOCTYPE html>
<html>
......
<title>Welcome to nginx!</title>
......
</html>
创建集群外部也可访问的Service
# 上面创建的Service的type类型为ClusterIP,这个ip地址只用集群内部可访问
# 如果需要创建外部也可以访问的Service,需要修改type为NodePort
[root@master data]# kubectl expose deploy nginx-deploy --name=svc-nginx-deploy-test2 --type=NodePort --port=80 --target-port=80 -ndevservice/svc-nginx-deploy-test2 exposed

# 此时查看,出现了NodePort类型的Service,而且有一对Port(80:31569/TCP)
[root@master data]# kubectl get svc svc-nginx-deploy-test2 -ndev -owide
NAME                     TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE     SELECTOR
svc-nginx-deploy-test2   NodePort   10.104.82.11   <none>        80:31569/TCP   4m27s   run=nginx
# 接下来就可以通过集群外的主机访问 节点IP:31928访问服务了
# 例如在的电脑主机上通过浏览器访问下面的地址
http://192.168.6.128:31569

微信图片_20211228174128

删除Service
[root@master data]# kubectl delete svc svc-nginx-deploy-test -ndev
service "svc-nginx-deploy-test" deleted
配置方式

创建svc-nginx.yaml,内容如下:

apiVersion: v1
kind: Service
metadata:
  name: svc-nginx-deploy-test-file
  namespace: dev
spec:
  clusterIP: 10.109.179.231 #固定svc的内网ip
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx
  type: ClusterIP
# 创建命令:
[root@master data]# kubectl create -f svc-nginx.yaml 
service/svc-nginx-deploy-test-file created
# 删除命令:
[root@master data]# kubectl delete -f svc-nginx.yaml 
service "svc-nginx-deploy-test-file" deleted

至此,已经介绍完了Namespace、Pod、Label、Deployment、Service资源的基本操作。

最后

以上就是开放马里奥为你收集整理的Kubernetes集群常用命令的全部内容,希望文章能够帮你解决Kubernetes集群常用命令所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(44)

评论列表共有 0 条评论

立即
投稿
返回
顶部