Kubernetes Basics - Parts II
Info
This workshop segment expect you to be inside a cloned repository of k8s-specs
.
| git clone https://github.com/vfarcic/k8s-specs.git
cd k8s-specs
|
Points to cover
- Using ConfigMaps To Inject Configuration Files
- Using Secrets To Hide Confidential Information
- Dividing A Cluster Into Namespaces
- Securing Kubernetes Clusters
- Managing Resources
ConfigMap
Create from files
| kubectl create cm my-config --from-file=cm/prometheus-conf.yml \
--from-file=cm/prometheus.yml
|
| kubectl create -f cm/alpine.yml
|
| kubectl exec -it alpine -- ls /etc/config
|
| kubectl exec -it alpine -- cat /etc/config/prometheus-conf.yml
|
| kubectl delete -f cm/alpine.yml
|
| kubectl delete cm my-config
|
Create from literals
| kubectl create cm my-config \
--from-literal=something=else --from-literal=weather=sunny
|
| kubectl create -f cm/alpine.yml
|
| kubectl exec -it alpine -- ls /etc/config
|
| kubectl exec -it alpine -- cat /etc/config/something
|
| kubectl delete -f cm/alpine.yml
|
| kubectl delete cm my-config
|
Create from environment files
| kubectl create cm my-config --from-env-file=cm/my-env-file.yml
|
| kubectl get cm my-config -o yaml
|
Secrets
Generic secrets
| kubectl create secret generic my-creds \
--from-literal=username=jdoe --from-literal=password=incognito
|
| kubectl get secret my-creds -o json
|
| kubectl get secret my-creds -o jsonpath="{.data.username}" \
| base64 --decode
|
| kubectl get secret my-creds -o jsonpath="{.data.password}" \
| base64 --decode
|
| kubectl apply -f secret/jenkins.yml
|
| kubectl rollout status deploy jenkins
|
| POD_NAME=$(kubectl get pods -l service=jenkins,type=master \
-o jsonpath="{.items[*].metadata.name}")
|
| kubectl exec -it $POD_NAME -- ls /etc/secrets
|
| kubectl exec -it $POD_NAME -- cat /etc/secrets/jenkins-user
|
| IP=$(minikube ip) # If minikube
|
| open "http://$IP/jenkins"
|
Cleanup
| kubectl delete -f secret/jenkins.yml
kubectl delete secret my-creds
|
Namespaces
Create initial release
| IMG=vfarcic/go-demo-2
TAG=1.0
|
| cat ns/go-demo-2.yml | sed -e "s@image: $IMG@image: $IMG:$TAG@g" \
| kubectl create -f -
|
| kubectl rollout status deploy go-demo-2-api
|
Retrieve existing Namespaces
Explore existing namespaces
| kubectl -n kube-public get all
|
| kubectl -n kube-system get all
|
| kubectl -n default get all
|
Create new namespace
| kubectl create ns testing
kubectl get ns
|
Change default namespace
Deploy to a new namespace
| kubectl config use-context testing
|
| TAG=2.0
DOM=go-demo-2.com
|
| cat ns/go-demo-2.yml | sed -e "s@image: $IMG@image: $IMG:$TAG@g" \
| sed -e "s@host: $DOM@host: $TAG\.$DOM@g" \
| kubectl create -f -
|
| kubectl rollout status deploy go-demo-2-api
|
| curl -H "Host: go-demo-2.com" "http://$IP/demo/hello"
|
| curl -H "Host: 2.0.go-demo-2.com" "http://$IP/demo/hello"
|
Communicate accross namespaces
Make sure we use the default
namespace again.
Now we run a new container, and make sure it has curl.
| kubectl run test --image=alpine --restart=Never sleep 10000
|
| kubectl get pod test
kubectl exec -it test -- apk add -U curl
|
Now, lets exec into the container (kubectl exec
) and use curl to test the services' DNS.
Delete a Namespace
| kubectl delete ns testing
|
| kubectl -n testing get all
|
| curl -H "Host: go-demo-2.com" "http://$IP/demo/hello"
|
| kubectl set image deployment/go-demo-2-api \
api=vfarcic/go-demo-2:2.0 --record
|
| curl -H "Host: go-demo-2.com" "http://$IP/demo/hello"
|
Cleanup
| kubectl delete -f ns/go-demo-2.yml
kubectl delete pod test
|
Resource limits & requests - SHORT
View resources
| cat res/go-demo-2-random.yml
|
| kubectl create -f res/go-demo-2-random.yml --record --save-config
|
| kubectl rollout status deployment go-demo-2-api
|
| kubectl describe deploy go-demo-2-api
|
Too little memory
| cat res/go-demo-2-insuf-mem.yml
|
| kubectl apply -f res/go-demo-2-insuf-mem.yml --record
|
| kubectl describe pod go-demo-2-db
|
Too much memory
| cat res/go-demo-2-insuf-node.yml
|
| kubectl apply -f res/go-demo-2-insuf-node.yml --record
|
| kubectl describe pod go-demo-2-db
|
Cleanup
| kubectl delete -f res/go-demo-2-insuf-node.yml
|
Securing Kubernetes
GKE
Resource limits & requests
Enable Heapster
Heapster enables Container Cluster Monitoring and Performance Analysis for Kubernetes (versions v1.0.6 and higher), and platforms which include it.
Info
GKE has heapster installed and enabled by default.
Warning
Heapster is now EOL, but still serves a purpose for this demo.
Please do NOT use this in production systems.
View resources
| cat res/go-demo-2-random.yml
|
| kubectl create -f res/go-demo-2-random.yml --record --save-config
|
| kubectl rollout status deployment go-demo-2-api
|
| kubectl describe deploy go-demo-2-api
|
Expose heapster api endpoint
Info
The version of heapster
might be different.
Please confirm the actual name with the command below.
| kubectl get deployment -n kube-system
|
Measure consumption
Info
You can also use a tool such as Kube Capacity for easier access to these metrics.
| kubectl -n kube-system get pods
|
| kubectl -n kube-system get svc heapster-api -o json
|
Measure consumption 2
Measure consumption 3
| BASE_URL="http://$ADDR:$PORT/api/v1/model/namespaces/default/pods"
curl "$BASE_URL"
|
| DB_POD_NAME=$(kubectl get pods -l service=go-demo-2 -l type=db \
-o jsonpath="{.items[0].metadata.name}")
|
| curl "$BASE_URL/$DB_POD_NAME/containers/db/metrics"
|
| curl "$BASE_URL/$DB_POD_NAME/containers/db/metrics/memory/usage"
|
| curl "$BASE_URL/$DB_POD_NAME/containers/db/metrics/cpu/usage_rate"
|
Resource discrepancies
| cat res/go-demo-2-insuf-mem.yml
|
| kubectl apply -f res/go-demo-2-insuf-mem.yml --record
|
| kubectl describe pod go-demo-2-db
|
| cat res/go-demo-2-insuf-node.yml
|
| kubectl apply -f res/go-demo-2-insuf-node.yml --record
|
| kubectl describe pod go-demo-2-db
|
Resource discrepancies 2
| kubectl apply -f res/go-demo-2-random.yml --record
|
| kubectl rollout status deployment go-demo-2-db
|
| kubectl rollout status deployment go-demo-2-api
|
Adjusting resources
| DB_POD_NAME=$(kubectl get pods -l service=go-demo-2 \
-l type=db -o jsonpath="{.items[0].metadata.name}")
|
| curl "$BASE_URL/$DB_POD_NAME/containers/db/metrics/memory/usage"
|
| curl "$BASE_URL/$DB_POD_NAME/containers/db/metrics/cpu/usage_rate"
|
| API_POD_NAME=$(kubectl get pods -l service=go-demo-2 \
-l type=api -o jsonpath="{.items[0].metadata.name}")
|
| curl "$BASE_URL/$API_POD_NAME/containers/api/metrics/memory/usage"
|
| curl "$BASE_URL/$API_POD_NAME/containers/api/metrics/cpu/usage_rate"
|
Adjusting resources 2
| kubectl apply -f res/go-demo-2.yml --record
|
| kubectl rollout status deployment go-demo-2-api
|
QOS
| kubectl describe pod go-demo-2-db
|
| cat res/go-demo-2-qos.yml
|
| kubectl apply -f res/go-demo-2-qos.yml --record
|
| kubectl rollout status deployment go-demo-2-db
|
| kubectl describe pod go-demo-2-db
|
| kubectl describe pod go-demo-2-api
|
| kubectl delete -f res/go-demo-2-qos.yml
|
Defaults & Limitations
| kubectl create namespace test
|
| kubectl -n test create -f res/limit-range.yml \
--save-config --record
|
| kubectl describe namespace test
|
| cat res/go-demo-2-no-res.yml
|
| kubectl -n test create -f res/go-demo-2-no-res.yml \
--save-config --record
|
| kubectl -n test rollout status deployment go-demo-2-api
|
Defaults & Limitations 2
| kubectl -n test describe pod go-demo-2-db
|
| kubectl -n test apply -f res/go-demo-2.yml --record
|
| kubectl -n test get events -w
|
| kubectl -n test run test --image alpine --requests memory=100Mi \
--restart Never sleep 10000
|
| kubectl -n test run test --image alpine --requests memory=1Mi \
--restart Never sleep 10000
|
| kubectl delete namespace test
|
Resource Quotas
| kubectl create -f res/dev.yml --record --save-config
|
| kubectl -n dev describe quota dev
|
| kubectl -n dev create -f res/go-demo-2.yml --save-config --record
|
| kubectl -n dev rollout status deployment go-demo-2-api
|
| kubectl -n dev describe quota dev
|
Resource Quotas 2
| cat res/go-demo-2-scaled.yml
|
| kubectl -n dev apply -f res/go-demo-2-scaled.yml --record
|
| kubectl -n dev get events
|
| kubectl describe namespace dev
|
| kubectl -n dev apply -f res/go-demo-2.yml --record
|
| kubectl -n dev rollout status deployment go-demo-2-api
|
Resource Quotas 3
| cat res/go-demo-2-mem.yml
|
| kubectl -n dev apply -f res/go-demo-2-mem.yml --record
|
| kubectl -n dev get events | grep mem
|
| kubectl describe namespace dev
|
| kubectl -n dev apply -f res/go-demo-2.yml --record
|
| kubectl -n dev rollout status deployment go-demo-2-api
|
| kubectl expose deployment go-demo-2-api -n dev \
--name go-demo-2-api --port 8080 --type NodePort
|
Cleanup