本文主要是介绍Kubernetes集群命令行工具kube_ctl 常用命令,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Kubernetes集群命令行工具kube_ctl 常用命令
kubectl是Kubernetes集群的命令行工具,通过kubectl能够对集群本身进行管理,并能够在集群上进行容器化应用的安装部署。
1、配置kubectl 命令自动补全
[root@k8s-master ~]#source <(kubectl completion bash)
2、K8S集群创建好后查看集群信息
[root@k8s-master ~]# kubectl cluster-infoKubernetes master is running at https://10.0.0.15:6443KubeDNS is running at https://10.0.0.15:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxyTo 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-nginxdeployment.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 <none> 443/TCP 48d
nginx ClusterIP 10.101.197.235 <none> 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 ymalallocatable:cpu: "2"ephemeral-storage: "48294789041"hugepages-1Gi: "0" hugepages-2Mi: "0"memory: 2792832Kipods: "110"
allocatable
:表示一个Node的可分配资源量
8、查看Node节点的标签labels
[root@k8s-master ~]# kubectl get nodes --show-labelsNAME STATUS ROLES AGE VERSION LABELSk8s-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 <none> 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=truek8s-slave2 Ready <none> 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 logs -f {pod name} –c {container name}$ docker logs -f {docker name}
11、deployment替换容器镜像进行应用更新发布
[root@k8s-master containers]# kubectl set image deployment/test-nginx test-nginx=nginx:1.9.1deployment.apps/test-nginx image updated
12、查询deployment滚动升级状态,是否成功
[root@k8s-master containers]# kubectl rollout status deployment test-nginxdeployment "test-nginx" successfully rolled out
13、查看deployment升级记录
- 查看升级版本
[root@k8s-master containers]# kubectl rollout history deployment test-nginxdeployment.apps/test-nginxREVISION CHANGE-CAUSE1 <none>2 <none>
- 回滚版本2
[root@k8s-master containers]# kubectl rollout undo deployment test-nginx --to-revision=2#回滚到版本2deployment.apps/test-nginx with revision #2Pod Template:Labels: pod-template-hash=58b4bc4b88run=test-nginxContainers:test-nginx:Image: nginx:1.9.1Port: <none>Host Port: <none>Environment: <none>Mounts: <none>Volumes: <none>
14、service 的分类及创建
- clusterip:
[root@k8s-master ~]# kubectl create service clusterip svc-cp --tcp=80:8080
- nodeport
[root@k8s-master ~]# kubectl create service nodeport svc-np --tcp=1234:80
- headless
[root@k8s-master ~]# kubectl create service clusterip svc-hl --clusterip="None"
15、kube-dns 集群域名解析验证
- 下载busybox 的yaml文件并启动pod:
[root@k8s-master ~]# wget https://kubernetes.io/examples/admin/dns/busybox.yaml[root@k8s-master ~]# kubectl apply -f busybox.yaml
- 进入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]# lsetcd.yaml kube-apiserver.yaml kube-controller-manager.yaml kube-scheduler.yaml[root@k8s-master manifests]#
这篇关于Kubernetes集群命令行工具kube_ctl 常用命令的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!