2022-03-16 day106 kubernets ELK日志收集与helm

作业创建redis

网络:
1.创建hadless无头服务
2.名称为redis-hadless

存储:
1.使用LocalPV
2.持久化目录/data/k8s/redis-data
3.延迟绑定
4.storageClassName类型local-storage
5.pvc通过storageClassName绑定LocalPV

POD:
1.使用StatefulSets控制器
2.三副本
3.POD名称redis-0,redis-1,redis-2
4.PVC模板使用storageClassName关联LocalPV
5.不需要从节点,只需要三个master即可
6.端口号暴露的是6379

效果:
1.创建三个有序的RedisPOD
2.数据持久化到对应的PV上
3.删除POD,自动重建POD,数据不会丢失,集群正常

配置文件:
1.使用configMap持久化
2.思考配置文件需要什么

Redis集群创建命令:
在宿主机执行即可
redis-cli --cluster reshard 10.0.0.51:6380
--cluster-from all
--cluster-to (redis-cli -c -h 10.0.0.51 -p 6380 cluster nodes|awk '/6390/{print1}')
--cluster-slots 4096
--cluster-yes

创建redis目录


image.png

创建redis-hadless

使用statefulset部署elasticsearch集群

image.png

1.准备环境-两个node节点都操作,master节点不用操作
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -w vm.max_map_count=262144
mkdir -p /data/k8s/es
chmod -R g+rwx /data/k8s/es
chgrp -R 0 /data/k8s/es

chgrp: 改变所属用户组
用法:chgrp -R 用户名 目录/文件
0是root用户


image.png
image.png
image.png

2.创建LocalPV

cat > es-pv.yaml << 'EOF'
apiVersion: v1                                    #接口版本
kind: PersistentVolume                     #资源类型为pv
metadata:                                       #创建pv元数据
  name: pv-local-node1                    #pv元数据名字
spec:                                                #定义pod运行配置
  capacity:                                       #pv的容量
    storage: 5Gi                               #存储容量为5G
  volumeMode: Filesystem          #文件系统为Filesystem
  accessModes:                   #访问类型
  - ReadWriteOnce             #读写权限,并且只能被单个pod挂载
  persistentVolumeReclaimPolicy: Retain   #回收策略,保留数据
  storageClassName: local-storage          #存储类名字
  local:                                     #存储路径
    path: /data/k8s/es          
  nodeAffinity:                       #node亲和性
    required:                         #硬策略
      nodeSelectorTerms:        #node选择器
      - matchExpressions:     #匹配规则
        - key: kubernetes.io/hostname  #键为kubernetes.io/hostname
          operator: In  #操作in,必须的意思
          values:
          - node1  #值为node1
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv-local-node2
spec:
  capacity:
    storage: 5Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  storageClassName: local-storage
  local:
    path: /data/k8s/es
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node2
EOF
    
image.png
image.png
image.png

针对 PV 持久卷,Kubernetes 支持两种卷模式(volumeModes):Filesystem(文件系统) 和 Block(块)。
volumeMode 是一个可选的 API 参数。 如果该参数被省略,默认的卷模式是 Filesystem。

volumeMode 属性设置为 Filesystem 的卷会被 Pod 挂载(Mount) 到某个目录。 如果卷的存储来自某块设备而该设备目前为空,Kuberneretes 会在第一次挂载卷之前 在设备上创建文件系统。


image.png

3.创建延迟绑定

cat > es-StorageClass.yaml << 'EOF'
apiVersion: storage.k8s.io/v1            #接口类型
kind: StorageClass                      #资源类型为StorageClass 
metadata:                         #StorageClass的元数据
  name: local-storage        #StorageClass的名字为 local-storage
provisioner: kubernetes.io/no-provisioner # 我正在使用具有3个副本的PV(kubernetes.io/no-provisioner storageClass)使用本地卷部署StatefulSet。 为两个工作节点都创建了2个PV
volumeBindingMode: WaitForFirstConsumer #等待第一个消费者才绑定
EOF
image.png

