k8s 部署 skywalking 并持久化到es

1、k8s中安装部署 skywalking

skywalking集群情况下需要保证用同一数据源,这里我们存储方式改为es

1.1 部署elasticsearch

docker run -it -d -p 9200:9200 -p 9300:9300 -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -e "discovery.type=single-node"   elasticsearch:7.14.2

1.2 创建skywalking-oap的yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: skywalking-oap
  namespace: default
  labels:
    app: skywalking-oap
spec:
  replicas: 1
  selector:
    matchLabels:
      app: skywalking-oap
  template:
    metadata:
      labels:
        app: skywalking-oap
    spec:
      containers:
	    - env:
         - name: SW_STORAGE
		   value: elasticsearch7  ##存储方式
		 - name: SW_STORAGE_ES_CLUSTER_NODES
		   value: 10.0.24.5:9200 ##es地址
          name: skywalking-oap
          image: apache/skywalking-oap-server:8.4.0-es7  ##镜像
          imagePullPolicy: IfNotPresent ##如果存在就不拉去取
          ports:
            - containerPort: 11800
              name: grpc
            - containerPort: 12800
              name: rest
---
apiVersion: v1
kind: Service
metadata:
  name: skywalking-oap
  namespace: default
  labels:
    service: skywalking-oap
spec:
  ports:
    - port: 12800
      name: rest
    - port: 11800
      name: grpc
  selector:
    app: skywalking-oap

1.3 创建skywalking-ui的yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: skywalking-ui
  namespace: default
  labels:
    app: skywalking-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: skywalking-ui
  template:
    metadata:
      labels:
        app: skywalking-ui
    spec:
      containers:
        - name: skywalking-ui
          image: apache/skywalking-ui:8.4.0
          ports:
            - containerPort: 8080
              name: page
          env:
            - name: SW_OAP_ADDRESS
              value: skywalking-oap:12800 ##skywalking-oap监听端口
---
apiVersion: v1
kind: Service
metadata:
  name: skywalking-ui
  namespace: default
  labels:
    service: skywalking-ui
spec:
  ports:
    - port: 8080
      name: page
      nodePort: 30200
  type: NodePort
  selector:
    app: skywalking-ui

1.3 k8s中部署 skywalking-oap.yaml、skywalking-ui.yaml

kubectl create -f skywalking-oap.yaml
kubectl create -f skywalking-ui.yaml

在这里插入图片描述
在这里插入图片描述
k8s 部署 skywalking 并持久化到es_第1张图片

1.4 查看skywalking-ui

k8s 部署 skywalking 并持久化到es_第2张图片

2、部署项目使用skywalking进行链路追踪

2.1 下载 skywalking-agent

https://skywalking.apache.org/downloads/#Agents
k8s 部署 skywalking 并持久化到es_第3张图片

2.2 idea 中使用skywalking-agent

2.2.1 创建项目server-order 并创建web控制层

@RestController
public class OrderController {
    @GetMapping("/getuuid")
    public String getuuid(){
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }
}

2.2.2 创建项目web-monitor 并创建feign和monitor的控制层

@FeignClient(value = "server-order")
@Component
public interface OrderService {
    @GetMapping("/getuuid")
    public String getuuid();

}
@RestController
public class MonitorController {
    @Autowired
    private OrderService orderService;
    @GetMapping("send")
    public String send(){
        String getuuid = orderService.getuuid();
        return getuuid;
    }
}

2.2.3 增加环境变量

-javaagent:D:\opts\apache-skywalking-java-agent-8.8.0\skywalking-agent\skywalking-agent.jar
-Dskywalking.collector.backend_service=ip地址:端口
-Dskywalking.agent.service_name=服务名称

k8s 部署 skywalking 并持久化到es_第4张图片
k8s 部署 skywalking 并持久化到es_第5张图片
k8s 部署 skywalking 并持久化到es_第6张图片

2.2.3 查看skywalking-ui链路

k8s 部署 skywalking 并持久化到es_第7张图片
k8s 部署 skywalking 并持久化到es_第8张图片
k8s 部署 skywalking 并持久化到es_第9张图片
k8s 部署 skywalking 并持久化到es_第10张图片

2.3 k8s 部署项目增加skywalking链路追踪

2.3.1 构建镜像

FROM java:8
WORKDIR /project
ENV JAVA_OPTS=""
ENV SKYWALKING_OPTS=""
CPOY skywalking-agent.jar  skywalking-agent.jar
COPY web-monitor.jar web-monitor.jar
ENTRYPOINT ["sh","-c","java -jar ${JAVA_OPTS} web-monitor.jar ${SKYWALKING_OPTS} "]

2.3.2 创建k8s无状态服务

apiVersion: extensions/v1beta1 #版本
kind: Deployment  #资源类型
metadata:
  labels:
    workload.user.cattle.io/workloadselector: deployment-cloud-web-monitor  #标签,k8s对于资源控制主要是根据labels和selector进行的
  name: web-monitor  #资源名称
  namespace: deafult #所属命名空间
spec:
  progressDeadlineSeconds: 600  
  replicas: 1  #一个pod
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      workload.user.cattle.io/workloadselector: deployment-cloud-web-monitor
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate #发布策略
  template:    #创建模板
    metadata:
      labels:
        workload.user.cattle.io/workloadselector: deployment-cloud-web-monitor
    spec:
      containers:
      - env:
        - name: JAVA_OPTS
          value: -Xmx3550M -Xms3550M -Xmn2G -Xss256k
		- name: SKYWALKING_OPTS
          value: -javaagent:./skywalking-agent.jar -Dskywalking.agent.service_name=web-monitor -Dskywalking.collector.backend_service=skywalking-oap:11800
        image: 192.1.1.1/project/web-monitor:stable  #镜像
        imagePullPolicy: Always  #镜像拉取策略
        name: web-monitor   #容器名称
        ports:
        - containerPort: 8513  #暴露端口
          name: 8513tcp02
          protocol: TCP
        resources:
          limits:
            memory: 4Gi
        stdin: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        tty: true
        volumeMounts:
        - mountPath: /log  #容器内部挂载位置
          name: vol1
      dnsPolicy: ClusterFirst
      restartPolicy: Always  #重启策略
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
      volumes:
      - name: vol1
        persistentVolumeClaim: #挂载的pvc
          claimName: log-pvc

2.3.3 注意点

在上边的镜像中 将skywalking-agent.jar 拷贝到镜像里,这一步可以使用pv挂载,另外也可以将skywalking-agent用initc来做链路追踪

你可能感兴趣的:(Kubernetes,SpringCloud,kubernetes,skywalking,elasticsearch)