这是第三篇,kubernetes编排应用。
kubernetes
Kubernetes是一个开源的,用于管理云平台中多个主机上的容器化的应用,Kubernetes的目标是让部署容器化的应用简单并且高效(powerful),Kubernetes提供了应用部署,规划,更新,维护的一种机制。
Kubernetes在希腊语中意思是船长或领航员,这也恰好与它在容器集群管理中的作用吻合,即作为装载了集装箱(Container)的众多货船的指挥者,负担着全局调度和运行监控的职责。因为Kubernetes在k和s之间有8个字母,所以又简称k8s
快速体验k8s,可以使用Docker for mac中集成的k8s。
启动k8s后,等待其初始化完成,然后 docker ps
可以看到k8s启动了一系列的容器:
kubectl version
查看集群版本:
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:50:19Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:41:49Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"linux/amd64"}
kubectl get nodes
查看k8s集群节点:
NAME STATUS ROLES AGE VERSION
docker-desktop Ready master 5m53s v1.19.3
kubectl get service
查看k8s默认启动的服务:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6m32s
部署应用及测试
编写应用部署文件
- flaskapp文件
k8s/laravelapp.yaml
apiVersion: v1
kind: Service
metadata:
name: docker2istio-app
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: docker2istio-app
# type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: docker2istio-app
spec:
selector:
matchLabels:
app: docker2istio-app
replicas: 1
template:
metadata:
labels:
app: docker2istio-app
spec:
containers:
- image: laravelapp:0.0.2
name: docker2istio-app
ports:
- containerPort: 8080
了解这个部署文件,需要先大概了解一下k8s的运作方式。k8s通过api server提供restful接口,用于集群交互。每一个部署对象,都有apiVersion,kind, metadata, spec这几个关键字。
- 定义了
Service
和Deployment
2个类型的对象。Service
表示k8s
对外提供的服务,Deployment
表示某个service
的部署方式。 Service
对象的ports
描述了服务端口,这个是集群内部网络的端口。Service
对象的selector
描述了服务如何选择对于的部署,采用标签name: laravelapp
,这是一种解耦合的依赖关系。Deployment
的replicas
描述了容器的副本个数,下文会演示如何扩充。Deployment
的containers
描述了镜像名称,服务端口等。
- redis服务文件
k8s/redis.yaml
apiVersion: v1
kind: Service
metadata:
name: docker2istio-redis
spec:
ports:
- name: tcp
port: 6379
protocol: TCP
targetPort: 6379
selector:
app: docker2istio-redis
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: docker2istio-redis
spec:
selector:
matchLabels:
app: docker2istio-redis
replicas: 1
template:
metadata:
labels:
app: docker2istio-redis
spec:
containers:
- image: redis:4-alpine3.8
name: docker2istio-redis
ports:
- containerPort: 6379
redis的部署文件和flaskapp的部署文件类似。
- nginx服务文件
k8s/nginx.yaml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
default.conf: |
upstream laravelapp {
server docker2istio-app:8080;
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
proxy_pass http://laravelapp;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
---
apiVersion: v1
kind: Service
metadata:
name: docker2istio-nginx
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: docker2istio-nginx
type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: docker2istio-nginx
spec:
selector:
matchLabels:
app: docker2istio-nginx
replicas: 1
template:
metadata:
labels:
app: docker2istio-nginx
spec:
containers:
- image: nginx:1.15.8-alpine
name: docker2istio-nginx
ports:
- containerPort: 80
volumeMounts:
- name: nginx-config-volume
mountPath: /etc/nginx/conf.d/default.conf
subPath: default.conf
volumes:
- name: nginx-config-volume
configMap:
name: nginx-config
nginx的部署文件,变化在:
- 多出了ConfigMap对象,这个对象主要定义了nginx.conf文件,其内容和nginx\default.conf一致。
- nginx的container中mount了一个configmap对象作为nginx的配置文件。
部署应用到集群
使用kubectl apply -f k8s
命令将编写yaml文件提交到k8s集群,集群会自动根据yaml文件的声明,进行部署。
service "flaskapp" created
deployment.extensions "flaskapp" created
configmap "nginx-config" created
service "nginx" created
deployment.extensions "nginx" created
service "redis" created
deployment.extensions "redis" created
这里的
kubectl apply -f k8s
表示将k8s目录下的文件都提交给k8s集群。当然,也可以逐个文件提交kubectl apply -f k8s/redis.yaml
。
访问应用
先kubectl get service
检查一下k8s内的服务:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
docker2istio-app ClusterIP 10.98.64.15 <none> 8080/TCP 26h
docker2istio-nginx NodePort 10.108.153.233 <none> 80:31686/TCP 26h
docker2istio-redis ClusterIP 10.103.23.64 <none> 6379/TCP 26h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5d
注意nginx服务部分的PORTS
为80:31686/TCP
,这表示将容器的80端口暴露到本机网络的31686
端口,和我们之前的docker启动时候的-p 80:80
参数类似。
服务是由Pod提供的,继续检查一下pods的状况kubectl get pods
:
NAME READY STATUS RESTARTS AGE
docker2istio-app-5cc58bcc57-4c7dr 1/1 Running 3 27h
docker2istio-nginx-7d49b55db5-9n5l9 1/1 Running 9 27h
docker2istio-redis-756fcf5456-768d9 1/1 Running 3 27h
当然,也可以直接查看docker的容器docker ps
:
➜ docker2istio docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c56653667130 b411e34b4606 "nginx -g 'daemon of…" 2 hours ago Up 2 hours k8s_docker2istio-nginx_docker2istio-nginx-7d49b55db5-9n5l9_default_5ac81d4f-9091-4c77-9b2c-598734f74e1c_9
fcae490ce46b e704287ce753 "/storage-provisione…" 2 hours ago Up 2 hours k8s_storage-provisioner_storage-provisioner_kube-system_0594dc50-2b33-4712-92c3-1e682a8db384_13
38eb9b9af0dc ae70b17240ec "docker-entrypoint.s…" 2 hours ago Up 2 hours k8s_docker2istio-redis_docker2istio-redis-756fcf5456-768d9_default_5fd060cd-7b61-4e3a-bb12-5775106a7ed6_3
b177bda23afd 28f9cef537c0 "docker-php-entrypoi…" 2 hours ago Up 2 hours k8s_docker2istio-app_docker2istio-app-5cc58bcc57-4c7dr_default_0be267c0-fbe8-47b8-8c4a-c67702c18a47_3
fc1ccb6fe8c9 bfe3a36ebd25 "/coredns -conf /etc…" 2 hours ago Up 2 hours k8s_coredns_coredns-f9fd979d6-p9tf6_kube-system_a79f0b4a-5c40-4953-8e02-6a3648cc0244_10
2adc76dea90f 79da37e5a3aa "/kube-vpnkit-forwar…" 2 hours ago Up 2 hours k8s_vpnkit-controller_vpnkit-controller_kube-system_85c3fdde-ba87-4c84-9f9c-489f427c8ea6_9
800ddf7b28d7 bfe3a36ebd25 "/coredns -conf /etc…" 2 hours ago Up 2 hours k8s_coredns_coredns-f9fd979d6-x29ff_kube-system_f3d2620c-cad1-4e0b-a5ec-1762fba7215c_10
c2a5e526dba9 k8s.gcr.io/pause:3.2 "/pause" 2 hours ago Up 2 hours k8s_POD_docker2istio-nginx-7d49b55db5-9n5l9_default_5ac81d4f-9091-4c77-9b2c-598734f74e1c_3
fed66fbc5da0 cdef7632a242 "/usr/local/bin/kube…" 2 hours ago Up 2 hours k8s_kube-proxy_kube-proxy-vwg94_kube-system_3574d835-a14f-47e4-9713-795bab685344_9
.....
最后使用 curl http://127.0.0.1:31686
访问服务
Hello World by 10.1.0.130 from 127.0.0.1 ! 该页面已被访问 1 次。
扩容
k8s集群下,扩容非常简单
kubectl edit deployment/docker2istio-app
修改其中的** replicas: 3 **。
也可以修改k8s\laravelapp.yaml
中的值,然后kubectl apply -f k8s\laravelapp.yaml
另外,如果镜像有更新,也是采用修改flaskapp.yaml文件然后apply的方式。
kubectl get pods -o wide
检查扩容结果, 这里使用了 -o wide
,可以显示更多信息
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
docker2istio-app-5cc58bcc57-4c7dr 1/1 Running 3 27h 10.1.0.127 docker-desktop <none> <none>
docker2istio-app-5cc58bcc57-fzq5z 1/1 Running 0 54s 10.1.0.131 docker-desktop <none> <none>
docker2istio-app-5cc58bcc57-jd7rn 1/1 Running 0 54s 10.1.0.132 docker-desktop <none> <none>
docker2istio-nginx-7d49b55db5-9n5l9 1/1 Running 9 27h 10.1.0.130 docker-desktop <none> <none>
docker2istio-redis-756fcf5456-768d9 1/1 Running 3 27h 10.1.0.128 docker-desktop <none> <none>
多次访问服务:
➜ docker2istio curl http://127.0.0.1:31686
Hello World by 10.1.0.157 from 10.1.0.155 ! 该页面已被访问 4 次。
➜ docker2istio curl http://127.0.0.1:31686
Hello World by 10.1.0.157 from 10.1.0.153 ! 该页面已被访问 5 次。
—End—
迭代
- 2020年11月12日 16:30 初稿