4.创建无头服务

cat > es-svc.yaml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: es
  labels:
    app: es
spec:
  selector:
    app: es
    ports:
    - name: http 
      port: 9200
   - name: http-cluster
      port: 9300
  clusterIP: None 
EOF
image.png

image.png

5.创建configmap

cat > es-configmap.yaml << 'EOF'
apiVersion: v1
kind: ConfigMap
metadata:
  name: es-config
data:
  elasticsearch.yml: |
    cluster.name: es-k8s
    network.host: 0.0.0.0
    node.name: ${HOSTNAME}     
    discovery.seed_hosts: es-0.es,es-1.es  #相互发现
    cluster.initial_master_nodes: es-0   #初始化容器就是自己
EOF

运行每一个es都有一个环境变量


image.png
image.png

6.创建StatefulSet

cat >es-StatefulSet.yaml<<'EOF'
apiVersion: apps/v1       #接口类型
kind: StatefulSet         #资源类型为statefulset
metadata:                     #statefulset元数据
  name: es              #statefulset的名字
spec:                #定义pod运行的配置
  serviceName: es    #定义pod运行的名字为es
  replicas: 2           #定义pod运行的副本数2
  selector:             #定义pod运行选择器
    matchLabels:  #匹配的标签
      app: es        #标签
  template:         #创建pod的运行配置模板
    metadata:     #创建pod 的元数据
      labels:       #创建pod 的标签
        app: es  #具体标签app:es
    spec:    #创建pod中运行的具体配置
      volumes:  #创建pod中挂载的目录
      - name: es-config #名字叫 es-config
        configMap:               #挂载configMap
         name: es-config      #下面一块都是configMap
         items: 
         - key: elasticsearch.yml
           path: elasticsearch.yml
      initContainers:
      - name: init-sysctl
        image: busybox
        imagePullPolicy: IfNotPresent
        securityContext:
          privileged: true
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
      containers:               #创建容器的具体配置
      - name: es               #容器名字叫es
        image: elasticsearch:7.17.0  #下载镜像
        imagePullPolicy: IfNotPresent #镜像下载方式
        resources:                              #资源配置
          requests:                            #最小资源配置
            cpu: 500m                      #cpu500m
            memory: 500Mi              #内存500M
          limits:                              #最大资源配置
            cpu: 1000m                 #cpu1000m
            memory: 1Gi              #内存1G
        readinessProbe:        #就绪探针
          httpGet:
            path: /_cat/health
            port: 9200
          initialDelaySeconds: 10   
          periodSeconds: 10                     
        ports:
        - name: http
          containerPort: 9200
        - name: http-cluster
          containerPort: 9300
        env:
        - name: ES_JAVA_OPTS
          value: "-Xms512m -Xmx512m"              
        volumeMounts:
        - name: pvc
          mountPath: /usr/share/elasticsearch/data
        - name: es-config
          mountPath: /usr/share/elasticsearch/config/elasticsearch.yml
          subPath: elasticsearch.yml
  volumeClaimTemplates:           #创建pvc
  - metadata:
      name: pvc
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
      storageClassName: local-storage     #匹配storage-class名字
EOF
image.png
image.png
image.png

执行


image.png

7.创建对外访问的svc

cat > es-svc-cluster.yaml << 'EOF'
apiVersion: v1
kind: Service
metadata:
  name: es-svc
  labels:
    app: es-svc
spec:
  selector:
    app: es
  ports:
  - name: http 
    port: 9200
    targetPort: 9200 #目标port
EOF 
image.png
image.png

8.创建ingress

cat > es-ingress.yaml << 'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: es-ingress
spec:
  rules:
  - host: es.k8s.com
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
          service:
            name: es-svc
            port:
              number: 9200
EOF
image.png

注意:要安装ingress


image.png

image.png

电脑还要配置host解析


image.png

电脑访问结果


image.png

安装kibana

