本文主要是介绍4、Deployment 运行应用,提示Flag --replicas has been deprecated, has no effect and will be removed in the,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
例如 kubectl create
、kubectl replace
、kubectl edit
和 kubectl patch
。
[root@master ~]# kubectl run nginx-app --image=nginx --replicas=2
Flag --replicas has been deprecated, has no effect and will be removed in the future.
pod/nginx-app created
在K8S v1.18.0以后,–replicas已弃用 ,推荐用 deployment 创建 pods
使用yaml文件的方式,创建nginx服务
这里还是修改一下以前下面的步骤还是有些问题的下面创建了没有deployment ,只有一个pod
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app 1/1 Running 0 46s
[root@master ~]# kubectl get deployment
No resources found in default namespace.
处理方式一:继续需要命令
step1、 删除上面语句产生的nnginx-app
[root@master ~]# kubectl delete pod nginx-app
pod "nginx-app" deleted
[root@master ~]#
step2、使用kubectl create 命令
[root@master ~]# kubectl create deployment nginx-app --image=nginx --replicas=2
deployment.apps/nginx-app created
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 2/2 2 2 36s
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-d6ff45774-9679z 1/1 Running 0 69s
nginx-app-d6ff45774-vpd2p 1/1 Running 0 69s
step 3、使用命令查看
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app-d6ff45774-9679z 1/1 Running 0 113s 10.244.2.61 node2 <none> <none>
nginx-app-d6ff45774-vpd2p 1/1 Running 0 113s 10.244.2.62 node2 <none> <none>
step4、暴漏服务
[root@master ~]# kubectl expose deployment nginx-app --port=80 --type=LoadBalancer
service/nginx-app exposed
[root@master ~]#
step5:查看服务状态(查看对外的端口)
kubectl get services
[root@master ~]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-app LoadBalancer 10.1.180.216 <pending> 80:31972/TCP 2m14s
[root@master ~]# kubectl get pod,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-app-d6ff45774-9679z 1/1 Running 0 54m
pod/nginx-app-d6ff45774-vpd2p 1/1 Running 0 54mNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 219d
service/mysql ClusterIP 10.1.223.6 <none> 3306/TCP 195d
service/nginx-app LoadBalancer 10.1.180.216 <pending> 80:31972/TCP 50m
[root@master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.1.0.1 <none> 443/TCP 219d
mysql ClusterIP 10.1.223.6 <none> 3306/TCP 195d
nginx-app LoadBalancer 10.1.180.216 <pending> 80:31972/TCP 51m
删除services:kubectl delete services nginx-app 有时候删除了deployment但是services还在
step6:浏览器校验(集群ip:端口)
处理方式二:使用yaml
step1:删除上面语句生成的nginx-app
#找到原先安装的pod的name
kubectl get pods
#删除上面语句产生的nnginx-app
kubectl delete pods nginx-app
step2:vi nginx.yaml
将下面的内容粘贴到nginx.yaml
apiVersion: apps/v1 # API 版本号
kind: Deployment # 类型,如:Pod/ReplicationController/Deployment/Service/Ingress
metadata: # Kind 的名称name: nginx-app
spec:selector:matchLabels: # 容器标签的名字,发布 Service 时,selector 需要和这里对应app: nginxreplicas: 2 # 部署的实例数量template:metadata:labels:app: nginxspec:containers: # 配置容器,数组类型,说明可以配置多个容器- name: nginx # 容器名称image: nginx:1.17 # 容器镜像imagePullPolicy: IfNotPresent # 只有镜像不存在时,才会进行镜像拉取ports:- containerPort: 80 # Pod 端口
step3:创建
kubectl apply -f nginx.yaml
① kubectl 发送部署请求到 API Server。
② API Server 通知 Controller Manager 创建一个 deployment 资源。
③ Scheduler 执行调度任务,将两个副本 Pod 分发到 k8s-node1 和 k8s-node2。
④ k8s-node1 和 k8s-node2 上的 kubectl 在各自的节点上创建并运行 Pod。
补充两点:
-
应用的配置和当前状态信息保存在 etcd 中,执行
kubectl get pod
时 API Server 会从 etcd 中读取这些数据。 -
flannel 会为每个 Pod 都分配 IP。因为没有创建 service,目前 kube-proxy 还没参与进来。
step4:查看
查看语句 Pods
kubectl get pods / kubectl get deployment/kubectl get pod -o wide
[root@master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-app-7f4fc68488-2vrkf 1/1 Running 0 21h
nginx-app-7f4fc68488-vs6g6 1/1 Running 0 21h
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-app 2/2 2 2 21h
[root@master ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-app-7f4fc68488-n25m9 1/1 Running 0 14m 10.244.1.3 node1 <none> <none>
nginx-app-7f4fc68488-vln2j 1/1 Running 0 14m 10.244.2.4 node2 <none> <none>
step5:查看更详细的信息kubectl describe deployment
[root@master ~]# kubectl describe deployment
Name: nginx-app
Namespace: default
CreationTimestamp: Mon, 14 Dec 2020 13:21:29 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: app=nginxContainers:nginx:Image: nginx:1.17Port: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>
Conditions:Type Status Reason---- ------ ------Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-app-7f4fc68488 (2/2 replicas created)
Events: <none>
Events
是 Deployment 的日志,记录了 ReplicaSet 的启动过程。(这里没有记录)
通过上面的分析,也验证了 Deployment 通过 ReplicaSet 来管理 Pod 的事实。接着我们将注意力切换到nginx-app-7f4fc68488,执行 kubectl get replicaset
:或者 kubectl get rs
[root@master ~]# kubectl get replicaset
NAME DESIRED CURRENT READY AGE
nginx-app-7f4fc68488 2 2 2 101m
两个副本已经就绪,用 kubectl describe replicaset
查看详细信息:
[root@master ~]# kubectl describe replicaset nginx-app-7f4fc68488
Name: nginx-app-7f4fc68488
Namespace: default
Selector: app=nginx,pod-template-hash=7f4fc68488
Labels: app=nginxpod-template-hash=7f4fc68488
Annotations: deployment.kubernetes.io/desired-replicas: 2deployment.kubernetes.io/max-replicas: 3deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx-app
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:Labels: app=nginxpod-template-hash=7f4fc68488Containers:nginx:Image: nginx:1.17Port: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>
Events: <none>
[root@master ~]#
Controlled By
指明此 ReplicaSet 是由 Deployment nginx-app
创建。Events
记录了两个副本 Pod 的创建。接着我们来看 Pod,执行 kubectl get pod
:
[root@master ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-app-7f4fc68488-n25m9 1/1 Running 0 103m
nginx-app-7f4fc68488-vln2j 1/1 Running 0 103m
两个副本 Pod 都处于 Running
状态,用 kubectl describe pod
查看更详细的信息:
[root@master ~]# kubectl describe pod
Name: nginx-app-7f4fc68488-n25m9
Namespace: default
Priority: 0
Node: node1/10.70.36.251
Start Time: Mon, 14 Dec 2020 13:21:29 +0800
Labels: app=nginxpod-template-hash=7f4fc68488
Annotations: <none>
Status: Running
IP: 10.244.1.3
IPs:IP: 10.244.1.3
Controlled By: ReplicaSet/nginx-app-7f4fc68488
Containers:nginx:Container ID: docker://f131d31db0a45cc206c9ca3e36573e7687917c94caed09b5ab1357b7df44aa7dImage: nginx:1.17Image ID: docker-pullable://nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Mon, 14 Dec 2020 13:22:01 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-6x5lr (ro)
Conditions:Type StatusInitialized True Ready True ContainersReady True PodScheduled True
Volumes:default-token-6x5lr:Type: Secret (a volume populated by a Secret)SecretName: default-token-6x5lrOptional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>Name: nginx-app-7f4fc68488-vln2j
Namespace: default
Priority: 0
Node: node2/10.70.36.252
Start Time: Mon, 14 Dec 2020 13:21:29 +0800
Labels: app=nginxpod-template-hash=7f4fc68488
Annotations: <none>
Status: Running
IP: 10.244.2.4
IPs:IP: 10.244.2.4
Controlled By: ReplicaSet/nginx-app-7f4fc68488
Containers:nginx:Container ID: docker://9cde45864776ac8915490db7d78621a93ca016a69f77a11a3f3e0d48dd9e2f33Image: nginx:1.17Image ID: docker-pullable://nginx@sha256:6fff55753e3b34e36e24e37039ee9eae1fe38a6420d8ae16ef37c92d1eb26699Port: 80/TCPHost Port: 0/TCPState: RunningStarted: Mon, 14 Dec 2020 13:29:09 +0800Ready: TrueRestart Count: 0Environment: <none>Mounts:/var/run/secrets/kubernetes.io/serviceaccount from default-token-6x5lr (ro)
Conditions:Type StatusInitialized True Ready True ContainersReady True PodScheduled True
Volumes:default-token-6x5lr:Type: Secret (a volume populated by a Secret)SecretName: default-token-6x5lrOptional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300snode.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events: <none>
Controlled By
指明此 Pod 是由 ReplicaSet nginx-app-7f4fc68488
创建。Events
记录了 Pod 的启动过程。如果操作失败(比如 image 不存在),也能在这里查看到原因。
总结一下这个过程:
-
用户通过
kubectl
创建 Deployment。 -
Deployment 创建 ReplicaSet。
-
ReplicaSet 创建 Pod
从上图也可以看出,对象的命名方式是:子对象的名字
= 父对象名字
+ 随机字符串或数字
step6:如果要删除这些资源,执行 kubectl delete deployment nginx-deployment
或者 kubectl delete -f nginx.yml
。
step7:暴露服务
kubectl expose deployment nginx-app --port=80 --type=LoadBalancer
step8:查看服务状态(查看对外的端口)
kubectl get services
step9:浏览器校验
这篇关于4、Deployment 运行应用,提示Flag --replicas has been deprecated, has no effect and will be removed in the的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!