https://docs.rancher.cn/k3s/

    国内安装地址

    1. curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server
    1. [root@node01 ~]# systemctl status k3s.service
    2. k3s.service - Lightweight Kubernetes
    3. Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: disabled)
    4. Active: active (running) since Sat 2021-11-27 16:04:34 CST; 7s ago
    5. Docs: https://k3s.io
    6. Process: 2482 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
    7. Process: 2480 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
    8. Main PID: 2485 (k3s-server)
    9. Tasks: 44
    10. Memory: 467.1M
    11. CGroup: /system.slice/k3s.service
    12. ├─2485 /usr/local/bin/k3s server
    13. └─2524 containerd
    14. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.320240 2485 iptables.go:172] Deleting iptables rule: -s 10.42.0.0/16 ! -...QUERADE
    15. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.320344 2485 iptables.go:172] Deleting iptables rule: -d 10.42.0.0/16 -j ACCEPT
    16. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.321345 2485 iptables.go:172] Deleting iptables rule: ! -s 10.42.0.0/16 -... RETURN
    17. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.321659 2485 iptables.go:160] Adding iptables rule: -s 10.42.0.0/16 -j ACCEPT
    18. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.323496 2485 iptables.go:172] Deleting iptables rule: ! -s 10.42.0.0/16 -...QUERADE
    19. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.324686 2485 iptables.go:160] Adding iptables rule: -d 10.42.0.0/16 -j ACCEPT
    20. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.326062 2485 iptables.go:160] Adding iptables rule: -s 10.42.0.0/16 -d 10... RETURN
    21. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.328981 2485 iptables.go:160] Adding iptables rule: -s 10.42.0.0/16 ! -d ...QUERADE
    22. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.331398 2485 iptables.go:160] Adding iptables rule: ! -s 10.42.0.0/16 -d ... RETURN
    23. Nov 27 16:04:35 node01 k3s[2485]: I1127 16:04:35.333897 2485 iptables.go:160] Adding iptables rule: ! -s 10.42.0.0/16 -d ...QUERADE
    24. Hint: Some lines were ellipsized, use -l to show in full.
    25. [root@node01 ~]#
    1. [root@node01 ~]# crictl info
    2. {
    3. "status": {
    4. "conditions": [
    5. {
    6. "type": "RuntimeReady",
    7. "status": true,
    8. "reason": "",
    9. "message": ""
    10. },
    11. {
    12. "type": "NetworkReady",
    13. "status": true,
    14. "reason": "",
    15. "message": ""
    16. }
    17. ]
    18. },
    19. "cniconfig": {
    20. "PluginDirs": [
    21. "/var/lib/rancher/k3s/data/9d8f9670e1bff08a901bc7bc270202323f7c2c716a89a73d776c363ac1971018/bin"
    22. ],
    23. "PluginConfDir": "/var/lib/rancher/k3s/agent/etc/cni/net.d",
    24. "PluginMaxConfNum": 1,
    25. "Prefix": "eth",
    26. "Networks": [
    27. {
    28. "Config": {
    29. "Name": "cni-loopback",
    30. "CNIVersion": "0.3.1",
    31. "Plugins": [
    32. {
    33. "Network": {
    34. "type": "loopback",
    35. "ipam": {},
    36. "dns": {}
    37. },
    38. "Source": "{\"type\":\"loopback\"}"
    39. }
    40. ],
    41. "Source": "{\n\"cniVersion\": \"0.3.1\",\n\"name\": \"cni-loopback\",\n\"plugins\": [{\n \"type\": \"loopback\"\n}]\n}"
    42. },
    43. "IFName": "lo"
    44. },
    45. {
    46. "Config": {
    47. "Name": "cbr0",
    48. "CNIVersion": "0.3.1",
    49. "Plugins": [
    50. {
    51. "Network": {
    52. "type": "flannel",
    53. "ipam": {},
    54. "dns": {}
    55. },
    56. "Source": "{\"delegate\":{\"forceAddress\":true,\"hairpinMode\":true,\"isDefaultGateway\":true},\"type\":\"flannel\"}"
    57. },
    58. {
    59. "Network": {
    60. "type": "portmap",
    61. "capabilities": {
    62. "portMappings": true
    63. },
    64. "ipam": {},
    65. "dns": {}
    66. },
    67. "Source": "{\"capabilities\":{\"portMappings\":true},\"type\":\"portmap\"}"
    68. }
    69. ],
    70. "Source": "{\n \"name\":\"cbr0\",\n \"cniVersion\":\"0.3.1\",\n \"plugins\":[\n {\n \"type\":\"flannel\",\n \"delegate\":{\n \"hairpinMode\":true,\n \"forceAddress\":true,\n \"isDefaultGateway\":true\n }\n },\n {\n \"type\":\"portmap\",\n \"capabilities\":{\n \"portMappings\":true\n }\n }\n ]\n}\n"
    71. },
    72. "IFName": "eth0"
    73. }
    74. ]
    75. },
    76. "config": {
    77. "containerd": {
    78. "snapshotter": "overlayfs",
    79. "defaultRuntimeName": "runc",
    80. "defaultRuntime": {
    81. "runtimeType": "",
    82. "runtimeEngine": "",
    83. "PodAnnotations": null,
    84. "ContainerAnnotations": null,
    85. "runtimeRoot": "",
    86. "options": null,
    87. "privileged_without_host_devices": false,
    88. "baseRuntimeSpec": ""
    89. },
    90. "untrustedWorkloadRuntime": {
    91. "runtimeType": "",
    92. "runtimeEngine": "",
    93. "PodAnnotations": null,
    94. "ContainerAnnotations": null,
    95. "runtimeRoot": "",
    96. "options": null,
    97. "privileged_without_host_devices": false,
    98. "baseRuntimeSpec": ""
    99. },
    100. "runtimes": {
    101. "runc": {
    102. "runtimeType": "io.containerd.runc.v2",
    103. "runtimeEngine": "",
    104. "PodAnnotations": null,
    105. "ContainerAnnotations": null,
    106. "runtimeRoot": "",
    107. "options": null,
    108. "privileged_without_host_devices": false,
    109. "baseRuntimeSpec": ""
    110. }
    111. },
    112. "noPivot": false,
    113. "disableSnapshotAnnotations": true,
    114. "discardUnpackedLayers": false
    115. },
    116. "cni": {
    117. "binDir": "/var/lib/rancher/k3s/data/9d8f9670e1bff08a901bc7bc270202323f7c2c716a89a73d776c363ac1971018/bin",
    118. "confDir": "/var/lib/rancher/k3s/agent/etc/cni/net.d",
    119. "maxConfNum": 1,
    120. "confTemplate": ""
    121. },
    122. "registry": {
    123. "mirrors": {
    124. "docker.io": {
    125. "endpoint": [
    126. "https://registry-1.docker.io"
    127. ],
    128. "rewrite": null
    129. }
    130. },
    131. "configs": null,
    132. "auths": null,
    133. "headers": null
    134. },
    135. "imageDecryption": {
    136. "keyModel": ""
    137. },
    138. "disableTCPService": true,
    139. "streamServerAddress": "127.0.0.1",
    140. "streamServerPort": "10010",
    141. "streamIdleTimeout": "4h0m0s",
    142. "enableSelinux": false,
    143. "selinuxCategoryRange": 1024,
    144. "sandboxImage": "rancher/pause:3.1",
    145. "statsCollectPeriod": 10,
    146. "systemdCgroup": false,
    147. "enableTLSStreaming": false,
    148. "x509KeyPairStreaming": {
    149. "tlsCertFile": "",
    150. "tlsKeyFile": ""
    151. },
    152. "maxContainerLogSize": 16384,
    153. "disableCgroup": false,
    154. "disableApparmor": false,
    155. "restrictOOMScoreAdj": false,
    156. "maxConcurrentDownloads": 3,
    157. "disableProcMount": false,
    158. "unsetSeccompProfile": "",
    159. "tolerateMissingHugetlbController": true,
    160. "disableHugetlbController": true,
    161. "ignoreImageDefinedVolumes": false,
    162. "containerdRootDir": "/var/lib/rancher/k3s/agent/containerd",
    163. "containerdEndpoint": "/run/k3s/containerd/containerd.sock",
    164. "rootDir": "/var/lib/rancher/k3s/agent/containerd/io.containerd.grpc.v1.cri",
    165. "stateDir": "/run/k3s/containerd/io.containerd.grpc.v1.cri"
    166. },
    167. "golang": "go1.16.8",
    168. "lastCNILoadStatus": "OK"
    169. }
    170. [root@node01 ~]#

    添加镜像加速配置
    https://docs.rancher.cn/docs/k3s/advanced/_index#%E9%85%8D%E7%BD%AE-containerd

    1. [root@node01 ~]# crictl info |grep registry
    2. "registry": {
    3. "https://registry-1.docker.io"
    4. [root@node01 ~]# ll /var/lib/rancher/k3s/agent/etc/containerd/config.toml
    5. -rw-r--r-- 1 root root 576 Nov 27 16:04 /var/lib/rancher/k3s/agent/etc/containerd/config.toml
    6. [root@node01 ~]# touch /var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl
    7. [root@node01 ~]# ll /var/lib/rancher/k3s/agent/etc/containerd
    8. total 4
    9. -rw-r--r-- 1 root root 576 Nov 27 16:04 config.toml
    10. -rw-r----- 1 root root 0 Nov 27 16:12 config.toml.tmpl
    11. [root@node01 ~]# vim /var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl
    12. [root@node01 ~]#
    13. [root@node01 ~]# ll /var/lib/rancher/k3s/agent/etc/containerd
    14. total 8
    15. -rw-r--r-- 1 root root 576 Nov 27 16:04 config.toml
    16. -rw-r----- 1 root root 169 Nov 27 16:13 config.toml.tmpl
    17. [root@node01 ~]# cat /var/lib/rancher/k3s/agent/etc/containerd/config.toml.tmpl
    18. # 在 config.toml.tmpl 文件中添加
    19. [plugins.cri.registry.mirrors]
    20. [plugins.cri.registry.mirrors."docker.io"]
    21. endpoint = ["https://docker.mirrors.ustc.edu.cn"]
    22. [root@node01 ~]#
    1. [root@node01 ~]# crictl info |grep mirror
    2. "mirrors": {
    3. "https://docker.mirrors.ustc.edu.cn"
    4. [root@node01 ~]#
    1. systemctl restart k3s.service

    —docker 指定 Docker 为运行时

    1. curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server --docker
    1. k3s kubectl get node
    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 ~]#