kubectl 命令
命令说明
| 命令格式 | 命令说明 |
|---|---|
| kubectl run 资源名称 -参数 --image=镜像名称:标签 | 创建资源对象,常用参数-i交互,-t终端 |
| kubectl get 查询资源 可选参数 -o wide 显示主机信息 | 常用查询的资源 node|deployment|pod |
| kubectl exec -it 容器id 执行的命令 | 同 docker exec 指令,进入容器内 |
| kubectl describe 资源类型 资源名称 | 查询资源的详细信息 |
| kubectl attach | 同 docker attach 指令,连接容器 |
| kubectl logs 容器id | 查看容器控制台的标准输出 |
| kubectl delete 资源类型 资源名称 | 删除指定的资源 |
| kubectl create|apply -f 资源文件 | 执行指定的资源文件 |
# get 查询信息 [root@master ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION master Ready master 19h v1.17.6 node-0001 Ready16h v1.17.6 [root@master ~]# kubectl get nodes master -o wide # -o 指定输出的格式 NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME master Ready master 2d21h v1.17.6 192.168.1.21 CentOS Linux 7 (Core) 3.10.0-1160.53.1.el7.x86_64 docker://18.6.3 [root@master ~]# kubectl get pod No resources found in default namespace. [root@master ~]# kubectl get namespaces 查看命名空间 NAME STATUS AGE default Active 9h kube-node-lease Active 9h kube-public Active 9h kube-system Active 9h [root@master ~]# kubectl -n kube-system get pod NAME READY STATUS RESTARTS AGE kube-flannel-ds-amd64-hf2jp 1/1 Running 0 41m kube-flannel-ds-amd64-rtl4l 1/1 Running 0 41m ... ... # describe 查询详细信息 [root@master ~]# kubectl -n kube-system describe pod kube-flannel-ds-amd64-rtl4l Name: kube-proxy-4tbp6 Namespace: kube-system ... ...
命名空间
常用拍错:
排错三兄弟运用:
Pod与控制器
创建镜像:
[root@master ~]# ls
coredns.tar.gz flannel kube-apiserver.tar.gz kube-proxy.tar.gz pause.tar.gz
etcd.tar.gz init kube-controller-manager.tar.gz kube-scheduler.tar.gz
[root@master ~]# cd init
[root@master init]# ls
kubeadm-init.yaml master-init.log
[root@master init]# curl http://192.168.1.100:5000/v2/_catalog #查询名称
{"repositories":["coredns","etcd","flannel","kube-apiserver","kube-controller-manager","kube-proxy","kube-scheduler","myos","pause"]}
[root@master init]# curl http://192.168.1.100:5000/v2/myos/tags/list #查询标签
{"name":"myos","tags":["php-fpm","httpd","v1804","nginx"]}
[root@master init]# kubectl run haha -it --image=192.168.1.100:5000/myos:v1804 #创建
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
If you don't see a command prompt, try pressing enter.
[root@haha-7f5c65f7c7-rfd9x /]# Session ended, resume using 'kubectl attach haha-7f5c65f7c7-rfd9x -c haha -i -t' command when the pod is running
[root@master init]# kubectl get pods
NAME READY STATUS RESTARTS AGE
haha-7f5c65f7c7-rfd9x 1/1 Running 0 68s
[root@master init]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
haha-7f5c65f7c7-rfd9x 1/1 Running 0 80s 10.244.3.2 node-0003
[root@master init]# ping 10.244.3.2
PING 10.244.3.2 (10.244.3.2) 56(84) bytes of data.
64 bytes from 10.244.3.2: icmp_seq=1 ttl=63 time=0.305 ms
64 bytes from 10.244.3.2: icmp_seq=2 ttl=63 time=0.263 ms
64 bytes from 10.244.3.2: icmp_seq=3 ttl=63 time=0.216 ms
64 bytes from 10.244.3.2: icmp_seq=4 ttl=63 time=0.209 ms
64 bytes from 10.244.3.2: icmp_seq=5 ttl=63 time=0.215 ms
64 bytes from 10.244.3.2: icmp_seq=6 ttl=63 time=0.213 ms
64 bytes from 10.244.3.2: icmp_seq=7 ttl=63 time=0.233 ms
^C
--- 10.244.3.2 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 6000ms
rtt min/avg/max/mdev = 0.209/0.236/0.305/0.034 ms
# 进入容器 [root@master ~]# kubectl exec -it testos-79778b4895-s8mxl -- /bin/bash [root@testos-79778b4895-s8mxl /]#
[root@master ~]# kubectl attach -it testos-79778b4895-s8mxl [root@testos-79778b4895-s8mxl /]# # 查看终端日志 [root@master ~]# kubectl logs testweb-7bf98b9576-v566c AH00558: httpd: Could not reliably determine the server fully qualified domain name, using 10.244.4.2. Set the 'ServerName' directive globally to suppress this message # 执行指定的资源文件 [root@master flannel]# kubectl apply -f kube-flannel.yml # 删除资源,直接删除POD会自动重建 [root@master ~]# kubectl delete pod testos-79778b4895-s8mxl pod "testos-79778b4895-s8mxl" deleted [root@master ~]# kubectl delete deployments testos deployment.apps "testos" deleted
资源文件
在k8s集群中,latest标签是不会被缓存,如果想缓存可以使用其他标签
[root@master ~]# curl http://192.168.1.100:5000/v2/myos/tags/list
{"name":"myos","tags":["nginx","php-fpm","v1804","httpd"]}
pod 资源文件
[root@master config]# vim mypod.yaml
--- # 资源定义起始标志
kind: Pod #当前创建资源的类型
apiVersion: v1 #当前格式的版本
metadata: #当前资源的源数据
name: mypod #当前资源的名称
spec: #当前资源的详细定义
containers: #容器定义
- name: mylinux #容器名称,多个容器在一个pod中名称不能重复
image: 192.168.1.100:5000/myos:v1804 #启动容器的镜像地址
stdin: true #相当于-i参数,分配标准输出
tty: true #相当于-t参数,分配终端
[root@master ~]# kubectl apply -f mypod.yaml
pod/mypod created
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
mypod 1/1 Running 0 13s
[root@master ~]# kubectl delete -f mypod.yaml
pod "mypod" deleted
[root@master ~]#
deployment 资源文件
[root@master ~]# vim myapache.yaml
--- #资源定义起始标志
kind: Deployment #当前创建资源的类型
apiVersion: apps/v1 #当前格式的版本
metadata: #当前资源的源数据
name: myapache #当前资源的名字
annotations: #容器定义
kubernetes.io/change-cause: httpd.v1
spec: #当前资源的详细定义
selector: #资源匹配选择器,主要确定资源的方式
matchLabels: #匹配卷标
myapp: httpd #具体匹配的东西,与了labels里面定义相同
replicas: 1 #pod副本数量
template: #资源模板
metadata:
labels: #声明标签
myapp: httpd #定义标签名字
spec:
containers:
- name: webcluster
image: 192.168.1.100:5000/myos:httpd
stdin: false
tty: false
ports:
- protocol: TCP
containerPort: 80
restartPolicy: Always
[root@master ~]# kubectl apply -f myapache.yaml
deployment.apps/myapache created
[root@master ~]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
myapache 1/1 1 1 19s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
myapache-69b494dc5c-bff95 1/1 Running 0 28s
[root@master ~]#
集群扩容、更新与回滚
[root@master ~]# kubectl scale deployment myapache --replicas=5 deployment.apps/myapache scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapache-7d689bf8f-2rj2k 1/1 Running 0 18s myapache-7d689bf8f-9fvjm 1/1 Running 0 28m myapache-7d689bf8f-mmgjc 1/1 Running 0 18s myapache-7d689bf8f-rhhcw 1/1 Running 0 18s myapache-7d689bf8f-z6ngb 1/1 Running 0 18s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-7d689bf8f-2rj2k 1/1 Running 0 113s 10.244.3.8 node-0003myapache-7d689bf8f-9fvjm 1/1 Running 0 30m 10.244.3.7 node-0003 myapache-7d689bf8f-mmgjc 1/1 Running 0 113s 10.244.1.4 node-0002 myapache-7d689bf8f-rhhcw 1/1 Running 0 113s 10.244.2.7 node-0001 myapache-7d689bf8f-z6ngb 1/1 Running 0 113s 10.244.2.6 node-0001 [root@master ~]# curl http://10.244.3.8 this is apache [root@master ~]# curl http://10.244.2.6 this is apache
[root@master ~]# kubectl scale deployment myapache --replicas=1 deployment.apps/myapache scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapache-7d689bf8f-mmgjc 1/1 Running 0 3m31s myapache-7d689bf8f-rhhcw 0/1 Terminating 0 3m31s myapache-7d689bf8f-z6ngb 0/1 Terminating 0 3m31s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-7d689bf8f-mmgjc 1/1 Running 0 3m46s 10.244.1.4 node-0002
更新
[root@master ~]# kubectl scale deployment myapache --replicas=6 deployment.apps/myapache scaled [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapache-7d689bf8f-k2jtq 1/1 Running 0 13s myapache-7d689bf8f-l4jfg 1/1 Running 0 13s myapache-7d689bf8f-mdnwn 1/1 Running 0 13s myapache-7d689bf8f-mmgjc 1/1 Running 0 19m myapache-7d689bf8f-pbpzn 1/1 Running 0 13s myapache-7d689bf8f-tzr4j 1/1 Running 0 13s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-7d689bf8f-k2jtq 1/1 Running 0 88s 10.244.1.5 node-0002myapache-7d689bf8f-l4jfg 1/1 Running 0 88s 10.244.3.9 node-0003 myapache-7d689bf8f-mdnwn 1/1 Running 0 88s 10.244.2.8 node-0001 myapache-7d689bf8f-mmgjc 1/1 Running 0 20m 10.244.1.4 node-0002 myapache-7d689bf8f-pbpzn 1/1 Running 0 88s 10.244.3.10 node-0003 myapache-7d689bf8f-tzr4j 1/1 Running 0 88s 10.244.2.9 node-0001 [root@master ~]# curl http://10.244.1.5 this is apache [root@master ~]# curl http://10.244.3.9 this is apache
[root@master ~]# kubectl edit deployments.apps myapache #edit 编辑修改,实现动态更新 deployment.apps/myapache edited
历史回滚:
[root@master ~]# kubectl rollout history deployment myapache deployment.apps/myapache REVISION CHANGE-CAUSE 3 httpd.v1 4 nginx.v1
[root@master ~]# kubectl rollout undo deployment myapache --to-revision=3 deployment.apps/myapache rolled back [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-7d689bf8f-5lz2z 1/1 Running 0 74s 10.244.2.17 node-0001myapache-7d689bf8f-895pv 1/1 Running 0 75s 10.244.2.16 node-0001 myapache-7d689bf8f-8kqr2 1/1 Running 0 75s 10.244.3.17 node-0003 myapache-7d689bf8f-n8k6j 1/1 Running 0 73s 10.244.1.13 node-0002 myapache-7d689bf8f-ncxqt 1/1 Running 0 75s 10.244.1.12 node-0002 myapache-7d689bf8f-s5mq9 1/1 Running 0 74s 10.244.3.18 node-0003
默认保存10个历史版本
节点标签选择器
使用 nodeName 参数让容器运行在指定节点上
[root@master ~]# kubectl delete -f myapache.yaml deployment.apps "myapache" deleted [root@master ~]# kubectl apply -f myapache.yaml deployment.apps/myapache created [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapache-7d689bf8f-m4cv5 1/1 Running 0 9s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-7d689bf8f-m4cv5 1/1 Running 0 38s 10.244.3.19 node-0003[root@master ~]# kubectl delete -f myapache.yaml deployment.apps "myapache" deleted [root@master ~]# vim myapache.yaml [root@master ~]# kubectl apply -f myapache.yaml deployment.apps/myapache created [root@master ~]# kubectl get pods NAME READY STATUS RESTARTS AGE myapache-5777cd74f9-f88qm 1/1 Running 0 11s [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-5777cd74f9-f88qm 1/1 Running 0 17s 10.244.2.18 node-0001 [root@master ~]#
打标签:
[root@master ~]# kubectl delete -f myapache.yaml deployment.apps "myapache" deleted [root@master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS master Ready master 10h v1.17.6 kubernetes.io/hostname=master ... ... node-0001 Ready10h v1.17.6 kubernetes.io/hostname=node-0001 ... ... node-0002 Ready 10h v1.17.6 kubernetes.io/hostname=node-0002 ... ... node-0003 Ready 10h v1.17.6 kubernetes.io/hostname=node-0003 ... ... [root@master ~]# kubectl label nodes node-0002 node-0003 disktype=ssd node/node-0002 labeled node/node-0003 labeled [root@master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS master Ready master 10h v1.17.6 kubernetes.io/hostname=master ... ... node-0001 Ready 10h v1.17.6 kubernetes.io/hostname=node-0001 ... ... node-0002 Ready 10h v1.17.6 disktype=ssd ... ... node-0003 Ready 10h v1.17.6 disktype=ssd ... ... [root@master ~]# vim myapache.yaml --- kind: Deployment apiVersion: apps/v1 metadata: name: myapache spec: selector: matchLabels: myapp: httpd replicas: 1 template: metadata: labels: myapp: httpd spec: nodeSelector: # 新添加 disktype: ssd # 新添加 containers: - name: webcluster image: 192.168.1.100:5000/myos:httpd stdin: false tty: false ports: - protocol: TCP containerPort: 80 restartPolicy: Always [root@master ~]# kubectl scale deployment myapache --replicas=3 deployment.apps/myapache scaled [root@master ~]# kubectl get pod -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE myapache-xxx 1/1 Running 0 9s 10.244.1.7 node-0003 myapache-xxx 1/1 Running 0 9s 10.244.2.8 node-0002 myapache-xxx 1/1 Running 0 21s 10.244.2.7 node-0002 [root@master ~]# kubectl delete -f myapache.yaml deployment.apps "myapache" deleted [root@master ~]# kubectl label nodes node-0002 node-0003 disktype- node/node-0002 labeled node/node-0003 labeled [root@master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS master Ready master 10h v1.17.6 kubernetes.io/hostname=master ... ... node-0001 Ready 10h v1.17.6 kubernetes.io/hostname=node-0001 ... ... node-0002 Ready 10h v1.17.6 kubernetes.io/hostname=node-0002 ... ... node-0003 Ready 10h v1.17.6 kubernetes.io/hostname=node-0003 ... ... [root@master ~]#
[root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOS GATES myapache-5777cd74f9-f88qm 1/1 Running 0 17s 10.244.2.18 node-00013d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0001,kubernetes.io/os=linux node-0002 Ready 3d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0002,kubernetes.io/os=linux node-0003 Ready 3d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0003,kubernetes.io/os=linux [root@master ~]# kubectl label nodes node-0002 node-0003 disktype=ssd
[root@master ~]# kubectl label nodes node-0002 node-0003 disktype=ssd node/node-0002 labeled node/node-0003 labeled [root@master ~]# kubectl get nodes --show-labels NAME STATUS ROLES AGE VERSION LABELS master Ready master 3d4h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/arch=amd64,kubernetes.io/hostname=master,kubernetes.io/os=linux,node-role.kubernetes.i node-0001 Ready3d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0001,kubernetes.io/os=linux node-0002 Ready 3d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetessd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0002,kubernetes.io/os=linux node-0003 Ready 3d2h v1.17.6 beta.kubernetes.io/arch=amd64,beta.kubernetessd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node-0003,kubernetes.io/os=linux [root@master ~]#
再次创建:
[root@master ~]# kubectl apply -f myapache.yaml deployment.apps/myapache configured [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-66b4559787-btvvv 1/1 Running 0 26s 10.244.3.20 node-0003
对集群进行扩容:
[root@master ~]# kubectl scale deployment myapache --replicas=8 deployment.apps/myapache scaled [root@master ~]# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES myapache-66b4559787-4258w 1/1 Running 0 2s 10.244.1.16 node-0002myapache-66b4559787-7ljcm 1/1 Running 0 2s 10.244.1.15 node-0002 myapache-66b4559787-btvvv 1/1 Running 0 110s 10.244.3.20 node-0003 myapache-66b4559787-hvw9k 1/1 Running 0 2s 10.244.1.14 node-0002 myapache-66b4559787-l9hrt 1/1 Running 0 2s 10.244.3.21 node-0003 myapache-66b4559787-pv928 1/1 Running 0 2s 10.244.3.23 node-0003 myapache-66b4559787-qz658 1/1 Running 0 2s 10.244.3.22 node-0003 myapache-66b4559787-z2n4d 1/1 Running 0 2s 10.244.1.17 node-0002 [root@master ~]#