cat > kibana-all.yaml <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kibana
  labels:
    k8s-app: kibana
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: kibana
  template:
    metadata:
      labels:
        k8s-app: kibana
    spec:
      containers:
      - name: kibana
        imagePullPolicy: IfNotPresent
        image: kibana:7.17.0
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
          limits:
            cpu: 500m
            memory: 1Gi
        env:
          - name: ELASTICSEARCH_HOSTS  #环境变量
            value: http://es-svc:9200               #es地址
        ports:
        - containerPort: 5601
---
apiVersion: v1                                                 #kibana的svc
kind: Service
metadata:
  name: kibana-svc
spec:
  ports:
  - port: 5601
    protocol: TCP
    targetPort: kibana-port
  selector:
    k8s-app: kibana
---
apiVersion: networking.k8s.io/v1                            #kibana的ingress
kind: Ingress
metadata:
  name: kibana
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: kibana.k8s.com
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
          service:
            name: kibana-svc
            port:
              number: 5601
EOF

image.png
image.png

电脑端设置host解析


image.png

等待一段时间等待机器起来


image.png

再起一个nginx来产生日志

并且把nginx日志改成json格式

[root@master ~/k8s_yml/ELK/nginx]# cat nginx-all.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    user  nginx;
    worker_processes  1;
    
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;
    
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';
        log_format json '{ "time_local": "$time_local", '
                              '"remote_addr": "$remote_addr", '
                              '"referer": "$http_referer", '
                              '"request": "$request", '
                              '"status": $status, '
                              '"bytes": $body_bytes_sent, '
                              '"http_user_agent": "$http_user_agent", '
                              '"x_forwarded": "$http_x_forwarded_for", '
                              '"up_addr": "$upstream_addr",'
                              '"up_host": "$upstream_http_host",'
                              '"upstream_time": "$upstream_response_time",'
                              '"request_time": "$request_time"'
        ' }';
        access_log  /var/log/nginx/access.log  json;
    
        sendfile        on;
        #tcp_nopush     on;
    
        keepalive_timeout  65;
    
        #gzip  on;
    
        include /etc/nginx/conf.d/*.conf;
    }
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-dp
  labels:
    app: nginx-dp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx-dp
  template:
    metadata:
      name: nginx-dp
      labels:
        app: nginx-dp
    spec:
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /data/logs
          type: DirectoryOrCreate
      - name: nginx-config
        configMap:
          name: nginx-config
          items: 
          - key: nginx.conf
            path: nginx.conf
      containers:
      - name: nginx
        image: nginx:1.14.0
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - mountPath: /var/log/nginx/
          name: hostpath-volume
        - name: nginx-config
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  labels: 
    app: nginx-svc
spec:
  selector:
    app: nginx-dp
  ports:
  - name: nginx
    port: 80 
    targetPort: 80
  type: ClusterIP

这里面包括configmap,service,deployment
curl nginx地址
curl 10.2.2.70


image.png
image.png

创建filebeat

image.png

创建 filebeat-cm.yml文件

[root@master ~/k8s_yml/ELK/filebeat]# cat filebeat-cm.yml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
data:
  filebeat.yml: |
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /data/logs/access.log
      #json.keys_under_root: true
      #json.overwrite_keys: true
      tags: ["nginx"]
    
    output.elasticsearch:
      hosts: ["es-svc:9200"]
      index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
      
    setup.ilm.enabled: false
    setup.template.enabled: false

创建 DaemonSet资源,即每一个节点都要安装一个filebeat收集日志,master节点除外
把ConfigMap也写道里面一起
创建filebeat-ds-all.yaml

[root@master ~/k8s_yml/ELK/filebeat]# cat filebeat-ds-all.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
data:
  filebeat.yml: |
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /data/logs/access.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: ["nginx"]
    
    output.elasticsearch:
      hosts: ["es-svc:9200"]
      index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
      
    setup.ilm.enabled: false
    setup.template.enabled: false
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: filebeat-ds
  labels:
    app: filebeat-ds
spec:
  selector:
    matchLabels:
      app: filebeat 
  template:
    metadata:
      name: filebeat
      labels:
        app: filebeat 
    spec:
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /data/logs
          type: DirectoryOrCreate
      - name: filebeat-config
        configMap:
         name: filebeat-config
         items: 
         - key: filebeat.yml
           path: filebeat.yml
      containers:
      - name: filebeat
        image: elastic/filebeat:7.17.0
        imagePullPolicy: IfNotPresent
        volumeMounts:
        - name: hostpath-volume
          mountPath: /data/logs
        - name: filebeat-config
          mountPath: /usr/share/filebeat/filebeat.yml
          subPath: filebeat.yml
image.png

结果就是这样


image.png

如果是tomcat收集日志的话

先创建mysql目录
创建mysql-dp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-dp
  labels:
    app: mysql-dp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql-dp
  template:
    metadata:
      namespace: tomcat
      name: mysql-dp
      labels:
        app: mysql-dp
    spec:
      volumes:
      - name: data
        hostPath:
          path: /data/mysql
      imagePullSecrets:
      - name: harbor-secret
      containers:
      - name: mysql
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        livenessProbe:
          exec:
            command: [/bin/sh, -c, 'mysql -uroot -p123456 -e "show databases;"']
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          exec:
            command: [/bin/sh, -c, 'mysql -uroot -p123456 -e "show databases;"']
          initialDelaySeconds: 10
          periodSeconds: 10
        ports:
        - name: http
          containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD 
          value: '123456' 
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql

再创建mysql-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  labels: 
    app: mysql-svc
spec:
  selector:
    app: mysql-dp
  ports:
  - name: mysql-dp 
    port: 3306 
    targetPort: 3306

创建app目录
创建comfigmap

cat >create_cm.sh<<'EOF'
#!/bin/bash
kubectl create configmap tomcat-cm --from-file=server.xml=./server.xml
EOF

添加server.xml文件


image.png
cat >server.xml <<'EOF'
<?xml version='1.0' encoding='utf-8'?>
<!--
  31Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="{&quot;clientip&quot;:&quot;%h&quot;,&quot;ClientUser&quot;:&quot;%l&quot;,&quot;authenticated&quot;:&quot;%u&quot;,&quot;AccessTime&quot;:&quot;%t&quot;,&quot;method&quot;:&quot;%r&quot;,&quot;status&quot;:&quot;%s&quot;,&quot;SendBytes&quot;:&quot;%b&quot;,&quot;Query?string&quot;:&quot;%q&quot;,&quot;partner&quot;:&quot;%{Referer}i&quot;,&quot;AgentVersion&quot;:&quot;%{User-Agent}i&quot;}"/>
      </Host>
    </Engine>
  </Service>
</Server>
EOF

创建tomcat-svc.yaml

cat >tomcat-svc.yaml<<'EOF'
apiVersion: v1
kind: Service
metadata:
  name: tomcat-svc
  labels: 
    app: tomcat-svc
spec:
  selector:
    app: tomcat-dp
  ports:
  - name: tomcat 
    port: 8080
    targetPort: 8080
EOF

创建tomcat-dp.yaml

cat > tomcat-dp.yaml<<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-dp
  labels:
    app: tomcat-dp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: tomcat-dp
  template:
    metadata:
      namespace: tomcat
      name: tomcat-dp
      labels:
        app: tomcat-dp
    spec:
      volumes:
      - name: hostpath-volume
        hostPath:
          path: /data/logs/tomcat/
          type: DirectoryOrCreate
      - name: tomcat-cm
        configMap:
          name: tomcat-cm
          items: 
          - key: server.xml
            path: server.xml
      initContainers:
      - name: init
        image: busybox:latest
        args: [/bin/sh, -c, 'until nc -zv mysql-svc 3306;do echo nonono;sleep 1;done']
      containers:
      - name: tomcat
        image: kubeguide/tomcat-app:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 8080
        livenessProbe:
          httpGet:
            path: /demo/index.jsp
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 10
        readinessProbe:
          httpGet:
            path: /demo/index.jsp
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 10
        env:
        - name: MYSQL_SERVICE_HOST 
          value: mysql-svc
        volumeMounts:
        - name: tomcat-cm 
          mountPath: /usr/local/tomcat/conf/server.xml
          subPath: server.xml
        - name: hostpath-volume
          mountPath: /usr/local/tomcat/logs
EOF

创建ingress文件,tomcat-ingress.yaml

cat >tomcat-ingress.yaml <<'EOF'
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tomcat-ingress
spec:
  rules:
  - host: tomcat.k8s.com
    http:
      paths:
      - path: /
        pathType: ImplementationSpecific
        backend:
          service:
            name: tomcat-svc
            port:
              number: 8080

EOF

那么filebeat就要修改了,添加tomcat标记

apiVersion: v1
kind: ConfigMap
metadata:
  name: filebeat-config
data:
  filebeat.yml: |
    filebeat.inputs:
    - type: log
      enabled: true
      paths:
        - /data/logs/nginx/access.log
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: ["nginx"]

    - type: log
      enabled: true
      paths:
        - /data/logs/tomcat/localhost_access_log.*.txt
      json.keys_under_root: true
      json.overwrite_keys: true
      tags: ["tomcat"]
        
    output.elasticsearch:
      hosts: ["es-svc:9200"]
      indices:
        - index: "nginx-access-%{[agent.version]}-%{+yyyy.MM}"
          when.contains:
            tags: "nginx"
        - index: "tomcat-access-%{[agent.version]}-%{+yyyy.MM}"
          when.contains:
            tags: "tomcat"
    setup.ilm.enabled: false
    setup.template.enabled: false

helm

就k8s的包管理工具
比如centos有自己的包管理工具 yum
docker有docker pull镜像
乌班图有apt

k8s都是一个一个资源都需要自己写deployment,pv,pvc等等

但是k8s有一个helm,别人写好了一个模板,里面都有,只需要该一个变量就可以用

Helm基础使⽤
安装
wget https://get.helm.sh/helm-v3.6.3-linux-amd64.tar.gz
tar zxf helm-v3.6.3-linux-amd64.tar.gz -C /opt/
cp /opt/linux-amd64/helm /usr/local/bin/
helm version

image.png

仓库指南
https://artifacthub.io/packages/search?kind=0

添加仓库
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo add stable https://charts.helm.sh/stable

helm官网


image.png

查看仓库列表
helm repo list


image.png

更新helm
helm repo update


image.png

查找一个叫nginx的软件包
helm search repo nginx


image.png

查看具体软件信息,比如bitnami/nginx ,chart 图表的意思
helm show chart bitnami/nginx
helm show all bitnami/nginx


image.png

all更加详细信息


image.png

查看可以配置的参数
helm show values bitnami/nginx
image.png

安装软件
helm install nginx bitnami/nginx
helm install bitnami/nginx --generate-name

查看已经部署的版本
helm list

image.png

查看已经安装的软件信息
helm status mysql-1628598079


image.png

卸载软件
helm uninstall nginx-xxxx


image.png

命令补全

source <(helm completion bash)
echo "source <(helm completion bash)" >> ~/.bash_profile

3.实战:⾃定义nginx包变量
查看官⽅变量说明:

https://github.com/bitnami/charts/tree/master/bitnami/nginx

⾃定义变量:

cat > nginx_values.yaml<<'EOF'
replicaCount: 2
podLabels:
  app: mynginx
service:
  type: ClusterIP
  port: 80
ingress:
  enabled: true
  pathType: ImplementationSpecific
  hostname: nginx.k8s.com
  path: /
EOF

10.测试运行效果
helm install --debug --dry-run -f nginx-values.yml nginx bitnami/nginx


image.png
image.png

正式安装
helm install -f nginx_values.yaml -n nginx-helm nginx bitnami/nginx


image.png

运行之前要创建一个命名空间
kubectl create namespace nginx-helm


image.png

查看资源创建情况
kubectl -n nginx-helm get all


image.png
image.png

电脑配置host解析

访问测试

再次执行会报错,可以使用更新命令


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容