This example demonstrates how Postgres-client can get connectivity to Postgres-server deployment via NSM. Client pod and server deployment located on different nodes.
Make sure that you have completed steps from features
Note: Admission webhook is required and should be started at this moment.
WH=$(kubectl get pods -l app=admission-webhook-k8s -n nsm-system --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
kubectl wait --for=condition=ready --timeout=1m pod ${WH} -n nsm-system
- Create test namespace:
NAMESPACE=($(kubectl create -f https://raw.githubusercontent.com/networkservicemesh/deployments-k8s/c89573ec0f82ee02c425c43da72b026397cb52fc/examples/features/namespace.yaml)[0])
NAMESPACE=${NAMESPACE:10}
- Get all available nodes to deploy:
NODES=($(kubectl get nodes -o go-template='{{range .items}}{{ if not .spec.taints }}{{index .metadata.labels "kubernetes.io/hostname"}} {{end}}{{end}}'))
- Create postgres client deployment and set
nodeSelector
to the first node:
cat > postgres-cl.yaml <<EOF
---
apiVersion: v1
kind: Pod
metadata:
name: postgres-cl
annotations:
networkservicemesh.io: kernel://my-postgres-service/nsm-1
labels:
app: postgres-cl
"spiffe.io/spiffe-id": "true"
spec:
containers:
- name: postgres-cl
image: postgres
imagePullPolicy: IfNotPresent
env:
- name: POSTGRES_HOST_AUTH_METHOD
value: trust
nodeSelector:
kubernetes.io/hostname: ${NODES[0]}
EOF
- Add to nse-kernel the postgres container and set
nodeSelector
it to the second node:
cat > patch-nse.yaml <<EOF
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nse-kernel
spec:
template:
spec:
containers:
- name: postgres
image: postgres
imagePullPolicy: IfNotPresent
ports:
- containerPort: 5432
env:
- name: POSTGRES_DB
value: test
- name: POSTGRES_USER
value: admin
- name: POSTGRES_PASSWORD
value: admin
- name: nse
env:
- name: NSM_SERVICE_NAMES
value: my-postgres-service
- name: NSM_CIDR_PREFIX
value: 172.16.1.100/31
nodeSelector:
kubernetes.io/hostname: ${NODES[1]}
EOF
- Create kustomization file:
cat > kustomization.yaml <<EOF
---
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
namespace: ${NAMESPACE}
bases:
- https://github.com/networkservicemesh/deployments-k8s/apps/nse-kernel?ref=c89573ec0f82ee02c425c43da72b026397cb52fc
resources:
- postgres-cl.yaml
patchesStrategicMerge:
- patch-nse.yaml
EOF
- Deploy postgres-nsc and postgres-nse
kubectl apply -k .
- Wait for applications ready:
kubectl wait --for=condition=ready --timeout=5m pod -l app=nse-kernel -n ${NAMESPACE}
kubectl wait --for=condition=ready --timeout=1m pod postgres-cl -n ${NAMESPACE}
- Find NSC and NSE pods by labels:
NSC=$(kubectl get pods -l app=postgres-cl -n ${NAMESPACE} --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
NSE=$(kubectl get pods -l app=nse-kernel -n ${NAMESPACE} --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
- Try to connect from postgres-nsc to database from postgresql service:
kubectl exec ${NSC} -n ${NAMESPACE} -c postgres-cl -- sh -c 'PGPASSWORD=admin psql -h 172.16.1.100 -p 5432 -U admin test'
Delete ns:
kubectl delete ns ${NAMESPACE}