Kubernetes hello world
Kubernetes
Kubernetes is a system for deployment and management of containers.
https://www.youtube.com/watch?v=HlAXp0-M6SY
On mac one can install it with
brew install kubectl
brew install kubernetes-cli
It is recommended to add aliases to shell. For example, for zshell
with ohmyzsh
you can do this
by adding kubectl
to plugins
in .zshrc
:
plugins=(... kubectl)
Then you can simply write k
instead of kubectl
in what follows. See
https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/kubectl
for more aliases.
minikube
We are going to use minikube. On mac you can install it with:
brew install minikube
In order to start, run:
minikube start
Check the status
minikube status
or run a dashboard
minikube dashboard
Run simple docker container instance with kubectl
https://kubernetes.io/docs/reference/kubectl/cheatsheet/
kubectl run nginx --image=nginx --generator=run-pod/v1
kubectl get pods -o wide
kubectl describe pods nginx
More about pods https://kubernetes.io/docs/concepts/workloads/pods/pod/.
Create service
You can get to pod by forwarding the port by
kubectl port-forward nginx 8081:80
However, the best way is to create a service:
kubectl expose pod nginx --port 80 --type LoadBalancer
kubectl get services
Run it
minikube service nginx
Running custom docker container
If you have crated a docker container and push it docker hub you can run then in k8s, see Docker: Hello World for instructions.
You can run it in the same imperative way as before:
kubectl run flaskhelloworld --image=barteks/flaskhelloworld\
--generator=run-pod/v1
kubectl expose pod flaskhelloworld --port 80 --type LoadBalancer
There is also declarative way to do this. First create a file
flaskhelloworld-pod.yaml
apiVersion: v1
kind: Pod
metadata:
labels:
run: flaskhelloworld
name: flaskhelloworld
spec:
containers:
- name: flaskhelloworld
image: barteks/flaskhelloworld:latest
ports:
- containerPort: 80
Run kubectl explain pod
for more info.
Now you can create pod with:
kubectl apply -f flaskhelloworld-pod.yaml
Then for service, create file flaskhelloworld-svc.yaml
:
apiVersion: v1
kind: Service
metadata:
name: flaskhelloworld
namespace: default
spec:
ports:
- nodePort: 31182
port: 80
protocol: TCP
targetPort: 80
selector:
run: flaskhelloworld
type: LoadBalancer
And for creating service you can run:
kubectl apply -f flaskhelloworld-svc.yaml
Now run service and open port for minikube:
minikube service flaskhelloworld
If your file is in github you can try:
kubectl apply -f https://raw.githubusercontent.com/sbartek/sample_flask_app/master/flaskhelloworld/flaskhelloworld-pod.yaml
kubectl apply -f https://raw.githubusercontent.com/sbartek/sample_flask_app/master/flaskhelloworld/flaskhelloworld-svc.yaml
Useful commands
Login to shell
kubectl exec -it <pod_name> -- sh
What is running
You can see what is installed on you k8s custer by:
kubectl get all
Clean up
You can delete service/deployment etc by
kubectl delete service nginx
kubectl delete deployment nginx
Then you can stop and delete minikube by:
minikube stop
minikube delete
Links
Updated: 2019-12-21