Kubectl是管理k8s集群的命令行工具,通过生成的json格式传递给apiserver进行创建、查看、管理的操作。
[root@master ~]# kubectl --help '帮助信息'
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 使用 replication controller, service, deployment 或者 pod
并暴露它作为一个 新的 Kubernetes Service
run 在集群中运行一个指定的镜像
set 为 objects 设置一个指定的特征
Basic Commands (Intermediate):
explain 查看资源的文档
get 显示一个或更多 resources
edit 在服务器上编辑一个资源
delete Delete resources by filenames, stdin, resources and names, or by resources and
label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale 为 Deployment, ReplicaSet, Replication Controller 或者 Job
设置一个新的副本数量
autoscale 自动调整一个 Deployment, ReplicaSet, 或者 ReplicationController
的副本数量
Cluster Management Commands:
certificate 修改 certificate 资源.
cluster-info 显示集群信息
top Display Resource (CPU/Memory/Storage) 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 复制 files 和 directories 到 containers 和从容器中复制 files 和
directories.
auth Inspect authorization
Advanced Commands:
apply 通过文件名或标准输入流(stdin)对资源进行配置
patch 使用 strategic merge patch 更新一个资源的 field(s)
replace 通过 filename 或者 stdin替换一个资源
wait Experimental: Wait for a specific condition on one or many resources.
convert 在不同的 API versions 转换配置文件
Settings Commands:
label 更新在这个资源上的 labels
annotate 更新一个资源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
alpha Commands for features in alpha
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).
使用kubectl命令管理项目的生命周期,创建–》发布–》更新–》回滚–》删除
创建
kubectl run命令
[root@master ~]# kubectl get pods
No resources found.
[root@master ~]# kubectl run nginx-deployment --image=nginx --port=80 --replicas=3 '创建一个nginx的pod'
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx-deployment created
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5477945587-5bnmw 0/1 ContainerCreating 0 14s
nginx-deployment-5477945587-8scx8 0/1 ContainerCreating 0 14s
nginx-deployment-5477945587-ss57l 0/1 ContainerCreating 0 14s
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5477945587-5bnmw 1/1 Running 0 82s
nginx-deployment-5477945587-8scx8 1/1 Running 0 82s
nginx-deployment-5477945587-ss57l 1/1 Running 0 82s
[root@master ~]# kubectl get all '查看更详细信息:副本资源和控制器资源'
NAME READY STATUS RESTARTS AGE
pod/nginx-deployment-5477945587-5bnmw 1/1 Running 0 4m5s
pod/nginx-deployment-5477945587-8scx8 1/1 Running 0 4m5s
pod/nginx-deployment-5477945587-ss57l 1/1 Running 0 4m5s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-deployment 3 3 3 3 4m5s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-deployment-5477945587 3 3 3 4m5s
发布
发布nginx service提供负载均衡的功能
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort '将刚刚创建的暴露出来'
service/nginx-service exposed
[root@master ~]# kubectl get svc '查看service服务'
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d
nginx-service NodePort 10.0.0.28 <none> 80:31918/TCP 42s
查看资源对象简写
[root@master ~]# kubectl api-resources
NAME SHORTNAMES APIGROUP NAMESPACED KIND
bindings true Binding
componentstatuses cs false ComponentStatus
configmaps cm true ConfigMap
endpoints ep true Endpoints
events ev true Event
limitranges limits true LimitRange
namespaces ns false Namespace
nodes no false Node
persistentvolumeclaims pvc true PersistentVolumeClaim
persistentvolumes pv false PersistentVolume
pods po true Pod
podtemplates true PodTemplate
replicationcontrollers rc true ReplicationController
resourcequotas quota true ResourceQuota
secrets true Secret
serviceaccounts sa true ServiceAccount
services svc true Service
mutatingwebhookconfigurations admissionregistration.k8s.io false MutatingWebhookConfiguration
validatingwebhookconfigurations admissionregistration.k8s.io false ValidatingWebhookConfiguration
customresourcedefinitions crd,crds apiextensions.k8s.io false CustomResourceDefinition
apiservices apiregistration.k8s.io false APIService
controllerrevisions apps true ControllerRevision
daemonsets ds apps true DaemonSet
deployments deploy apps true Deployment
replicasets rs apps true ReplicaSet
statefulsets sts apps true StatefulSet
tokenreviews authentication.k8s.io false TokenReview
localsubjectaccessreviews authorization.k8s.io true LocalSubjectAccessReview
selfsubjectaccessreviews authorization.k8s.io false SelfSubjectAccessReview
selfsubjectrulesreviews authorization.k8s.io false SelfSubjectRulesReview
subjectaccessreviews authorization.k8s.io false SubjectAccessReview
horizontalpodautoscalers hpa autoscaling true HorizontalPodAutoscaler
cronjobs cj batch true CronJob
jobs batch true Job
certificatesigningrequests csr certificates.k8s.io false CertificateSigningRequest
leases coordination.k8s.io true Lease
events ev events.k8s.io true Event
daemonsets ds extensions true DaemonSet
deployments deploy extensions true Deployment
ingresses ing extensions true Ingress
networkpolicies netpol extensions true NetworkPolicy
podsecuritypolicies psp extensions false PodSecurityPolicy
replicasets rs extensions true ReplicaSet
kuboardaddonresources kbar kuboard.cn true KuboardAddonResource
kuboardaddons kba kuboard.cn true KuboardAddon
kuboardlayouts klt kuboard.cn false KuboardLayout
kuboardlicenses klcs kuboard.cn false KuboardLicense
networkpolicies netpol networking.k8s.io true NetworkPolicy
poddisruptionbudgets pdb policy true PodDisruptionBudget
podsecuritypolicies psp policy false PodSecurityPolicy
clusterrolebindings rbac.authorization.k8s.io false ClusterRoleBinding
clusterroles rbac.authorization.k8s.io false ClusterRole
rolebindings rbac.authorization.k8s.io true RoleBinding
roles rbac.authorization.k8s.io true Role
priorityclasses pc scheduling.k8s.io false PriorityClass
storageclasses sc storage.k8s.io false StorageClass
volumeattachments storage.k8s.io false VolumeAttachment
查看关联后端的节点
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.179.121:6443,192.168.179.124:6443 12d
nginx-service 172.17.27.4:80,172.17.71.4:80,172.17.71.5:80 2d14h
网络状态详细信息
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE
nginx-7697996758-46qhn 1/1 Running 0 2d15h 172.17.27.4 192.168.179.122 <none>
nginx-7697996758-59r8v 1/1 Running 0 2d15h 172.17.71.4 192.168.179.123 <none>
nginx-7697996758-q4wb2 1/1 Running 0 2d15h 172.17.71.5 192.168.179.123 <none>
服务暴露的端口
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 10d
nginx-service NodePort 10.0.0.28 <none> 80:31918/TCP 42s
在node01操作,查看负载均衡端口31918
kubernetes里kube-proxy支持三种模式,在v1.8之前使用的是iptables 以及 userspace两种模式,在kubernetes 1.8之后引入了ipvs模式
[root@node01 ~]# yum install ipvsadm -y
[root@node01 ~]# ipvsadm -L -n
TCP 127.0.0.1:31918 rr
-> 172.17.27.4:80 Masq 1 0 0
-> 172.17.71.4:80 Masq 1 0 0
-> 172.17.71.5:80 Masq 1 0 0
在node02操作 同样安装ipvsadmin工具查看
[root@node02 ~]# yum install ipvsadm -y
[root@node02 ~]# ipvsadm -L -n
TCP 127.0.0.1:31918 rr
-> 172.17.27.4:80 Masq 1 0 0
-> 172.17.71.4:80 Masq 1 0 0
-> 172.17.71.5:80 Masq 1 0 0
在master01操作 查看访问日志(注意:如果访问其他node无法访问检查proxy组件)
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-46qhn 1/1 Running 0 2d15h
nginx-7697996758-59r8v 1/1 Running 0 2d15h
nginx-7697996758-q4wb2 1/1 Running 0 2d15h
[root@master ~]# kubectl logs nginx-7697996758-q4wb2
/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
172.17.71.1 - - [09/Oct/2020:09:37:42 +0000] "GET / HTTP/1.1" 200 612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70" "-"
2020/10/09 09:37:42 [error] 28#28: *1 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 172.17.71.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.179.123:31918", referrer: "http://192.168.179.123:31918/"
172.17.71.1 - - [09/Oct/2020:09:37:42 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://192.168.179.123:31918/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.70" "-"
更新
更新nginx 为1.10版本
浏览器重新加载刷新页面查看nginx版本信息
[root@master ~]# kubectl set --help
Configure application resources
These commands help you make changes to existing application resources.
Available Commands:
env Update environment variables on a pod template
image 更新一个 pod template 的镜像
resources 在对象的 pod templates 上更新资源的 requests/limits
selector 设置 resource 的 selector
serviceaccount Update ServiceAccount of a resource
subject Update User, Group or ServiceAccount in a RoleBinding/ClusterRoleBinding
Usage:
kubectl set SUBCOMMAND [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).
获取修改模板
[root@master ~]# kubectl set image --help
kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
[root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.10 '更新版本'
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-6d97cf94f-22vbh 1/1 Running 0 2m5s
nginx-6d97cf94f-2bjpz 1/1 Running 0 88s
nginx-6d97cf94f-ndsbz 1/1 Running 0 2m28s
回滚
[root@master ~]# kubectl rollout --help
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history 显示 rollout 历史
pause 标记提供的 resource 为中止状态
resume 继续一个停止的 resource
status 显示 rollout 的状态
undo 撤销上一次的 rollout
Usage:
kubectl rollout SUBCOMMAND [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).
查看历史版本
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
执行回滚
[root@master ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-9xtkt 1/1 Running 0 58s
nginx-7697996758-flz65 1/1 Running 0 19s
nginx-7697996758-wvfvs 1/1 Running 0 49s
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
删除
[root@master ~]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx 3 3 3 3 2d15h
[root@master ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get deployment
No resources found.
删除服务svc
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
nginx-service NodePort 10.0.0.28 <none> 80:31918/TCP 2d15h
[root@master ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 12d
其他常规操作
查看具体资源的详细信息
[root@master ~]# kubectl run nginx --image=nginx:latest --port=80 --replicas=3 '创建资源'
kubectl run --generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead.
deployment.apps/nginx create
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-7697996758-2czcx 1/1 Running 0 82s
nginx-7697996758-7gg8t 1/1 Running 0 82s
nginx-7697996758-wsv22 1/1 Running 0 82s
[root@master ~]# kubectl describe pod nginx-7697996758-wsv22 '查看pod的详细信息'
Name: nginx-7697996758-wsv22
Namespace: default
Priority: 0
PriorityClassName: <none>
Node: 192.168.179.122/192.168.179.122
Start Time: Mon, 12 Oct 2020 09:06:35 +0800
Labels: pod-template-hash=7697996758
run=nginx
Annotations: <none>
Status: Running
IP: 172.17.27.4
Controlled By: ReplicaSet/nginx-7697996758
Containers:
nginx:
Container ID: docker://5d8a45bd99dad1cd3640d2f7463237789a9006f5d879a1a47a64433e08eae0a9
Image: nginx:latest
Image ID: docker-pullable://nginx@sha256:fc66cdef5ca33809823182c9c5d72ea86fd2cef7713cf3363e1a0b12a5d77500
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Mon, 12 Oct 2020 09:06:43 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-99dmr (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-99dmr:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-99dmr
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m5s default-scheduler Successfully assigned default/nginx-7697996758-wsv22 to 192.168.179.122
Normal Pulling 2m4s kubelet, 192.168.179.122 pulling image "nginx:latest"
Normal Pulled 117s kubelet, 192.168.179.122 Successfully pulled image "nginx:latest"
Normal Created 117s kubelet, 192.168.179.122 Created container
Normal Started 117s kubelet, 192.168.179.122 Started container
查看deployment资源
[root@master ~]# kubectl describe deployment/nginx
Name: nginx
Namespace: default
CreationTimestamp: Mon, 12 Oct 2020 09:06:35 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx:latest
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-7697996758 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 22m deployment-controller Scaled up replica set nginx-7697996758 to 3
进入pod
[root@master ~]# kubectl exec -it nginx-7697996758-wsv22 bash
root@nginx-7697996758-wsv22:/# ls
bin docker-entrypoint.d home media proc sbin tmp
boot docker-entrypoint.sh lib mnt root srv usr
dev etc lib64 opt run sys va