$ kubectl cluster-info Kubernetes master is running at https://192.168.99.100:8443 KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
$ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready <none> 48m v1.16.0
创建应用
1 2 3 4 5 6 7
$ kubectl run nginx --image=gcr.azk8s.cn/google_containers/nginx 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 deployments NAME READY UP-TO-DATE AVAILABLE AGE nginx 0/1 1 0 4s
$ kubectl describe pods nginx Name: nginx-694785858b-dkxrj Namespace: default Priority: 0 Node: minikube/10.0.2.15 Start Time: Mon, 07 Oct 2019 11:13:33 +0800 Labels: pod-template-hash=694785858b run=nginx Annotations: <none> Status: Pending IP: IPs: <none> Controlled By: ReplicaSet/nginx-694785858b Containers: nginx: Container ID: Image: gcr.azk8s.cn/google_containers/nginx Image ID: Port: 8080/TCP Host Port: 0/TCP State: Waiting Reason: ContainerCreating Ready: False Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-7dsd7 (ro) Conditions: Type Status Initialized True Ready False ContainersReady False PodScheduled True Volumes: default-token-7dsd7: Type: Secret (a volume populated by a Secret) SecretName: default-token-7dsd7 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 <unknown> default-scheduler Successfully assigned default/nginx-694785858b-dkxrj to minikube Normal Pulling 26s kubelet, minikube Pulling image "gcr.azk8s.cn/google_containers/nginx"
看到正在拉取镜像,经过了4分钟左右,终于起来了:
1 2 3 4 5 6 7 8 9 10 11 12
Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned default/nginx-694785858b-dkxrj to minikube Normal Pulling 3m11s kubelet, minikube Pulling image "gcr.azk8s.cn/google_containers/nginx" Normal Pulled 19s kubelet, minikube Successfully pulled image "gcr.azk8s.cn/google_containers/nginx" Normal Created 19s kubelet, minikube Created container nginx Normal Started 19s kubelet, minikube Started container nginx
$ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE nginx 1/1 1 1 3m9s
查看应用
开启代理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
$ echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; $ kubectl proxy
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}') $ echo Name of the Pod: $POD_NAME nginx-6db489d4b7-lbzgr
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/ <!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>
# 进入容器 $ kubectl exec -it $POD_NAME bash root@nginx-6db489d4b7-lbzgr:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
$ curl $(minikube ip):$NODE_PORT <!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>
打标签
通过标签查询
1 2 3 4 5 6 7
$ kubectl get pods -l run=nginx NAME READY STATUS RESTARTS AGE nginx-6db489d4b7-lbzgr 1/1 Running 0 71m
$ kubectl get services -l run=nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.103.123.100 <none> 80:30119/TCP 38m
设置变量:
1 2 3 4
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME Name of the Pod: nginx-6db489d4b7-lbzgr
打标签
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# 打 $ kubectl label pod $POD_NAME app=v1 pod/nginx-6db489d4b7-lbzgr labeled
$ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE nginx 2/2 2 2 130m
$ kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES nginx-6db489d4b7-kqnj4 1/1 Running 0 46m 172.17.0.5 minikube <none> <none> nginx-6db489d4b7-lbzgr 1/1 Running 0 130m 172.17.0.4 minikube <none> <none>
$ kubectl describe deployments/nginx Name: nginx Namespace: default CreationTimestamp: Mon, 07 Oct 2019 11:25:14 +0800 Labels: run=nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: run=nginx Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 47m deployment-controller Scaled up replica set nginx-6db489d4b7 to 3 Normal ScalingReplicaSet 38s deployment-controller Scaled down replica set nginx-6db489d4b7 to 2
升级
设置新的镜像
1 2 3 4 5
$ kubectl set image deployments/nginx nginx=gcr.azk8s.cn/google_containers/nginx:v1.16.1
$ kubectl get deployments NAME READY UP-TO-DATE AVAILABLE AGE nginx 2/2 1 2 162m
查看更新状态
1 2
$ kubectl rollout status deployments/nginx Waiting for deployment "nginx" rollout to finish: 1 out of 2 new replicas have been updated...
回滚
1 2
$ kubectl rollout undo deployments/nginx deployment.apps/nginx rolled back