kubectl controls the Kubernetes cluster manager.
首先要理解,下面几个概念命令,kubectl命令基本也是针对这些概念而言的。
node
pods
services
///////////////////////////////////////////////////////////////////////////////////
kubectl describe 描述资源对象
kubectl describe nodes <node-name> # 显示Node的详细信息
kubectl describe pods <pod-name> # 显示Pod的详细信息
首先 下面2个命令默认结果一致
kubectl describe nodes
kubectl describe node
页面中,也仅仅只有一个node docker-desktop
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/node?namespace=_all
? ~ kubectl describe node
Name: docker-desktop
这个名字是第一行的输出
后翻可以看到namespace
kubectl describe node docker-desktop
这个输出很详细,有Namespace输出信息
kubernetes-dashboard
kube-system
个人觉得kubectl describe node比kubectl get nodes获取的信息更多一些
? ~ kubectl get nodes docker-desktop
NAME STATUS ROLES AGE VERSION
docker-desktop Ready master 17h v1.18.8
kubectl get 显示一个或更多resources资源
kubectl get cs # 查看集群状态
kubectl get nodes # 查看集群节点信息
kubectl get ns # 查看集群命名空间
kubectl get svc -n kube-system # 查看指定命名空间的服务
kubectl get pod <pod-name> -o wide # 查看Pod详细信息
kubectl get pod <pod-name> -o yaml # 以yaml格式查看Pod详细信息
kubectl get pods # 查看资源对象,查看所有Pod列表
kubectl get rc,service # 查看资源对象,查看rc和service列表
kubectl get pod,svc,ep --show-labels # 查看pod,svc,ep能及标签信息
kubectl get all --all-namespaces # 查看所有的namespaces
kubectl clster-info 显示集群信息
? ~ kubectl cluster-info
Kubernetes master is running at https://kubernetes.docker.internal:6443
KubeDNS is running at https://kubernetes.docker.internal:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'
查看创建的pod
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/pod?namespace=_all
查看所有 pod 列表, -n 后跟 namespace, 查看指定的命名空间
通过界面能看到有2个namespace
kubernetes-dashboard
kube-system
Examples:
# List all pods in ps output format.
kubectl get pods
? ~ kubectl get pods -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-694557449d-7h8k6 1/1 Running 0 16h
kubernetes-dashboard-7d9ddf9f8f-wprd7 1/1 Running 0 16h
? ~ kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-66bff467f8-5qhsc 1/1 Running 0 17h
coredns-66bff467f8-wdzmj 1/1 Running 0 17h
etcd-docker-desktop 1/1 Running 0 17h
kube-apiserver-docker-desktop 1/1 Running 5 17h
kube-controller-manager-docker-desktop 1/1 Running 5 17h
kube-proxy-fj96d 1/1 Running 0 17h
kube-scheduler-docker-desktop 1/1 Running 0 17h
storage-provisioner 1/1 Running 0 17h
vpnkit-controller 1/1 Running 0 17h
? ~ kubectl get pods -n kube-system kube-scheduler-docker-desktop
NAME READY STATUS RESTARTS AGE
kube-scheduler-docker-desktop 1/1 Running 0 16h
? ~ kubectl get pods -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-694557449d-7h8k6 1/1 Running 0 16h
kubernetes-dashboard-7d9ddf9f8f-wprd7 1/1 Running 0 16h
查看svc信息
? ~ kubectl describe svc
Name: kubernetes
Namespace: default
Labels: component=apiserver
provider=kubernetes
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.96.0.1
Port: https 443/TCP
TargetPort: 6443/TCP
Endpoints: 192.168.65.3:6443
Session Affinity: None
Events: <none>
查看nodeport 所有的services
? ~ kubectl get services -A
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 17h
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 17h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.102.121.205 <none> 8000/TCP 16h
kubernetes-dashboard kubernetes-dashboard ClusterIP 10.98.196.76 <none> 443/TCP 16h
-n后面跟 NAMESPACE
? ~ kubectl get service -n kube-system -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 16h k8s-app=kube-dns
? ~ kubectl get service -n kube-system -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 15h k8s-app=kube-dns
? ~ kubectl get service -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.102.121.205 <none> 8000/TCP 16h
kubernetes-dashboard ClusterIP 10.98.196.76 <none> 443/TCP 16h
# 根据 yaml 创建资源, apply 可以重复执行,create 不行
kubectl create -f pod.yaml
kubectl apply -f pod.yaml
kubectl run 在集群中运行一个指定的镜像
参考:
http://kubernetes.kansea.com/docs/user-guide/kubectl/kubectl_run/
run后面跟pods 为 nginx
? ~ kubectl run nginx --image=centos --port=80 --replicas=1
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/nginx created
可以看到上面这个命令术不会被弃用
并且只创建一个nginx 容器实例
在K8S v1.18.0 以后,--replicas已弃用 ,推荐用 deployment 创建 pod。
如果需要创建可以通过如下方式操作:
kubectl apply -f nginx.yaml
This action is equivalent to:kubectl apply -f <spec.yaml>
? ~ kubectl run nginx --image=centos --port=80
pod/nginx created
? ~ kubectl logs nginx
Error from server (BadRequest): container "nginx" in pod "nginx" is waiting to start: ContainerCreating
查看这个pods nginx的状态 kubectl describe pods nginx
kubctl获取pod状态
? ~ kubectl get pods nginx
NAME READY STATUS RESTARTS AGE
nginx 0/1 CrashLoopBackOff 37 168m
查询异常pod名称为 nginx
查看此状态pod详细情况
? ~ kubectl describe pods nginx
这里能看到获取的IP地址10.1.0.10
查看此pod日志
kubectl logs nginx
这里日志是空的,所以
我现在删除这个pods nginx
kubectl delete pods nginx
pod "nginx" deleted
//////////下面是OK的过程/////////
再次简单启动:
? ~ kubectl run nginx --image=nginx
查看日志
? ~ kubectl logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
10.1.0.10 - - [14/Oct/2020:08:54:59 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.64.0" "-"
10.1.0.10 - - [14/Oct/2020:08:55:07 +0000] "HEAD / HTTP/1.1" 200 0 "-" "curl/7.64.0" "-"
下面请求下看看,而且日志已经记录了
执行 pod 的 命令比如curl
? ~ kubectl exec nginx -- curl -I 10.1.0.10
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 612 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
HTTP/1.1 200 OK
Server: nginx/1.19.3
Date: Wed, 14 Oct 2020 08:55:07 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT
Connection: keep-alive
ETag: "5f7340cf-264"
Accept-Ranges: bytes
通过bash获得 pod 中某个容器的TTY,相当于登录容器
kubectl exec -it <pod-name> -c <container-name> -- bash
eg:
kubectl exec -it redis-master-cln81 -- bash
? ~ kubectl exec -it nginx -- bash
root@nginx:/# w
bash: w: command not found
root@nginx:/# curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.19.3
Date: Wed, 14 Oct 2020 09:01:44 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT
Connection: keep-alive
ETag: "5f7340cf-264"
Accept-Ranges: bytes
root@nginx:/# curl -I http://10.1.0.10
HTTP/1.1 200 OK
Server: nginx/1.19.3
Date: Wed, 14 Oct 2020 09:01:56 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 29 Sep 2020 14:12:31 GMT
Connection: keep-alive
ETag: "5f7340cf-264"
Accept-Ranges: bytes
重启 pod
kubectl get pod <POD名称> -n <NAMESPACE名称> -o yaml | kubectl replace --force -f -