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,然後可以用上面那個例子,去做負載均衡了
kubectl create deployment mcw01dep-nginx --image=nginx --replicas=3
生成應用,副本。pod裡是分為三個單一的。
[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