Macho000

クラスターの作成

## start k8s
minikube start

## see running cluster-info
kubectl cluster-info 

## shows all nodes that can be used to host application
kubectl get nodes 

アプリケーションのデプロイ

## deploy app on kubernetes
kubectl create deployment 

## deploy and set app image location
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

## list your deployments
kubectl get deployments

Open second window

# to run proxy
echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output a response. Please click the first Terminal Tab\n"; 
kubectl proxy

and then we have a connection between our host and k8s cluster.

# see all those APIs through the proxy endpoint
curl http://localhost:8001/version

# get the pod name
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

# access the pod through the API 
curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/

アプリケーションの探索

# retrieve logs
kubectl logs $POD_NAME

# execute commands directly on the container
kubectl exec $POD_NAME -- env

# start a bash session in the Pod's container
kubectl exec -ti $POD_NAME -- bash

# see source code
cat server.js

# check the application output
curl localhost:8080

アプリケーションの公開

# list the current Services from our cluster
kubectl get services

# create a new service and expose it to external traffic
kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

# find out what port was opened externally
kubectl describe services/kubernetes-bootcamp

# create an environment variable that has node port value
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT

# test the app is exposed outside of the cluster
curl $(minikube ip):$NODE_PORT

# see the created label
kubectl describe deployment

# use the label to query our list of Pods
kubectl get pods -l app=kubernetes-bootcamp

# use the label to query our list of Services
kubectl get services -l app=kubernetes-bootcamp

# get the name of the pod and store it to POD_NAME
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
echo Name of the Pod: $POD_NAME

# apply a new label
kubectl label pods $POD_NAME version=v1

# check the new label
kubectl describe pods $POD_NAME

# check the label is attached to our Pod
kubectl get pods -l version=v1

# delete service
kubectl delete service -l app=kubernetes-bootcamp

# Confirm that the service is gone:
kubectl get services

# confirm that route is not exposed anymore 
# ---> app is not reachable
curl $(minikube ip):$NODE_PORT

# confirm that the app is still runnning inside the pod
kubectl exec -ti $POD_NAME -- curl localhost:8080

オートスケーリング

# see the ReplicaSet created by the Deployment
kubectl get rs

# scale to 4 replicas
kubectl scale deployments/kubernetes-bootcamp --replicas=4

#kubernetes