Mac下kubernetes初体验
背景
前面一篇文章讲了背景,本文只记录一下使用方法。
初体验
安装docker
brew install --cask docker
安装 minikube
brew cask install minikube
$ minikube version
minikube version: v1.3.0
commit: 43969594266d77b555a207b0f3e9b3fa1dc92b1f
$ minikube status
host: Running
kubelet: Running
apiserver: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100
部署一个app
k8s版本
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.2", GitCommit:"cff46ab41ff0bb44d8584413b598ad8360ec1def", GitTreeState:"clean", BuildDate:"2019-01-13T23:15:13Z", GoVersion:"go1.11.4", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.2", GitCommit:"f6278300bebbb750328ac16ee6dd3aa7d3549568", GitTreeState:"clean", BuildDate:"2019-08-05T09:15:22Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}
查看节点
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 16m v1.15.2
部署
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
$ kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
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/kubernetes-bootcamp created
或者
$ kubectl run kubernetes-bootcamp --image=docker.io/jocatalin/kubernetes-bootcamp:v1 --port=8080
确认部署状态
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 68s
启动proxy
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
请求服务
$ curl "localhost:8001/version"
{
"major": "1",
"minor": "15",
"gitVersion": "v1.15.2",
"gitCommit": "f6278300bebbb750328ac16ee6dd3aa7d3549568",
"gitTreeState": "clean",
"buildDate": "2019-08-05T09:15:22Z",
"goVersion": "go1.12.5",
"compiler": "gc",
"platform": "linux/amd64"
}
查看pods
kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-5lbts 1/1 Running 0 14m
查看更详细的pods
kubectl get pods -o wide
请求pods
k8s为每一个pod创建了一个可访问的url,如:
curl "localhost:8001/api/v1/namespaces/default/pods/kubernetes-bootcamp-5b48cfdcbd-5lbts/proxy/"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-5lbts | v=1
与APP交互
get pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-skkhw 1/1 Running 0 6s
describe pods
$ kubectl describe pods
Name: kubernetes-bootcamp-5b48cfdcbd-skkhw
Namespace: default
Priority: 0
Node: minikube/172.17.0.88
Start Time: Fri, 09 Aug 2019 09:23:05 +0000
Labels: pod-template-hash=5b48cfdcbd
run=kubernetes-bootcamp
Annotations: <none>
Status: Running
IP: 172.18.0.3
Controlled By: ReplicaSet/kubernetes-bootcamp-5b48cfdcbd
Containers:
kubernetes-bootcamp:
Container ID: docker://9bb852fc86507d3094bffec47cfbb8035b836d28adb6682c8b8fa5c834e68494
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 09 Aug 2019 09:23:08 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5z4wd (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-5z4wd:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5z4wd
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 42s default-scheduler Successfully assigned default/kubernetes-bootcamp-5b48cfdcbd-skkhw to minikube
Normal Pulled 39s kubelet, minikube Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 39s kubelet, minikube Created container kubernetes-bootcamp
Normal Started 39s kubelet, minikube Started container kubernetes-bootcamp
查看容器日志
$ kubectl logs kubernetes-bootcamp-5b48cfdcbd-5lbts
Kubernetes Bootcamp App Started At: 2019-08-09T08:42:11.232Z | Running On: kubernetes-bootcamp-5b48cfdcbd-5lbts
Running On: kubernetes-bootcamp-5b48cfdcbd-5lbts | Total Requests: 1 | App Uptime: 699.441 seconds | Log Time: 2019-08-09T08:53:50.673Z
Running On: kubernetes-bootcamp-5b48cfdcbd-5lbts | Total Requests: 2 | App Uptime: 818.769 seconds | Log Time: 2019-08-09T08:55:50.001Z
在容器中执行命令
单次执行
$ kubectl exec kubernetes-bootcamp-5b48cfdcbd-5lbts env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=kubernetes-bootcamp-5b48cfdcbd-5lbts
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
NPM_CONFIG_LOGLEVEL=info
NODE_VERSION=6.3.1
HOME=/root
交互式的shell
$ kubectl exec -it kubernetes-bootcamp-5b48cfdcbd-5lbts bash
通过 Service ip 来暴露 Pods服务
查看pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-9rwwr 1/1 Running 0 5s
查看services
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 19s
通过service暴露服务
将容器的8080端口暴露出去
$ 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 116s
kubernetes-bootcamp NodePort 10.97.58.53 <none> 8080:30797/TCP 4s
查看 minikube的ip地址
$ minikube ip
172.17.0.89
直接访问对应的资源
$ curl "172.17.0.89:30797"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-9rwwr | v=1
通过 Service labels 来暴露 Pods服务
查看label
deployment会自动为Pod创建一个label,可以通过 describe deployment
命令来查看。
$ kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Fri, 09 Aug 2019 11:34:58 +0000
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: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
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-5b48cfdcbd (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 12m deployment-controller Scaled up replica set kubernetes-bootcamp-5b48cfdcbd to 1
可见:Labels: run=kubernetes-bootcamp
根据label查看nodes
$ kubectl get pods -l run=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-6cmnn 1/1 Running 0 13m
根据label查看services
$ kubectl get services -l run=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.97.218.44 <none> 8080:31523/TCP 6m57s
给pod上的app做label
给name为 kubernetes-bootcamp-5b48cfdcbd-6cmnn
的pod做label,label名称为v1
$ kubectl label pod kubernetes-bootcamp-5b48cfdcbd-6cmnn app=v1
pod/kubernetes-bootcamp-5b48cfdcbd-6cmnn labeled
确认一下label打上了
$ kubectl describe pods kubernetes-bootcamp-5b48cfdcbd-6cmnn
Name: kubernetes-bootcamp-5b48cfdcbd-6cmnn
Namespace: default
Priority: 0
Node: minikube/172.17.0.36
Start Time: Fri, 09 Aug 2019 11:35:06 +0000
Labels: app=v1
pod-template-hash=5b48cfdcbd
run=kubernetes-bootcamp
Annotations: <none>
Status: Running
IP: 172.18.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-5b48cfdcbd
Containers:
kubernetes-bootcamp:
Container ID: docker://7aad4132a5b16346903e902491136c6107801cdec6a1b647b2c3dc76f7f70fe9
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Fri, 09 Aug 2019 11:35:08 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-6l7s5 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-6l7s5:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-6l7s5
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 19m default-scheduler Successfully assigned default/kubernetes-bootcamp-5b48cfdcbd-6cmnn to minikube
Normal Pulled 19m kubelet, minikube Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 19m kubelet, minikube Created container kubernetes-bootcamp
Normal Started 19m kubelet, minikube Started container kubernetes-bootcamp
根据label查看资源
$ kubectl get pods -l app=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-6cmnn 1/1 Running 0 20m
删除 Service
$ kubectl delete service -l run=kubernetes-bootcamp
service "kubernetes-bootcamp" deleted
确认删除成功
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 23m
再次确认删除成功
$ curl 172.17.0.36:31523
curl: (7) Failed to connect to 172.17.0.36 port 31523: Connection refused
服务的扩容
查看现有deployment
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 4m18s
目前1/1表示当前为1台,预期也为1台。
扩容
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.extensions/kubernetes-bootcamp scaled
确认扩容成功
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 5m54s
查看新的Pods
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-5b48cfdcbd-2h5kn 1/1 Running 0 3m27s 172.18.0.7 minikube <none> <none>
kubernetes-bootcamp-5b48cfdcbd-6sjtp 1/1 Running 0 3m27s 172.18.0.6 minikube <none> <none>
kubernetes-bootcamp-5b48cfdcbd-mz2hx 1/1 Running 0 3m27s 172.18.0.8 minikube <none> <none>
kubernetes-bootcamp-5b48cfdcbd-z98rx 1/1 Running 0 8m24s 172.18.0.4 minikube <none> <none>
查看 deployment 事件日志
从deployment事件日志中,可以看到刚才的操作过程。
$ kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Fri, 09 Aug 2019 12:05:53 +0000
Labels: run=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=kubernetes-bootcamp
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
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-5b48cfdcbd (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 10m deployment-controller Scaled up replica set kubernetes-bootcamp-5b48cfdcbd to 1
Normal ScalingReplicaSet 5m41s deployment-controller Scaled up replica set kubernetes-bootcamp-5b48cfdcbd to 4
从最后一行中,可见扩容至了4个节点。
负载均衡的配置
查看服务的IP和Port
$ 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.81.186
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30674/TCP
Endpoints: 172.18.0.4:8080,172.18.0.6:8080,172.18.0.7:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
不停的调用curl,发现运行的节点不同。
$ curl "172.17.0.15:30674"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-mz2hx | v=1
$ curl "172.17.0.15:30674"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-mz2hx | v=1
$ curl "172.17.0.15:30674"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-6sjtp | v=1
$ curl "172.17.0.15:30674"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-2h5kn | v=1
$ curl "172.17.0.15:30674"
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5b48cfdcbd-6sjtp | v=1
下线缩容机器
缩容到两台机器
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.extensions/kubernetes-bootcamp scaled
查看新的deployments
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 17m
查看pods情况
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IPNODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-5b48cfdcbd-6sjtp 1/1 Running 0 12m 172.18.0.6minikube <none> <none>
kubernetes-bootcamp-5b48cfdcbd-z98rx 1/1 Running 0 17m 172.18.0.4minikube <none> <none>
升级App
查看当前deployment
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 93s
查看当前pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-2hzs6 1/1 Running 0 2m5s
kubernetes-bootcamp-5b48cfdcbd-9x72m 1/1 Running 0 2m5s
kubernetes-bootcamp-5b48cfdcbd-t8whf 1/1 Running 0 2m5s
kubernetes-bootcamp-5b48cfdcbd-whdb6 1/1 Running 0 2m5s
查看当前pod镜像版本
$ kubectl describe pods | grep Image
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
更新镜像版本
告知 deployments去更新镜像
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.extensions/kubernetes-bootcamp image updated
查看当前的pods状态
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-5b48cfdcbd-2hzs6 1/1 Terminating 0 6m26s
kubernetes-bootcamp-5b48cfdcbd-9x72m 1/1 Terminating 0 6m26s
kubernetes-bootcamp-5b48cfdcbd-t8whf 1/1 Terminating 0 6m26s
kubernetes-bootcamp-5b48cfdcbd-whdb6 1/1 Terminating 0 6m26s
kubernetes-bootcamp-cfc74666-2sr4g 1/1 Running 0 20s
kubernetes-bootcamp-cfc74666-6z79p 1/1 Running 0 20s
kubernetes-bootcamp-cfc74666-95zk2 1/1 Running 0 18s
kubernetes-bootcamp-cfc74666-h26sn 1/1 Running 0 18s
再查看一次pods状态
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-cfc74666-2sr4g 1/1 Running 0 54s
kubernetes-bootcamp-cfc74666-6z79p 1/1 Running 0 54skubernetes-bootcamp-cfc74666-95zk2 1/1 Running 0 52s
kubernetes-bootcamp-cfc74666-h26sn 1/1 Running 0 52s
查看pods的镜像版本
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-cfc74666-2sr4g 1/1 Running 0 54s
kubernetes-bootcamp-cfc74666-6z79p 1/1 Running 0 54skubernetes-bootcamp-cfc74666-95zk2 1/1 Running 0 52s
kubernetes-bootcamp-cfc74666-h26sn 1/1 Running 0 52s
yaml管理
导出为yaml文件
导出deployment
kubectl get -o yaml deployment kubernetes-bootcamp
导出 services
kubectl get -o yaml service atom-monitor-server
创建
kubectl create -f kubernetes-bootcamp.yaml
更新
kubectl replace -f kubernetes-bootcamp.yaml