脚本安装:

  1. wget -q -O - https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash
  2. curl -s https://raw.githubusercontent.com/rancher/k3d/master/install.sh | bash

homebrew安装:

  1. brew install k3d

设置环境变量:

  1. export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"

本地安装kubectl:

  1. cat <<EOF > /etc/yum.repos.d/kubernetes.repo
  2. [kubernetes]
  3. name=Kubernetes
  4. baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
  5. enabled=1
  6. gpgcheck=1
  7. gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
  8. http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
  9. EOF
  10. yum install kubectl -y
  11. ln /usr/bin/kubectl /usr/local/bin/kubectl

查看docker容器内集群信息:

  1. kubectl cluster-info

查看帮助文档:

  1. $ k3d -h
  2. NAME:
  3. k3d - Run k3s in Docker!
  4. USAGE:
  5. k3d [global options] command [command options] [arguments...]
  6. VERSION:
  7. v1.7.0
  8. COMMANDS:
  9. check-tools, ct Check if docker is running
  10. shell Start a subshell for a cluster
  11. create, c Create a single- or multi-node k3s cluster in docker containers
  12. add-node [EXPERIMENTAL] Add nodes to an existing k3d/k3s cluster (k3d by default)
  13. delete, d, del Delete cluster
  14. stop Stop cluster
  15. start Start a stopped cluster
  16. list, ls, l List all clusters
  17. get-kubeconfig Get kubeconfig location for cluster
  18. import-images, i Import a comma- or space-separated list of container images from your local docker daemon into the cluster
  19. version print k3d and k3s version
  20. help, h Shows a list of commands or help for one command
  21. GLOBAL OPTIONS:
  22. --verbose Enable verbose output
  23. --timestamp Enable timestamps in logs messages
  24. --help, -h show help
  25. --version, -v print the version

创建k3s集群

创建一个集群,设置参数不使用traefik:

  1. k3d create --server-arg "--no-deploy=traefik"

下载arkade

  1. curl -sSL https://dl.get-arkade.dev | sudo sh

接下来可以通过arkade安装应用:

  1. arkade install --help

删除默认集群:

  1. k3d delete

暴露服务

参考 https://github.com/rancher/k3d/blob/master/docs/examples.md

Ingress

1、创建集群:

  1. k3d create --publish 8081:80 --workers 2

2、设置环境变量:

  1. export KUBECONFIG="$(k3d get-kubeconfig --name='k3s-default')"

安装kubectl,参考上文。查看集群信息:

  1. $ kubectl cluster-info
  2. Kubernetes master is running at https://localhost:6443
  3. CoreDNS is running at https://localhost:6443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
  4. Met

查看节点信息:

  1. $ kubectl get node -o wide
  2. NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
  3. k3d-k3s-default-worker-1 Ready <none> 10m v1.17.3+k3s1 172.19.0.4 <none> Unknown 4.10.4-1.el7.elrepo.x86_64 containerd://1.3.3-k3s1
  4. k3d-k3s-default-worker-0 Ready <none> 10m v1.17.3+k3s1 172.19.0.3 <none> Unknown 4.10.4-1.el7.elrepo.x86_64 containerd://1.3.3-k3s1
  5. k3d-k3s-default-server Ready master 10m v1.17.3+k3s1 172.19.0.2 <none> Unknown 4.10.4-1.el7.elrepo.x86_64 containerd://1.3.3-k3s1

3、创建nginx deployment:

  1. kubectl create deployment nginx --image=nginx

4、创建一个clueterip服务:

  1. kubectl create service clusterip nginx --tcp=80:80

5、创建一个Ingress,注意:k3s默认安装了treafik ingress

  1. cat << EOF | kubectl apply -f -
  2. apiVersion: extensions/v1beta1
  3. kind: Ingress
  4. metadata:
  5. name: nginx
  6. annotations:
  7. ingress.kubernetes.io/ssl-redirect: "false"
  8. spec:
  9. rules:
  10. - http:
  11. paths:
  12. - path: /
  13. backend:
  14. serviceName: nginx
  15. servicePort: 80
  16. EOF

查看状态:

  1. kubectl get pod,svc,ing
  2. NAME READY STATUS RESTARTS AGE
  3. pod/nginx-86c57db685-vsmlq 1/1 Running 0 3m44s
  4. NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
  5. service/kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 8m45s
  6. service/nginx ClusterIP 10.43.71.250 <none> 80/TCP 3m38s
  7. NAME HOSTS ADDRESS PORTS AGE
  8. ingress.extensions/nginx * 172.19.0.3 80 119s

可以看到ingress的地址为 172.19.0.3,是docker容器的IP,k3d创建k3s集群时,通过 --publish 8081:80 将容器的80端口暴露到宿主机的8081了,所以可以本地访问8081
6、本地访问:

  1. curl localhost:8081/

通过浏览器访问:http://144.34.194.100:8081/

NodePort

1、创建集群:
将k3d-k3s-default-worker-0节点上的30080端口暴露到宿主机的8082端口

  1. k3d create --publish 8082:30080@k3d-k3s-default-worker-0 --workers 2

2、3步骤和上面一样
4、创建Service

  1. cat << EOF | kubectl apply -f -
  2. apiVersion: v1
  3. kind: Service
  4. metadata:
  5. labels:
  6. app: nginx
  7. name: nginx
  8. spec:
  9. ports:
  10. - name: 80-80
  11. nodePort: 30080
  12. port: 80
  13. protocol: TCP
  14. targetPort: 80
  15. selector:
  16. app: nginx
  17. type: NodePort
  18. EOF

5、本地访问:

  1. curl localhost:8082/