Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Kubernetes - Services

Back


Pod Communication


Service


Types of Services


Imperative Commands

Command Description
kubectl get svc List all Services in the current namespace.
kubectl describe svc service_name Show detailed information about a specific Service.
kubectl create svc clusterip svc_name --tcp=80 Create a ClusterIP service
kubectl create svc nodeport svc_name --tcp=80 Create a NodePort service
kubectl create svc loadbalancer svc_name --tcp=80 Create a LoadBalancer service
kubectl delete svc svc_name Delete a Service by name.

Declarative Manifest

Field Description
clusterIP The internal IP address within the cluster. blank:k8s assign; None:headless service.
type the type of Service object: ClusterIP(default), NodePort, LoadBalancer, and ExternalName.
selector the label keys and values
ports List of ports exposed by this service. can specify the name, protocol, appProtocol, port, nodePort, and targetPort.

ClusterIP


apiVersion: v1
kind: Service
metadata:
  name: back-end
spec:
  type: ClusterIP
  ports:
    - targetPort: 80 # the port exposed on backend
      port: 80 # the port exposed on service
  selector: # link the service to the pods
    app: myapp
    type: back-end
kubectl create -f service-cip-def.yaml

kubectl get svc

Load Balancer


apiVersion: v1
kind: Service
metadata:
  name: myservice
spec:
  type: LoadBalancer
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30008

Common Commands


Service Networking


Lab: Get network info

# get master node ip
kubectl get node -o wide
# NAME           STATUS   ROLES           AGE   VERSION   INTERNAL-IP      EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION    CONTAINER-RUNTIME
# controlplane   Ready    control-plane   39m   v1.34.0   192.168.81.31    <none>        Ubuntu 22.04.5 LTS   5.15.0-1083-gcp   containerd://1.6.26

# get master node ip range
ip a
# 4: eth0@if22929: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1410 qdisc noqueue state UP group default
#     link/ether 22:8d:d9:de:30:0b brd ff:ff:ff:ff:ff:ff link-netnsid 0
#     inet 192.168.81.31/32 scope global eth0
#        valid_lft forever preferred_lft forever
#     inet6 fe80::208d:d9ff:fede:300b/64 scope link
#        valid_lft forever preferred_lft forever
# get the cidr from controller manager conf
cat /etc/kubernetes/manifests/kube-controller-manager.yaml | grep cluster-cidr
# - --cluster-cidr=172.17.0.0/16
# get the service config from the aip server
cat /etc/kubernetes/manifests/kube-apiserver.yaml | grep service-cluster-ip-range
# - --service-cluster-ip-range=172.20.0.0/16
kubectl get pod -A | grep proxy
# kube-system   kube-proxy-6c4gt                           1/1     Running   0          59m
# kube-system   kube-proxy-sjt5p                           1/1     Running   0          59m

# get the type of proxy
k logs kube-proxy-sjt5p    -n kube-system
# I1203 18:43:42.896019       1 server_linux.go:132] "Using iptables Proxier"

# get how the proxy is deployed
kubectl get all -A | grep kube-proxy  # use daemonset
# kube-system   pod/kube-proxy-6c4gt                           1/1     Running   0          66m
# kube-system   pod/kube-proxy-sjt5p                           1/1     Running   0          65m
# kube-system   daemonset.apps/kube-proxy   2         2         2       2            2           kubernetes.io/os=linux   66m