We will get to know of deployments, services, and registry inside Kubernetes with the help of microk8s. Part of my
Kubernetes course. First, enable the registry and dns addons:
We need a registry because it will store our newly created application image.
And then a DNS to ensure that the pods and services inside of the cluster can communicate effectively.
microk8s.enable registry, dns
then check with
microk8s.status to see if they are enabled
and:
microk8s.kubectl get all --all-namespaces
to see if the pods are running
Also, we can check inside the registry pod all the messages, emitted during the pod creation. For this and with the information for the specific pod id of the registry from the previous command just type
microk8s.kubectl -n container-registry describe pod registry-xxxxxxx-xxxx
in case of problems:
1) edit your /etc/hosts file and comment the ipv6 entry
#::1 ip6-localhost ip6-loopback
2) check if the service is listening on port 32000 with:
sudo lsof -i:32000
get the
CLUSTER-IP address from the registry and access it with port
5000
from the browser try to access the catalog of images offered:
http://xxx.xxx.xxx.xxx:5000/v2/_catalog
Note: The container registry is supported by Docker.
Now it is time to push our docker image inside the registry:
1) List the images:
docker image ls
and find our application image there.
Note: In order to be able to use the local Docker/Kubernetes registry the image has to be with tag:
localhost:32000/image-name
for this just get the
image_id from docker image ls and use the following command:
docker image tag
image_id localhost:32000/image-name:v1
2) Push the name of our application container into the registry
docker push localhost:32000/image-name:v1
3) Check inside the browser the same:
http://xxx.xxx.xxx.xxx:5000/v2/_catalog
and also:
http://xxx.xxx.xxx.xxx:5000/v2/image-name/tags/list
We will now use the image from the registry and based on this image will create a deployment:
node-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: node-deployment
labels:
app: simple-api
spec:
replicas: 2
selector:
matchLabels:
app: simple-api
template:
metadata:
labels:
app: simple-api
spec:
containers:
- name: simple-api
image:
localhost:32000/image-name:v1 ports:
- containerPort:
3000
As you can see every container will have a name:simple-api, on this base, new pods will be created with the
app=simple-api label.
Finally, our deployment (also tagged with app=simple-api) will create replicas of the pods who match the label:
app=simple-api
We can also see that we are using/referencing the image
localhost:32000/image-name:v1 straight from our registry as well as exposing port 3000 of our node application to be accessible outside of the
container.
Let's now run the .yaml manifest with
microk8s.kubectl apply -f node-deployment.yaml
Note: If you experience problems with the registry just enable these firewall rules:
sudo ufw allow in on cbr0 && sudo ufw allow out on cbr0sudo ufw default allow routedsudo iptables -P FORWARD ACCEPT
as well as restart the docker daemon:
sudo systemctl restart docker
Now, when the deployment is successful, we should have 2 pods created, that we can see from
microk8s.kubectl get pods --all-namespaces.
We can now enter inside of each by using:
microk8s.kubectl exec -it node-deployment-xxxxxxxxxx-xxxx -- /bin/bash
and here we can test the network connectivity as well as see the files.
Ok, let's now reveal our beautiful application outside of the deployment pods with the help of a service. And here is its yaml file:
node-service.yaml
apiVersion: v1
kind: Service
metadata:
name: node
spec:
type: NodePort
ports:
- port:
30000
targetPort:
3000 selector:
app: simple-api
Ok, as you can see here we are matching all pods which are created by deployments having labels of app=simple-api !
Then we are using NodePort type of service, which means that we target
inside of the container port 3000 and exposing it outside as 30000 (as from the service)
Ok, let's apply the service with
microk8s.apply -f node-service.yaml
Let's run again:
microk8s.kubectl get pods --all-namespaces and we see that we've got a
CLUSTER-IP assigned from Kubernetes - this is an IP assigned to our service, which we can use with the port of 30000. So go ahead and browse:
http://your_CLUSTER-IP:30000
Since our cluster
Node is running on localhost, we see from the output another (this time randomly) assigned port next to 30000:
34467. We can use this
NodePort to access our cluster with
http://127.0.0.1:34467
Let's see our endpoints(several pods) which backup and are responsible for our service:
microk8s.kubectl get endpoints
We can access these endpoints from our browser.
The benefit we get is that: multiple pods(endpoints) can be accessed from a single location, we just have to know and point to it by its name. And this is our
service!
Congratulations!