K8S 之 kubectl详解

妖精的绣舞 提交于 2020-02-03 18:43:16

一、kubectl陈述式管理方法

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

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

2、查看default命名空间下的所有资源
[root@test-nodes1 ~]# 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、创建与删除命名空间
[root@test-nodes1 ~]# kubectl create namespace test
namespace/test created
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# kubectl delete namespace test
namespace "test" deleted

4、创建deployment(pods控制器)资源
[root@test-nodes1 ~]# 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资源控制器
[root@test-nodes1 ~]# kubectl get deployment -n kube-public
NAME       READY   UP-TO-DATE   AVAILABLE   AGE
nignx-test   0/1     1            0           70s

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

7、查看kube-public空间下的pods资源
[root@test-nodes1 ~]# 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
[root@test-nodes2 ~]# 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)详细信息
[root@test-nodes1 ~]# 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资源
[root@test-nodes1 ~]# kubectl get pods -n kube-public
NAME                          READY   STATUS             RESTARTS   AGE
nignx-test-655d6fbcb5-r9t57   1/1     Running            0          24m
[root@test-nodes1 ~]# kubectl exec -ti nignx-test-655d6fbcb5-r9t57 /bin/bash -n kube-public
#与docker 操作方法一致
root@nignx-test-655d6fbcb5-r9t57:/# 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: eth0@if13: <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资源(即重启)
[root@test-nodes1 ~]# kubectl delete pod nignx-test-655d6fbcb5-r9t57 -n kube-public
pod "nignx-test-655d6fbcb5-r9t57" deleted
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# kubectl delete deploy nignx-dp -n kube-public
deployment.extensions "nignx-dp" deleted

13、为nginx-dp pod资源创建service资源,保障pod的高可用,通过集群IP访问
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# 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>
[root@test-nodes1 ~]# kubectl expose deployment nginx-dp --port=80 -n kube-public
service/nginx-dp exposed
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# 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
[root@test-nodes1 ~]# 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>
[root@test-nodes1 ~]# 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         
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!