kubectl详解
kubectl详解
- kubectl详解
- 一、陈述式管理
- 1. 陈述式资源管理方法
- 2. k8s相关信息查看
- 3. K8S模拟项目
- 4. 金丝雀发布/灰度发布(Canary Release)
- 二、声明式管理
- 一、陈述式管理
一、陈述式管理
1. 陈述式资源管理方法
- kubernetes集群管理集群资源的唯一入口是通过相应的方法调用apiserver的接口
- kubectl是官方的CLI命令行工具,用于与apiserver进行通信,将用户在命令行输入的命令,组织并转化为apiserver能识别的信息,进而实现管理k8s各种资源的一种有效途径
- kubectl的命令大全
kubectl --help
k8s官方中文文档:http://docs.kubernetes.org.cn/683.html - 对资源的增、删、查操作比较容易,但对改的操作就不容易了
2. k8s相关信息查看
2.1 查看版本信息
kubectl 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
2.2 查看资源对象简写
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
2.3 查看集群信息
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'.
2.4 配置kubectl自动补全
source <(kubectl completion bash)
[root@master ~]# source <(kubectl completion bash)
可通过TAB键实现命令补全,建议将其写入etc/profile
2.5 查看日志
journalctl -u kubelet -f
[root@master ~]# journalctl -u kubelet -f
-- Logs begin at 一 2021-11-01 18:58:09 CST. --
......
2.6 基本信息查看
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的资源
2.6.1 查看master节点状态
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"}
2.6.2 查看命名空间
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
2.7 命名空间操作
2.7.1 查看default命名空间的所有资源
kubectl get all [-n default]
由于deafult为缺省空间,当不指定命名空间时默认查看default命名空间
[root@master ~]# kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 94m
2.7.2 创建命名空间app
kubectl create ns app
[root@master ~]# kubectl create ns app
namespace/app created
kubectl get ns
[root@master ~]# kubectl get ns
NAME STATUS AGE
app Active 6s
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
2.7.3 删除命名空间app
kubectl delete ns app
[root@master ~]# kubectl delete ns app
namespace "app" deleted
kubectl get ns
[root@master ~]# kubectl get ns
NAME STATUS AGE
default Active 26h
kube-node-lease Active 26h
kube-public Active 26h
kube-system Active 26h
2.8 deployment/pod操作
2.8.1 在命名空间kube-public创建副本控制器(deployment)来启动Pod(nginx-test)
kubectl create deployment nginx-test --image=nginx -n kube-public
[root@master ~]# kubectl create deployment nginx-test --image=nginx -n kube-public
deployment.apps/nginx-test created
kubectl get deploy -n kube-public
[root@master ~]# kubectl get deploy -n kube-public
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 37s
2.8.2 描述某个资源的详细信息
kubectl describe deployment nginx-test -n kube-public
[root@master ~]# kubectl describe deployment nginx-test -n kube-public
Name: nginx-test
Namespace: kube-public
CreationTimestamp: Tue, 02 Nov 2021 21:46:26 +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-65c98cd596 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 2m36s deployment-controller Scaled up replica set nginx-test-65c98cd596 to 1
kubectl describe pod nginx-test -n kube-public
[root@master ~]# kubectl describe pod nginx-test -n kube-public
Name: nginx-test-65c98cd596-nq9cq
Namespace: kube-public
Priority: 0
Node: node01/192.168.122.11
Start Time: Tue, 02 Nov 2021 21:54:38 +0800
Labels: app=nginx-test
pod-template-hash=65c98cd596
Annotations: <none>
Status: Running
IP: 10.244.1.32
Controlled By: ReplicaSet/nginx-test-65c98cd596
Containers:
nginx:
Container ID: docker://4100bd07d49493c393353137bb7e4c05d58d8eaf51925be6b0d2b5a2b79f3862
Image: nginx
Image ID: docker-pullable://nginx@sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 02 Nov 2021 21:54:54 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-b8mv5 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-b8mv5:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-b8mv5
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 56s default-scheduler Successfully assigned kube-public/nginx-test-65c98cd596-nq9cq to node01
Normal Pulling 56s kubelet, node01 Pulling image "nginx"
Normal Pulled 40s kubelet, node01 Successfully pulled image "nginx"
Normal Created 40s kubelet, node01 Created container nginx
Normal Started 40s kubelet, node01 Started container nginx
2.8.3 查看命名空间kube-public中pod信息
kubectl get pods -n kube-public
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-nq9cq 1/1 Running 0 21s
2.8.4 登录容器
kubectl exec可以跨主机登录容器,docker exec只能在容器所在主机登录
kubectl exec -it nginx-test-65c98cd596-nq9cq bash -n kube-public
[root@master ~]# kubectl exec -it nginx-test-65c98cd596-nq9cq bash -n kube-public
root@nginx-test-65c98cd596-nq9cq:/# exit
exit
command terminated with exit code 127
2.8.5 删除(重启)pod资源
由于存在deployment/rc之类的副本控制器,删除pod也会重新拉起来
kubectl delete pod nginx-test-65c98cd596-nq9cq -n kube-public
[root@master ~]# kubectl delete pod nginx-test-65c98cd596-nq9cq -n kube-public
pod "nginx-test-65c98cd596-nq9cq" deleted
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-9jj2v 1/1 Running 0 18s
2.8.6 若无法删除,总是处于terminate状态,则要强行删除pod
kubectl delete pod -n --force --grace-period=0
grace-period表示过渡存活期,默认30s,在删除pod之前允许pod慢慢终止其上的容器进程,从而优雅的退出,0表示立即终止pod
[root@master ~]# kubectl delete pod nginx-test-65c98cd596-9jj2v -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-65c98cd596-9jj2v" force deleted
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-fgjcj 0/1 ContainerCreating 0 6s
2.8.7 扩缩容
2.8.7.1 扩容
kubectl scale deployment nginx-test --replicas=3 -n kube-public
[root@master ~]# kubectl scale deployment nginx-test --replicas=3 -n kube-public
deployment.extensions/nginx-test scaled
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-4zm54 1/1 Running 0 22s
nginx-test-65c98cd596-68nb7 1/1 Running 0 22s
nginx-test-65c98cd596-fgjcj 1/1 Running 0 55s
2.8.7.2 缩容
kubectl scale deployment nginx-test --replicas=1 -n kube-public
[root@master ~]# kubectl scale deployment nginx-test --replicas=1 -n kube-public
deployment.extensions/nginx-test scaled
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-4zm54 0/1 Terminating 0 50s
nginx-test-65c98cd596-68nb7 0/1 Terminating 0 50s
nginx-test-65c98cd596-fgjcj 1/1 Running 0 83s
[root@master ~]# kubectl get pods -n kube-public
NAME READY STATUS RESTARTS AGE
nginx-test-65c98cd596-fgjcj 1/1 Running 0 90s
2.8.8 删除副本控制器
kubectl delete deployment nginx-test -n kube-public
kubectl delete deployment/nginx-test -n kube-public
[root@master ~]# kubectl delete deployment nginx-test -n kube-public
deployment.extensions "nginx-test" deleted
[root@master ~]# kubectl get deployment -n kube-public
No resources found.
2.9 增加/删除label
2.9.1 增加label
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
2.9.2 删除label
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
3. K8S模拟项目
3.1 项目的生命周期
创建-->发布-->更新-->回滚-->删除
3.2 创建kubectl run命令
● 创建并运行一个或多个容器镜像
● 创建一个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
3.3 发布kubectl expose命令
● 将资源暴露为新的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
3.3.1 Service的作用
Kubernetes之所以需要Service,一方面是因为Pod的IP不是固定的(Pod可能会重建),另一方面是因为一组Pod实例之间总会有负载均衡的需求。
Service通过Label Selector实现的对一组的Pod的访问。
对于容器应用而言,Kubernetes提供了基于VIP(虚拟IP)的网桥的方式访问Service,再由Service重定向到相应的Pod。
3.3.2 Service的类型
● ClusterIP:提供一个集群内部的虚拟IP以供Pod访问(Service默认类型)
● NodePort:在每个Node上打开一个端口以供外部访问,Kubernetes将会在每个Node上打开一个端口并且每个Node的端口都是一样的,通过NodeIP:NodePort的方式
● LoadBalancer:通过外部的负载均衡器来访问,通常在云平台部署LoadBalancer还需要额外的费用。
3.3.3 查看Pod网络状态详细信息和Service暴露的端口
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
3.3.4 查看关联后端的节点
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
3.3.5 查看service的描述信息
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>
3.3.6 查看负载均衡端口
在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
......
3.3.7 访问查看
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>
curl 192.168.122.12:32107
[root@master ~]# curl 192.168.122.12: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>
3.3.8 查看访问日志
[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" "-"
3.4 更新kubectl set
● 更改现有应用资源一些信息。
kubectl set --help查看使用帮助
3.4.1 获取修改模板
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
......
3.4.2 查看当前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: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
3.4.3 将nginx版本更新为1.15
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
3.4.4 监听pod状态
处于动态监听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%的比例进行滚动更新。
3.4.5 查看pod的ip变化
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改变
3.4.6 重新查看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: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
3.5 回滚kubectl rollout
● 对资源进行回滚管理
kubectl rollout --help查看使用帮助
3.5.1 查看历史版本
kubectl rollout history deployment/nginx
[root@master ~]# kubectl rollout history deployment/nginx
deployment.extensions/nginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
3.5.2 执行回滚到上一个版本
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
3.5.3 执行回滚到指定版本
查看历史版本
[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
3.5.4 检查回滚状态
kubectl rollout status deployment/nginx
[root@master ~]# kubectl rollout status deployment/nginx
deployment "nginx" successfully rolled out
3.6 删除kubectl delete
3.6.1 删除副本控制器
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.
3.6.2 删除service
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
4. 金丝雀发布/灰度发布(Canary Release)
4.1 金丝雀发布简介
Deployment控制器支持自定义控制更新过程中的滚动节奏,如“暂停(pause)”或“继续(resume)”更新操作。比如等待第一批新的Pod资源创建完成后立即暂停更新过程,此时,仅存在一部分新版本的应用,主体部分还是旧的版本。然后,在筛选一小部分的用户请求路由到新版本的Pod应用,继续观察能否稳定地按期望的方式运行。确定没问题之后再继续完成余下的Pod资源滚动更新,否则立即回滚更新操作。这就是所谓的金丝雀发布。
4.2 更新deployment的版本,并配置暂停deployment
4.2.1 创建pods
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
4.2.2 发布服务
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
4.2.3 查看nginx版本
[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
4.2.4 定义版本CHANGE-CAUSE
4.2.4.1 查看历史版本
在不定义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>
4.2.4.2 定义版本CHANGE-CAUSE
一般通过修改配置的方式定义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"
......
4.2.4.3 再次查看历史版本
[root@master ~]# kubectl rollout history deploy/nginx-test
deployment.extensions/nginx-test
REVISION CHANGE-CAUSE
1 nginx1.14
4.2.5 更新nginx版本为1.15并配置暂停
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
4.2.6 观察更新状态
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...
4.2.7 监控更新的过程
可以看到已经新增了一个pod,但是并未按照预期的状态去删除一个旧的资源,就是因为使用了pause暂停命令
kubectl get pods -w
[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
4.2.8 查看nginx版本
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
4.2.9 查看并更新历史版本change-cause
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
4.2.10 resume继续更新
测试新版本没问题继续更新
kubectl rollout resume deploy/nginx-test
[root@master ~]# kubectl rollout resume deploy/nginx-test
deployment.extensions/nginx-test resumed
4.2.11 查看最后的更新情况
[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
二、声明式管理
1. 声明式管理方法
- 适合于对资源的修改操作
- 声明式资源管理方法依赖于资源配置清明文件对资源进行管理
资源配置清单文件有两种格式:yaml(人性化,易读),json(易于api接口解析) - 对资源的观念里,是通过实现定义在同一资源配置清单内,再通过陈述式命令应用到k8s集群里
- 语法格式:kubectl create/apply/delete -f -o yaml
2. 查看资源配置清单
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: {}
3. 解释资源配置清单
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
......
4. 修改资源配置清单并应用
4.1 离线修改
修改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
4.2 在线修改
直接使用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
5. 删除资源配置清单
5.1 陈述式删除
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
5.2 声明式删除
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
kubectl详解的更多相关文章
- Kubernetes,kubectl常用命令详解
kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...
- 第九章 kubectl命令行工具使用详解
1.管理k8s核心资源的三种基础方法 陈述式管理方法:主要依赖命令行CLI工具进行管理 声明式管理方法:主要依赖统一资源配置清单(manifest)进行管理 GUI式管理方法:主要依赖图形化操作界面( ...
- kubernetes之kubectl与YAML详解1
k8s集群的日志,带有组件的信息,多看日志. kubectl命令汇总 kubectl命令汇总 kubectl命令帮助信息 [root@mcwk8s04 ~]# kubectl -h kubectl c ...
- 详解k8s零停机滚动发布微服务 - kubernetes
1.前言 在当下微服务架构盛行的时代,用户希望应用程序时时刻刻都是可用,为了满足不断变化的新业务,需要不断升级更新应用程序,有时可能需要频繁的发布版本.实现"零停机"." ...
- kubernetes系列07—Pod控制器详解
本文收录在容器技术学习系列文章总目录 1.Pod控制器 1.1 介绍 Pod控制器是用于实现管理pod的中间层,确保pod资源符合预期的状态,pod的资源出现故障时,会尝试 进行重启,当根据重启策略无 ...
- Kubernetes 实践指南之Kubernetes 的命令行工具详解
kubectl作为客户端CLI工具,可以让用户通过命令行的方式对Kubernetes集群进行管理.本节内容将对kubectl的子命令和用法进行详细描述. 一.kubectl 用法概述 kubectl语 ...
- Docker Kubernetes Service 网络服务代理模式详解
Docker Kubernetes Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注 ...
- Docker Kubernetes 服务发现原理详解
Docker Kubernetes 服务发现原理详解 服务发现支持Service环境变量和DNS两种模式: 一.环境变量 (默认) 当一个Pod运行到Node,kubelet会为每个容器添加一组环境 ...
- Kubernetes学习之路(二十)之K8S组件运行原理详解总结
目录 一.看图说K8S 二.K8S的概念和术语 三.K8S集群组件 1.Master组件 2.Node组件 3.核心附件 四.K8S的网络模型 五.Kubernetes的核心对象详解 1.Pod资源对 ...
随机推荐
- Ranger-AdminServer安装Version2.0.0
Ranger-AdminServer安装, 对应的Ranger版本2.0.0. 1.安装规划 RangerAdmin安装依赖如下组件: mysql solr IP/机器名 安装软件 运行进程 dap2 ...
- scp 文件传输
1.推送 scp -r imageAPP/ root@ip:/data/soft/ 本地当前文件下的 imageAPP文件 推送到ip服务器 /data/soft/ 目录下 2.拉取 scp -r r ...
- spring boot pom.xml 提示 ignored 具体解决
1.出现这个情况 2. 进入设置,找到 去掉勾选即可
- PowerShell【Do While、Do Until篇】
1 $num=0 2 while($num -le 10) 3 { 4 $num 5 $num+=1 6 } 1 $num=0 2 do 3 { 4 $num 5 $num+=1 6 } 7 whil ...
- xftp 6 的 使用
1.前言 xftp是个向云服务器linux系统传输文件的软件,装载在window系统 简单易用 2.下载 官方下载地址:https://www.netsarang.com/zh/xftp-downlo ...
- 【填坑往事】使用Rxjava2的distinct操作符处理自定义数据类型去重的问题
最近碰到一个问题,自定义数据类型列表中出现了重复数据,需要去重.处理去重的办法很多,比如借助Set集合类,使用双重循环拿每一个元素和其他元素对比等.这里介绍一种简单而且比较优雅的方式:使用Rxjava ...
- SSM工程常见问题
1.引入Junit测试 <!--引入junit单元测试,两个一起引用,spring-test版本要与Spring-core保持一致--> <dependency> <gr ...
- 用Less 的 js方式替代 bootStrap 里 [class*=”span”]
Bootstrap 里的 grid system 里面 (源代码) 有这么一段, [class*="span"] { float: left; margin-left: @grid ...
- 达索CATIA许可证(License)管理使用和优化
现下主流的V6版本CATIA,是由达索公司提供授权的浮动型License,其客户端通过企业内网从许可证服务器获得许可证,最少要有一个服务器端DS License Server提供一定数量的Licens ...
- 解决nRF Connect for PC无法连接网络的问题(非FQ)
各位小伙伴是不是也遇到过国内不能正常使用nRF Connect的问题,现在教大家一个十分有效的办法. 1.找到nrf connect的脚本配置文件"apps.json",默认在&q ...