Today I continued with a Kubernetes training on Udemy, thanks Bret. After launching a bunch of commands in the terminal, I decided to create this post for when I need to remember these concepts again.
There are currently 3 types, ClusterIP, NodePort and LoadBalancer. Well, there is also the Ingress type, but we’ll leave it for now.
Working with clusterIP type in k8s, firstly I will create a deployment object with simple http server, using my-httpenv as name. With Deployment object, k8s create a pod.
kubectl create deployment my-httpenv --image=bretfisher/httpenv
Getting info from k8s:[lun 21/08/30 11:26 CEST][s000][x86_64/darwin20.0/20.6.0][5.8]
zsh 3649 % kubectl get pods -w
NAME READY STATUS RESTARTS AGE
my-httpenv-67cf6c9747-4qz7m 0/1 Pending 0 0s
my-httpenv-67cf6c9747-4qz7m 0/1 Pending 0 0s
my-httpenv-67cf6c9747-4qz7m 0/1 ContainerCreating 0 0s
my-httpenv-67cf6c9747-4qz7m 1/1 Running 0 5s
scale it to five replicas…[lun 21/08/30 11:34 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]
zsh 3652 % kubectl scale deployment my-httpenv --replicas=5
deployment.apps/my-httpenv scaled
creating the service using my-httpenv as name, using expose command. Creating a clusterIp by default service.
[lun 21/08/30 11:34 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]zsh 3653 % kubectl expose deployment my-httpenv --port 8888
service/my-httpenv exposed
Trying to run a container with linux utils commands, but not working because is not longer compatible in my Docker Desktop version.
[lun 21/08/30 11:38 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]
zsh 3655 % kubectl run --generator run-pod/v1 tmp-shell --rm -it --image bretfisher/netshoot -- bash
Error: unknown flag: --generator
See 'kubectl run --help' for usage.
Running container with linux utils, like curl[lun 21/08/30 11:49 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]
zsh 3662 % kubectl run --rm -it --image bretfisher/netshoot -- bash
If you don't see a command prompt, try pressing enter.root @ /
[1] 🐳 →
Running curl to test already created pod. my-httpenv is the name of the serviceroot @ /
[1] 🐳 → curl my-httpenv:8888
{"HOME":"/root","HOSTNAME":"my-httpenv-67cf6c9747-dgqcb","KUBERNETES_PORT":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.96.0.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.96.0.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
of course you can use ip address to curl it
[lun 21/08/30 12:26 CEST][s002][x86_64/darwin20.0/20.6.0][5.8]
zsh 1009 master% kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 45d
my-httpenv ClusterIP 10.104.67.45 8888/TCP 60m
root @ /
[6] 🐳 → curl 10.104.67.45:8888
{"HOME":"/root","HOSTNAME":"my-httpenv-67cf6c9747-c6kll","KUBERNETES_PORT":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.96.0.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.96.0.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
What do i have actually?zsh 1011 master% kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-httpenv-67cf6c9747-4qz7m 1/1 Running 0 82m
pod/my-httpenv-67cf6c9747-65v4r 1/1 Running 0 76m
pod/my-httpenv-67cf6c9747-c6kll 1/1 Running 0 76m
pod/my-httpenv-67cf6c9747-dgqcb 1/1 Running 0 76m
pod/my-httpenv-67cf6c9747-mg6p7 1/1 Running 0 76m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 45d
service/my-httpenv ClusterIP 10.104.67.45 8888/TCP 74m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-httpenv 5/5 5 5 82m
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-httpenv-67cf6c9747 5 5 5 82m
Creating service of type NodePort using expose deployment, with previous already created service, my-httpenv.[lun 21/08/30 13:01 CEST][s003][x86_64/darwin20.0/20.6.0][5.8]
zsh 1018 [1] master% kubectl expose deployment my-httpenv --port 8888 --name my-httpenv-np --type NodePort
service/my-httpenv-np exposed
Checking if new NodePort service is up and running… 8888:32381, 8888 is internal port, 32381 is external portzsh 1020 [1] master% kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-httpenv-67cf6c9747-4qz7m 1/1 Running 0 95m
pod/my-httpenv-67cf6c9747-65v4r 1/1 Running 0 89m
pod/my-httpenv-67cf6c9747-c6kll 1/1 Running 0 89m
pod/my-httpenv-67cf6c9747-dgqcb 1/1 Running 0 89m
pod/my-httpenv-67cf6c9747-mg6p7 1/1 Running 0 89m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 45d
service/my-httpenv ClusterIP 10.104.67.45 8888/TCP 87m
service/my-httpenv-np NodePort 10.110.61.178 8888:32381/TCP 3m4s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-httpenv 5/5 5 5 95m
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-httpenv-67cf6c9747 5 5 5 95m
Accessing to my-httpenv-np from outside worl, in this case, from my mac,
i am using localhost because Docker Dekstop uses vpnkit to access to 10.110.61.178 address.
in linux you can use ip address.
[lun 21/08/30 13:04 CEST][s003][x86_64/darwin20.0/20.6.0][5.8]
zsh 1021 master% curl localhost:32381
{"HOME":"/root","HOSTNAME":"my-httpenv-67cf6c9747-4qz7m","KUBERNETES_PORT":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP":"tcp://10.96.0.1:443","KUBERNETES_PORT_443_TCP_ADDR":"10.96.0.1","KUBERNETES_PORT_443_TCP_PORT":"443","KUBERNETES_PORT_443_TCP_PROTO":"tcp","KUBERNETES_SERVICE_HOST":"10.96.0.1","KUBERNETES_SERVICE_PORT":"443","KUBERNETES_SERVICE_PORT_HTTPS":"443","PATH":"/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}
Creating service of type LoadBalancer
[lun 21/08/30 13:15 CEST][s003][x86_64/darwin20.0/20.6.0][5.8]
zsh 2046 master% kubectl expose deployment my-httpenv --port 8888 --name my-httpenv-lb --type LoadBalancer
service/my-httpenv-lb exposed
just checking…
[lun 21/08/30 18:07 CEST][s003][x86_64/darwin20.0/20.6.0][5.8]
zsh 2050 master% kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 45d
my-httpenv ClusterIP 10.104.67.45 8888/TCP 6h30m
my-httpenv-lb LoadBalancer 10.103.151.179 localhost 8888:32333/TCP 2m3s
my-httpenv-np NodePort 10.110.61.178 8888:32381/TCP 5h6m
[lun 21/08/30 18:07 CEST][s003][x86_64/darwin20.0/20.6.0][5.8]
just deleting services and deployment, if you want.zsh 1032 master% kubectl get all
NAME READY STATUS RESTARTS AGE
pod/bash 1/1 Running 0 5h7m
pod/my-httpenv-67cf6c9747-4qz7m 1/1 Running 0 6h48m
pod/my-httpenv-67cf6c9747-65v4r 1/1 Running 0 6h43m
pod/my-httpenv-67cf6c9747-c6kll 1/1 Running 0 6h43m
pod/my-httpenv-67cf6c9747-dgqcb 1/1 Running 0 6h43m
pod/my-httpenv-67cf6c9747-mg6p7 1/1 Running 0 6h43m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 443/TCP 45d
service/my-httpenv ClusterIP 10.104.67.45 8888/TCP 6h40m
service/my-httpenv-lb LoadBalancer 10.103.151.179 localhost 8888:32333/TCP 12m
service/my-httpenv-np NodePort 10.110.61.178 8888:32381/TCP 5h16m
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-httpenv 5/5 5 5 6h48m
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-httpenv-67cf6c9747 5 5 5 6h48m
[lun 21/08/30 18:17 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]
kubectl delete service my-httpenv my-httpenv-np my-httpenv-lb
kubectl delete deployment my-httpenv
[lun 21/08/30 18:17 CEST][s001][x86_64/darwin20.0/20.6.0][5.8]
zsh 1033 master% kubectl get namespaces
NAME STATUS AGE
default Active 45d
kube-node-lease Active 45d
kube-public Active 45d
kube-system Active 45d
// in prod environment, it is better to use full declarative mode, apply -f FILE.yaml.
[vie 21/09/03 11:38 CEST][s000][x86_64/darwin20.0/20.6.0][5.8]
zsh 1065 main% kubectl apply -f k8s-yaml/app.yml
service/app-nginx-service created
deployment.apps/app-nginx-deployment created
[vie 21/09/03 11:40 CEST][s000][x86_64/darwin20.0/20.6.0][5.8]zsh 1066 main% kubectl apply -f k8s-yaml/pod.yml
pod/nginx created
[vie 21/09/03 11:40 CEST][s000][x86_64/darwin20.0/20.6.0][5.8]
zsh 1067 main% kubectl get all
NAME READY STATUS RESTARTS AGE
pod/app-nginx-deployment-56845f96cc-7pftc 1/1 Running 0 15s
pod/app-nginx-deployment-56845f96cc-8twkn 1/1 Running 0 15s
pod/app-nginx-deployment-56845f96cc-sb4m4 1/1 Running 0 15s
pod/bash 1/1 Running 4 3d22h
pod/my-httpenv-67cf6c9747-4qz7m 1/1 Running 4 4d
pod/my-httpenv-67cf6c9747-65v4r 1/1 Running 4 4d
pod/my-httpenv-67cf6c9747-c6kll 1/1 Running 4 4d
pod/my-httpenv-67cf6c9747-dgqcb 1/1 Running 4 4d
pod/my-httpenv-67cf6c9747-mg6p7 1/1 Running 4 4d
pod/nginx 1/1 Running 0 6s
pod/nginx-deployment-595bdfc79f-2rxff 1/1 Running 0 115s
pod/nginx-deployment-595bdfc79f-8w2zq 1/1 Running 0 115s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/app-nginx-service NodePort 10.109.33.148 80:32351/TCP 16s
service/kubernetes ClusterIP 10.96.0.1 443/TCP 48d
service/my-httpenv ClusterIP 10.104.67.45 8888/TCP 4d
service/my-httpenv-lb LoadBalancer 10.103.151.179 localhost 8888:32333/TCP 3d17h
service/my-httpenv-np NodePort 10.110.61.178 8888:32381/TCP 3d22h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/app-nginx-deployment 3/3 3 3 15s
deployment.apps/my-httpenv 5/5 5 5 4d
deployment.apps/nginx-deployment 2/2 2 2 115s
NAME DESIRED CURRENT READY AGE
replicaset.apps/app-nginx-deployment-56845f96cc 3 3 3 15s
replicaset.apps/my-httpenv-67cf6c9747 5 5 5 4d
replicaset.apps/nginx-deployment-595bdfc79f 2 2 2 115s
// how many resources can i use?[vie 21/09/03 11:55 CEST][s000][x86_64/darwin20.0/20.6.0][5.8]
zsh 1071 main% kubectl api-resources | wc -l
58