安装
1. 安装 k3s
curl -sfL https://get.k3s.io | sh -
国内用户使用以下方法加速安装:
curl -sfL http://rancher-mirror.cnrancher.com/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -
2. 更改 k3s 中 containerd 默认镜像地址
因为 k3s 默认不使用 docker, 所以修改 docker 的 mirror 是没用的,这里我们需要修改 containerd mirror 地址:
sudo vim /etc/rancher/k3s/registries.yaml
输入:
mirrors: docker.io: endpoint: - "https://*.mirror.aliyuncs.com"
注意:
- 这里的
docker.io不应该被替换为其他关键词;- 阿里云加速器地址在这里获取: https://cr.console.aliyun.com/cn-shanghai/instances/mirrors
重启 k3s:
sudo systemctl restart k3s
安装后出现的问题
1. The connection to the server localhost:8080 was refused – did you specify the right host or port?
这是 kubectl 的配置文件没有正确指向 k3s 集群导致的,检查以下几点:
- kubectl 配置文件不对,执行:
mkdir ~/.kube/ && cp /etc/rancher/k3s/k3s.yaml ~/.kube/config - 不要使用 sudo,否则会读取 root 用户的
.kube目录下配置文件
Hello World 之 k3s
本章节涉及到 pod, deployment, service, ingress, 算是一份配置文件快速启动一个可用的 k3s 应用。
1. 新建配置文件
mkdir k8s-helloworld && cd k8s-helloworld touch ping.yaml
为什么叫
ping.yaml而不是helloworld.yaml, 因为里面用于演示的应用就叫 ping。
就当做是我的恶趣味吧?。
再给配置文件填上内容:
cat <<EOF >ping.yaml apiVersion: v1 kind: Service metadata: name: ping-service spec: selector: app: ping ports: - protocol: TCP port: 80 targetPort: 8080 --- apiVersion: apps/v1 kind: Deployment metadata: name: ping-deployment labels: app: ping spec: replicas: 3 selector: matchLabels: app: ping template: metadata: labels: app: ping spec: containers: - name: ping image: bpazy/ping:v1.0.0 ports: - containerPort: 8080 --- apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: traefik-ping namespace: default annotations: kubernetes.io/ingress.class: traefik spec: rules: - host: ping.me.k3s http: paths: - path: pathType: ImplementationSpecific backend: service: name: ping-service port: number: 80 EOF
然后编辑本地 hosts 文件,填入 192.168.31.31 ping.me.k3s,注意这里的 IP 地址需要替换为 k3s 机器所在地址。
2. 使 k3s 应用该配置文件
kubectl apply -f ping.yaml
稍等片刻,访问该域名: http://ping.me.k3s/ping,你会得到以下响应:
{
"addr":[ "10.42.0.34" ], "message":"pong" }
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211562.html原文链接:https://javaforall.net
