This guide demonstrates a TCP client and server application within the service mesh communicating using OSM’s TCP routing capability.
Prerequisites
- Kubernetes cluster running Kubernetes v1.20.0 or greater.
- Have OSM installed.
- Have
kubectlavailable to interact with the API server. - Have
osmCLI available for managing the service mesh.
Demo
The following demo shows a TCP client sending data to a tcp-echo server, which then echoes back the data to the client over a TCP connection.
Set the namespace where OSM is installed.
osm_namespace=osm-system # Replace osm-system with the namespace where OSM is installed if different
Deploy the
tcp-echoservice in thetcp-demonamespace. Thetcp-echoservice runs on port9000with theappProtocolfield set totcp, which indicates to OSM that TCP routing must be used for traffic directed to thetcp-echoservice on port9000.# Create the tcp-demo namespacekubectl create namespace tcp-demo# Add the namespace to the meshosm namespace add tcp-demo# Deploy the servicekubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.1/manifests/apps/tcp-echo.yaml -n tcp-demo
Confirm the
tcp-echoservice and pod is up and running.$ kubectl get svc,po -n tcp-demoNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEservice/tcp-echo ClusterIP 10.0.216.68 <none> 9000/TCP 97sNAME READY STATUS RESTARTS AGEpod/tcp-echo-6656b7c4f8-zt92q 2/2 Running 0 97s
Deploy the
curlclient into thecurlnamespace.# Create the curl namespacekubectl create namespace curl# Add the namespace to the meshosm namespace add curl# Deploy curl client in the curl namespacekubectl apply -f https://raw.githubusercontent.com/openservicemesh/osm-docs/release-v1.1/manifests/samples/curl/curl.yaml -n curl
Confirm the
curlclient pod is up and running.$ kubectl get pods -n curlNAME READY STATUS RESTARTS AGEcurl-54ccc6954c-9rlvp 2/2 Running 0 20s
Using Permissive Traffic Policy Mode
We will enable service discovery using permissive traffic policy mode, which allows application connectivity to be established without the need for explicit SMI policies.
Enable permissive traffic policy mode
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enablePermissiveTrafficPolicyMode":true}}}' --type=merge
Confirm the
curlclient is able to send and receive a response from thetcp-echoservice using TCP routing.$ kubectl exec -n curl -ti "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')" -c curl -- sh -c 'echo hello | nc tcp-echo.tcp-demo 9000'echo response: hello
The
tcp-echoservice should echo back the data sent by the client. In the above example, the client sendshello, and thetcp-echoservice responds withecho response: hello.
Using SMI Traffic Policy Mode
When using SMI traffic policy mode, explicit traffic policies must be configured to allow application connectivity. We will set up SMI policies to allow the curl client to communicate with the tcp-echo service on port 9000.
Enable SMI traffic policy mode by disabling permissive traffic policy mode
kubectl patch meshconfig osm-mesh-config -n "$osm_namespace" -p '{"spec":{"traffic":{"enablePermissiveTrafficPolicyMode":false}}}' --type=merge
Confirm the
curlclient is unable to send and receive a response from thetcp-echoservice in the absence of SMI policies.$ kubectl exec -n curl -ti "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')" -c curl -- sh -c 'echo hello | nc tcp-echo.tcp-demo 9000'command terminated with exit code 1
Configure SMI traffic access and routing policies.
kubectl apply -f - <<EOF# TCP route to allows access to tcp-echo:9000apiVersion: specs.smi-spec.io/v1alpha4kind: TCPRoutemetadata:name: tcp-echo-routenamespace: tcp-demospec:matches:ports:- 9000---# Traffic target to allow curl app to access tcp-echo service using a TCPRoutekind: TrafficTargetapiVersion: access.smi-spec.io/v1alpha3metadata:name: tcp-accessnamespace: tcp-demospec:destination:kind: ServiceAccountname: tcp-echonamespace: tcp-demosources:- kind: ServiceAccountname: curlnamespace: curlrules:- kind: TCPRoutename: tcp-echo-routeEOF
Confirm the
curlclient is able to send and receive a response from thetcp-echoservice using SMI TCP route.$ kubectl exec -n curl -ti "$(kubectl get pod -n curl -l app=curl -o jsonpath='{.items[0].metadata.name}')" -c curl -- sh -c 'echo hello | nc tcp-echo.tcp-demo 9000'echo response: hello
