(How to setup kubectl and minikube) |
|||
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
== K8S on Minikube == | == K8S on Minikube == | ||
I have | I have setup my Kubernetes environment locally. For this setup i am using a HP Workstation as this is the best option i have to do something local. To save on costs running this in the cloud i bought a secondhand machine<br> | ||
I do intend to also rollout images on AWS but that will follow in a later stage<br> | |||
Taking my notes here, based on information from Kubernetes.io The setup is described for a Linux environment, ofcourse. The instruction apply to MacOs as well, just download the installers for OSX.<br> | Taking my notes here, based on information from Kubernetes.io The setup is described for a Linux environment, ofcourse. The instruction apply to MacOs as well, just download the installers for OSX.<br> | ||
Line 14: | Line 14: | ||
</code> | </code> | ||
=== Virtualbox === | |||
Next, make sure Virtualbox is installed, find it here for your OS: https://www.virtualbox.org/wiki/Downloads.<br> | Next, make sure Virtualbox is installed, find it here for your OS: https://www.virtualbox.org/wiki/Downloads.<br> | ||
First add the repo to your sources.list and import the key<br> | |||
Add the following line to /etc/apt/sources.list<br> | |||
<code> | |||
deb http://ftp.debian.org/debian stretch-backports main contrib | |||
</code><br> | |||
Download the key and import it<br> | |||
<code> | |||
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | apt-key add - | |||
</code><br> | |||
Update apt and next install virtualbox<br> | |||
<code> | |||
apt install virtualbox-5.2 | |||
</code><br> | |||
=== Kubectl === | === Kubectl === | ||
Get the installer and make it executable, | Get the installer and make it executable, commands a run as root user<br> | ||
LINUX | LINUX<br> | ||
<code> | <code> | ||
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod + x kubectl | curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod +x kubectl | ||
</code><br> | </code><br> | ||
OSX | |||
OSX <br> | |||
<code> | <code> | ||
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl" | curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl" | ||
Line 33: | Line 51: | ||
</code><br> | </code><br> | ||
=== | === Minikube === | ||
Do the same thing for | Do the same thing for minikube<br> | ||
LINUX | |||
LINUX<br> | |||
<code> | <code> | ||
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube- | curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube | ||
</code><br> | </code><br> | ||
OSX | |||
OSX<br> | |||
<code> | <code> | ||
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 | curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube | ||
</code><br> | </code><br> | ||
Now move it into your path<br> | Now move it into your path<br> | ||
<code> | <code> | ||
mv | mv minikube /usr/local/bin | ||
</code><br> | </code><br> | ||
Line 74: | Line 93: | ||
kubeconfig: Configured | kubeconfig: Configured | ||
</pre><br> | </pre><br> | ||
Check if kubectl can access the cluster<br> | Check if kubectl can access the cluster<br> | ||
<code> | <code> | ||
Line 85: | Line 103: | ||
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump' | To further debug and diagnose cluster problems, use 'kubectl cluster-info dump' | ||
</pre><br> | </pre><br> | ||
So, you are good to go from here and make a deployment<br> | So, you are good to go from here and make a deployment<br> | ||
Line 91: | Line 108: | ||
Just a small hello world kind of test and exposing to local port 8888<br> | Just a small hello world kind of test and exposing to local port 8888<br> | ||
Create the deployment | === Create the deployment === | ||
Create the deployment of hello-minikube<br> | |||
<code> | <code> | ||
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10 | kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10 | ||
</code><br> | </code><br> | ||
=== Expose the deployment === | |||
Expose the deployment to local port 8888 | Expose the deployment to local port 8888<br> | ||
<code> | <code> | ||
kubectl expose deployment hello-minikube --type=NodePort --port=8888 | kubectl expose deployment hello-minikube --type=NodePort --port=8888 | ||
</code><br> | </code><br> | ||
=== Check Status === | |||
Now, we can check for the deployment which is a pod<br> | Now, we can check for the deployment which is a pod<br> | ||
<code> | <code> | ||
kubectl get pod | kubectl get pod | ||
</code><br> | </code><br> | ||
This will produce you with some info:<br> | This will produce you with some info:<br> | ||
<pre> | <pre> | ||
NAME READY STATUS RESTARTS AGE | NAME READY STATUS RESTARTS AGE | ||
hello-minikube-64b64df8c9-krr6p 0/1 ContainerCreating 0 37s | hello-minikube-64b64df8c9-krr6p 0/1 ContainerCreating 0 37s | ||
</pre> | </pre><br> | ||
To see where the service is running:<br> | |||
To see where is running<br> | |||
<code> | <code> | ||
minikube service hello-minikube --url | minikube service hello-minikube --url | ||
</code> | </code><br> | ||
This shows you the url:<br> | |||
<pre> | <pre> | ||
http://192.168.99.100:32463 | http://192.168.99.100:32463 | ||
</pre> | </pre> | ||
=== End of the show === | |||
When you have stopped enjoying the magic:<br> | When you have stopped enjoying the magic:<br> | ||
<code> | <code> | ||
Line 131: | Line 150: | ||
<code> | <code> | ||
kubectl delete services hello-minikube | kubectl delete services hello-minikube | ||
</code> | </code><br> | ||
To remove the minikube cluster<br> | To remove the minikube cluster<br> | ||
<code> | <code> | ||
minikube delete | |||
</code><br> | |||
This completes the instruction to setup a local test environment. I will continue to add examples and tips&tricks soon.<br> | |||
</ | == Scaling Applications == | ||
Jumping ahead a little bit now to scaling of applications in Kubernetes.<br> | |||
<pre> | |||
- Scaling in Kubernetes is done using the Replication Controller. | |||
- The replication controller will ensure a number of replicas is running at all times. | |||
- Pods created will automatically be replaced upon failure, termination or delete. | |||
- It can also be used to make sure a pod will be running even after a reboot. | |||
</pre> | |||
=== Configuration Example === | |||
This is done by changing your configuration a little. Here we will run 2 replicas of the demo app i created preliminary and have put on docker hub.<br> | |||
<pre> | |||
apiVersion: v1 | |||
kind: Replicationcontroller | |||
metadata: | |||
name: demo | |||
spec: | |||
replicas: 2 | |||
selector: | |||
app:demo | |||
template: | |||
metadata: | |||
labels: | |||
app: demo | |||
spec: | |||
containers: | |||
- name: demo | |||
image: phenixops/demo | |||
ports: | |||
- containerPort: 80 | |||
</pre> | |||
[[Category:Kubernetes]] | [[Category:Kubernetes]] |
Latest revision as of 22:41, 1 February 2023
K8S on Minikube
I have setup my Kubernetes environment locally. For this setup i am using a HP Workstation as this is the best option i have to do something local. To save on costs running this in the cloud i bought a secondhand machine
I do intend to also rollout images on AWS but that will follow in a later stage
Taking my notes here, based on information from Kubernetes.io The setup is described for a Linux environment, ofcourse. The instruction apply to MacOs as well, just download the installers for OSX.
Pre check and dependencies
First you need to be sure your machine is supporting Virtualization, otherwise all effort is lost.
VMX or SVM should be highlighted, provided you run Linux.
grep -E --color 'vmx|svm' /proc/cpuinfo
Virtualbox
Next, make sure Virtualbox is installed, find it here for your OS: https://www.virtualbox.org/wiki/Downloads.
First add the repo to your sources.list and import the key
Add the following line to /etc/apt/sources.list
deb http://ftp.debian.org/debian stretch-backports main contrib
Download the key and import it
wget -q https://www.virtualbox.org/download/oracle_vbox_2016.asc -O- | apt-key add -
Update apt and next install virtualbox
apt install virtualbox-5.2
Kubectl
Get the installer and make it executable, commands a run as root user
LINUX
curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl && chmod +x kubectl
OSX
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
Move it to the right dir in your path
mv kubectl /usr/local/bin
Minikube
Do the same thing for minikube
LINUX
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
OSX
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && chmod +x minikube
Now move it into your path
mv minikube /usr/local/bin
Check Setup
Now switch back to your login user and try if things are working
This will download some images and install them using Virtualbox.
Start Minikube
minikube start
Check the status
minikube status
which will provide you with info:
minikube type: Control Plane host: Running kubelet: Running apiserver: Running kubeconfig: Configured
Check if kubectl can access the cluster
kubectl cluster-info
Kubernetes master is running at https://192.168.99.100:8443 KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'
So, you are good to go from here and make a deployment
Hello World
Just a small hello world kind of test and exposing to local port 8888
Create the deployment
Create the deployment of hello-minikube
kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.10
Expose the deployment
Expose the deployment to local port 8888
kubectl expose deployment hello-minikube --type=NodePort --port=8888
Check Status
Now, we can check for the deployment which is a pod
kubectl get pod
This will produce you with some info:
NAME READY STATUS RESTARTS AGE hello-minikube-64b64df8c9-krr6p 0/1 ContainerCreating 0 37s
To see where the service is running:
minikube service hello-minikube --url
This shows you the url:
http://192.168.99.100:32463
End of the show
When you have stopped enjoying the magic:
minikube stop
To delete all the good work
Removing just the deployment of hello-minikube
kubectl delete services hello-minikube
To remove the minikube cluster
minikube delete
This completes the instruction to setup a local test environment. I will continue to add examples and tips&tricks soon.
Scaling Applications
Jumping ahead a little bit now to scaling of applications in Kubernetes.
- Scaling in Kubernetes is done using the Replication Controller. - The replication controller will ensure a number of replicas is running at all times. - Pods created will automatically be replaced upon failure, termination or delete. - It can also be used to make sure a pod will be running even after a reboot.
Configuration Example
This is done by changing your configuration a little. Here we will run 2 replicas of the demo app i created preliminary and have put on docker hub.
apiVersion: v1 kind: Replicationcontroller metadata: name: demo spec: replicas: 2 selector: app:demo template: metadata: labels: app: demo spec: containers: - name: demo image: phenixops/demo ports: - containerPort: 80