Skip to content

Instantly share code, notes, and snippets.

@DD-ScottBeamish
Last active March 9, 2018 18:14
Show Gist options
  • Save DD-ScottBeamish/2620a8cdd1c816afd553f64740d604db to your computer and use it in GitHub Desktop.
Save DD-ScottBeamish/2620a8cdd1c816afd553f64740d604db to your computer and use it in GitHub Desktop.
K8S + RBAC + AutoDisco + ConfigMap + Separate Namespaces
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: datadog-agent
spec:
selector:
matchLabels:
name: datadog-agent
template:
metadata:
labels:
app: datadog-agent
name: datadog-agent
name: datadog-agent
spec:
nodeSelector:
label: local
spec:
serviceAccountName: datadog
containers:
- image: datadog/agent:latest
imagePullPolicy: Always
name: datadog-agent
ports:
- containerPort: 8125
name: dogstatsdport
protocol: UDP
- containerPort: 8126
name: traceport
protocol: TCP
env:
- name: DD_API_KEY
value: <YOUR_API_KEY>
- name: DD_COLLECT_KUBERNETES_EVENTS
value: "true"
- name: DD_LEADER_ELECTION
value: "true"
- name: KUBERNETES
value: "yes"
- name: DD_KUBERNETES_KUBELET_HOST
valueFrom:
fieldRef:
fieldPath: status.hostIP
resources:
requests:
memory: "128Mi"
cpu: "100m"
limits:
memory: "512Mi"
cpu: "250m"
volumeMounts:
- name: dockersocket
mountPath: /var/run/docker.sock
- name: procdir
mountPath: /host/proc
readOnly: true
- name: cgroups
mountPath: /host/sys/fs/cgroup
readOnly: true
- name: datadog-agent-config
mountPath: /etc/datadog-agent/conf.d/mysql.d
subPath: mysql.d
livenessProbe:
exec:
command:
- ./probe.sh
initialDelaySeconds: 15
periodSeconds: 5
volumes:
- hostPath:
path: /run/docker.sock
name: dockersocket
- hostPath:
path: /proc
name: procdir
- hostPath:
path: /sys/fs/cgroup
name: cgroups
- name: datadog-agent-config
configMap:
name: datadog-agent-config
items:
- key: mysql-autoconf.yaml
path: mysql-autoconf.yaml
# Your admin user needs the same permissions to be able to grant them
# Easiest way is to bind your user to the cluster-admin role
# See https://cloud.google.com/container-engine/docs/role-based-access-control#setting_up_role-based_access_control
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: datadog
subjects:
- kind: ServiceAccount
name: datadog
namespace: default
roleRef:
kind: ClusterRole
name: datadog
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: datadog
rules:
- nonResourceURLs:
- "/version" # Used to get apiserver version metadata
- "/healthz" # Healthcheck
verbs: ["get"]
- apiGroups: [""]
resources:
- "nodes"
- "namespaces" #
- "events" # Cluster events + kube_service cache invalidation
- "services" # kube_service tag
verbs: ["get", "list"]
- apiGroups: [""]
resources:
- "configmaps"
resourceNames: ["datadog-leader-elector"]
verbs: ["get", "delete", "update"]
- apiGroups: [""]
resources:
- "configmaps"
verbs: ["create"]
kind: ConfigMap
apiVersion: v1
metadata:
name: datadog-agent-config
namespace: default
data:
mysql-autoconf.yaml: |-
init_config:
instances:
- server: %%host%%
port: %%port%%
---
# You need to use that account for your dd-agent DaemonSet
apiVersion: v1
kind: ServiceAccount
metadata:
name: datadog
automountServiceAccountToken: true
apiVersion: v1
kind: Pod
metadata:
name: mysql
annotations:
service-discovery.datadoghq.com/mysql.check_names: '["mysql"]'
service-discovery.datadoghq.com/mysql.init_configs: '[{}]'
service-discovery.datadoghq.com/mysql.instances: '[{"server":"%%host%%","port":"%%port%%","user":"root","pass":"different"}]'
spec:
containers:
- image: mysql
name: mysql
ports:
- containerPort: 3306
name: mysqlport
protocol: TCP
env:
- name: MYSQL_ROOT_PASSWORD
value: different
apiVersion: v1
kind: Pod
metadata:
name: mysql
annotations:
service-discovery.datadoghq.com/mysql.check_names: '["mysql"]'
service-discovery.datadoghq.com/mysql.init_configs: '[{}]'
service-discovery.datadoghq.com/mysql.instances: '[{"server":"%%host%%","port":"%%port%%","user":"root","pass":"abc123"}]'
spec:
containers:
- image: mysql
name: mysql
ports:
- containerPort: 3306
name: mysqlport
protocol: TCP
env:
- name: MYSQL_ROOT_PASSWORD
value: abc123
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "dev",
"labels": {
"name": "dev"
}
}
}
{
"kind": "Namespace",
"apiVersion": "v1",
"metadata": {
"name": "prod",
"labels": {
"name": "prod"
}
}
}
@DD-ScottBeamish
Copy link
Author

DD-ScottBeamish commented Mar 6, 2018

Installing the Container via DaemonSet

  1. Create the Datadog ServiceAccount

kubectl create -f datadog-service-account.yaml

  1. Create the Datadog ClusterRole which provides access to the various objects required to gather metrics

kubectl create -f datadog-cluster-role.yaml

  1. Create the ClusterRoleBinding to map the ClusterRole to the ServiceAccount

kubectl create -f datadog-cluster-role-binding.yaml

  1. Create the ConfigMap which contains the kubernetes.yaml and kubernetes_state.yaml configuration files

kubectl create -f datadog-config-map.yaml

  1. Create the DaemonSet which instructs the scheduler to run 1 instance of the Datadog Agent container on each kubelet

kubectl create --namespace=default -f datadog-agent-daemonset.yaml

  1. Create the namespaces

kubectl create -f namespace-dev.json
kubectl create -f namespace-prod.json

  1. Create the mysql tenants

kubectl create --namespace=dev -f mysql-dev.yaml
kubectl create --namespace=prod -f mysql-prod.yaml

@mshutt
Copy link

mshutt commented Mar 6, 2018

Wow those are my docs reused :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment