[root@master ~]# kubectl
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain Get documentation for a resource
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a deployment, replica set, or replication controller
autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info Display cluster information
top Display resource (CPU/memory) usage
cordon 标记 node 为 unschedulable
uncordon 标记 node 为 schedulable
drain Drain node in preparation for maintenance
taint 更新一个或者多个 node 上的 taints
Troubleshooting and Debugging Commands:
describe 显示一个指定 resource 或者 group 的 resources 详情
logs 输出容器在 pod 中的日志
attach Attach 到一个运行中的 container
exec 在一个 container 中执行一个命令
port-forward Forward one or more local ports to a pod
proxy 运行一个 proxy 到 Kubernetes API server
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 文件
plugin Provides utilities for interacting with plugins
version 输出 client 和 server 的版本信息
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
查看node06具体信息
[root@master ~]# kubectl describe node node06
使用k8s 干跑创建一个名为:ngxin-test , 镜像为nginx1.14 暴露端口80
[root@master ~]# kubectl run nginx-test --image=nginx:1.14-aline --port=80 --dry-run=client
run并不会创建 deployment
kubectl run nginx-test1 --image=nginx --port=80
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test 0/1 ImagePullBackOff 0 117s
nginx-test1 1/1 Running 0 59s
[root@master ~]# kubectl get deployment
No resources found in default namespace.
kubectl create deployment nginx-test1 --image=nginx
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-9q4jf 1/1 Running 0 5m40s 10.244.1.3 node06 <none> <none>
nginx-test 0/1 ImagePullBackOff 0 10m 10.244.1.2 node06 <none> <none>
nginx-test1 1/1 Running 0 9m48s 10.244.2.4 node07 <none> <none>
nginx-test1-664f6969cd-r77qh 1/1 Running 0 4m7s 10.244.2.5 node07 <none> <none>
删除pods
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-9q4jf 1/1 Running 0 13m 10.244.1.3 node06 <none> <none>
nginx-test 0/1 ImagePullBackOff 0 18m 10.244.1.2 node06 <none> <none>
nginx-test1 1/1 Running 0 17m 10.244.2.4 node07 <none> <none>
nginx-test1-664f6969cd-r77qh 1/1 Running 0 11m 10.244.2.5 node07 <none> <none>
[root@master ~]# kubectl delete pods nginx-test
pod "nginx-test" deleted
删除deployment(如果删除的pods为deployment方式创建,则删除pods后仍会新建一个pods,需要删除deployment)
[root@master ~]# kubectl delete pods nginx-test1-664f6969cd-r77qh
pod "nginx-test1-664f6969cd-r77qh" deleted
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6799fc88d8-9q4jf 1/1 Running 0 18m 10.244.1.3 node06 <none> <none>
nginx-test1-664f6969cd-ldm2s 0/1 ContainerCreating 0 4s <none> node06 <none> <none>
[root@master ~]# kubectl delete deployment nginx-test1-664f6969cd-ldm2s
Error from server (NotFound): deployments.apps "nginx-test1-664f6969cd-ldm2s" not found
[root@master ~]# kubectl delete deployment nginx-test1
deployment.apps "nginx-test1" deleted
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6799fc88d8-9q4jf 1/1 Running 0 20m
暴露服务
[root@master ~]# kubectl expose deployment nginx --name=nginx --port=80 --target-port=80 --protocol=TCP
service/nginx exposed
svc_ip:svc_port =10.96.0.0/16
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
nginx ClusterIP 10.99.62.108 <none> 80/TCP 6m51s
使用node06访问,能在节点访问,但是外部依旧无法访问
[root@master ~]# kubectl get pods -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
coredns-7f6cbbb7b8-cp9kh 1/1 Running 0 12d 10.244.2.2 node07 <none> <none>
coredns-7f6cbbb7b8-z8fns 1/1 Running 0 12d 10.244.2.3 node07 <none> <none>
etcd-master 1/1 Running 0 12d 192.168.176.189 master <none> <none>
kube-apiserver-master 1/1 Running 0 12d 192.168.176.189 master <none> <none>
kube-controller-manager-master 1/1 Running 0 12d 192.168.176.189 master <none> <none>
kube-flannel-ds-sdxhr 1/1 Running 0 12d 192.168.176.133 node07 <none> <none>
kube-flannel-ds-wtd7s 1/1 Running 0 12d 192.168.176.193 node06 <none> <none>
kube-flannel-ds-xz6f8 1/1 Running 0 12d 192.168.176.189 master <none> <none>
kube-proxy-gfjxf 1/1 Running 0 12d 192.168.176.133 node07 <none> <none>
kube-proxy-mlvjh 1/1 Running 0 12d 192.168.176.189 master <none> <none>
kube-proxy-ztrfr 1/1 Running 0 12d 192.168.176.193 node06 <none> <none>
kube-scheduler-master 1/1 Running 0 12d 192.168.176.189 master <none> <none>
[root@master ~]# kubectl get svc -n kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 12d
获取nginx服务详细信息
[root@master ~]# kubectl describe svc nginx
Name: nginx
Namespace: default
Labels: app=nginx
Annotations: <none>
Selector: app=nginx
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.99.62.108
IPs: 10.99.62.108
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.3:80
Session Affinity: None
Events: <none>
显示标签
[root@master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
client 0/1 Error 0 19m run=client
nginx-6799fc88d8-9q4jf 1/1 Running 0 63m app=nginx,pod-template-hash=6799fc88d8
创建名为myapp的服务,并扩容/缩容
[root@master ~]# kubectl create deployment myapp --image=ikubernetes/myapp:v1
deployment.apps/myapp created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
client 0/1 Error 0 48m
myapp-7d4b7b84b-ksd6d 0/1 ContainerCreating 0 18s
nginx-6799fc88d8-9q4jf 1/1 Running 0 92m
[root@master ~]# kubectl expose deployment myapp --name=myapp --port=80
service/myapp exposed
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
myapp ClusterIP 10.102.213.62 <none> 80/TCP 2s
nginx ClusterIP 10.99.62.108 <none> 80/TCP 75m
[root@master ~]# kubectl scale --replicas=5 deployment myapp
deployment.apps/myapp scaled
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 0/1 Error 0 61m 10.244.2.6 node07 <none> <none>
myapp-7d4b7b84b-6w5pb 1/1 Running 0 61s 10.244.1.6 node06 <none> <none>
myapp-7d4b7b84b-bc4s9 1/1 Running 0 61s 10.244.1.5 node06 <none> <none>
myapp-7d4b7b84b-hj6ng 1/1 Running 0 61s 10.244.2.8 node07 <none> <none>
myapp-7d4b7b84b-ksd6d 1/1 Running 0 14m 10.244.2.7 node07 <none> <none>
myapp-7d4b7b84b-l5vw4 1/1 Running 0 61s 10.244.2.9 node07 <none> <none>
nginx-6799fc88d8-9q4jf 1/1 Running 0 105m 10.244.1.3 node06 <none> <none>
[root@master ~]# kubectl scale --replicas=2 deployment myapp
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
client 0/1 Error 0 62m 10.244.2.6 node07 <none> <none>
myapp-7d4b7b84b-6w5pb 1/1 Running 0 118s 10.244.1.6 node06 <none> <none>
myapp-7d4b7b84b-bc4s9 1/1 Running 0 118s 10.244.1.5 node06 <none> <none>
nginx-6799fc88d8-9q4jf 1/1 Running 0 106m 10.244.1.3 node06 <none> <none>
灰度滚动更新
[root@master ~]# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2
deployment.apps/myapp image updated
[root@master ~]# kubectl rollout status deployment myapp
Waiting for deployment "myapp" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 3 out of 4 new replicas have been updated...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "myapp" rollout to finish: 3 of 4 updated replicas are available...
deployment "myapp" successfully rolled out
外部访问方法
kubectl edit svc myapp
kubectl get svc
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 12d
myapp NodePort 10.102.213.62 <none> 80:31618/TCP 22m
nginx ClusterIP 10.99.62.108 <none> 80/TCP 98m
查看某个pod的详细信息
kubectl describe pods client
查看某个pod的日志
kubectl logs client