https://kubernetes.io/docs/tutorials/kubernetes-basics/
minikube start minikube dashboard
Create Deployment
kubectl create deployment hello-node –image=registry.k8s.io/e2e-test-images/agnhost:2.39 – /agnhost netexec –http-port=8080 kubectl get deployments kubectl get pods kubectl get events kubectl config view kubectl logs hello-node-5f76cf6ccf-br9b5
Expose pod as a service:
kubectl expose deployment hello-node –type=LoadBalancer –port=8080 kubectl get services
minikube service hello-node # access service that was exposed on minikube, on normal cloud it would create an LB
minikube addons list minikube addons enable metrics-server kubectl get pod,svc -n kube-system # show metrics-server running kubectl top pods # show pod CPU and mem, needs metrics-server minikube addons disable metrics-server
kubectl delete service hello-node kubectl delete deployment hello-node minikube stop
==============================================================================
- pods run on a private, isolated network and can see each other
- containers in a Pod share an IP Address and port space
kubectl create deployment kubernetes-bootcamp –image=gcr.io/google-samples/kubernetes-bootcamp:v1
kubectl get pods kubectl get deployments kubectl describe pods kubectl describe deployments
kubectl logs kubernetes-bootcamp-644c5687f4-v29xk # show pod logs kubectl exec kubernetes-bootcamp-644c5687f4-v29xk – env # check pod env kubectl exec -ti kubernetes-bootcamp-644c5687f4-v29xk – bash # get shell on a pod
cat server.js # check the code within the pod curl http://localhost:8080 # check the app from within the pod exit # exit the pod
Alternate way to grab pod name:
export POD_NAME=$(kubectl get pods -o go-template –template ‘\n’) echo Name of the Pod: $POD_NAME
Viewing with a proxy:
kubectl proxy # create a proxy that will forward communications into the cluster-wide, private network # !!!! runs in the foreground
curl http://localhost:8001/version # See APIs hosted through proxy
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME:8080/proxy/ # access app through proxy
=============================================================== Services / Exposing Ports
Kubernetes object manifests
Services types:
ClusterIP (default) - internal cluster IP NodePort - exposed on the node, for each node ( NodeIP:NodePort ) LoadBalancer - external LB in cloud, fixed, external IP ExternalName - maps to DNS name ??? need more info ???
service with a selector | v Deployment ReplicaSet Labeled Pods
kubectl get pods kubectl get services kubectl expose deployment/kubernetes-bootcamp –type=”NodePort” –port 8080 kubectl get services
Find which port was opened:
kubectl describe services/kubernetes-bootcamp
- should have new internal and external ports ??? - looks like - internal, balanced on new IP, same port - nodeport, node ip / node port ( new port )
Test access outside of cluster:
export NODE_PORT=”$(kubectl get services/kubernetes-bootcamp -o go-template=’’)” echo “NODE_PORT=$NODE_PORT” curl http://”$(minikube ip):$NODE_PORT”
=============================================================== Labeling things:
kubectl describe deployment kubectl get pods -l app=kubernetes-bootcamp # use label to query pod list kubectl get services -l app=kubernetes-bootcamp # use label to query service list
Get pod name:
export POD_NAME=”$(kubectl get pods -o go-template –template ‘\n’)” echo “Name of the Pod: $POD_NAME”
kubectl label pods “$POD_NAME” version=v1 # apply new label to pod kubectl describe pods “$POD_NAME” # check pod label kubectl get pods -l version=v1 # list pods with this label
kubectl delete service -l app=kubernetes-bootcamp # delete service kubectl get services # verify
curl http://”$(minikube ip):$NODE_PORT” # verify can’t connect from host kubectl exec -ti $POD_NAME – curl http://localhost:8080 # verify can still connect from inside pod
=============================================================== Scaling
kubectl expose deployment/kubernetes-bootcamp –type=”LoadBalancer” –port 8080
???? is the built in LB of a scaled service different from an external LB ?????
- looks like node port is ballanced, at least on a per node basis. So each pod on a node is ballenced behind that nodeport.
kubectl get deployments
NAME lists the names of the Deployments in the cluster. READY shows the ratio of CURRENT/DESIRED replicas UP-TO-DATE displays the number of replicas that have been updated to achieve the desired state. AVAILABLE displays how many replicas of the application are available to your users. AGE displays the amount of time that the application has been running.
kubectl get rs # show replica sets
kubectl scale deployments/kubernetes-bootcamp –replicas=4 # scale it up
kubectl get deployments # verify kubectl get pods -o wide # verify pods increased kubectl describe deployments/kubernetes-bootcamp # view the deployment, should have more pods kubectl describe services/kubernetes-bootcamp # find the exposed IP and port
Get the node port:
export NODE_PORT=”$(kubectl get services/kubernetes-bootcamp -o go-template=’’)” echo NODE_PORT=$NODE_PORT
curl http://”$(minikube ip):$NODE_PORT” # execute multiple times, should hit different pods
kubectl scale deployments/kubernetes-bootcamp –replicas=2 # scale down kubectl get deployments # verify kubectl get pods -o wide # verify
=============================================================== Rolling release
kubectl get pods # list pods kubectl describe pods # show image version on pods
Update image used by the deployment, will do a rolling release:
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=docker.io/jocatalin/kubernetes-bootcamp:v2
kubectl get pods # verify new pods, watch old pods terminate
curl http://”$(minikube ip):$NODE_PORT” # different pod each time, should be new version kubectl rollout status deployments/kubernetes-bootcamp # confirm the update kubectl describe pods # verify version
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
kubectl get deployments # failed, not desired number of pods kubectl get pods # failed, some pods have failed kubectl describe pods # shows event error, can’t pulll image
kubectl rollout undo deployments/kubernetes-bootcamp # roll back - revert to previous known state kubectl get pods # verify kubectl describe pods # verify
Delete cluster:
kubectl delete deployments/kubernetes-bootcamp services/kubernetes-bootcamp
=======================================================
kubectl apply -f https://k8s.io/examples/service/load-balancer-example.yaml
================================================================================
========================================
nginx-deployment.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80
kubectl apply -f nginx-deployment.yaml
kubectl get deployments kubectl get pods