Prometheus server deployment

With our new namespace available, it's time to create a very simple Prometheus configuration and save it on a ConfigMap using the following manifest:

apiVersion: v1
kind: ConfigMap
metadata:
name: prometheus-config
namespace: monitoring
data:
prometheus.yml: |
scrape_configs:
- job_name: prometheus
static_configs:
- targets:
- localhost:9090

Apply the previous manifest using the following command:

kubectl apply -f prometheus-configmap.yaml

Now, it's time to start a new deployment of Prometheus, making sure we mount the previously configured ConfigMap into the pod we are deploying. The Deployment object is configured with the following metadata:

apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-deployment
namespace: monitoring
labels:
app: prometheus

The Prometheus container will be started with its configuration file and data directory coming from volume mounts, shown as follows:

        args:
- --config.file=/etc/config/prometheus.yml
- --storage.tsdb.path=/data
volumeMounts:
- name: config-volume
mountPath: /etc/config/prometheus.yml
subPath: prometheus.yml
- name: prometheus-data
mountPath: /data
subPath: ""

The config-volume volume is created from a ConfigMap, while the prometheus-data volume is created with an empty directory. This can be seen in the following snippet:

      volumes:
- name: config-volume
configMap:
name: prometheus-config
- name: prometheus-data
emptyDir: {}

Apply the previous manifest using the following command:

kubectl apply -f prometheus-deployment.yaml

We can follow the deployment status using this snippet:

kubectl rollout status deployment/prometheus-deployment -n monitoring

We should look at the logs of our Prometheus instance using the following command:

kubectl logs --tail=20 -n monitoring -l app=prometheus

After a successful deployment, we're ready to assign a new service to our instance, choosing NodePort so we can access it without requiring port-forwarding, like so:

kind: Service
apiVersion: v1
metadata:
name: prometheus-service
namespace: monitoring
spec:
selector:
app: prometheus
type: NodePort
ports:
- name: prometheus-port
protocol: TCP
port: 9090
targetPort: 9090

Apply the previous manifest using the following:

kubectl apply -f prometheus-service.yaml

And you're ready to check your new Prometheus service using the following code snippet:

minikube service prometheus-service -n monitoring

This will open your browser on the Prometheus service endpoint. You can now check the running configuration and targets using the Prometheus web interface:

Figure 5.5: Prometheus initial configuration

Now that we have Prometheus running in Kubernetes, we can start adding targets for it to scrape. In the next section, we will have a look at how you can achieve this.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.59.187