Skip to content

About

Demo common scenarios both internal and external communication for kubernetes cluster

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Kubernetes Networking

This is a collection of scenarios that we use to simulate kubernetes' networking to know core kubernetes' components to support various communication types

Kubernetes Networking

Scenarios include internal and external communication

To support on this we are going to deploy:

  • 1 cluster has 3 nodes (1 master + 2 workers): use the docker's kubernetes built-in with cluster type = kind or we can create your customized cluster if we want
  • 2 pods
  • pod1: has two containers
  • pod2: Temporary pod like one-shot pod run to verify and delete after that

Container communicate with another container inside pod1

  • Create configmap nginx-config
kubectl apply -f configmap.yaml
  • Deploy pod 1 with 2 containers
    • nginx: serve default http request to response http status code 200
    • busybox: test connection to container nginx in pod 1 via ping command ping -c 3 localhost:3000
kubectl apply -f pod1-deployment.yaml
  • Check log of busybox container to see the result success reaching 3 packets transmitted, 3 packets received, 0% packet loss
# get pods for a namespace to retrieve the target pod
kubectl get pod -n <target-namespace>
# retrieve logs of a container on target pod
kubectl logs <target-pod-name> busybox

Container in pod2 communicate with another container in pod1 via IP address

  • Retrieve target pod1's IP address
kubectl get pods -n <target-namespace> -o wide
  • Create a tmp-pinger pod
# ping to `pod1` based on the output from ip ddress above
kubectl run tmp-pinger --image busybox -it --rm -- ping -c 3 <target-pod-ip-address>
  • Or tmp-curler
# retrieve the data from `pod1` by sending curl request o the output from ip ddress above
kubectl run tmp-curler --image=curlimages/curl --rm -i --tty --restart=Never -- curl <http://<target-pod-ip-address>:80>

Container in pod2 communicate with another container in pod1 via service

  • Create service to eliminate ephemeral IP address when pod is restarted, scaled or deployed
kubectl apply -f service.yaml
  • Create a new temporary pod tmp-curler to retrieve the data from pod1 via curl request via static name nginx-service
kubectl run tmp-curler --image=curlimages/curl --rm -i --tty --restart=Never -- curl http://nginx-service:80
  • [Optional] we can restart deployment to see the new pod's Ip address was changed and perform the above command to see how service helps to abstract this change
kubectl rollout restart deployment.apps/pod1-deployment

External client communicate with pod inside the kubernetes' cluster internally

For development

  • Expose the docker container port e.g. 8081
kubectl port-forward service/nginx-service 8081:80
  • Retrieve the data from pod1 either accessing via browser address localhost:8081 or curl http://localhost:8081

For Production

  • Create a ingress to expose to external clients
kubectl apply -f ingress.yaml
  • Add the record 127.0.0.1 kube-networking.local to your hosts e.g. /etc/hosts

  • Retrieve the data from pod1 via ingress customized domain kube-networking.localeither accessing via browser or curl request to the http://kube-networking.local

We can explore more option to expose via service with type NodePort | LoadBalancer

Troubleshooting the connection

Get pods ip to verify your target pod's id address

kubectl get pods -n <target-namespace> -o wide

Check open ports for existing containers inside pod

kubectl exec -it <target-pod-name> -n <target-namespace> -- netstat -tuln

Check networkpolicy to see if any restrictions add on

kubectl get networkpolicy -n default

Ensure ingress controller is installed in your cluster to ensure the routing work

[Optional] If we use the docker's kubernetes built-in. We can create with the following command

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.15.1/deploy/static/provider/cloud/deploy.yaml

About

Demo common scenarios both internal and external communication for kubernetes cluster

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors