K8S 之 kubectl详解

一、kubectl陈述式管理方法

kubectl小洁:
kubectl是官方的CLI命令行工具,用于apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理k8s各种资源的一种有效途径。

1、查看当前集群所有命名空间
[[email protected] ~]# kubectl get namespace
NAME              STATUS   AGE
default           Active   42h
kube-node-lease   Active   42h
kube-public       Active   42h
kube-system       Active   42h

2、查看default命名空间下的所有资源
[[email protected] ~]# kubectl get all -n default
NAME                  READY   STATUS             RESTARTS   AGE
pod/nginx-ds-76fr8    0/1     ImagePullBackOff   0          39h
pod/nginx-ds-zz7jn    0/1     ErrImagePull       0          39h
pod/nginx-ds1-qg45q   1/1     Running            0          39h
pod/nginx-ds1-whnmv   1/1     Running            0          39h
#pod资源

NAME                 TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   192.168.0.1   <none>        443/TCP   42h
#service资源

NAME                       DESIRED   CURRENT   READY   UP-TO-DATE   AVAILABLE   NODE SELECTOR   AGE
daemonset.apps/nginx-ds    2         2         0       2            0           <none>          39h
daemonset.apps/nginx-ds1   2         2         2       2            2           <none>          39h
#pod控制器

3、创建与删除命名空间
[[email protected] ~]# kubectl create namespace test
namespace/test created
[[email protected] ~]# kubectl get namespace
NAME              STATUS   AGE
default           Active   42h
kube-node-lease   Active   42h
kube-public       Active   42h
kube-system       Active   42h
test              Active   6s
[[email protected] ~]# kubectl delete namespace test
namespace "test" deleted

4、创建deployment(pods控制器)资源
[[email protected] ~]# kubectl create deployment nignx-dp --image=test-harbor.cedarhd.com/public/nginx:v1.7.9 -n kube-public
deployment.apps/nignx-dp created

5、查看kube-public命名空间下的deployment资源控制器
[[email protected] ~]# kubectl get deployment -n kube-public
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nignx-test   0/1     1            0           70s

6、查看kube-public命名空间下pods的运行情况
[[email protected] ~]# kubectl get deployment -n kube-public
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
nignx-test   1/1     1            1           21s

7、查看kube-public空间下的pods资源
[[email protected] ~]# kubectl get pods -n kube-public -o wide
NAME                          READY   STATUS             RESTARTS   AGE    IP           NODE                      NOMINATED NODE   READINESS GATES
nignx-dp-7f6d4979bd-nnc57     0/1     ImagePullBackOff   0          13m    172.7.21.4   test-nodes1.cedarhd.com   <none>           <none>
nignx-test-655d6fbcb5-r9t57   1/1     Running            0          4m1s   172.7.22.4   test-nodes2.cedarhd.com   <none>           <none>

8、在test-nodes2.cedarhd.com节点上curl 172.7.22.4 nginx
[[email protected] ~]# curl 172.7.22.4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>

