Kubernetes cluster
Creating clusters
#To setup the
project and cluster
gcloud config set
project peaceful-signer-194319
gcloud config set
compute/zone us-central1-a
#get the Kubernetes
Engine server config
gcloud container
get-server-config --zone us-central1-a
#get only the
default Kubernetes Engine server version
gcloud container
get-server-config --zone us-central1-a pipe grep
defaultClusterVersion:
#create (start) a
cluster on Google Cloud Platform with 2 nodes and cluster version
1.9.2
gcloud container
clusters create mycluster --num-nodes=2 --cluster-version 1.9.2-gke.1
#to get the cluster
credentials and configure the kubectl command-line tool
gcloud container
clusters get-credentials mycluster
#to start Minikune
on windows
minikube start
--kubernetes-version v1.8.0 --VM-driver hyperv
--hyperv-virtual-switch "Primary Virtual Switch"
Resizing a cluster
#setup the project
and cluster
gcloud config
set project [PROJECT_ID]
gcloud config
set compute/zone [COMPUTE/ZONE]
gcloud config set
project peaceful-signer-194319
gcloud config
set compute/zone us-central1-a
#to
create(start) a cluster on GCP with 2 nodes using the default cluster
version
gcloud container
clusters create mycluster --num-nodes=2
#List existing
clusters in the default zone for running containers
gcloud container
clusters list
#Resize the
cluster, We will change it to one single Node in the Node pool
gcloud container
clusters resize mycluster --size 1
#To list the GCP
K8 cluster
gcloud container
clusters list
Stopping a Cluster
#Although you
can’t really ‘stop’ a cluster on GCP you can ensure that no
Pods are going
# to be deployed
by setting the number of nodes to 0
gcloud container
clusters resize mycluster –size=0
#To list the GCP
K8 cluster
gcloud container
clusters list
#for Minikube on
windows
kubectl version
kubectl get all
minikube stop
Deleting a Cluster
# Delete an
existing cluster
# --zone
overrides the default COMPUTE/ZONE setting
# --async
doesn’t wait for the operation to complete returns to the prompt
immediately
# there are also
GCLOUD WIDE FLAGS like –project, --quiet, --account and so on
glcoud container
clusters delete mycluster –zone=us-centrall-a –async
#for Minikube
minikube stop
minikube delete
#rm -rf
~/.minikube for Linux
Upgrading a Cluster
# list existing
clusters in the default zone
gcloud container
clusters list
# to fine the
supported Kubernetes master and node versions for upgrades
gcloud container
get-server-config
#To upgrade the
cluster version
gcloud container
clusters upgrade mycluster --master --cluster-version 1.9.2-gke.1
#To upgrade
nodes
gcloud container
clusters upgrade mycluster
#minikube
upgrade version
minikube
get-k8-versions
minikube start
--kubernetes-version v1.8.0 --VM-driver hyperv
--hyperv-virtual-switch "Primary Virtual Switch"
minikube stop
minikube start
--kubernetes-version v1.9.0 --VM-driver hyperv
--hyperv-virtual-switch "Primary Virtual Switch"
kubectl version
| sls "Server Version:"
Understanding Kubeconfig
#To setup the
project and cluster
gcloud config set
project peaceful-signer-194319
gcloud config set
compute/zone us-central1-a
#get the Kubernetes
Engine server config
gcloud container
get-server-config --zone us-central1-a
#create (start) a
cluster on Google Cloud Platform with 2 nodes and cluster version
1.9.2
gcloud container
clusters create mycluster --num-nodes=2
gcloud container
clusters create mycluster-dev --num-nodes=2
# here is where
the default is kubeconfig file is located and it’s names config
ls ~/.kube
# if you want to
look at the file, you can ‘cat ~/.kube/config’, just be aware
that your
#
certificate-authority-data will be in full view if you do
kubectl config
view
#kubectl config
SUBCOMMAND
kubectl config
–kubeconfig=d-config get-clusters
kubectl config
–kubeconfig=p-config get-clusters
kubectl config
get-clusters
#kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#config
Sharing a Cluster
kubectl get
namespaces
# Start minikube
minikube start
--kubernetes-version v1.9.0 --VM-driver hyperv
--hyperv-virtual-switch "Primary Virtual Switch"
#alter your
config located at ~/.kube/config
#create two new
namespaces
kubectl create
-f C:\os_kube\course4\121653\ns-dev.yaml
kubectl create
-f C:\os_kube\course4\121653\ns-test.yaml
kubectl get
namespaces –show-labels
#set some
contexts in the configuration for the cluster
kubectl config
set-context dev --namespace=dev --cluster=minikube –user=Joe-dev
kubectl config
set-context test --namespace=test --cluster=minikube –user=Joe-test
kubectl config
use-context dev
kubectl config
current-context
#create a
deployment in the dev context
kubectl run
nginx --image=nginx –replicas=3
#now switch
contexts to test, and see whether we can see the deployment
kubectl config
use-context test
kubectl get all
ns-dev.yaml
------------
apiVersion:v1
kind: Namespace
metadata:
name: dev
labels:
name: dev
ns-test.yaml
------------
apiVersion:v1
kind: Namespace
metadata:
name: test
labels:
name: test
Authentication Clusters
#set project and
cluster
gcloud config
set project peaceful-signer-194319
gcloud config
set compute/zone us-central1-a
# On GCP add a
user with IAM and give user the role Kubernetes Engine Viewer
# let’s look
at the iam policy for this project
gcloud projects
get-iam-policy peaceful-signer-194319 --format json > iam.json
Creating a Alpha Cluster
# Alpha cluster
are not for productions
# they can’t
upgraded and automatically deleted after 30 days
# To create
alpha cluster
gcloud
alpha container clusters create alphacluster
--enable-kubernetes-alpha –cluster-version=1.9.2-gke.1
# you can get
the authentication credentials for the cluster
gcloud
alpha container clusters get-credentials alphacluster
#later to list
the cluster and see when the alpha clusters expire
gcloud alpha
container clusters list
Using Cluster Autoscaler
#set project and
cluster
gcloud config
set project peaceful-signer-194319
gcloud config
set compute/zone us-central1-a
# To create an
autoscaling cluster use ‘cloud container clusters create’
# ‘--enable
autoscaling’ enables autoscalin
# --min-nodes
and --max-nodes are the minimum number of nodes and the maximum
number of nodes
gcloud container
clusters create mycluster --num-nodes 1 --enable-autoscaling
--min-nodes 1 --max-nodes 5
#now let’s
deploy to the deployment, removing all but 1 pod
# then check the
deplyment and nodes
# you’ll see
the nodes scale back in a few minues
kubectl create
-f deployment.yaml
kubectl get
nodes
kubectl get
deploy
#now let’s
apply a change to the deployment, removing all but 1 pod
# then check the
deplyment and nodes
# you’ll see
the nodes scale back in a few minues
kubectl apply -f
alterdeployment.yaml
kubectl get
nodes
kubectl get
deploy
# To disable
autoscaling for a specific node pool
#
‘--no--enable-autoscaling’ tell the cluster to disable
autoscaling
# this set the
cluster size at its current default node pool size. And can still be
manually updated.
gcloud container
clusters update mycluster –no-enable-autoscaling –node-pool
default-pool