run 或create deployment 部署一个应用
kubernetes 部署 nginx ,使用 kubectl get deployment 时出现 No resources found in default namespace
结果验证:
kubectl run mcw01_nginx --image=nginx #能get pod,但不能get deployment ,不能get replicaset
kubectl create deployment mcw01dep-nginx --image=nginx #既能get pod ,又能get deployment,又能get replicaset
kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2 #它不能 创建副本了,
kubectl create deployment mcw01dep-nginx --image=nginx #那么这个命令是否可以支持创建多个副本
首先我确保了master和nodes节点的正常运行,还有各个组件处于健康状态
使用 kubectl 命令创建监听 80 端口的 Nginx Pod(Kubernetes 运行容器的最小单元)
kubectl run mcw01_nginx --image=nginx --port=80
运行结果:
[machangwei@mcw7 ~]$ kubectl run nginx-deployment --image=nginx:1.7.9 --replicas=2 Error: unknown flag: --replicas See 'kubectl run --help' for usage. [machangwei@mcw7 ~]$ kubectl run mcw01_nginx --image=nginx --port=80 #不能使用下划线命名,但是可以用短横线 The Pod "mcw01_nginx" is invalid: * metadata.name: Invalid value: "mcw01_nginx": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*') * spec.containers[0].name: Invalid value: "mcw01_nginx": a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character (e.g. 'my-name', or '123-abc', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?') [machangwei@mcw7 ~]$ [machangwei@mcw7 ~]$ kubectl run mcw01-nginx --image=nginx --port=80 pod/mcw01-nginx created
可以看到,这里提示只是成功创建了一个pod 叫mcw01_nginx,在以往的版本中似乎会顺便创建一个deployment,显然这里没有
使用 kubectl get deployment命令查看deployment
[machangwei@mcw7 ~]$ kubectl get deployment No resources found in default namespace.
问题就出现了,的确没有,那我们就需要手动创建
使用 kubectl create deployment mcw01dep-nginx --image=nginx
[machangwei@mcw7 ~]$ kubectl create deployment mcw01dep-nginx --image=nginx deployment.apps/mcw01dep-nginx created
再次使用kubectl get deployment查看,可以看到已经有了一个mcw01dep-nginx 的 deployment
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw01dep-nginx 1/1 1 1 48s
再次使用kubectl get replicaset看副本,有mcw01dep-nginx命名的副本
[machangwei@mcw7 ~]$ kubectl get replicaset NAME DESIRED CURRENT READY AGE mcw01dep-nginx-5dd785954d 1 1 1 6m26s
使用kubectl get pod也能看到mcw01dep-nginx命名的Pod。
[machangwei@mcw7 ~]$ kubectl get pod NAME READY STATUS RESTARTS AGE httpd-app 1/1 Running 0 3h12m mcw01-nginx 1/1 Running 0 3m55s mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 94s mcw1httpd-app 1/1 Running 0 162m nginx-deployment 1/1 Running 0 12m
查看刚刚部署的应用
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw01dep-nginx 1/1 1 1 9m37s [machangwei@mcw7 ~]$ kubectl describe deployment mcw01dep-nginx Name: mcw01dep-nginx Namespace: default CreationTimestamp: Tue, 04 Jan 2022 09:49:28 +0800 Labels: app=mcw01dep-nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: app=mcw01dep-nginx 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=mcw01dep-nginx 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: mcw01dep-nginx-5dd785954d (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set mcw01dep-nginx-5dd785954d to 1
查看刚刚部署的应用的副本
[machangwei@mcw7 ~]$ kubectl get replicaset NAME DESIRED CURRENT READY AGE mcw01dep-nginx-5dd785954d 1 1 1 15m [machangwei@mcw7 ~]$ kubectl describe replicaset mcw01dep-nginx-5dd785954d Name: mcw01dep-nginx-5dd785954d Namespace: default Selector: app=mcw01dep-nginx,pod-template-hash=5dd785954d Labels: app=mcw01dep-nginx pod-template-hash=5dd785954d Annotations: deployment.kubernetes.io/desired-replicas: 1 deployment.kubernetes.io/max-replicas: 2 deployment.kubernetes.io/revision: 1 Controlled By: Deployment/mcw01dep-nginx Replicas: 1 current / 1 desired Pods Status: 1 Running / 0 Waiting / 0 Succeeded / 0 Failed Pod Template: Labels: app=mcw01dep-nginx pod-template-hash=5dd785954d Containers: nginx: Image: nginx Port: <none> Host Port: <none> Environment: <none> Mounts: <none> Volumes: <none> Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal SuccessfulCreate 15m replicaset-controller Created pod: mcw01dep-nginx-5dd785954d-sb8ps
查看刚刚部署的应用的Pod信息
[machangwei@mcw7 ~]$ kubectl describe pod mcw01dep-nginx-5dd785954d-sb8ps Name: mcw01dep-nginx-5dd785954d-sb8ps Namespace: default Priority: 0 Node: mcw9/10.0.0.139 Start Time: Tue, 04 Jan 2022 09:49:27 +0800 Labels: app=mcw01dep-nginx pod-template-hash=5dd785954d Annotations: <none> Status: Running IP: 10.244.0.4 IPs: IP: 10.244.0.4 Controlled By: ReplicaSet/mcw01dep-nginx-5dd785954d Containers: nginx: Container ID: docker://374ff9c1a60e9562f7e05ec4eaa5150e3380b836a26f3b0fe565e6f9dea52cef Image: nginx Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Port: <none> Host Port: <none> State: Running Started: Tue, 04 Jan 2022 09:49:30 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h56zh (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: kube-api-access-h56zh: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: true QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 18m default-scheduler Successfully assigned default/mcw01dep-nginx-5dd785954d-sb8ps to mcw9 Normal Pulling 18m kubelet Pulling image "nginx" Normal Pulled 18m kubelet Successfully pulled image "nginx" in 1.193138975s Normal Created 18m kubelet Created container nginx Normal Started 18m kubelet Started container nginx
指定命名空间的查询Pod详情,应该是一样的
[machangwei@mcw7 ~]$ kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default httpd-app 1/1 Running 0 3h31m default mcw01-nginx 1/1 Running 0 22m default mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 20m default mcw1httpd-app 1/1 Running 0 3h1m default nginx-deployment 1/1 Running 0 31m kube-system coredns-6d8c4cb4d-2296m 1/1 Running 1 (9h ago) 13h kube-system coredns-6d8c4cb4d-lphb2 1/1 Running 1 (9h ago) 13h kube-system etcd-mcw7 1/1 Running 3 13h kube-system kube-apiserver-mcw7 1/1 Running 2 13h kube-system kube-controller-manager-mcw7 1/1 Running 4 (9h ago) 13h kube-system kube-flannel-ds-5pxmj 0/1 Error 102 (5m12s ago) 8h kube-system kube-flannel-ds-8gzfq 1/1 Running 0 9h kube-system kube-flannel-ds-s4qmj 0/1 CrashLoopBackOff 95 (62s ago) 7h46m kube-system kube-proxy-4lmsx 1/1 Running 0 8h kube-system kube-proxy-c5zmm 1/1 Running 0 13h kube-system kube-proxy-npmtv 1/1 Running 0 7h46m kube-system kube-scheduler-mcw7 1/1 Running 3 (9h ago) 13h [machangwei@mcw7 ~]$ kubectl describe pod mcw01dep-nginx-5dd785954d-sb8ps --namespace=default Name: mcw01dep-nginx-5dd785954d-sb8ps Namespace: default Priority: 0 Node: mcw9/10.0.0.139 Start Time: Tue, 04 Jan 2022 09:49:27 +0800 Labels: app=mcw01dep-nginx pod-template-hash=5dd785954d Annotations: <none> Status: Running IP: 10.244.0.4 IPs: IP: 10.244.0.4 Controlled By: ReplicaSet/mcw01dep-nginx-5dd785954d Containers: nginx: Container ID: docker://374ff9c1a60e9562f7e05ec4eaa5150e3380b836a26f3b0fe565e6f9dea52cef Image: nginx Image ID: docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31 Port: <none> Host Port: <none> State: Running Started: Tue, 04 Jan 2022 09:49:30 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-h56zh (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: kube-api-access-h56zh: Type: Projected (a volume that contains injected data from multiple sources) TokenExpirationSeconds: 3607 ConfigMapName: kube-root-ca.crt ConfigMapOptional: <nil> DownwardAPI: true QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s node.kubernetes.io/unreachable:NoExecute op=Exists for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 21m default-scheduler Successfully assigned default/mcw01dep-nginx-5dd785954d-sb8ps to mcw9 Normal Pulling 21m kubelet Pulling image "nginx" Normal Pulled 21m kubelet Successfully pulled image "nginx" in 1.193138975s Normal Created 21m kubelet Created container nginx Normal Started 21m kubelet Started container nginx [machangwei@mcw7 ~]$
对刚刚部署的应用做几种资源的查询
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw01dep-nginx 1/1 1 1 23m [machangwei@mcw7 ~]$ kubectl get replicaset NAME DESIRED CURRENT READY AGE mcw01dep-nginx-5dd785954d 1 1 1 23m [machangwei@mcw7 ~]$ kubectl get pod |grep mcw01dep-nginx mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 23m [machangwei@mcw7 ~]$ kubectl get pod -o wide |grep mcw01dep-nginx mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 23m 10.244.0.4 mcw9 <none> <none> [machangwei@mcw7 ~]$ kubectl get pod --all-namespaces |grep mcw01dep-nginx default mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 24m
创建同名的deployment是不可以的
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw01dep-nginx 1/1 1 1 31m [machangwei@mcw7 ~]$ kubectl create deployment mcw01dep-nginx --image=nginx error: failed to create deployment: deployments.apps "mcw01dep-nginx" already exists
使用负载均衡模式发布服务,让外网访问我的Nginx服务(docker)
kubectl expose deployment mcw01dep-nginx --port=80 --type=LoadBalancer
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw01dep-nginx 1/1 1 1 33m [machangwei@mcw7 ~]$ kubectl expose deployment mcw01dep-nginx --port=80 --type=LoadBalancer service/mcw01dep-nginx exposed
查看服务详情
kubectl describe service mcw01dep-nginx
输出如下:
[machangwei@mcw7 ~]$ kubectl get service #获取服务信息 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 13h mcw01dep-nginx LoadBalancer 10.96.184.54 <pending> 80:30102/TCP 74s [machangwei@mcw7 ~]$ kubectl describe service mcw01dep-nginx Name: mcw01dep-nginx Namespace: default Labels: app=mcw01dep-nginx Annotations: <none> Selector: app=mcw01dep-nginx Type: LoadBalancer IP Family Policy: SingleStack IP Families: IPv4 IP: 10.96.184.54 IPs: 10.96.184.54 Port: <unset> 80/TCP TargetPort: 80/TCP NodePort: <unset> 30102/TCP Endpoints: 10.244.0.4:80 Session Affinity: None External Traffic Policy: Cluster Events: <none>
这里我就可以利用 节点的ip+暴露出来的端口 检测服务是否访问成功
[machangwei@mcw7 ~]$ kubectl get pod -o wide |grep mcw01dep-nginx mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 43m 10.244.0.4 mcw9 <none> <none> [machangwei@mcw7 ~]$ kubectl describe pod mcw01dep-nginx-5dd785954d-sb8ps|grep Node: Node: mcw9/10.0.0.139
由上找到服务所在的Node ip ,找到服务暴露出来的NodePort ,然后在外部就可以使用Node ip :NodePort来访问服务
比如这里的访问:
我在master节点访问node2节点上的服务,这个服务是node2ip,端口,映射到node2的应用docker上的80服务
[machangwei@mcw7 ~]$ curl 10.0.0.139:30102 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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>
笔记本浏览器*问:
进入服务所在的主机节点,查看容器的ip,与之前看的容器ip等信息一致
[root@mcw9 ~]$ docker ps|grep mcw01dep-nginx 374ff9c1a60e nginx "/docker-entrypoint.…" 49 minutes ago Up 49 minutes k8s_nginx_mcw01dep-nginx-5dd785954d-sb8ps_default_13d59a7a-c16f-44f1-b442-de7252ae4bac_0 388265797ee1 registry.aliyuncs.com/google_containers/pause:3.6 "/pause" 49 minutes ago Up 49 minutes k8s_POD_mcw01dep-nginx-5dd785954d-sb8ps_default_13d59a7a-c16f-44f1-b442-de7252ae4bac_0 [root@mcw9 ~]$ docker exec -it 374 hostname -i 10.244.0.4 [root@mcw9 ~]$ docker exec -it 388 hostname -i OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "hostname": executable file not found in $PATH: unknown [root@mcw9 ~]$
在Nginx服务的主机上,是能直接通容器ip的
[root@mcw9 ~]$ docker exec -it 374 hostname -i 10.244.0.4 [root@mcw9 ~]$ docker exec -it 388 hostname -i OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "hostname": executable file not found in $PATH: unknown [root@mcw9 ~]$ [root@mcw9 ~]$ [root@mcw9 ~]$ [root@mcw9 ~]$ [root@mcw9 ~]$ ping 10.244.0.4 PING 10.244.0.4 (10.244.0.4) 56(84) bytes of data. 64 bytes from 10.244.0.4: icmp_seq=1 ttl=64 time=0.256 ms 64 bytes from 10.244.0.4: icmp_seq=2 ttl=64 time=0.059 ms ^C --- 10.244.0.4 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1000ms rtt min/avg/max/mdev = 0.059/0.157/0.256/0.099 ms [root@mcw9 ~]$ curl 10.244.0.4:80 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } 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@mcw9 ~]$
命令行部署多个副本的应用
部署多个Nginx,然后可以用上面那个例子,去做负载均衡了
[machangwei@mcw7 ~]$ kubectl get deployment NAME READY UP-TO-DATE AVAILABLE AGE mcw-depcluster 3/3 3 3 92s mcw01dep-nginx 1/1 1 1 61m [machangwei@mcw7 ~]$ kubectl get replicaset NAME DESIRED CURRENT READY AGE mcw-depcluster-547cfdc745 3 3 3 101s mcw01dep-nginx-5dd785954d 1 1 1 61m [machangwei@mcw7 ~]$ kubectl get pod NAME READY STATUS RESTARTS AGE httpd-app 1/1 Running 0 4h12m mcw-depcluster-547cfdc745-258sz 1/1 Running 0 118s mcw-depcluster-547cfdc745-q2785 1/1 Running 0 118s mcw-depcluster-547cfdc745-wrq74 1/1 Running 0 118s mcw01-nginx 1/1 Running 0 63m mcw01dep-nginx-5dd785954d-sb8ps 1/1 Running 0 61m mcw1httpd-app 1/1 Running 0 3h42m nginx-deployment 1/1 Running 0 71m [machangwei@mcw7 ~]$ kubectl get pod --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE default httpd-app 1/1 Running 0 4h13m default mcw-depcluster-547cfdc745-258sz 1/1 Running 0 2m49s default mcw-depcluster-547cfdc745-q2785 1/1 Running 0 2m49s default mcw-depcluster-547cfdc745-wrq74 1/1 Running 0 2m49s