9、查看deployment下具体pods(nginx-test)详细信息
[[email protected] ~]# kubectl describe deployment nignx-test -n kube-public
Name:                   nignx-test
Namespace:              kube-public
CreationTimestamp:      Mon, 03 Feb 2020 02:02:59 -0500
Labels:                 app=nignx-test
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nignx-test
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nignx-test
  Containers:
   nginx:
    Image:        test-harbor.cedarhd.com/public/nginx:v1.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:   nignx-test-655d6fbcb5 (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  22m   deployment-controller  Scaled up replica set nignx-test-655d6fbcb5 to 1

10、进入nginx-test pod资源
[[email protected] ~]# kubectl get pods -n kube-public
NAME                          READY   STATUS             RESTARTS   AGE
nignx-test-655d6fbcb5-r9t57   1/1     Running            0          24m
[[email protected] ~]# kubectl exec -ti nignx-test-655d6fbcb5-r9t57 /bin/bash -n kube-public
#与docker 操作方法一致
[email protected]:/# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
12: [email protected]: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:07:16:04 brd ff:ff:ff:ff:ff:ff
    inet 172.7.22.4/24 brd 172.7.22.255 scope global eth0
       valid_lft forever preferred_lft forever

11、删除pod资源(即重启)
[[email protected] ~]# kubectl delete pod nignx-test-655d6fbcb5-r9t57 -n kube-public
pod "nignx-test-655d6fbcb5-r9t57" deleted
[[email protected] ~]# kubectl get pods -n kube-public -o wide
NAME                          READY   STATUS             RESTARTS   AGE   IP           NODE                      NOMINATED NODE   READINESS GATES
nignx-dp-7f6d4979bd-nnc57     0/1     ImagePullBackOff   0          40m   172.7.21.4   test-nodes1.cedarhd.com   <none>           <none>
nignx-test-655d6fbcb5-c42z8   1/1     Running            0          18s   172.7.21.5   test-nodes1.cedarhd.com   <none>           <none>

12、删除deployment
[[email protected] ~]# kubectl get deploy -n kube-public
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
nignx-dp     0/1     1            0           53m
nignx-test   1/1     1            1           36m
[[email protected] ~]# kubectl delete deploy nignx-dp -n kube-public
deployment.extensions "nignx-dp" deleted

13、为nginx-dp pod资源创建service资源,保障pod的高可用,通过集群IP访问
[[email protected] ~]# kubectl get all -n kube-public
NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-dp-5b9b697bcc-jtrlp   1/1     Running   0          17s
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   1/1     1            1           17s
NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-5b9b697bcc   1         1         1       17s
[[email protected] ~]# kubectl get pods -n kube-public -o wide
NAME                        READY   STATUS    RESTARTS   AGE     IP           NODE                      NOMINATED NODE   READINESS GATES
nginx-dp-5b9b697bcc-jtrlp   1/1     Running   0          3m19s   172.7.21.4   test-nodes1.cedarhd.com   <none>           <none>
[[email protected] ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[[email protected] ~]# kubectl get svc -n kube-public      #查看service资源
NAME       TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
nginx-dp   ClusterIP   192.168.224.56   <none>        80/TCP    12m
[[email protected] ~]# kubectl get all -n kube-public
NAME                            READY   STATUS    RESTARTS   AGE
pod/nginx-dp-5b9b697bcc-jtrlp   1/1     Running   0          5m21s
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE
service/nginx-dp   ClusterIP   192.168.224.56   <none>        80/TCP    79s
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-dp   1/1     1            1           5m21s
NAME                                  DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-dp-5b9b697bcc   1         1         1       5m21s
[[email protected] ~]# curl 192.168.224.56
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    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>
[[email protected] ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  192.168.0.1:443 nq
  -> 10.3.153.221:6443            Masq    1      0          0
  -> 10.3.153.222:6443            Masq    1      0          0
TCP  192.168.224.56:80 nq
  -> 172.7.21.4:80                Masq    1      0          0         

原文地址:https://blog.51cto.com/12965094/2468947

时间: 2024-11-04 04:49:41

K8S 之 kubectl详解的相关文章

k8s之yaml详解

k8s之yaml详解 apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: web04-pod #资源的名字,在同一个namespace中必须唯一 labels: #设定资源的标签 k8s-app: apache version: v1 kubernetes.io/cluster-service: "true" annotations: #自

k8s应用配置详解

1. 概述 k8s主要通过Object定义各种部署任务(例如:部署应用.部署Ingress路由规则.部署service等等),通过kubectl命令远程操作k8s集群. Object的定义通常以Yaml格式进行描述. Yaml是一种直观的数据序列化格式,主要通过缩进的方式组织数据. Yaml例子: # 注释 house: family: name: Doe parents: # - 开始,表示数组元素 - John - Jane children: - Paul - Mark - Simone

Prometheus+Grafan监控k8s集群详解

一,Prometheus概述 1,什么是Prometheus?Prometheus是最初在SoundCloud上构建的开源系统监视和警报工具包,自2012年成立以来,许多公司和组织都采用了Prometheus,该项目拥有非常活跃的开发人员和用户社区.现在,它是一个独立的开源项目,并且独立与任何公司维护.为了强调这一点并阐明项目的治理结构,Prometheus在2016年加入了 Cloud Native Computing Foundation(云原生计算基金会(CNCF)),这是继kuberne

k8s认证授权详解

理解认证授权 1.1 为什么要认证 想理解认证,我们得从认证解决什么问题.防止什么问题的发生入手. 防止什么问题呢?是防止有人入侵你的集群,root你的机器后让我们集群依然安全吗?不是吧,root都到手了,那就为所欲为,防不胜防了. 其实网络安全本身就是为了解决在某些假设成立的条件下如何防范的问题.比如一个非常重要的假设就是两个节点或者ip之间的通讯网络是不可信任的,可能会被第三方窃取,也可能会被第三方篡改.就像我们上学时候给心仪的女孩传纸条,传送的过程可能会被别的同学偷看,甚至内容可能会从我喜

【Kubernetes】kubectl命令详解 &#646984;

目录 用法概述 子命令详解 参数列表 输出格式 操作示例 原文: http://blog.gqylpy.com/gqy/385 @ 用法概述 kubectl 命令行的语法如下: kubectl [command] [TYPE] [NAME] [flags] command:子命令,用于操作 Kubernetes 集群资源对象的命令,例如 create.delete.describe.get.apply 等. TYPE:资源对象类型,区分大小写,能以单数.复数形式或者简写形式表达. NAME:资源

从零开始入门 K8s| 详解 Pod 及容器设计模式

作者|张磊 阿里云容器平台高级技术专家,CNCF 官方大使 一.为什么需要 Pod 容器的基本概念 我们知道 Pod 是 Kubernetes 项目里面一个非常重要的概念,也是非常重要的一个原子调度单位,但是为什么我们会需要这样一个概念呢?在使用容器 Docker 的时候,也没有这个说法.其实,如果想要理解 Pod,首先要理解容器,所以来回顾一下容器的概念: 容器的本质实际上是一个进程,是一个视图被隔离,资源受限的进程. 容器里面 PID=1 的进程就是应用本身,这意味着管理虚拟机等于管理基础设

kubernetes资源创建详解【持续完善中】

目录 资源创建详解 一:Pod及常用参数 1.简介 2.模板 3.删除pod 4.设置Pod主机名 5.镜像拉取策略(ImagePullPolicy) 二:RC 1.简介 2.模板 三:Deployment 1.简介 2.模板 四:HPA 1.简介 2.模板 五:StatefulSet 1.简介 2.模板 六:PV和PVC 八:扩展 8.1.Pod调度到指定的Node 资源创建详解 一:Pod及常用参数 1.简介 2.模板 3.删除pod 示例流程如下: 用户发送删除pod的命令,默认宽限期是3

Kubernetes之yaml文件详解-v1.15.x

Kubernetes之yaml文件详解 K8S 创建资源的方式 K8S有两种创建资源的方式:kubectl 命令和 yaml 配置文件. kubectl命令行:最为简单,一条命令就OK,但缺点也很明显,你并不知道这条命令背后到底做了哪些事!yaml配置文件:提供了一种让你知其然更知其所以然的方式.优势如下: 完整性:配置文件描述了一个资源的完整状态,可以很清楚地知道一个资源的创建背后究竟做了哪些事:灵活性:配置文件可以创建比命令行更复杂的结构:可维护性:配置文件提供了创建资源对象的模板,能够重复

Kubernetes-Ingress资源详解

什么是Ingress #阿里云称之为ingress路由!在 Kubernetes 集群中,主要用于接入外部请求到k8s内部,Ingress是授权入站连接到达集群服务的规则集合,为您提供七层负载均衡能力.您可以给 Ingress 配置提供外部可访问的 URL.负载均衡.SSL.基于名称的虚拟主机等.git地址:https://github.com/kubernetes/Ingress-nginx Service缺点 #定义service以后,尤其是NodePort集群访问,需要经过2级转换调度,而