kubectl是Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。

创建一个pod
`[root@k8s-master ~]# kubectl run nginx --replicas=3 --labels="app=nginx-example" --image=nginx:1.10 --port=80`

查看所有资源信息

[root@k8s-master ~]# kubectl get all
NAME                       READY     STATUS    RESTARTS   AGE
po/nginx-f95d765f9-8b6bp   1/1       Running   0          3m
po/nginx-f95d765f9-cfm6d   1/1       Running   0          3m
po/nginx-f95d765f9-lktk6   1/1       Running   0          3m

查看pod的详细信息

[root@k8s-master ~]# kubectl describe po/nginx-f95d765f9-8b6bp

查看创建的pod
[root@k8s-master ~]# kubectl get rs
NAME              DESIRED   CURRENT   READY     AGE
nginx-f95d765f9   3         3         3         11m

查看创建的pod

[root@k8s-master ~]# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx     3         3         3            3           13m

查看集群

[root@k8s-master ~]# kubectl get cs
\NAME                 STATUS    MESSAGE              ERROR
scheduler            Healthy   ok                   
controller-manager   Healthy   ok                   
etcd-2               Healthy   {"health": "true"}   
etcd-1               Healthy   {"health": "true"}   
etcd-0               Healthy   {"health": "true"} 

查看service

[root@k8s-master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.10.10.1           443/TCP   1d

查看pod的标签

[root@k8s-master ~]# kubectl get pods --show-labels
NAME                    READY     STATUS    RESTARTS   AGE       LABELS
nginx-f95d765f9-8b6bp   1/1       Running   0          19m       app=nginx-example,pod-template-hash=951832195
nginx-f95d765f9-cfm6d   1/1       Running   0          19m       app=nginx-example,pod-template-hash=951832195
nginx-f95d765f9-lktk6   1/1       Running   0          19m       app=nginx-example,pod-template-hash=951832195

创建一个pod

[root@k8s-master ~]# kubectl run busybox --image=busybox --command -- ping baidu.com

查看指定的标签

[root@k8s-master ~]# kubectl get pods -l run=busybox
NAME                       READY     STATUS    RESTARTS   AGE
busybox-5d4f595646-dzjv4   1/1       Running   0          4m

查看pod分配到哪个节点上

[root@k8s-master ~]# kubectl get pods -o wide
NAME                       READY     STATUS    RESTARTS   AGE       IP            NODE
busybox-5d4f595646-dzjv4   1/1       Running   0          5m        172.17.11.4   192.168.30.22
nginx-f95d765f9-8b6bp      1/1       Running   0          27m       172.17.11.2   192.168.30.22
nginx-f95d765f9-cfm6d      1/1       Running   0          27m       172.17.80.2   192.168.30.23
nginx-f95d765f9-lktk6      1/1       Running   0          27m       172.17.11.3   192.168.30.22

查看标签并运行了哪些镜像或容器

[root@k8s-master ~]# kubectl get deploy -o wide
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE       CONTAINERS   IMAGES       SELECTOR
busybox   1         1         1            1           8m        busybox      busybox      run=busybox
nginx     3         3         3            3           29m       nginx        nginx:1.10   app=nginx-example

发布并暴露端口使用户可以访问
根据nginx这个标签进行创建
[root@k8s-master ~]# kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service

查看标签

[root@k8s-master ~]# kubectl get deploy
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
busybox   1         1         1            1           14m
nginx     3         3         3            3           36m

查看service,端口已经暴露给用户

[root@k8s-master ~]# kubectl get svc
NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.10.10.1             443/TCP        1d
nginx-service   NodePort    10.10.10.173           88:35442/TCP   1m

Node1和node2都可以访问
内部访问

[root@k8s-node1 ~]# curl 10.10.10.173:88



Welcome to nginx!
[root@k8s-node2 ~]# curl 10.10.10.173:88



Welcome to nginx!

用户可通过外部访问我们的应用
访问http://192.168.30.22:35442
kubectl工具管理应用_第1张图片
访问http://192.168.30.23:35442
kubectl工具管理应用_第2张图片