本文是在官网的教程中进行的,官方提供了在线的命令行工具和安装好的minikube工具及环境,方便新手快速入门,如下是主要包含的内容。
假设已经预先安装好了minikube,执行如下命令初始化一个单节点的集群:
minikube start
试试k8s集群是否初始化成功:
kubectl version
然后可以查看集群的信息和节点信息:
$ kubectl cluster-info
Kubernetes control plane is running at https://172.17.0.24:8443
KubeDNS is running at https://172.17.0.24:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 8m25s v1.20.2
在部署应用之前,我们查看pod的信息:
$ kubectl get pods
No resources found in default namespace.
然后我们使用如下命令部署一个应用并查看应用的信息:
$ kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
deployment.apps/kubernetes-bootcamp created
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 53s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-57978f5f5d-wk8kg 1/1 Running 0 62s
应用虽然部署完成了,但是默认情况下我们只能在集群内部才能访问pod(参考下面3.3节内容),命令行curl访问应用的接口则是属于集群外部访问pod了,所以要实现curl接口需要做如下的设置:
新启动一个该节点的命令行,执行如下命令:
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
表示节点的8001端口能访问到集群中的pod了:
$ curl http://localhost:8001/version
{
"major": "1",
"minor": "20",
"gitVersion": "v1.20.2",
"gitCommit": "faecb196815e248d3ecfb03c680a4507229c2a56",
"gitTreeState": "clean",
"buildDate": "2021-01-13T13:20:00Z",
"goVersion": "go1.15.5",
"compiler": "gc",
"platform": "linux/amd64"
}
主动将应用的端口暴露出去(参考如下第四章节内容):
$ 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 15m
kubernetes-bootcamp NodePort 10.106.152.94 <none> 8080:31281/TCP 12s
$ curl http://localhost:31281/version
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-57978f5f5d-wk8kg | v=1
应用部署完成后,我们需要通过一些手段来监控和运维我们的应用。
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-q8mrb 1/1 Running 0 26s
$ kubectl describe pods
Name: kubernetes-bootcamp-fb5c67579-q8mrb
Namespace: default
Priority: 0
Node: minikube/172.17.0.129
Start Time: Tue, 08 Feb 2022 08:39:44 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.6
IPs:
IP: 172.18.0.6
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://e243e942198e2d1a1d37db5820e210ada432221c1ca915b7859fbc3af5543d72
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: Tue, 08 Feb 2022 08:39:49 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-kqhb5 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-kqhb5:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-kqhb5
Optional: false
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 3m38s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-q8mrb to minikube
Normal Pulled 3m34s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 3m34s kubelet Created container kubernetes-bootcamp
Normal Started 3m33s kubelet Started container kubernetes-bootcamp
使用容器名称查看容器中的运行日志。
$ kubectl logs kubernetes-bootcamp-fb5c67579-q8mrb
Kubernetes Bootcamp App Started At: 2022-02-08T08:39:49.327Z | Running On: kubernetes-bootcamp-fb5c67579-q8mrb
# 进入容器内部并执行bash命令
$ kubectl exec -ti kubernetes-bootcamp-fb5c67579-q8mrb -- bash
# 测试容器内部命令
root@kubernetes-bootcamp-fb5c67579-q8mrb:/# pwd
# 容器内部curl访问pod
root@kubernetes-bootcamp-fb5c67579-q8mrb:/# curl localhost:8080
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-q8mrb | v=1
# 退出容器
root@kubernetes-bootcamp-fb5c67579-q8mrb:/# exit
exit
在第二章中我们介绍了,部署完成pod之后,默认情况下只能在集群内部才能访问到该pod,想要通过外部访问该pod,就一定要使用Service。
我们如上已经创建了service,并将其暴露在外了,此处不再赘述,可以参考第二章节的方案二。
我们可以通过使用label来筛选想要查询的资源信息:
$ kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-rvbxv 1/1 Running 0 2m15s
$ kubectl get services -l app=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.97.252.94 <none> 8080:31926/TCP 76s
当我们不再需要该service的时候,可以通过如下命令删除,删除后,外部将无法再访问运行的pod。
$ curl localhost:31926
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-rvbxv | v=1
$ kubectl delete service -l app=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 5m13s
$ curl localhost:31926
curl: (7) Failed to connect to localhost port 31926: Connection refused
当流量有变化的时候,我们就需要动态地对pod进行扩容:
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 6m10s
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-fb5c67579 1 1 1 6m6s
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 8m32s
$ kubectl get rsNAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-fb5c67579 4 4 1 7m27s
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-82nhh 1/1 Running 0 2m13s 172.18.0.8 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-96t49 1/1 Running 0 9m6s 172.18.0.5 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-jz5vw 1/1 Running 0 2m13s 172.18.0.9 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-vfpn8 1/1 Running 0 2m13s 172.18.0.7 minikube <none> <none>
$ kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Tue, 08 Feb 2022 09:16:30 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=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: app=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-fb5c67579 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 9m45s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
Normal ScalingReplicaSet 2m50s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 4
此时我们已经将pod扩展到4个副本了,此时我们在集群外部访问Service,那么请求会被自动地以负载均衡的方式分发到各个pod上。
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.104.229.171
IPs: 10.104.229.171
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30848/TCP
Endpoints: 172.18.0.5:8080,172.18.0.7:8080,172.18.0.8:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
# 如下请求将会被负载均衡地分发到各个pod上
$ curl localhost:30848
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-96t49 | v=1
$ curl localhost:30848
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-vfpn8 | v=1
$ curl localhost:30848
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-jz5vw | v=1
$ curl localhost:30848
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-82nhh | v=1
当我们需要缩容的时候也很简单:
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 17m
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-82nhh 1/1 Terminating 0 9m38s 172.18.0.8 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-96t49 1/1 Running 0 16m 172.18.0.5 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-jz5vw 1/1 Running 0 9m38s 172.18.0.9 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-vfpn8 1/1 Terminating 0 9m38s 172.18.0.7 minikube <none> <none>
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-96t49 1/1 Running 0 18m 172.18.0.5 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-jz5vw 1/1 Running 0 11m 172.18.0.9 minikube <none> <none>
如果新版本的应用镜像已经发布,如何将旧版本的deployment升级呢?
# 查看当前pod的信息,检查使用的应用镜像版本等信息
$ kubectl describe pods
...
# 告诉deployment中的kubernetes-bootcamp,需要将镜像替换为指定的其它镜像
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
# 开始替换
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-55vrt 1/1 Running 0 4s
kubernetes-bootcamp-7d44784b7c-cftsl 1/1 Running 0 4s
kubernetes-bootcamp-7d44784b7c-dhc42 1/1 Running 0 6s
kubernetes-bootcamp-7d44784b7c-lg2bd 1/1 Running 0 7s
kubernetes-bootcamp-fb5c67579-9bddh 1/1 Terminating 0 5m42s
kubernetes-bootcamp-fb5c67579-sfwwh 1/1 Terminating 0 5m42s
kubernetes-bootcamp-fb5c67579-sx9kt 1/1 Terminating 0 5m42s
kubernetes-bootcamp-fb5c67579-x4n6h 1/1 Terminating 0 5m42s
# 替换完成
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-55vrt 1/1 Running 0 5m
kubernetes-bootcamp-7d44784b7c-cftsl 1/1 Running 0 5m
kubernetes-bootcamp-7d44784b7c-dhc42 1/1 Running 0 5m2s
kubernetes-bootcamp-7d44784b7c-lg2bd 1/1 Running 0 5m3s
# 查看当前pod的信息,检查使用的应用镜像版本等信息
$ kubectl describe pods
...
如果新版本镜像部署有问题,还可以及时回滚:
# 回滚deployments/kubernetes-bootcamp到上一个版本
$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
# 回滚中
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-55vrt 1/1 Terminating 0 10m
kubernetes-bootcamp-7d44784b7c-cftsl 1/1 Terminating 0 10m
kubernetes-bootcamp-7d44784b7c-dhc42 1/1 Terminating 0 10m
kubernetes-bootcamp-7d44784b7c-lg2bd 1/1 Running 0 10m
kubernetes-bootcamp-fb5c67579-6lbbh 0/1 ContainerCreating 0 1s
kubernetes-bootcamp-fb5c67579-84hrk 0/1 ContainerCreating 0 1s
kubernetes-bootcamp-fb5c67579-n8xnp 1/1 Running 0 4s
kubernetes-bootcamp-fb5c67579-qgrs7 1/1 Running 0 4s
# 回滚完成
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-6lbbh 1/1 Running 0 95s
kubernetes-bootcamp-fb5c67579-84hrk 1/1 Running 0 95s
kubernetes-bootcamp-fb5c67579-n8xnp 1/1 Running 0 98s
kubernetes-bootcamp-fb5c67579-qgrs7 1/1 Running 0 98s
# 查看当前pod的信息,检查使用的应用镜像版本等信息
$ kubectl describe pods
...
学习 Kubernetes 基础知识 | Kubernetes