Skip to content

Commit f318b08

Browse files
committed
Updates to some advanced demos.
1 parent b9f214f commit f318b08

File tree

6 files changed

+57
-48
lines changed

6 files changed

+57
-48
lines changed

authentication/README.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ There are multiple ways to authenticate to a Kubernetes Cluster. This Demo will
1111
To try this method log in to the diagnose container using:
1212

1313
```
14-
kubectl exec -ti diagnose-... s
14+
kubectl exec -ti deploy/diagnose -- sh
1515
```
1616

17-
(diagnose-... is the complete name of the pod found via shell completion or `kubectl get pods`)
18-
1917
Every pod in kubernetes will get service account credentials injected at a well-known location which is `/var/run/secrets/kubernetes.io/serviceaccount/`. When you list this directory you will find a certificate, a file containing the namespace this pod runs in and a token to access the API. We export the last into an environment variable:
2018

2119
```
@@ -26,8 +24,8 @@ Now the API can be accessed with curl like this:
2624

2725
```
2826
curl --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
29-
--header "Authorization: Bearer $TOKEN" \
30-
--url https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT_HTTPS/api/
27+
--header "Authorization: Bearer ${TOKEN}" \
28+
https://${KUBERNETES_SERVICE_HOST}:${KUBERNETES_SERVICE_PORT_HTTPS}/api/
3129
```
3230

3331
With the argument "--cacert" curl gets will be able to validate the https certificate. The header contains the token and is used to identify to the API Server. The Url contains a few more environment variables (KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT_HTTPS). These contain the relevant adress of the API server inside the cluster and will also be injected into containers.
@@ -48,10 +46,24 @@ kubectl get pod diagnose-... -o jsonpath={.spec.serviceAccount}
4846

4947
## Creating and using a service account token from outside the cluster
5048

51-
Accessing the api of a minikube server can be done using:
49+
First you need to set the local IP address and port for accessing the API.
50+
51+
If you are using minikube use:
52+
53+
```
54+
export KUBE_API_HOST=$(minikube ip):8443
55+
```
56+
57+
If you are using kind set the variable using:
58+
59+
```
60+
export KUBE_API_HOST=$(docker port kind-control-plane 6443)
61+
```
62+
63+
Accessing the api of a server can now be done using:
5264

5365
```
54-
curl -k https://$(minikube ip):8443/api
66+
curl -k https://$KUBE_API_HOST/api
5567
```
5668

5769
We ignore the certificate validation for this example. If you try this you will get a status code 403 forbidden telling you that User 'system:anonymous' can not get the path /api.
@@ -62,13 +74,13 @@ To authorize we can create a new service account and use its token to authorize
6274
kubectl create serviceaccount foo
6375
export TOKEN_NAME=$(kubectl get serviceaccount foo -o=jsonpath="{.secrets[0].name}")
6476
export TOKEN=$(kubectl get secret $TOKEN_NAME -o=jsonpath={.data.token}|base64 -D)
65-
curl -k --header "Authorization: Bearer $TOKEN" https://$(minikube ip):8443/api
77+
curl -k --header "Authorization: Bearer $TOKEN" https://$KUBE_API_HOST/api
6678
```
6779

6880
First a service account named foo is created. Then 'kubectl get' is used with jsonpath to extract the name of the token secret. In the next step this token is extracted into an environment variable called TOKEN. Finally this TOKEN is used as a header to make the same curl command as above. This time the result is a listing of API versions. But this service account is also not allowed to do more, like listing namespaces. Try:
6981

7082
```
71-
curl -k --header "Authorization: Bearer $TOKEN" https://$(minikube ip):8443/api/v1/namespaces
83+
curl -k --header "Authorization: Bearer $TOKEN" https://$KUBE_API_HOST/api/v1/namespaces
7284
```
7385

7486
With the following command you can assign the admin role to the service account:
@@ -93,6 +105,7 @@ These steps are included in the script `generate_user.sh` in this directory. You
93105
```
94106
./generate_user.sh
95107
```
108+
(This script currently assumes you are using minikube. So it would not work using kind.)
96109

97110
There is now a new Kubenetes configuration generated in auth_data/config. You can use this config to authenticate by setting the KUBECONFIG environment variable:
98111

monitoring/README.md

+27-31
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,59 @@
11
# Monitoring
22

3-
## cAdvisor
3+
## Enable Metrics Server
44

5-
cAdvisor is an Agent included in every kubelet and thus installed on every node. It collects measurements of the containers running on the node as well as on the node itself.
6-
7-
**Hint**
8-
For the following urls you need the ip address of the node you want to look at. If you are using minikube you get it via
9-
10-
```
11-
minikube ip
12-
```
13-
14-
The following urls are useful for looking at cAdvisor directly:
5+
You need to enable the metrics server in order to use metrics and any connected service or command. If you are using minikube you are doning this by enabling the addon:
156

167
```
17-
http://<node-ip>:4194/
8+
minikube addons enable metrics-server
189
```
1910

20-
for the cAdvisor interface. And:
11+
## Accessing the metrics API directly
2112

