安装kubectl
curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.4/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
安装k3d
curl -s https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash
# 单master
k3d create
export KUBECONFIG=$(k3d get-kubeconfig)
# 集群1master +2 worker
k3d create rancher/k3s:v1.0.0 --workers 2
export KUBECONFIG=$(k3d get-kubeconfig)
如果重复部署,注意删除$(k3d get-kubeconfig)所指向的config秘钥配置
k3d 常用命令
[root@riyimei ~]# curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
Preparing to install k3d into /usr/local/bin
k3d installed into /usr/local/bin/k3d
Run 'k3d --help' to see what you can do with it.
[root@riyimei ~]# which k3d
/usr/local/bin/k3d
[root@riyimei ~]# k3d version
k3d version v3.0.1
k3s version v1.18.6-k3s1 (default)
[root@riyimei ~]# k3d help
https://k3d.io/
k3d is a wrapper CLI that helps you to easily create k3s clusters inside docker.
Nodes of a k3d cluster are docker containers running a k3s image.
All Nodes of a k3d cluster are part of the same docker network.
Usage:
k3d [flags]
k3d [command]
Available Commands:
cluster Manage cluster(s)
completion Generate completion scripts for [bash, zsh, powershell | psh]
help Help about any command
image Handle container images.
kubeconfig Manage kubeconfig(s)
node Manage node(s)
version Show k3d and default k3s version
Flags:
-h, --help help for k3d
--verbose Enable verbose output (debug logging)
--version Show k3d and default k3s version
Use "k3d [command] --help" for more information about a command.
[root@riyimei ~]#
部署
[root@riyimei ~]# k3d cluster create mycluster
INFO[0000] Created network 'k3d-mycluster'
INFO[0000] Created volume 'k3d-mycluster-images'
INFO[0001] Creating node 'k3d-mycluster-server-0'
INFO[0004] Pulling image 'docker.io/rancher/k3s:v1.18.6-k3s1'
INFO[0011] Creating LoadBalancer 'k3d-mycluster-serverlb'
INFO[0014] Pulling image 'docker.io/rancher/k3d-proxy:v3.0.1'
INFO[0029] Cluster 'mycluster' created successfully!
INFO[0029] You can now use it like this:
kubectl cluster-info
[root@riyimei ~]# kubectl cluster-info
Kubernetes master is running at https://0.0.0.0:24588
CoreDNS is running at https://0.0.0.0:24588/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
Metrics-server is running at https://0.0.0.0:24588/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
[root@riyimei ~]#
[root@riyimei ~]# k3d kubeconfig merge mycluster --switch-context
/root/.k3d/kubeconfig-mycluster.yaml
[root@riyimei ~]#
[root@riyimei ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k3d-mycluster-server-0 Ready master 6m47s v1.18.6+k3s1
[root@riyimei ~]#
[root@riyimei ~]# kubectl version --short
Client Version: v1.17.4
Server Version: v1.18.6+k3s1
[root@riyimei ~]#
[root@riyimei ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
50881422e646 rancher/k3d-proxy:v3.0.1 "/bin/sh -c nginx-pr…" 4 minutes ago Up 4 minutes 80/tcp, 0.0.0.0:24588->6443/tcp k3d-mycluster-serverlb
218c19e52163 rancher/k3s:v1.18.6-k3s1 "/bin/k3s server --t…" 4 minutes ago Up 4 minutes k3d-mycluster-server-0
[root@riyimei ~]# k3d cluster
Manage cluster(s)
Usage:
k3d cluster [flags]
k3d cluster [command]
Available Commands:
create Create a new cluster
delete Delete cluster(s).
list List cluster(s)
start Start existing k3d cluster(s)
stop Stop existing k3d cluster(s)
Flags:
-h, --help help for cluster
Global Flags:
--verbose Enable verbose output (debug logging)
Use "k3d cluster [command] --help" for more information about a command.
[root@riyimei ~]#
[root@riyimei ~]# k3d cluster list
NAME SERVERS AGENTS LOADBALANCER
mycluster 1/1 0/0 true
mycluster2 1/1 0/0 true
[root@riyimei ~]#
[root@riyimei ~]# k3d cluster list
NAME SERVERS AGENTS LOADBALANCER
mycluster 1/1 0/0 true
mycluster2 1/1 0/0 true
[root@riyimei ~]# k3d cluster stop mycluster2
INFO[0000] Stopping cluster 'mycluster2'
[root@riyimei ~]# k3d cluster list
NAME SERVERS AGENTS LOADBALANCER
mycluster 1/1 0/0 true
mycluster2 0/1 0/0 true
[root@riyimei ~]#