Portainer with k3s
Using Portainer with your k3s setup is a great way to manage your Kubernetes cluster through a user-friendly web interface. Below are the steps to install and configure Portainer on your k3s cluster.
Step 1: Install Portainer
-
Access the Master Node (VM1):
- SSH into your master node (VM1) where k3s is running.
-
Deploy Portainer:
- You can deploy Portainer using a Kubernetes manifest. Create a file named
portainer-deployment.yamlwith the following content:
apiVersion: apps/v1 kind: Deployment metadata: name: portainer namespace: portainer spec: replicas: 1 selector: matchLabels: app: portainer template: metadata: labels: app: portainer spec: containers: - name: portainer image: portainer/portainer-ce ports: - containerPort: 9000 volumeMounts: - name: portainer-data mountPath: /data volumes: - name: portainer-data persistentVolumeClaim: claimName: portainer-data - You can deploy Portainer using a Kubernetes manifest. Create a file named
-
Create a Persistent Volume Claim:
- Create a file named
portainer-pvc.yamlto define a Persistent Volume Claim (PVC) for Portainer:
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: portainer-data namespace: portainer spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi - Create a file named
-
Create the Portainer Namespace:
- Create a namespace for Portainer:
kubectl create namespace portainer -
Apply the PVC and Deployment:
- Apply the PVC and Deployment files:
kubectl apply -f portainer-pvc.yaml kubectl apply -f portainer-deployment.yaml
Step 2: Expose Portainer
To access Portainer, you need to expose it. You can do this using a Service of type NodePort or LoadBalancer. Here’s how to create a NodePort service:
-
Create a Service for Portainer:
- Create a file named
portainer-service.yaml:
apiVersion: v1 kind: Service metadata: name: portainer namespace: portainer spec: type: NodePort ports: - port: 9000 targetPort: 9000 nodePort: 30000 # You can specify a port or let Kubernetes assign one selector: app: portainer - Create a file named
-
Apply the Service:
- Apply the service file:
kubectl apply -f portainer-service.yaml
Step 3: Access Portainer
-
Get the NodePort:
- If you specified a
nodePort, you can access Portainer using the IP address of any of your physical machines (192.168.123.100 or 192.168.123.230) and the port you specified (30000 in this example).
http://192.168.123.100:30000or
http://192.168.123.230:30000 - If you specified a
-
Initial Setup:
- When you first access Portainer, you will be prompted to create an admin user. Follow the instructions to set up your admin account.
-
Connect to the Kubernetes Environment:
- After logging in, you can add your k3s cluster as an environment. Portainer should automatically detect the Kubernetes environment since it is running in the same cluster.
Step 4: Manage Your Kubernetes Cluster
Once Portainer is set up, you can use it to manage your Kubernetes resources, deploy applications, and monitor your cluster.
Additional Considerations
- Security: Consider securing your Portainer instance, especially if you plan to expose it to the internet. You can use HTTPS and authentication methods to enhance security.
- Resource Limits: Depending on your cluster's resources, you may want to set resource limits for the Portainer deployment to ensure it doesn't consume too many resources.
By following these steps, you should have Portainer up and running on your k3s cluster, providing a convenient interface for managing your Kubernetes resources.