[Kubernetes] Create a ClusterIP Service

Kubernetes Create a ClusterIP Service

  • Creating a ClusterIP Service
    • Open two shell windows so we can watch this
      • kubectl get pods -w
    • In second window, lets start a simple http server using sample code
      • kubectl create deployment httpenv --image=bretfisher/httpenv
    • Scale it to 5 replicas
      • kubectl scale deployment/httpenv --replicas=5
    • Let's create a ClusterIP service (default)
      • kubectl expose deployment/httpenv --port 8888
  • Inspecting ClusterIP Service
    • Look up what IP was allocated
      • kubectl get service
    • Remember this IP is Cluster internal only, how do we curl it?
    • If you're on Docker Desktop (Host OS is not container OS)
      • kubectl run --generator=run-pod/v1 tmp-shell --rm -it --image bretfisher/netshoot -- bash
      • curl httpenv:8888
    • If you're on Linux host
      • curl [ip of service]:8888
  • Creating a NodePort Service
    • Let's expose a NodePort so we can access it via the host IP (including localhost on Windows/Linux/macOS)
      • kubectl expose deployment/httpenv --port 8888 --name httpenv-np --type NodePort
      • default type 是 ClusterIP 這邊指定為NodePort
    • Did you know that a NodePort service also creates a ClusterIP?
    • These three service types are additive, each one creates the ones above it:
      • ClusterIP
      • NodePort
      • LoadBalancer
  • Add a LoadBalancer Service
    • If you're on Docker Desktop, it provides a built-in LoadBalancer that publishes the --port on localhost
      • kubectl expose deployment/httpenv --port 8888 --name htpenv-lb --type LoadBalancer
      • curl localhost:8888
    • If you're on kubeadm, minikube, or microk8s
      • No built-in LB
      • You can still run the command, it'll just stay at "pending" (but its NodePort works)
  • Kubernetes Services DNS
    • Starting with 1.11, internal DNS is provided by CoreDNS
    • Like Swarm, this is DNS-Based Service Discovery
    • So far we've been using hostnames to access Services
      • curl
    • But that only works for Services in the same Namespace
      • kubectl get namespaces
    • Services also have a FQDN
      • curl ..svc.cluster.local
    • END