1. 安装kubectl
    2. curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.4/bin/linux/amd64/kubectl
    3. chmod +x ./kubectl
    4. sudo mv ./kubectl /usr/local/bin/kubectl
    5. 安装k3d
    6. curl -s https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash
    7. # 单master
    8. k3d create
    9. export KUBECONFIG=$(k3d get-kubeconfig)
    10. # 集群1master +2 worker
    11. k3d create rancher/k3s:v1.0.0 --workers 2
    12. export KUBECONFIG=$(k3d get-kubeconfig)
    13. 如果重复部署,注意删除$(k3d get-kubeconfig)所指向的config秘钥配置

    k3d 常用命令

    1. [root@riyimei ~]# curl -s https://raw.githubusercontent.com/rancher/k3d/main/install.sh | bash
    2. Preparing to install k3d into /usr/local/bin
    3. k3d installed into /usr/local/bin/k3d
    4. Run 'k3d --help' to see what you can do with it.
    1. [root@riyimei ~]# which k3d
    2. /usr/local/bin/k3d
    3. [root@riyimei ~]# k3d version
    4. k3d version v3.0.1
    5. k3s version v1.18.6-k3s1 (default)
    6. [root@riyimei ~]# k3d help
    7. https://k3d.io/
    8. k3d is a wrapper CLI that helps you to easily create k3s clusters inside docker.
    9. Nodes of a k3d cluster are docker containers running a k3s image.
    10. All Nodes of a k3d cluster are part of the same docker network.
    11. Usage:
    12. k3d [flags]
    13. k3d [command]
    14. Available Commands:
    15. cluster Manage cluster(s)
    16. completion Generate completion scripts for [bash, zsh, powershell | psh]
    17. help Help about any command
    18. image Handle container images.
    19. kubeconfig Manage kubeconfig(s)
    20. node Manage node(s)
    21. version Show k3d and default k3s version
    22. Flags:
    23. -h, --help help for k3d
    24. --verbose Enable verbose output (debug logging)
    25. --version Show k3d and default k3s version
    26. Use "k3d [command] --help" for more information about a command.
    27. [root@riyimei ~]#

    部署

    1. [root@riyimei ~]# k3d cluster create mycluster
    2. INFO[0000] Created network 'k3d-mycluster'
    3. INFO[0000] Created volume 'k3d-mycluster-images'
    4. INFO[0001] Creating node 'k3d-mycluster-server-0'
    5. INFO[0004] Pulling image 'docker.io/rancher/k3s:v1.18.6-k3s1'
    6. INFO[0011] Creating LoadBalancer 'k3d-mycluster-serverlb'
    7. INFO[0014] Pulling image 'docker.io/rancher/k3d-proxy:v3.0.1'
    8. INFO[0029] Cluster 'mycluster' created successfully!
    9. INFO[0029] You can now use it like this:
    10. kubectl cluster-info
    11. [root@riyimei ~]# kubectl cluster-info
    12. Kubernetes master is running at https://0.0.0.0:24588
    13. CoreDNS is running at https://0.0.0.0:24588/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
    14. Metrics-server is running at https://0.0.0.0:24588/api/v1/namespaces/kube-system/services/https:metrics-server:/proxy
    15. To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
    16. [root@riyimei ~]#
    17. [root@riyimei ~]# k3d kubeconfig merge mycluster --switch-context
    18. /root/.k3d/kubeconfig-mycluster.yaml
    19. [root@riyimei ~]#
    20. [root@riyimei ~]# kubectl get nodes
    21. NAME STATUS ROLES AGE VERSION
    22. k3d-mycluster-server-0 Ready master 6m47s v1.18.6+k3s1
    23. [root@riyimei ~]#
    24. [root@riyimei ~]# kubectl version --short
    25. Client Version: v1.17.4
    26. Server Version: v1.18.6+k3s1
    27. [root@riyimei ~]#
    1. [root@riyimei ~]# docker ps
    2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
    3. 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
    4. 218c19e52163 rancher/k3s:v1.18.6-k3s1 "/bin/k3s server --t…" 4 minutes ago Up 4 minutes k3d-mycluster-server-0
    1. [root@riyimei ~]# k3d cluster
    2. Manage cluster(s)
    3. Usage:
    4. k3d cluster [flags]
    5. k3d cluster [command]
    6. Available Commands:
    7. create Create a new cluster
    8. delete Delete cluster(s).
    9. list List cluster(s)
    10. start Start existing k3d cluster(s)
    11. stop Stop existing k3d cluster(s)
    12. Flags:
    13. -h, --help help for cluster
    14. Global Flags:
    15. --verbose Enable verbose output (debug logging)
    16. Use "k3d cluster [command] --help" for more information about a command.
    17. [root@riyimei ~]#
    1. [root@riyimei ~]# k3d cluster list
    2. NAME SERVERS AGENTS LOADBALANCER
    3. mycluster 1/1 0/0 true
    4. mycluster2 1/1 0/0 true
    5. [root@riyimei ~]#
    1. [root@riyimei ~]# k3d cluster list
    2. NAME SERVERS AGENTS LOADBALANCER
    3. mycluster 1/1 0/0 true
    4. mycluster2 1/1 0/0 true
    5. [root@riyimei ~]# k3d cluster stop mycluster2
    6. INFO[0000] Stopping cluster 'mycluster2'
    7. [root@riyimei ~]# k3d cluster list
    8. NAME SERVERS AGENTS LOADBALANCER
    9. mycluster 1/1 0/0 true
    10. mycluster2 0/1 0/0 true
    11. [root@riyimei ~]#