k8s官网 基础知识入门教程

官网链接为 https://kubernetes.io/docs/tutorials/kubernetes-basics/

基础操作环境为minikube

常见基础命令

查看基础的一些信息

# 查看版本
(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

PODS/DEPLOYMENTS

创建一个pod(由于网络问题,需要提前将image导入到minikube的机器中)

# 创建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的详细信息

# 查看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

SERVICES

为pod/deployment创建service

# 创建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的使用

# 使用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

动态调整

增加/调整分片

# 查看分片
➜ ~ 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

从上面可以看到服务已经更新,可以使用下面的方式验证一下

# 通过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
上一篇:stm32定时器中断类型分析


下一篇:通过WMI - Win32_Processor - ProcessorId获取到的并不是CPU的序列号,也并不唯一