- 一共有两个Pod:ELK和web应用;
- ELK的Pod会暴露两个服务,一个暴露logstash的5044端口,给filebeat用,另一个暴露kibana的5601端口,给搜索日志的用户访问的时候用;
- web应用暴露一个服务,给用户通过浏览器访问;
实战步骤简介
- 部署ELK的pod和服务;
- 部署web应用的pod和服务;
- web应用的pod从一个扩展为三个;
- 体验ELK;
- 部署ELK
一、部署ELK
1. ssh登录到可以执行kubectl命令的机器上去;
2. 创建elk的部署脚本elkhost.yaml,内容如下:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: elkhost spec: replicas: 1 template: metadata: labels: name: elkhost spec: containers: - name: elkhost image: sebp/elk:622 tty: true ports: [{ "containerPort": 5601 },{ "containerPort": 5044 }]
如上所示,暴露了两个端口:kibana的5601和logstash的5044;
3. 在elkhost.yaml所在目录执行命令kubectl create -f elkhost.yaml,即可创建elk对应的pod,如下:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl get pods NAME READY STATUS RESTARTS AGE elkhost-54c9bbd8d5-ffq68 1/1 Running 0 3m
4. 将kibana的5601端口以NodePort的方式对外暴露,这样外部就可以通过节点IP地址来访问kibana服务了,创建部署脚本elkkibana-svc.yaml,kibana的服务通过node节点的30001端口对外暴露,内容如下:
apiVersion: v1 kind: Service metadata: name: elkkibana spec: type: NodePort ports: - port: 5601 nodePort: 30001 selector: name: elkhost
5. 将logstash的5044端口以ClusterIP的方式对外暴露,这样其他pod的filebeat就可以通过服务名加5044端口来访问logstash服务了,创建部署脚本elkhost-svc.yaml,logstash的服务通过5044端口对K8S内部的pod暴露,内容如下:
apiVersion: v1 kind: Service metadata: name: elkhost spec: type: ClusterIP ports: - port: 5044 targetPort: 5044 selector: name: elkhost
6. 在elkhost-svc.yaml所在目录执行命令kubectl create -f elkhost-svc.yaml && kubectl create -f elkkibana-svc.yaml,即可创建elkhost和elkkibana这两个服务,如下:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl create -f elkhost-svc.yaml && kubectl create -f elkkibana-svc.yaml service "elkhost" created service "elkkibana" created root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE elkhost ClusterIP 10.43.103.244 <none> 5044/TCP 9s elkkibana NodePort 10.43.219.137 <none> 5601:30001/TCP 9s kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 16d
7. 执行命令查看elkhost的pod部署在K8S的哪个node节点上:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl describe pod elk Name: elkhost-54c9bbd8d5-ffq68 Namespace: default Node: willzhao-vostro-3267/192.168.31.89 Start Time: Mon, 30 Apr 2018 16:22:04 +0800 Labels: name=elkhost pod-template-hash=1075668481 Annotations: kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"elkhost-54c9bbd8d5","uid":"9096cde8-4c4f-11e8-a776-024f8a041a1a"... Status: Running ...
如上所示,Node: willzhao-vostro-3267/192.168.31.89显示了这个pod部署的节点IP是192.168.31.89;
8. 打开浏览器,输入192.168.31.89:30001,即可访问到Kibana服务,如下图:
二、部署Web应用
1. 创建elkwebdemo的部署脚本elkwebdemo.yaml,内容如下:
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: elkwebdemo spec: replicas: 1 template: metadata: labels: name: elkwebdemo spec: containers: - name: elkwebdemo image: bolingcavalry/elkdemo:0.0.1-SNAPSHOT tty: true ports: - containerPort: 8080
2. 在elkwebdemo.yaml所在目录执行命令kubectl create -f elkwebdemo.yaml,即可创建elk对应的pod,如下:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl get pod NAME READY STATUS RESTARTS AGE elkhost-944bcbcd4-8vpbs 1/1 Running 0 4m elkwebdemo-dddbcfc6f-x4pk6 1/1 Running 0 9s
3. 创建elk对外服务的部署脚本elkwebdemo-svc.yaml,web的服务通过node节点的30002端口对外暴露,内容如下:
apiVersion: v1 kind: Service metadata: name: elkwebdemo spec: type: NodePort ports: - port: 8080 nodePort: 30002 selector: name: elkwebdemo
5. 在elkwebdemo-svc.yaml所在目录执行命令kubectl create -f elkwebdemo-svc.yaml,即可创建elkwebdemo对应的pod,如下:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl get service NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE elkhost ClusterIP 10.43.103.244 <none> 5044/TCP 6m elkkibana NodePort 10.43.219.137 <none> 5601:30001/TCP 6m kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 16d
6. 执行命令查看elkwebdemo的pod部署在K8S的哪个node节点上:
root@willzhao-Vostro-3267:/usr/local/work/elkk8s# kubectl describe pod elkwebdemo Name: elkwebdemo-dddbcfc6f-8bbrb Namespace: default Node: willzhao-vostro-3267/192.168.31.89 Start Time: Mon, 30 Apr 2018 16:28:37 +0800 Labels: name=elkwebdemo pod-template-hash=888679729 Annotations: kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicaSet","namespace":"default","name":"elkwebdemo-dddbcfc6f","uid":"7a9364d1-4c50-11e8-a776-024f8a041a1... Status: Running ...
如上所示,Node: willzhao-vostro-3267/192.168.31.89显示了这个pod部署的节点IP是192.168.31.89;
7. 打开浏览器,输入http://192.168.31.89:30002/hello/tom,即可访问到web服务,如下图:
三、配置kibana
再次打开kibana页面,如下图所示,点击红框中的“Discover”,发现已经搜集到了上报的日志,如绿框所示:
如下图设置:创建索引名称
继续设置,如下图: 设置时间格式
再此点击左上角的”Discover”,既可开始搜索web应用日志,如下图:
四、Web应用扩容
现在我们模拟生产环境的在线扩容:
1. 将web应用从一个扩展到三个,执行以下命令:
kubectl scale deployment elkwebdemo --replicas=3
2. 可以看到web应用对应的pod已经扩展了,如下所示:
root@willzhao-Vostro-3267:~# kubectl scale deployment elkwebdemo --replicas=3 deployment "elkwebdemo" scaled root@willzhao-Vostro-3267:~# kubectl get pod NAME READY STATUS RESTARTS AGE elkhost-944bcbcd4-8vpbs 1/1 Running 0 1h elkwebdemo-dddbcfc6f-crzcp 1/1 Running 0 11s elkwebdemo-dddbcfc6f-tsppk 1/1 Running 0 11s elkwebdemo-dddbcfc6f-x4pk6 1/1 Running 0 1h
3. 为了能多上报一些日志,在浏览器上多刷新几次这个地址:http://192.168.31.89:30002/hello/tom
4. 回到kibana页面,如下图,点击红框中的“host”,展开的信息显示,这些日志来自三个host: