Kubernetes集群命令行工具kube_ctl 常用命令

1、配置kubectl 命令自动补全


[root@k8s-master ~]#source <(kubectl completion bash)


2、K8S集群创建好后查看集群信息

[root@k8s-master ~]# kubectl cluster-info

Kubernetes master is running at https://10.0.0.15:6443

KubeDNS is running at https://10.0.0.15:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

[root@k8s-master ~]#


3、查看deployment资源的详细信息

[root@k8s-master ~]# kubectl describe deployment test-nginx


4、deployment 扩容

[root@k8s-master ~]# kubectl scale deployment --replicas=2 test-nginx

deployment.apps/test-nginx scaled


5、通过kubectl命令行创建deployment

[root@k8s-master ~]# kubectl run nginx-deploy --image=nginx:1.14-alpine --port=80 --dry-run=true

nginx-deploy:是我们创建 deployment 的名字

--image : 指定使用的镜像

--port=80 :容器暴露的端口,该端口只能在 K8S 集群中访问

-dry-run=true : 检测语法是否错误,不会正式创建


6、通过Kubectl命令行创建service

[root@k8s-master ~]# kubectl expose deployment nginx-deploy --name=nginx --port=80 --protocol=TCP

service/nginx exposed

[root@k8s-master ~]# kubectl get svc

NAME        TYPE        CLUSTER-IP      EXTERNAL-IP  PORT(S)  AGE

kubernetes  ClusterIP  10.96.0.1                443/TCP  48d

nginx        ClusterIP  10.101.197.235          80/TCP    9s

[root@k8s-master ~]# kubectl expose deployment test-nginx --type=ClusterIP --name=no-nginx --port=8090 --target-port=80


7、查看一个完整的node定义

[root@k8s-master ~]# kubectl get nodes k8s-master -o ymal

allocatable:

    cpu: "2"

    ephemeral-storage: "48294789041"

    hugepages-1Gi: "0"                                                    ==>  一个Node的可分配资源量

    hugepages-2Mi: "0"

    memory: 2792832Ki

    pods: "110"


8、查看Node节点的标签labels

[root@k8s-master ~]# kubectl get nodes --show-labels

NAME        STATUS  ROLES    AGE  VERSION  LABELS

k8s-master  Ready    master  48d  v1.16.2  beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-master,kubernetes.io/os=linux,node-role.kubernetes.io/master=

k8s-slave1  Ready      48d  v1.16.2  beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,fluentd=true,ingress=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-slave1,kubernetes.io/os=linux,log=true

k8s-slave2  Ready      48d  v1.16.2  beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,fluentd=true,kubernetes.io/arch=amd64,kubernetes.io/hostname=k8s-slave2,kubernetes.io/os=linux


9、查看K8S组件日志

(1)组件日志: 

        /var/log/kube-apiserver.log

         /var/log/kube-proxy.log

         /var/log/kube-controller-manager.log

         /var/log/kubelet.log

(2)使用systemd管理的kubelet日志:

            $ journalctl –u kubelet

(3)使用K8S插件部署的pod应用日志:

             $ kubectl logs -f kube-proxy


10、K8S应用日志

# 从容器标准输出截获:

$ kubectl logs -f {pod name} –c {container name}

$ docker logs -f {docker name}

#直接进入容器内查看日志:

$ kubectl exec -it {pod} -c {container} /bin/sh

$ docker exec -it {container} /bin/sh


11、deployment替换容器镜像进行应用更新发布

[root@k8s-master containers]# kubectl set image deployment/test-nginx test-nginx=nginx:1.9.1

deployment.apps/test-nginx image updated


12、查询deployment滚动升级状态,是否成功

[root@k8s-master containers]# kubectl rollout status deployment test-nginx

deployment "test-nginx" successfully rolled out


13、查看deployment升级记录

[root@k8s-master containers]# kubectl rollout history deployment test-nginx

deployment.apps/test-nginx

REVISION  CHANGE-CAUSE

1       

2       

[root@k8s-master containers]# kubectl rollout history deployment test-nginx --revision=2

#回滚到版本2

deployment.apps/test-nginx with revision #2

Pod Template:

  Labels: pod-template-hash=58b4bc4b88

run=test-nginx

  Containers:

  test-nginx:

    Image: nginx:1.9.1

    Port:

    Host Port:

    Environment:

    Mounts:

  Volumes:


14、service 的分类及创建

(1)clusterip

[root@k8s-master ~]# kubectl create service clusterip svc-cp --tcp=80:8080

(2)nodeport

# nodeport类型包含clusterip类型

[root@k8s-master ~]# kubectl create service nodeport svc-np --tcp=1234:80

(3)headless

[root@k8s-master ~]# kubectl create service clusterip svc-hl --clusterip="None"


15、kube-dns 解析验证

(1)下载busybox 的yaml文件并启动pod

[root@k8s-master ~]# wget https://kubernetes.io/examples/admin/dns/busybox.yaml

[root@k8s-master ~]# kubectl apply -f busybox.yaml


(2)进入Pod里面使用nslookup查询

[root@k8s-master ~]# kubectl exec -it busybox -- nslookup kubernetes.default


16、查看全部namespaces下的所有POD信息

[root@k8s-master kubernetes]# kubectl get pods --all-namespaces

17、查看kubernetes 通过manifest方式构建的pod的yaml文件

[root@k8s-master kubelet]# cd /etc/kubernetes/manifests/

[root@k8s-master manifests]# ls

etcd.yaml  kube-apiserver.yaml  kube-controller-manager.yaml  kube-scheduler.yaml

[root@k8s-master manifests]#


你可能感兴趣的:(Kubernetes集群命令行工具kube_ctl 常用命令)