22-
```
23-
http://<node-ip>:4194/metrics
24-
```
13+
The Metrics API is not intended to be used directly. Use kubectl top (see below) instead. But if you want to access it you can still do it like this:
2514

26-
to retrieve the metrics in Prometheus format. This can be useful when you want to scrape node data directly from prometheus without going via Heapster.
15+
API Overview:
2716

28-
## Heapster
17+
```
18+
kubectl get --raw /apis/metrics.k8s.io/v1beta1/ |jq
19+
```
2920

30-
Heapster is itself a pod that collects the measurements of all cAdvisor instances in your cluster. When you are using minikube you can enable it using:
21+
List all Nodes where metrics are available
3122

3223
```
33-
minikube addons enable heapster
24+
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes |jq
3425
```
3526

36-
Heapster provides it's data again using the prometheus metrics format. As the addon does not provide an externally available service you have to login to the diagnose-pod ( see [../README.md](../README.md) ):
27+
Get Data for a specific Node:
3728

3829
```
39-
kubectl exec -ti diagnose-... sh
30+
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes/minikube |jq
4031
```
41-
42-
Inside that container you can get the monitoring data using:
32+
This might be useful if you want to extract e.g. the CPU usage of a node with a tool like jq:
4333

4434
```
45-
curl heapster.kube-system/metrics
35+
kubectl get --raw /apis/metrics.k8s.io/v1beta1/nodes/minikube |jq .usage.cpu
4636
```
4737

48-
When heapster is active you can see performance information on the Kubernetes dashboard and you can also use the top command at the commandline:
38+
## Using kubectl top
39+
40+
When metrics-server is active you can see performance information on the Kubernetes dashboard and you can also use the top command at the commandline:
4941

5042
```
5143
kubectl top node
5244
kubectl top pod
5345
```
5446

55-
## Grafana dashboard
47+
## Kubernetes dashboard
5648

57-
Where cAdvisor and Heapster work pretty much the same in most Kubernetes installations all further monitoring tools differ very often. It is usual to use a combination of Prometheus or Influx DB as timeseries database and Grafana as dashboard.
49+
You can install the kubernetes dashboard in minikube as addon using:
50+
51+
```
52+
minikube addons enable dashboard
53+
```
5854

59-
Minikube comes with a influx/grafana combination. You can open the dashboard using:
55+
Start the dashboard using:
6056

6157
```
62-
minikube -n kube-system service monitoring-grafana
63-
```
58+
minikube dashboard
59+
```

persistent_volumes/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ The persistent volume is the actual instance of the Volume claim. It can be main
2323
To see the persistence in action create some data inside the redis database. To get an redis-cli log into the pod using:
2424

2525
```
26-
kubectl exec -ti redis-... redis-cli
26+
kubectl exec -ti deploy/redis -- redis-cli
2727
```
2828

29-
where redis-... is the complete name of the redis pod. You should get a prompt and you can store a key and value like this:
29+
You should get a prompt and you can store a key and value like this:
3030

3131
```
3232
127.0.0.1:6379> set foo bla
@@ -39,7 +39,7 @@ OK
3939
Now the data is stored on the persistent volume. You can find the datafiles when logging into the redis pod by using the shell:
4040

4141
```
42-
kubectl exec -ti redis-... sh
42+
kubectl exec -ti deploy/redis -- sh
4343
/data # ls -lsa
4444
total 12
4545
4 drwxrwxrwx 2 root root 4096 Jun 5 14:24 .
@@ -63,7 +63,7 @@ pod "redis-5ff7b8476c-clkkt" deleted
6363
If you now list the pods again you will find, that there is a newly created redis pod. You can now test whether the persistence worked by connecting to the redis-cli again:
6464

6565
```
66-
kubectl exec -ti redis-... redis-cli
66+
kubectl exec -ti deploy/redis -- redis-cli
6767
127.0.0.1:6379> get foo
6868
"bla"
6969
127.0.0.1:6379>

persistent_volumes/persistent_redis.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec:
1515
run: redis
1616
spec:
1717
containers:
18-
- image: redis:3.2-alpine
18+
- image: redis:6-alpine
1919
name: redis
2020
command: ["redis-server", "--appendonly", "yes"] #<-- starting redis in persistent mode
2121
volumeMounts:

rbac/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ kubectl get pods
1515
You can find whether the current user is allowed to do a certain operation by using `kubectl config can-i` like:
1616

1717
```
18-
kubectl config can-i get po
18+
kubectl auth can-i get po
1919
```
2020

2121
## Creating read-only access
@@ -43,7 +43,7 @@ kubectl get pods
4343

4444
Also try
4545
```
46-
kubectl config can-i get po
46+
kubectl auth can-i get po
4747
```
4848

4949
To test whether you are allowed to do other operations try e.g.:

stateful_sets/stateful_nginx.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ spec:
1515
spec:
1616
containers:
1717
- name: nginx
18-
image: nginx:1.16
18+
image: nginx:stable-alpine
1919
volumeMounts:
2020
- name: content
2121
mountPath: /usr/share/nginx/html

0 commit comments

Comments
 (0)