Deploy NetworkPolicies
Duration: 5 min | Persona: Apps Operator
In this section, you will see the Policy Controller violation regarding to the missing NetworkPolicies in the Whereami namespace. Then, you will fix this violation by deploying the associated resources.
Initialize variables:
WORK_DIR=~/
source ${WORK_DIR}acm-workshop-variables.shSee the Policy Controller violations
See the Policy Controller violations in the GKE cluster, by running this command and click on this link:
echo -e "https://console.cloud.google.com/kubernetes/policy_controller/dashboard?project=${TENANT_PROJECT_ID}"You will see that the K8sRequireNamespaceNetworkPolicies Constraint has this violation: Namespace <whereami> does not have a NetworkPolicy.
Let’s fix it!
Define a default deny-all NetworkPolicy
Define a default deny-all NetworkPolicy:
cat <<EOF > ${WORK_DIR}$WHERE_AMI_DIR_NAME/base/networkpolicy_deny-all.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress
EOFUpdate the Kustomize base overlay:
cd ${WORK_DIR}$WHERE_AMI_DIR_NAME/base
kustomize edit add resource networkpolicy_deny-all.yamlDefine NetworkPolicy for the Whereami app
Define a fine granular NetworkPolicy:
cat <<EOF > ${WORK_DIR}$WHERE_AMI_DIR_NAME/base/networkpolicy_whereami.yaml
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: whereami
spec:
podSelector:
matchLabels:
app: whereami
policyTypes:
- Ingress
- Egress
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: ${INGRESS_GATEWAY_NAMESPACE}
podSelector:
matchLabels:
app: ${INGRESS_GATEWAY_NAME}
ports:
- port: 8080
protocol: TCP
egress:
- {}
EOFUpdate the Kustomize base overlay:
cd ${WORK_DIR}$WHERE_AMI_DIR_NAME/base
kustomize edit add resource networkpolicy_whereami.yamlDeploy Kubernetes manifests
cd ${WORK_DIR}$WHERE_AMI_DIR_NAME/
git add . && git commit -m "Whereami NetworkPolicies" && git push origin mainCheck deployments
List the Kubernetes resources managed by Config Sync in GKE cluster for the Whereami app repository:
Run this command and click on this link:
echo -e "https://console.cloud.google.com/kubernetes/config_management/packages?project=${TENANT_PROJECT_ID}"Wait until you see the Sync status column as Synced and the Reconcile status column as Current.
Run this command:
gcloud alpha anthos config sync repo describe \
--project $TENANT_PROJECT_ID \
--managed-resources all \
--sync-name repo-sync \
--sync-namespace $WHEREAMI_NAMESPACEWait and re-run this command above until you see "status": "SYNCED".
See the Policy Controller Constraints without any violations in the GKE cluster, by running this command and click on this link:
echo -e "https://console.cloud.google.com/kubernetes/policy_controller/dashboard?project=${TENANT_PROJECT_ID}"List the GitHub runs for the Whereami app repository:
cd ${WORK_DIR}$WHERE_AMI_DIR_NAME && gh run listCheck the Whereami app
Navigate to the Whereami app, click on the link displayed by the command below:
echo -e "https://${WHERE_AMI_INGRESS_GATEWAY_HOST_NAME}"You should still have the Whereami app working successfully.