Using Kubernetes Rbac.authorization.k8s.io With Operator.tigera.io
Introduction
In this guide, we will demonstrate how to set up Kubernetes RBAC (Role-Based Access Control) using the rbac.authorization.k8s.io
API group along with the operator.tigera.io
API for Calico. This setup will help you manage access control and network policies in your Kubernetes cluster using Pulumi.
Step-by-Step Explanation
Step 1: Install Pulumi and Configure AWS
- Install Pulumi CLI from Pulumi’s official website.
- Configure Pulumi to use AWS as the cloud provider by setting up your AWS credentials. You can follow the guide here.
Step 2: Create a New Pulumi Project
- Create a new directory for your project and navigate into it:
mkdir pulumi-k8s-rbac-calico cd pulumi-k8s-rbac-calico
- Initialize a new Pulumi project:
pulumi new aws-typescript
Step 3: Install Required Pulumi Packages
- Install the Pulumi Kubernetes package:
npm install @pulumi/kubernetes
- Install the Pulumi AWS package:
npm install @pulumi/aws
Step 4: Define Kubernetes RBAC and Calico Resources
- Open the
index.ts
file in your project directory. - Define the Kubernetes RBAC resources and Calico operator resources as shown below:
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Create a Kubernetes Namespace
const ns = new k8s.core.v1.Namespace("calico-namespace", {
metadata: { name: "calico-system" },
});
// Create a ClusterRole for Calico
const calicoClusterRole = new k8s.rbac.v1.ClusterRole("calico-cluster-role", {
metadata: { name: "calico-cluster-role" },
rules: [
{
apiGroups: [""],
resources: ["pods", "services", "endpoints", "nodes", "namespaces"],
verbs: ["get", "list", "watch"],
},
{
apiGroups: ["networking.k8s.io"],
resources: ["networkpolicies"],
verbs: ["get", "list", "watch"],
},
{
apiGroups: ["operator.tigera.io"],
resources: ["*"],
verbs: ["*"],
},
],
});
// Create a ClusterRoleBinding for Calico
const calicoClusterRoleBinding = new k8s.rbac.v1.ClusterRoleBinding("calico-cluster-role-binding", {
metadata: { name: "calico-cluster-role-binding" },
subjects: [
{
kind: "ServiceAccount",
name: "calico-node",
namespace: ns.metadata.name,
},
],
roleRef: {
kind: "ClusterRole",
name: calicoClusterRole.metadata.name,
apiGroup: "rbac.authorization.k8s.io",
},
});
// Create a ServiceAccount for Calico
const calicoServiceAccount = new k8s.core.v1.ServiceAccount("calico-service-account", {
metadata: {
name: "calico-node",
namespace: ns.metadata.name,
},
});
// Export the namespace name
export const namespaceName = ns.metadata.name;
Step 5: Deploy the Resources
- Run
pulumi up
to preview and deploy the changes. - Confirm the deployment to create the resources in your Kubernetes cluster.
Conclusion
In this guide, we have successfully set up Kubernetes RBAC using the rbac.authorization.k8s.io
API group and integrated it with the operator.tigera.io
API for Calico. This setup ensures that you have fine-grained access control and network policies managed by Calico in your Kubernetes cluster.
Full Code Example
import * as pulumi from "@pulumi/pulumi";
import * as k8s from "@pulumi/kubernetes";
// Create a Kubernetes Namespace
const ns = new k8s.core.v1.Namespace("calico-namespace", {
metadata: { name: "calico-system" },
});
// Create a ClusterRole for Calico
const calicoClusterRole = new k8s.rbac.v1.ClusterRole("calico-cluster-role", {
metadata: { name: "calico-cluster-role" },
rules: [
{
apiGroups: [""],
resources: ["pods", "services", "endpoints", "nodes", "namespaces"],
verbs: ["get", "list", "watch"],
},
{
apiGroups: ["networking.k8s.io"],
resources: ["networkpolicies"],
verbs: ["get", "list", "watch"],
},
{
apiGroups: ["operator.tigera.io"],
resources: ["*"],
verbs: ["*"],
},
],
});
// Create a ClusterRoleBinding for Calico
const calicoClusterRoleBinding = new k8s.rbac.v1.ClusterRoleBinding("calico-cluster-role-binding", {
metadata: { name: "calico-cluster-role-binding" },
subjects: [
{
kind: "ServiceAccount",
name: "calico-node",
namespace: ns.metadata.name,
},
],
roleRef: {
kind: "ClusterRole",
name: calicoClusterRole.metadata.name,
apiGroup: "rbac.authorization.k8s.io",
},
});
// Create a ServiceAccount for Calico
const calicoServiceAccount = new k8s.core.v1.ServiceAccount("calico-service-account", {
metadata: {
name: "calico-node",
namespace: ns.metadata.name,
},
});
// Export the namespace name
export const namespaceName = ns.metadata.name;
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.