See full details at setup K3S on Docker
Set-Alias docker podman
docker run -d --name k3s-server `
--privileged `
-p 6443:6443 `
-v k3s-data:/var/lib/rancher/k3s `
--network=host `
--hostname k3s-server `
-v "$(Get-Location)\k3s-config\registries.yaml:/etc/rancher/k3s/registries.yaml:ro" `
rancher/k3s server `
--node-name k3s-server `
--disable=metrics-server `
--kubelet-arg=fail-swap-on=false `
--cluster-cidr=10.42.0.0/16 `
--service-cidr=10.43.0.0/16 `
--kube-apiserver-arg=service-node-port-range=30000-32767 `
--https-listen-port=6443$kubeconfig = docker exec k3s-server cat /etc/rancher/k3s/k3s.yaml 2>$null
$kubeconfigDir = ".\"
$kubeconfig = $kubeconfig -replace '127.0.0.1', 'localhost'
$kubeconfigPath = Join-Path $kubeconfigDir "kubeconfig.yaml"
Set-Content -Path $kubeconfigPath -Value $kubeconfig
Write-Host "Kubeconfig saved to $kubeconfigPath" -ForegroundColor Green
Write-Host ""
Write-Host "To use kubectl with this cluster:" -ForegroundColor Yellow
Write-Host "`$env:KUBECONFIG = `"$(Resolve-Path $kubeconfigPath)`"" -ForegroundColor Gray
Write-Host "kubectl get nodes" -ForegroundColor Gray
$token = docker exec k3s-server cat /var/lib/rancher/k3s/server/node-token 2>$nulldocker run -d --name local-registry `
-p 5000:5000 `
-v registry-data:/var/lib/registry `
--network host `
--hostname local-registry `
--restart unless-stopped `
registry:2podman compose -f .\docker-compose.yml up -d
podman compose -f .\docker-compose.yml down
Write-Host "Registry URL: http://localhost:5000" -ForegroundColor Green
Write-Host "Registry catalog: http://localhost:5000/v2/_catalog" -ForegroundColor Green
Write-Host "Registry re-tag image: podman tag nginx:latest localhost:5000/nginx:latest" -ForegroundColor Green
Write-Host "Registry push image: podman push --tls-verify=false localhost:5000/nginx:latest" -ForegroundColor Green
Write-Host "Internal URL (for K3s): http://local-registry:5000" -ForegroundColor GreenOther commands to tag and push to local container registry
docker pull nginx:alpine
docker tag nginx:alpine localhost:5000/nginx:alpine
docker push --tls-verify=false localhost:5000/nginx:alpine
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-alpine-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx-alpine
template:
metadata:
labels:
app: nginx-alpine
spec:
containers:
- name: nginx-alpine
image: localhost:5000/nginx:alpine
ports:
- containerPort: 80kubectl apply -f .\yaml-samples\deployment.yaml
kubectl get podsservice.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-alpine-service
spec:
selector:
app: nginx-alpine
ports:
- protocol: TCP
port: 80
targetPort: 80
type: NodePortkubectl apply -f service.yamlkubectl get svc nginx-alpine-service$nodePort = (kubectl get svc nginx-alpine-service -o jsonpath='{.spec.ports[0].nodePort}')
Write-Host "Access the service at http://localhost:$nodePort" -ForegroundColor Greenservice.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-alpine-lb
spec:
selector:
app: nginx-alpine
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
kubectl apply -f service.yamlkubectl get svc nginx-alpine-lbCreate a ClusterIP Service (if not already)
service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-alpine
spec:
selector:
app: nginx-alpine
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIPDefine the Ingress Resource
ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-alpine-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: nginx.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-alpine
port:
number: 80Update /etc/hosts (for local testing)
Add this line to your host machine’s /etc/hosts file:
127.0.0.1 nginx.local
Add-Content -Path "C:\Windows\System32\drivers\etc\hosts" -Value "After deploying an Ingress Controller (like NGINX Ingress), access:
kubectl apply -f ingress.yaml
Write-Host "Access the service at http://nginx.local" -ForegroundColor Green---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-alpine-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx-alpine
template:
metadata:
labels:
app: nginx-alpine
spec:
containers:
- name: nginx-alpine
image: localhost:5000/nginx:alpine
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-alpine
spec:
selector:
app: nginx-alpine
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
name: nginx-alpine-lb
spec:
selector:
app: nginx-alpine
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-alpine-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
spec:
rules:
- host: nginx.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-alpine
port:
number: 80kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
kubectl create serviceaccount admin-user -n kubernetes-dashboard
kubectl create clusterrolebinding admin-user \
--clusterrole=cluster-admin \
--serviceaccount=kubernetes-dashboard:admin-user
kubectl -n kubernetes-dashboard describe secret \
$(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
kubectl proxy
Open http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/ in your browser.
- Go to Podman -> settings
Resources > Container EnginethenLogto see logs orTerminal - Go to Podman -> settings
Resources > Container EnginethenTerminalto execute command in VM