Certainly! Here’s a step-by-step guide to set up a single-node Kubernetes cluster on one machine (let’s call it ClusterHost), and then control it remotely from another machine (ControlHost) using a Python repository (e.g., with the Kubernetes Python client ).
1. Set Up Kubernetes Cluster on ClusterHost
a. Install Prerequisites
On ClusterHost (e.g., Ubuntu 22.04):
bash Copy code
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
b. Initialize the Cluster
bash Copy code
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
Save the output, especially the kubeadm join ... command (for adding nodes, if needed).
c. Set Up kubectl for Your User
bash Copy code
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
d. Install a Pod Network (e.g., Flannel)
bash Copy code
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
e. Allow Scheduling Pods on the Master Node (for single-node clusters)
bash Copy code
kubectl taint nodes --all node-role.kubernetes.io/control-plane-
2. Export kubeconfig for Remote Access
a. Copy kubeconfig to ControlHost
On ClusterHost:
bash Copy code
cat $HOME/.kube/config
Copy the output, or securely transfer the file to ControlHost (e.g., via scp):
bash Copy code
scp $HOME/.kube/config user@ControlHost:/home/user/cluster-config
b. Edit kubeconfig (if needed)
Ensure the server: field in the config points to the public IP or hostname of ClusterHost, not localhost or 127.0.0.1.
Example:
yaml Copy code
clusters:
- cluster:
certificate-authority-data: ...
server: https://<ClusterHost-IP>:6443
If your cluster is behind a firewall, open port 6443 for remote access.
3. Set Up Python Repository on ControlHost
a. Install Python and pip
bash Copy code
sudo apt-get update
sudo apt-get install -y python3 python3-pip
b. Install Kubernetes Python Client
bash Copy code
pip3 install kubernetes
c. Set KUBECONFIG Environment Variable
bash Copy code
export KUBECONFIG=/home/user/cluster-config
Or, in your Python code, specify the config file:
python Copy code
from kubernetes import client, config
config.load_kube_config(config_file='/home/user/cluster-config')
d. Example Python Code to Control the Cluster
Create a file, e.g., manage_k8s.py:
python Copy code
from kubernetes import client, config
# Load kubeconfig
config.load_kube_config(config_file='/home/user/cluster-config')
# List all pods in all namespaces
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
print(f"{i.status.pod_ip}\t{i.metadata.namespace}\t{i.metadata.name}")
# Example: Create a deployment
from kubernetes.client import V1Container, V1PodSpec, V1PodTemplateSpec, V1ObjectMeta, V1DeploymentSpec, V1Deployment
container = V1Container(
name="nginx",
image="nginx:1.14.2",
ports=[client.V1ContainerPort(container_port=80)]
)
template = V1PodTemplateSpec(
metadata=V1ObjectMeta(labels={"app": "nginx"}),
spec=V1PodSpec(containers=[container])
)
spec = V1DeploymentSpec(
replicas=1,
template=template,
selector={'matchLabels': {'app': 'nginx'}}
)
deployment = V1Deployment(
api_version="apps/v1",
kind="Deployment",
metadata=V1ObjectMeta(name="nginx-deployment"),
spec=spec
)
apps_v1 = client.AppsV1Api()
apps_v1.create_namespaced_deployment(
body=deployment,
namespace="default"
)
print("Deployment created.")
4. Run Your Python Repository
bash Copy code
python3 manage_k8s.py
Summary
ClusterHost : Set up Kubernetes, export kubeconfig.
ControlHost : Install Python, Kubernetes client, use kubeconfig to control the cluster via Python.
Security Note:
For production, secure your API server (firewall, VPN, RBAC, etc.).
Never expose your cluster’s API server to the public internet without proper security.
Let me know if you need a sample requirements.txt or a more advanced Python example!