#Config file of Kubernetes can be found at:
~/.kube/config
#### Deploy the dashboard
cp kube.yml ~/.kube/config
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
# Run this and copy the last token:
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
# On the client enter this:
kubectl proxy
# Open in your browser:
http://localhost:8001/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/
and paste the token
###
# Tutorial from https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
#####
# kubectl config view
Check the location and credentials that kubectl knows about
# kubectl cluster-info
# kubectl
CLI tool for interacting with the Kubernetes cluster.
# Checking status of the nodes:
$ kubectl get nodes # if you want additional info then enter:
$ kubectl get nodes -o wide
# You should see that the status='READY' for some of them
# Check status of top node
$ kubectl top node
# Deploy an app in kbs (the image is already in dockerhub)
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
# List the deployment:
$ kubectl get deployments
You obtain something like:
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 97s
# Check the existing pods
kubectl get pods
# Check the containers inside the pods (and what images are used to build those containers)
kubectl describe pods
#Open new terminal and run proxy to talk to the POD
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
# For communicating with the POD we need to 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
# Now we can get the logs of the container in the pod:
kubectl logs $POD_NAME
# Delete a pod by NAME:
kubectl delete pod nginx-7db9fccd9b-4zjdv
# Execute command in the container
kubectl exec $POD_NAME env
# Start bash session in the container:
kubectl exec -ti $POD_NAME bash
# List the services running:
kubectl get services
## Create a new service and expose it to external traffic
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
# Then check that the new service is running:
$ kubectl get services # and we see:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 2d2h
kubernetes-bootcamp NodePort 10.43.120.66 <none> 8080:30346/TCP 74s
#Now, we can inspect this service by doing:
$ kubectl describe services/kubernetes-bootcamp
#Retrieve Node port:
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
#Now we can test that the app is exposed by doing:
$ curl $CLUSTER_PUBLIC_IP:$NODE_PORT #where $CLUSTER_PUBLIC_IP is the IP given by EBI sysadmins
#Now, we can see the label that has been automatically assigned by default to our deployment:
$ kubectl describe deployment # look for label
#We can list the pods in our deployment by using that label:
$ kubectl get pods -l app=kubernetes-bootcamp # Notice the -l
#We can do the same with the services:
kubectl get services -l app=kubernetes-bootcamp
#We can assign a new label:
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ kubectl label pod $POD_NAME app=v1
#We can delete a service:
$ kubectl delete service kubernetes-bootcamp
############
# To see the ReplicaSet created by the deployment
$ kubectl get rs
Two important columns of this command are:
DESIRED displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.
CURRENT displays how many replicas are currently running.
# Scale to 4 replicas:
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
# Delete deployment
kubectl delete deployment nginx # Where nginx is the name of the deployment
#
#
##### Tutorial to deploy app from yaml file:
https://rancher.com/learning-paths/how-to-deploy-your-application-to-kubernetes/
Yaml file:
$ cat testdeploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysite
labels:
app: mysite
spec:
replicas: 1
selector:
matchLabels:
app: mysite
template:
metadata:
labels:
app: mysite
spec:
containers:
- name: mysite
image: kellygriffin/hello:v1
ports:
- containerPort: 80
#And then we can create this deployment:
$ kubectl apply -f testdeploy.yaml
# Check the details of the pod (make note of the container name)
$ kubectl get pods
# Once you have the container name, you can query the web server running on the container’s localhost by typing:
$ kubectl exec -it <your_container_name> curl localhost
You should see something like:
<!DOCTYPE html>
<html>
<head>
<title>Hello World This is Version 1 of our Application</title>
</html>
//
#### Creating a namespace
$ kubectl create namespace test
or with a *.yaml file:
kind: Namespace
apiVersion: v1
metadata:
name: test
labels:
name:test
and then you apply it:
$ kubectl apply -f test.yaml
//
# Command to get all namespaces:
$ kubectl get namespaces
//
# Manage active namespace with kubens:
1) First, install kubens as parte of kubectx
$ brew install kubectx
2) Highlight the default namespace:
$ kubens
3) We can switch to a new namespace:
$ kubens <newnamespace>
//
####### On ServiceAccounts
# List ServiceAccounts
$ kubectl get serviceaccounts #each namespace has a default ServiceAccount
# You can create a new ServiceAccount:
$ kubectl apply -f - <<EOF
apiVersion: v1
kind: ServiceAccount
metadata:
name: build-robot
EOF
# If we do a dump now of the ServiceAccount object we see that an automatic
# token has been generated:
$ kubectl get serviceaccounts/build-robot -o yaml