官網連接爲 https://kubernetes.io/docs/tutorials/kubernetes-basics/node
基礎操做環境爲minikubelinux
查看基礎的一些信息git
# 查看版本 (oracle) ➜ cdss_db git:(dameng) kubectl version Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:55:54Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"darwin/amd64"} Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.0", GitCommit:"fc32d2f3698e36b93322a3465f63a14e9f0eaead", GitTreeState:"clean", BuildDate:"2018-03-26T16:44:10Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"} # 查看節點 (oracle) ➜ cdss_db git:(dameng) kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 23h v1.10.0
建立一個pod(因爲網絡問題,須要提早將image導入到minikube的機器中)docker
# 建立pod (oracle) ➜ cdss_db git:(dameng) kubectl run kubernetes-bootcamp --image=gcr.io/hello-minikube-zero-install/hello-node:latest --port=8080 --image-pull-policy=Never deployment.apps "kubernetes-bootcamp" created # 查看pod (oracle) ➜ cdss_db git:(dameng) kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 5s # 查看deployment (oracle) ➜ cdss_db git:(dameng) kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 1 1 1 1 19m # 將服務暴露到外網(相對於k8s的內部網)環境 (oracle) ➜ cdss_db git:(dameng) kubectl proxy Starting to serve on 127.0.0.1:8001 # 測試----查看相關的接口 ➜ minikube curl 127.0.0.1:8001/version { "major": "1", "minor": "10", "gitVersion": "v1.10.0", "gitCommit": "fc32d2f3698e36b93322a3465f63a14e9f0eaead", "gitTreeState": "clean", "buildDate": "2018-03-26T16:44:10Z", "goVersion": "go1.9.3", "compiler": "gc", "platform": "linux/amd64" }% # 測試----測試對應接口 ➜ minikube curl 127.0.0.1:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-85459f8c6c-9jgfl/proxy/ Hello World!% # 停止proxy ctrl+c 便可中止
查看pod的詳細信息shell
# 查看pod信息 ➜ ~ kubectl describe pods Name: kubernetes-bootcamp-85459f8c6c-9jgfl Namespace: default Node: minikube/10.0.2.15 Start Time: Tue, 19 Mar 2019 14:04:18 +0800 Labels: pod-template-hash=4101594727 run=kubernetes-bootcamp Annotations: <none> Status: Running IP: 172.17.0.5 Controlled By: ReplicaSet/kubernetes-bootcamp-85459f8c6c Containers: kubernetes-bootcamp: Container ID: docker://9ad04cae7f054805acfe9af452210bca960c44b673c2e837e9c39cf70f40a717 Image: gcr.io/hello-minikube-zero-install/hello-node:latest Image ID: docker://sha256:40da0f31e97619946d2a9ca662cb9ccef3f9d45508cd3de58a93542ac2108121 Port: 8080/TCP Host Port: 0/TCP State: Running Started: Tue, 19 Mar 2019 14:04:20 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-c9lkr (ro) Conditions: Type Status Initialized True Ready True PodScheduled True Volumes: default-token-c9lkr: Type: Secret (a volume populated by a Secret) SecretName: default-token-c9lkr 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 26m default-scheduler Successfully assigned kubernetes-bootcamp-85459f8c6c-9jgfl to minikube Normal SuccessfulMountVolume 26m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-c9lkr" Normal Pulled 26m kubelet, minikube Container image "gcr.io/hello-minikube-zero-install/hello-node:latest" already present on machine Normal Created 26m kubelet, minikube Created container Normal Started 26m kubelet, minikube Started container # 查看pod日誌 kubectl logs kubernetes-bootcamp-85459f8c6c-9jgfl # 查看pod環境變量 ➜ ~ kubectl exec kubernetes-bootcamp-85459f8c6c-9jgfl env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=kubernetes-bootcamp-85459f8c6c-9jgfl KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 KUBERNETES_SERVICE_HOST=10.96.0.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 NODE_VERSION=6.14.2 YARN_VERSION=1.6.0 HOME=/root # 進入pod的shell環境 ➜ ~ kubectl exec -it kubernetes-bootcamp-85459f8c6c-9jgfl bash root@kubernetes-bootcamp-85459f8c6c-9jgfl:/# ls bin dev home lib64 mnt proc run server.js sys usr boot etc lib media opt root sbin srv tmp var
爲pod/deployment建立serviceapi
# 建立service ➜ minikube kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080 service "kubernetes-bootcamp" exposed # 查看deployment ➜ minikube kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 1 1 1 1 41m # 查看service ➜ minikube kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h kubernetes-bootcamp NodePort 10.111.228.62 <none> 8080:32383/TCP 1m # 查看service相關信息 ➜ minikube kubectl describe services/kubernetes-bootcamp Name: kubernetes-bootcamp Namespace: default Labels: run=kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.111.228.62 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 32383/TCP Endpoints: 172.17.0.5:8080 Session Affinity: None External Traffic Policy: Cluster Events: <none> # 觀察到NodePort,爲Node相關服務暴露的接口,配合node的ip,能夠直接訪問 ➜ minikube curl 192.168.99.101:32383 Hello World!%
labels的使用bash
# 使用describe能夠查看到對應的labels ➜ minikube kubectl describe deployment Name: kubernetes-bootcamp Namespace: default CreationTimestamp: Tue, 19 Mar 2019 14:04:17 +0800 Labels: run=kubernetes-bootcamp Annotations: deployment.kubernetes.io/revision=1 Selector: run=kubernetes-bootcamp Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge Pod Template: Labels: run=kubernetes-bootcamp Containers: kubernetes-bootcamp: Image: gcr.io/hello-minikube-zero-install/hello-node:latest Port: 8080/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: kubernetes-bootcamp-85459f8c6c (1/1 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 44m deployment-controller Scaled up replica set kubernetes-bootcamp-85459f8c6c to 1 # 上面labels爲run=kubernetes-bootcamp # 查看labels對應的pod/service ➜ minikube kubectl get pods -l run=kubernetes-bootcamp NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 47m ➜ minikube kubectl get services -l run=kubernetes-bootcamp NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-bootcamp NodePort 10.111.228.62 <none> 8080:32383/TCP 7m # 爲pod打新label,使用方法相似於docker的tag命令 ➜ minikube kubectl label pod kubernetes-bootcamp-85459f8c6c-9jgfl app=v1 pod "kubernetes-bootcamp-85459f8c6c-9jgfl" labeled ➜ minikube kubectl describe pods kubernetes-bootcamp-85459f8c6c-9jgfl Name: kubernetes-bootcamp-85459f8c6c-9jgfl Namespace: default Node: minikube/10.0.2.15 Start Time: Tue, 19 Mar 2019 14:04:18 +0800 Labels: app=v1 pod-template-hash=4101594727 run=kubernetes-bootcamp # 查看label對應的pods ➜ minikube kubectl get pods -l app=v1 NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 50m
刪除service網絡
# 刪除service,可使用label來刪除 ➜ ~ kubectl delete service -l run=kubernetes-bootcamp service "kubernetes-bootcamp" deleted # 查看service,能夠看到被刪除 ➜ ~ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23h # 服務已經不可用,但pod依然存在 ➜ ~ curl 192.168.99.101:32383 curl: (7) Failed to connect to 192.168.99.101 port 32383: Connection refused ➜ ~ kubectl exec -it kubernetes-bootcamp-85459f8c6c-9jgfl bash root@kubernetes-bootcamp-85459f8c6c-9jgfl:/# ➜ ~ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 54m
增長/調整分片oracle
# 查看分片 ➜ ~ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 1 1 1 1 1h # 增長分片 ➜ ~ kubectl scale deployments/kubernetes-bootcamp --replicas=3 deployment.extensions "kubernetes-bootcamp" scaled # 查看效果 ➜ ~ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 3 3 3 3 1h # 查看各個pod的信息 ➜ ~ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 1m 172.17.0.6 minikube kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h 172.17.0.5 minikube kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Running 0 1m 172.17.0.7 minikube # 使用describe命令能夠查看其變更(包含event) ➜ ~ kubectl describe deployments/kubernetes-bootcamp Name: kubernetes-bootcamp Namespace: default CreationTimestamp: Tue, 19 Mar 2019 14:04:17 +0800 Labels: run=kubernetes-bootcamp Annotations: deployment.kubernetes.io/revision=1 Selector: run=kubernetes-bootcamp Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 1 max unavailable, 1 max surge Pod Template: Labels: run=kubernetes-bootcamp Containers: kubernetes-bootcamp: Image: gcr.io/hello-minikube-zero-install/hello-node:latest Port: 8080/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Progressing True NewReplicaSetAvailable Available True MinimumReplicasAvailable OldReplicaSets: <none> NewReplicaSet: kubernetes-bootcamp-85459f8c6c (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 2m deployment-controller Scaled up replica set kubernetes-bootcamp-85459f8c6c to 3 # 暴露services,隨後查看詳細信息 ➜ ~ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080 service "kubernetes-bootcamp" exposed ➜ ~ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d kubernetes-bootcamp NodePort 10.97.170.139 <none> 8080:30424/TCP 6s ➜ ~ kubectl describe services/kubernetes-bootcamp Name: kubernetes-bootcamp Namespace: default Labels: run=kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.97.170.139 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 30424/TCP Endpoints: 172.17.0.5:8080,172.17.0.6:8080,172.17.0.7:8080 Session Affinity: None External Traffic Policy: Cluster Events: <none> # 驗證服務 ➜ ~ curl 192.168.99.101:30424 Hello World!% # 能夠從新調整分片 ➜ ~ kubectl scale deployments/kubernetes-bootcamp --replicas=2 deployment.extensions "kubernetes-bootcamp" scaled ➜ ~ kubectl get deployments NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 2 2 2 2 1h # 能夠查看到變化 ➜ ~ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 7m 172.17.0.6 minikube kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h 172.17.0.5 minikube kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Terminating 0 7m 172.17.0.7 minikube ➜ ~ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Running 0 7m kubernetes-bootcamp-85459f8c6c-9jgfl 1/1 Running 0 1h kubernetes-bootcamp-85459f8c6c-rlzbp 1/1 Terminating 0 7m
# 更新爲新的鏡像(首先須要導入新的鏡像,新鏡像返回的內容從hello world變動爲hello) ➜ minikube kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=node_hello:2 deployment.apps "kubernetes-bootcamp" image updated # 查看pods,能夠看到過程 ➜ minikube kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-85459f8c6c-4x8fd 1/1 Terminating 1 1h kubernetes-bootcamp-b4d449bd-vlnpd 1/1 Running 0 17m # services沒有發生變化 ➜ minikube kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d kubernetes-bootcamp NodePort 10.97.170.139 <none> 8080:30424/TCP 1h # describe能夠看到鏡像已經發生變化 ➜ minikube kubectl describe pod kubernetes-bootcamp-b4d449bd-vlnpd Name: kubernetes-bootcamp-b4d449bd-vlnpd Namespace: default Node: minikube/10.0.2.15 Start Time: Tue, 19 Mar 2019 16:07:19 +0800 Labels: pod-template-hash=60800568 run=kubernetes-bootcamp Annotations: <none> Status: Running IP: 172.17.0.5 Controlled By: ReplicaSet/kubernetes-bootcamp-b4d449bd Containers: kubernetes-bootcamp: Container ID: docker://fb59d3c98a1ca705640520954198d5bb336167a8fe1e40603e9281e714a997f4 Image: node_hello:2 Image ID: docker://sha256:74a687422f4cb90ac252a35b6e0bef78d5f4a546fa64b63afc672b763ce5e6b2 Port: 8080/TCP Host Port: 0/TCP State: Running Started: Tue, 19 Mar 2019 16:07:20 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-c9lkr (ro) Conditions: Type Status Initialized True Ready True PodScheduled True Volumes: default-token-c9lkr: Type: Secret (a volume populated by a Secret) SecretName: default-token-c9lkr 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 17m default-scheduler Successfully assigned kubernetes-bootcamp-b4d449bd-vlnpd to minikube Normal SuccessfulMountVolume 17m kubelet, minikube MountVolume.SetUp succeeded for volume "default-token-c9lkr" Normal Pulled 17m kubelet, minikube Container image "node_hello:2" already present on machine Normal Created 17m kubelet, minikube Created container Normal Started 17m kubelet, minikube Started container
從上面能夠看到服務已經更新,可使用下面的方式驗證一下app
# 經過describe查看service的nodeport ➜ ~ kubectl describe service kubernetes-bootcamp Name: kubernetes-bootcamp Namespace: default Labels: run=kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.97.170.139 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 30424/TCP Endpoints: 172.17.0.5:8080 Session Affinity: None External Traffic Policy: Cluster Events: <none> # 返回內容已經變動爲hello(以前爲hello world) ➜ minikube curl 192.168.99.101:30424 Hello% # 也能夠經過下面命令來驗證更新結果 ➜ ~ kubectl rollout status deployments/kubernetes-bootcamp deployment "kubernetes-bootcamp" successfully rolled out