slug | title | sidebar_label |
---|---|---|
create-netpols |
Configuring network policies |
Create Network Policies |
In some cases you want to explicitly allow access to your application. This can be done by creating network policies. 2 types of network policies are supported:
-
Policies for ingress traffic inside the cluster.
-
Policies for egress traffic to go outside of the cluster (to access external FQDNs).
The internal ingress network policies allow you to:
-
Deny all traffic to Pods (default mode)
-
Allow selected Workload Pods running on the cluster to access your Workload's Pods
-
Allow all traffic to the Pods of a Workload
Deny all
and Allow all
we don't need to explain right?
:::info
The Ingress Network Policies rely on Pod labels. We require that a single label covers Pods for a given workload. We recommend to use the otomi.io/app: <workload-name>
label.
:::
To allow other Workloads in the cluster to access your Workload's Pods, follow these steps:
-
Navigate to the
Network Policies
page in the Console and clickCreate Netpol
. -
Name the network policy and select the
ingress
rule type. -
Add the selector label name and value for the Workload Pods to be accessed. E.g.: use the
otomi.io/app
label. -
Select either
AllowAll
orAllowOnly
mode. -
If you select
AllowOnly
, specify the namespace (e.g.,team-labs
), and the selector label name and value for the Workload Pods to be accessed. -
Add more rules if needed.
The egress network policies allow you to:
-
Deny all traffic from the Pods of a Workload (default)
-
Allow all Pods within a namespace to access external FQDNs or IPs through an egress rule
To allow your Workload's Pods to access external FQDNs or IPs, follow these steps:
-
Navigate to the
Network Policies
page in the Console and clickCreate Netpol
. -
Name the network policy and select the
egress
rule type. -
Add the FQDN or IP to be accessed.
-
Add port number(s) and protocol if needed.
:::info The egress rules are namespace wide. You cannot bind an egress policy to one Workload only. :::
Build the vote
, worker
and result
images from this repo. Use the Build feature to build the images with mode: Docker
. Set the path
to ./vote-app/vote/Dockerfile
for the Vote image (and ./vote-app/worker/Dockerfile
for the Worker and ./vote-app/result/Dockerfile
for Result).
Use the postgresql
and the redis
charts from the Catalog to create a Redis master-replica cluster and a PostgreSQL database. For this lab, Redis authentication needs to be turned off by setting auth.enabled=false
.
Use the k8s-deployment
chart to deploy the vote app. Use the following values:
Name: vote
image:
repository: harbor.<your-domain>/team-<team-name>/vote
pullPolicy: IfNotPresent
tag: latest
containerPorts:
- name: http
containerPort: 80
protocol: TCP
env:
- name: REDIS_HOST
value: <redis-cluster-name>-master
replicaCount: 1
Use the k8s-deployment
chart to deploy the worker app. Use the following values:
Name: worker
image:
repository: harbor.<your-domain>/team-<team-name>/worker
pullPolicy: IfNotPresent
tag: latest
containerPorts:
- name: http
containerPort: 80
protocol: TCP
env:
- name: DATABASE_USER
valueFrom:
secretKeyRef:
name: <psql-cluster-name>-app
key: username
- name: DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: <psql-cluster-name>-app
key: password
- name: REDIS_HOST
value: <redis-cluster-name>-master
- name: DATABASE_HOST
value: <psql-cluster-name>-rw
replicaCount: 1
:::note The worker pod will show an error “Waiting for db” in the logs. This is an expected error that will be resolved when all the steps in the lab are done. :::
Use the k8s-deployment
chart to deploy the result app. Use the following values:
Name: result
image:
repository: harbor.<your-domain>/team-<team-name>/result
pullPolicy: IfNotPresent
tag: latest
containerPorts:
- name: http
containerPort: 80
protocol: TCP
env:
- name: DATABASE_USER
valueFrom:
secretKeyRef:
name: <psql-cluster-name>-app
key: username
- name: DATABASE_PASSWORD
valueFrom:
secretKeyRef:
name: <psql-cluster-name>-app
key: password
- name: DATABASE_HOST
value: <psql-cluster-name>-rw
- name: DATABASE_NAME
value: <psql-cluster-name>
replicaCount: 1
:::note The result pod will show an error “Waiting for db” in the logs. This is an expected error that will be resolved when all the steps in the lab are done. :::
- Register the
vote
service. - Set exposure to
External
.
- Register the
result
service. - Set exposure to
External
.
-
Create a new
Network policy
and select theingress
rule type. -
Add the selector label name
otomi.io/app
. -
Add the selector label value
<postgres-workload-name>
. -
Select
AllowOnly
. -
Add the namespace
team-<name>
, the selector label nameotomi.io/app
and the selector label valueworker
. -
Add the namespace
team-<name>
, the selector label nameotomi.io/app
and the selector label valueresult
.
-
Create a new
Network policy
and select theingress
rule type. -
Add the selector label name
otomi.io/app
. -
Add the selector label value
<redis-workload-name>
. -
Select
AllowOnly
. -
Add the namespace
team-<name>
, the selector label nameotomi.io/app
and the selector label valueworker
. -
Add the namespace
team-<name>
, the selector label nameotomi.io/app
and the selector label valuevote
.
-
Go to the external URL of the
vote
application. -
Click on
Cats
orDogs
. -
Now go to the external URL of the
result
application. -
You should see the result of your vote.
-
Navigate to the
Network Policies
page in the Console and clickCreate Netpol
. -
Name the network policy
apl-docs
and select theegress
rule type. -
Add the FQDN
apl-docs.net
to be accessed. -
Add port number
443
and protocolHTTPS
.
Deploy a Netshoot pod in your namespace within your Kubernetes cluster. You can do this using kubectl command:
kubectl run -i --tty --rm netshoot --image nicolaka/netshoot -n team-labs
:::info
The Netshoot pod is a network troubleshooting tool that includes a lot of network tools like curl
, dig
, nslookup
, ping
, traceroute
, etc.
:::
- Run the following command in the Netshoot pod:
curl https://apl-docs.net
You should see the HTML of the apl-docs.net website
- Run the following command to see the
<title data-rh="true">App Platform for LKE</title>
message:
curl -s https://apl-docs.net | grep -o '<title.*</title>'
- Type
exit
to exit the Netshoot pod.
When you exit the Netshoot pod, it will be removed from the cluster.