kubectl --help
k8s官方中文文档:
http://docs.kubernetes.org.cn/683.htmlkubectl version
[root@master ~]# kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.1", GitCommit:"4485c6f18cee9a5d3c3b4e523bd27972b1b53892", GitTreeState:"clean", BuildDate:"2019-07-18T09:18:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.1", GitCommit:"4485c6f18cee9a5d3c3b4e523bd27972b1b53892", GitTreeState:"clean", BuildDate:"2019-07-18T09:09:21Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
kubectl get nodes
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 20h v1.15.1
node01 Ready <none> 20h v1.15.1
node02 Ready <none> 20h v1.15.1
kubectl api-resources
[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
ingresses ing networking.k8s.io true Ingress
networkpolicies netpol networking.k8s.io true NetworkPolicy
runtimeclasses node.k8s.io false RuntimeClass
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
csidrivers storage.k8s.io false CSIDriver
csinodes storage.k8s.io false CSINode
storageclasses sc storage.k8s.io false StorageClass
volumeattachments storage.k8s.io false VolumeAttachment
kubectl cluster-info
[root@master ~]# kubectl cluster-info
Kubernetes master is running at https://192.168.122.10:6443
KubeDNS is running at https://192.168.122.10:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
source <(kubectl completion bash)
[root@master ~]# source <(kubectl completion bash)
可通过TAB键实现命令补全,建议将其写入 /etc/profile
journalctl -u kubelet -f
[root@master ~]# journalctl -u kubelet -f
-- Logs begin at 一 2021-11-01 18:58:09 CST. --
......
kubectl get [-o wide|json|yaml] [-n namespace]
获取资源的相关信息,-n指定命名空间,-o指定输出格式
resource可以是具体资源名称,如"pod nhinx-xxx";也可以是资源类型,如“pod,node,svc,deploy”多种资源使用逗号间隔;或者all(仅展示几种核心资源,并不完整)
–all-namespaces或-A:表示显示所有命名空间
–show-labels:显示所有标签
-l app:仅显示标签为app的资源
-l app=nginx:仅显示包含app标签,且值为nginx的资源
kubectl get componentstatuses
kubectl get cs
[root@master ~]# kubectl get componentstatuses
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
[root@master ~]# kubectl get cs
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-0 Healthy {"health":"true"}
kubectl get namespace
kubectl get ns
[root@master ~]# kubectl get namespace
NAME STATUS AGE
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
kubectl get all [-n default]
由于deafult为缺省空间,当不指定命名空间时默认查看default命名空间
[root@node1 ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.244.64.1 <none> 443/TCP 4h36m
kubectl create ns app
[root@node1 ~]# kubectl create ns app
namespace/app created
[root@node1 ~]# kubectl get ns
NAME STATUS AGE
app Active 3s
default Active 4h37m
ingress-controller Active 4h34m
kube-node-lease Active 4h37m
kube-public Active 4h37m
kube-system Active 4h37m
kubernetes-dashboard Active 4h33m
kubectl delete ns app
[root@node1 ~]# kubectl delete ns app
namespace "app" deleted
[root@node1 ~]# kubectl get ns
NAME STATUS AGE
default Active 4h37m
ingress-controller Active 4h35m
kube-node-lease Active 4h37m
kube-public Active 4h37m
kube-system Active 4h37m
kubernetes-dashboard Active 4h33m
kubectl create deployment nginx-test --image=nginx -n kube-public
[root@node1 ~]# kubectl get pod -n kube-public
No resources found in kube-public namespace.
[root@node1 ~]# kubectl create deployment nginx-test --image=nginx -n kube-public
deployment.apps/nginx-test created
[root@node1 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 0/1 1 0 23s
[root@node1 ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 2m24s
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 2m58s
kubectl describe deployment nginx-test -n kube-public
[root@node1 ~]# kubectl describe deployment nginx-test -n kube-public
Name: nginx-test
Namespace: kube-public
CreationTimestamp: Thu, 16 Dec 2021 20:35:23 +0800
Labels: app=nginx-test
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-test
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-test-795d659f45 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 4m26s deployment-controller Scaled up replica set nginx-test-795d659f45 to 1
kubectl describe pod nginx-test -n kube-public
[root@node1 ~]# kubectl describe pod nginx-test -n kube-public
......
kubectl get pods -n kube-public
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 6m4s
kubectl exec 可以跨主机登录容器,docker exec 只能在容器所在主机登录
[root@node1 ~]# kubectl exec -it nginx-test-795d659f45-n4nks bash -n kube-public
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-test-795d659f45-n4nks:/# ls
bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var
boot docker-entrypoint.d etc lib media opt root sbin sys usr
由于存在 deployment/rc 之类的副本控制器,删除 pod 也会重新拉起来
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-n4nks 1/1 Running 0 12m
[root@node1 ~]# kubectl delete pod nginx-test-795d659f45-n4nks -n kube-public
pod "nginx-test-795d659f45-n4nks" deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-65pwr 1/1 Running 0 11s
kubectl delete pod [] -n [] --force --grace-period=0
grace-period表示过渡存活期,默认30s,在删除pod之前允许pod慢慢终止其上的容器进程,从而优雅的退出,0表示立即终止pod
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-r2jwz 1/1 Running 0 13s
[root@node1 ~]# kubectl delete pod nginx-test-795d659f45-r2jwz -n kube-public --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx-test-795d659f45-r2jwz" force deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 0/1 ContainerCreating 0 9s
kubectl scale deployment nginx-test --replicas=3 -n kube-public
[root@node1 ~]# kubectl scale deployment nginx-test --replicas=3 -n kube-public
deployment.apps/nginx-test scaled
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 7m
nginx-test-795d659f45-gl6z2 0/1 ContainerCreating 0 101s
nginx-test-795d659f45-p2q9s 0/1 ContainerCreating 0 101s
kubectl scale deployment nginx-test --replicas=1 -n kube-public
[root@node1 ~]# kubectl scale deployment nginx-test --replicas=1 -n kube-public
deployment.apps/nginx-test scaled
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 8m4s
nginx-test-795d659f45-gl6z2 0/1 Terminating 0 2m45s
nginx-test-795d659f45-p2q9s 0/1 Terminating 0 2m45s
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 1/1 Running 0 8m19s
kubectl delete deployment nginx-test -n kube-public
kubectl delete deployment/nginx-test -n kube-public
[root@node1 ~]# kubectl delete deployment nginx-test -n kube-public
deployment.apps "nginx-test" deleted
[root@node1 ~]# kubectl get pod -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-795d659f45-6h9kj 0/1 Terminating 0 8m46s
[root@node1 ~]# kubectl get pod -n kube-public
No resources found in kube-public namespace.
kubectl label deploy nginx-test version=nginx1.14
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test
[root@master ~]# kubectl label deploy nginx-test version=nginx1.14
deployment.extensions/nginx-test labeled
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test,version=nginx1.14
kubectl label deploy nginx-test version-
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 19m run=nginx-test,version=nginx1.14
[root@master ~]# kubectl label deploy nginx-test version-
deployment.extensions/nginx-test labeled
[root@master ~]# kubectl get deploy --show-labels
NAME READY UP-TO-DATE AVAILABLE AGE LABELS
nginx-test 3/3 3 3 20m run=nginx-test
K8S 模拟项目 pod 发布
创建–>发布–>更新–>回滚–>删除
● 创建并运行一个或多个容器镜像
● 创建一个deployment或job来管理容器
kubectl run --help查看使用帮助
启动nginx实例,暴露容器端口80,设置副本数3
kubectl run nginx --image=nginx:1.14 --port=80 --replicas=3
[root@master ~]# kubectl run nginx --image=nginx:1.14 --port=80 --replicas=3
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.
deployment.apps/nginx created
kubectl get pods
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-65fc77987d-cwvwl 1/1 Running 0 7s
nginx-65fc77987d-m7cnn 1/1 Running 0 7s
nginx-65fc77987d-z7hvx 1/1 Running 0 7s
kubectl get all
[root@master ~]# kubectl get all
NAME READY STATUS RESTARTS AGE
pod/nginx-65fc77987d-cwvwl 1/1 Running 0 24s
pod/nginx-65fc77987d-m7cnn 1/1 Running 0 24s
pod/nginx-65fc77987d-z7hvx 1/1 Running 0 24s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 82s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 24s
NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-65fc77987d 3 3 3 24s
● 将资源暴露为新的Service
kubectl expose --help查看使用帮助
为Deployment的nginx创建Service,并通过Service的80端口转发至容器的80端口上,Service的名称为nginx-service,类型为NodePort
kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
[root@master ~]# kubectl expose deployment nginx --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
Kubernetes之所以需要Service,一方面是因为Pod的IP不是固定的(Pod可能会重建),另一方面是因为一组Pod实例之间总会有负载均衡的需求。
Service通过Label Selector实现的对一组的Pod的访问。
对于容器应用而言,Kubernetes提供了基于VIP(虚拟IP)的网桥的方式访问Service,再由Service重定向到相应的Pod。
● ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(Service默认类型)
● NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过NodeIP:NodePort的方式
● LoadBalancer:通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
kubectl get pods,svc -o wide
[root@master ~]# kubectl get pods,svc -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-65fc77987d-cwvwl 1/1 Running 0 61s 10.244.1.25 node01 <none> <none>
pod/nginx-65fc77987d-m7cnn 1/1 Running 0 61s 10.244.1.24 node01 <none> <none>
pod/nginx-65fc77987d-z7hvx 1/1 Running 0 61s 10.244.2.15 node02 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 119s <none>
service/nginx-service NodePort 10.1.155.154 <none> 80:32107/TCP 15s run=nginx
kubectl get endpoints
[root@master ~]# kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.122.10:6443 15m
nginx-service 10.244.1.24:80,10.244.1.25:80,10.244.2.15:80 13m
kubectl describe svc nginx
[root@master ~]# kubectl describe svc nginx
Name: nginx-service
Namespace: default
Labels: run=nginx
Annotations: <none>
Selector: run=nginx
Type: NodePort
IP: 10.1.155.154
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32107/TCP
Endpoints: 10.244.1.24:80,10.244.1.25:80,10.244.2.15:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
在node01节点上操作
[root@node01 ~]# yum install -y ipvsadm
[root@node01 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
......
TCP 192.168.122.11:32107 rr
#外部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
TCP 10.1.155.154:80 rr
#pod集群组内部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
在node02节点上操作
[root@node02 ~]# yum install -y ipvsadm
[root@node02 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
......
TCP 192.168.122.12:32107 rr
#外部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
TCP 10.1.155.154:80 rr
#pod集群组内部访问的IP和端口
-> 10.244.1.24:80 Masq 1 0 0
-> 10.244.1.25:80 Masq 1 0 0
-> 10.244.2.15:80 Masq 1 0 0
......
curl 10.1.155.154
[root@master ~]# curl 10.1.155.154
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
curl 192.168.122.11:32107
[root@master ~]# curl 192.168.122.11:32107
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master ~]# kubectl logs nginx-65fc77987d-cwvwl
10.244.0.0 - - [02/Nov/2021:12:58:38 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@master ~]# kubectl logs nginx-65fc77987d-m7cnn
10.244.1.1 - - [02/Nov/2021:13:00:06 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
[root@master ~]# kubectl logs nginx-65fc77987d-z7hvx
10.244.2.1 - - [02/Nov/2021:13:00:10 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
● 更改现有应用资源一些信息。
kubectl set --help查看使用帮助
kubectl set image --help获取
[root@master ~]# kubectl set image --help
......
Examples:
# Set a deployment's nginx container image to 'nginx:1.9.1', and its busybox container image to 'busybox'.
kubectl set image deployment/nginx busybox=busybox nginx=nginx:1.9.1
......
[root@master ~]# curl -I 192.168.122.11:32107
#通过报文头查看版本号
HTTP/1.1 200 OK
Server: nginx/1.14.2
#nginx版本号为1.14.2
Date: Tue, 02 Nov 2021 13:10:49 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
kubectl set image deployment/nginx nginx=nginx:1.15
[root@master ~]# kubectl set image deployment/nginx nginx=nginx:1.15
deployment.extensions/nginx image updated
处于动态监听pod状态,由于使用的是滚动更新方式,所以会先生成一个新的pod,然后删除一个旧的pod,往后以此类推
kubectl get pods -w
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-65fc77987d-cwvwl 1/1 Running 0 62m
nginx-65fc77987d-m7cnn 1/1 Running 0 62m
nginx-65fc77987d-z7hvx 1/1 Running 0 62m
nginx-6cbd4b987c-65qtx 0/1 ContainerCreating 0 23s
#新建第一个pod
nginx-6cbd4b987c-65qtx 1/1 Running 0 24s
nginx-65fc77987d-cwvwl 1/1 Terminating 0 63m
#第一个新pod运行后,删除一个旧pod
nginx-6cbd4b987c-27qz7 0/1 Pending 0 0s
nginx-6cbd4b987c-27qz7 0/1 Pending 0 0s
nginx-6cbd4b987c-27qz7 0/1 ContainerCreating 0 0s
#新建第二个pod
nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m
nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m
nginx-65fc77987d-cwvwl 0/1 Terminating 0 63m
nginx-6cbd4b987c-27qz7 1/1 Running 0 8s
nginx-65fc77987d-m7cnn 1/1 Terminating 0 63m
#第二个新pod运行后,删除第二个旧pod
nginx-6cbd4b987c-m467f 0/1 Pending 0 0s
nginx-6cbd4b987c-m467f 0/1 Pending 0 0s
nginx-6cbd4b987c-m467f 0/1 ContainerCreating 0 0s
#新建第三个pod
nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m
nginx-6cbd4b987c-m467f 1/1 Running 0 2s
nginx-65fc77987d-z7hvx 1/1 Terminating 0 63m
#第三个新pod运行后,删除第三个旧pod
nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m
nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m
nginx-65fc77987d-m7cnn 0/1 Terminating 0 63m
nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m
nginx-65fc77987d-z7hvx 0/1 Terminating 0 63m
更新规则可通过“kubetl describe deployment nginx”的“RollingUpdateStrategy”查看,默认配置为“25% max unavailable, 25% max surge”,即按照25%的比例进行滚动更新。
kubectl get pod -o wide
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-6cbd4b987c-27qz7 1/1 Running 0 6m19s 10.244.2.16 node02 <none> <none>
nginx-6cbd4b987c-65qtx 1/1 Running 0 6m43s 10.244.1.26 node01 <none> <none>
nginx-6cbd4b987c-m467f 1/1 Running 0 6m11s 10.244.1.27 node01 <none> <none>
pod更新后,ip改变
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.15.12
#nginx版本更新为1.15.12
Date: Tue, 02 Nov 2021 13:22:29 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
● 对资源进行回滚管理
kubectl rollout --help查看使用帮助
kubectl rollout history deployment/nginx
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
kubectl rollout undo deployment/nginx
[root@master ~]# kubectl rollout undo deployment/nginx
deployment.extensions/nginx rolled back
查看pod的ip变化
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
#回滚后ip再次改变
nginx-65fc77987d-2525z 1/1 Running 0 42s 10.244.2.17 node02 <none> <none>
nginx-65fc77987d-9qkzp 1/1 Running 0 40s 10.244.2.18 node02 <none> <none>
nginx-65fc77987d-qg75q 1/1 Running 0 41s 10.244.1.28 node01 <none> <none>
查看当前nginx版本
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.14.2
#nginx版本回到1.14.2
Date: Tue, 02 Nov 2021 13:31:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
查看历史版本
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
2 <none>
3 <none>
回到revison2,即1.15版本
kubectl rollout undo deployment/nginx --to-revision=2
[root@master ~]# kubectl rollout undo deployment/nginx --to-revision=2
deployment.extensions/nginx rolled back
查看pod的ip变化
[root@master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
#回滚后ip再次改变
nginx-6cbd4b987c-mhqbc 1/1 Running 0 37s 10.244.1.29 node01 <none> <none>
nginx-6cbd4b987c-tf462 1/1 Running 0 36s 10.244.2.19 node02 <none> <none>
nginx-6cbd4b987c-z86d6 1/1 Running 0 35s 10.244.1.30 node01 <none> <none>
查看当前nginx版本
[root@master ~]# curl -I 192.168.122.11:32107
HTTP/1.1 200 OK
Server: nginx/1.15.12
#nginx版本回到1.15.12
Date: Tue, 02 Nov 2021 13:36:42 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
kubectl rollout status deployment/nginx
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
kubectl delete deployment/nginx
[root@master ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 3/3 3 3 88m
[root@master ~]# kubectl delete deployment/nginx
deployment.extensions "nginx" deleted
[root@master ~]# kubectl get deploy
No resources found.
kubectl delete svc/nginx-service
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 91m
nginx-service NodePort 10.1.155.154 <none> 80:32107/TCP 89m
[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.1.0.1 <none> 443/TCP 91m
[root@master ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 92m
Deployment控制器支持自定义控制更新过程中的滚动节奏,如“暂停(pause)”或“继续(resume)”更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,在筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
kubectl run nginx-test --image=nginx:1.14 --replicas=3
[root@master ~]# kubectl run nginx-test --image=nginx:1.14 --replicas=3
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.
deployment.apps/nginx-test created
[root@master ~]# kubectl get pods,deploy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 51s 10.244.2.23 node02 <none> <none>
pod/nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 51s 10.244.1.37 node01 <none> <none>
pod/nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 51s 10.244.1.38 node01 <none> <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.extensions/nginx-test 3/3 3 3 51s nginx-test nginx:1.14 run=nginx-test
kubectl expose deploy nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort
[root@master ~]# kubectl expose deploy nginx-test --port=80 --target-port=80 --name=nginx-service --type=NodePort
service/nginx-service exposed
[root@master ~]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 23h <none>
nginx-service NodePort 10.1.36.134 <none> 80:30191/TCP 9s run=nginx-test
[root@master ~]# curl -I 192.168.122.11:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
#版本号为1.14.2
Date: Wed, 03 Nov 2021 11:54:11 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
在不定义CHANGE-CAUSE的情况下,缺省值为,当历史版本较多时,不便于咱们回滚时辨认版本号。因此,建议定义CHANGE-CAUSE为服务版本以帮助咱们辨认当前服务。
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 <none>
一般通过修改配置的方式定义change-cause
[root@master ~]# kubectl edit deploy/nginx-test
......
kind: Deployment
metadata:
annotations:
#下行可定义历史版本revision
deployment.kubernetes.io/revision: "1"
#在Deployment的matadata项下的annotations中如下行定义change-cause
kubernetes.io/change-cause: "nginx1.14"
......
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
kubectl set image deploy/nginx-test nginx-test=nginx:1.15 && kubectl rollout pause deploy/nginx-test
[root@master ~]# kubectl set image deploy/nginx-test nginx-test=nginx:1.15 && kubectl rollout pause deploy/nginx-test
deployment.extensions/nginx-test image updated
deployment.extensions/nginx-test paused
kubectl rollout status deploy/nginx-test
[root@master ~]# kubectl rollout status deploy/nginx-test
Waiting for deployment "nginx-test" rollout to finish: 1 out of 3 new replicas have been updated...
可以看到已经新增了一个pod,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令
kubectl get pods -w
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 8m28s 10.244.2.24 node02 <none> <none>
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 48m 10.244.2.23 node02 <none> <none>
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 48m 10.244.1.37 node01 <none> <none>
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 48m 10.244.1.38 node01 <none> <none>
kubectl get pod -o wide
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 8m28s 10.244.2.24 node02 <none> <none>
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 48m 10.244.2.23 node02 <none> <none>
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 48m 10.244.1.37 node01 <none> <none>
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 48m 10.244.1.38 node01 <none> <none>
查看nginx版本
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:45:37 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:46:59 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Wed, 03 Nov 2021 12:47:09 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 04 Dec 2018 14:44:49 GMT
Connection: keep-alive
ETag: "5c0692e1-264"
Accept-Ranges: bytes
[root@master ~]# curl -I 192.168.122.12:30191
HTTP/1.1 200 OK
Server: nginx/1.15.12
#新pod为1.15
Date: Wed, 03 Nov 2021 12:47:12 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Apr 2019 13:08:19 GMT
Connection: keep-alive
ETag: "5cb5d3c3-264"
Accept-Ranges: bytes
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
2 nginx1.14
kubectl edit deploy/nginx-test
[root@master ~]# kubectl edit deploy/nginx-test
kind: Deployment
metadata:
annotations:
#下行的revison自动更新为2
deployment.kubernetes.io/revision: "2"
#修改下行的change-cause为nginx1.15
kubernetes.io/change-cause: nginx1.15
kubectl rollout history deploy/nginx-test
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
2 nginx1.15
测试新版本没问题继续更新
kubectl rollout resume deploy/nginx-test
[root@master ~]# kubectl rollout resume deploy/nginx-test
deployment.extensions/nginx-test resumed
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-test-679dcbd68d-zw8w5 1/1 Running 0 61s
nginx-test-6cc7cd5547-4xqcb 1/1 Running 0 41m
nginx-test-6cc7cd5547-gcbh5 1/1 Running 0 41m
nginx-test-6cc7cd5547-nw6k6 1/1 Running 0 41m
nginx-test-6cc7cd5547-gcbh5 1/1 Terminating 0 63m
nginx-test-679dcbd68d-cljzh 0/1 Pending 0 0s
nginx-test-679dcbd68d-cljzh 0/1 Pending 0 0s
nginx-test-679dcbd68d-cljzh 0/1 ContainerCreating 0 0s
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-gcbh5 0/1 Terminating 0 63m
nginx-test-679dcbd68d-cljzh 1/1 Running 0 1s
nginx-test-6cc7cd5547-nw6k6 1/1 Terminating 0 63m
nginx-test-679dcbd68d-s2gck 0/1 Pending 0 0s
nginx-test-679dcbd68d-s2gck 0/1 Pending 0 0s
nginx-test-679dcbd68d-s2gck 0/1 ContainerCreating 0 0s
nginx-test-679dcbd68d-s2gck 1/1 Running 0 1s
nginx-test-6cc7cd5547-4xqcb 1/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-nw6k6 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
nginx-test-6cc7cd5547-4xqcb 0/1 Terminating 0 63m
kubectl get deploy/nginx-test -o yaml
[root@master ~]# kubectl get deploy/nginx-test -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "3"
kubernetes.io/change-cause: nginx1.14
creationTimestamp: "2021-11-03T11:47:56Z"
generation: 7
labels:
run: nginx-test
name: nginx-test
namespace: default
resourceVersion: "105895"
selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-test
uid: 634d471e-3907-4717-a02e-f5cce101d2f4
spec:
progressDeadlineSeconds: 600
replicas: 3
revisionHistoryLimit: 10
selector:
matchLabels:
run: nginx-test
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
run: nginx-test
spec:
containers:
- image: nginx:1.14
imagePullPolicy: IfNotPresent
name: nginx-test
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
status:
availableReplicas: 3
conditions:
- lastTransitionTime: "2021-11-03T11:47:58Z"
lastUpdateTime: "2021-11-03T11:47:58Z"
message: Deployment has minimum availability.
reason: MinimumReplicasAvailable
status: "True"
type: Available
- lastTransitionTime: "2021-11-03T12:50:56Z"
lastUpdateTime: "2021-11-03T12:57:35Z"
message: ReplicaSet "nginx-test-6cc7cd5547" has successfully progressed.
reason: NewReplicaSetAvailable
status: "True"
type: Progressing
observedGeneration: 7
readyReplicas: 3
replicas: 3
updatedReplicas: 3
kubectl get service nginx-service -o yaml
[root@master ~]# kubectl get service nginx-service -o yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-11-03T11:52:40Z"
labels:
run: nginx-test
name: nginx-service
namespace: default
resourceVersion: "100051"
selfLink: /api/v1/namespaces/default/services/nginx-service
uid: b2ec9834-864a-4146-9296-5420ac15451d
spec:
clusterIP: 10.1.36.134
externalTrafficPolicy: Cluster
ports:
- nodePort: 30191
port: 80
protocol: TCP
targetPort: 80
selector:
run: nginx-test
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
kubectl explain deployment.metadata
[root@master ~]# kubectl explain deployment.metadata
KIND: Deployment
VERSION: extensions/v1beta1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object metadata.
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
......
kubectl explain service.metadata
[root@master ~]# kubectl explain service.metadata
KIND: Service
VERSION: v1
RESOURCE: metadata <Object>
DESCRIPTION:
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
ObjectMeta is metadata that all persisted resources must have, which
includes all objects users must create.
FIELDS:
annotations <map[string]string>
Annotations is an unstructured key value map stored with a resource that
may be set by external tools to store and retrieve arbitrary metadata. They
are not queryable and should be preserved when modifying objects. More
info: http://kubernetes.io/docs/user-guide/annotations
......
修改yaml文件:并用kubectl apply -f xxxx.yaml文件使之生效
注意:当apply不生效时,先使用delete清除资源,再apply创建资源
kubectl get service nginx-service -o yaml > nginx-svc.yaml
[root@master ~]# kubectl get service nginx-service -o yaml > nginx-svc.yaml
[root@master ~]# vim nginx-svc.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2021-11-03T11:52:40Z"
labels:
run: nginx-test
name: nginx-service
namespace: default
resourceVersion: "100051"
selfLink: /api/v1/namespaces/default/services/nginx-service
uid: b2ec9834-864a-4146-9296-5420ac15451d
spec:
clusterIP: 10.1.36.134
externalTrafficPolicy: Cluster
ports:
- nodePort: 30191
#修改port为80
port: 8080
protocol: TCP
targetPort: 80
selector:
run: nginx-test
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
删除资源
kubectl delete -f nginx-svc.yaml
[root@master ~]# kubectl delete -f nginx-svc.yaml
service "nginx-service" deleted
新建资源
kubectl apply -f nginx-svc.yaml
[root@master ~]# kubectl apply -f nginx-svc.yaml
service/nginx-service created
查看service资源
kubectl get svc
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 25h
nginx-service NodePort 10.1.36.134 <none> 8080:30191/TCP 56s
直接使用kubectl edit service nginx-service在线编辑配置资源清单并保存退出即时生效(如port: 888)
PS:此修改方式不会对yaml文件内容修改
kubectl edit service nginx-service
[root@master ~]# kubectl edit service nginx-service
......
ports:
- nodePort: 30191
#修改port为888
port: 888
protocol: TCP
targetPort: 80
......
查看service资源
kubectl get svc
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 25h
nginx-service NodePort 10.1.36.134 <none> 888:30191/TCP 6m50s
kubectl delete service nginx-service
[root@master ~]# kubectl delete service nginx-service
service "nginx-service" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 25h
kubectl delete -f nginx-svc.yaml
[root@master ~]# kubectl apply -f nginx-svc.yaml
service/nginx-service created
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 25h
nginx-service NodePort 10.1.36.134 <none> 8080:30191/TCP 1s
[root@master ~]# kubectl delete -f nginx-svc.yaml
service "nginx-service" deleted
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 25h