Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一摸 k3s: 安装历程 #195

Open
Bpazy opened this issue Jul 21, 2021 · 2 comments
Open

一摸 k3s: 安装历程 #195

Bpazy opened this issue Jul 21, 2021 · 2 comments
Labels

Comments

@Bpazy
Copy link
Owner

Bpazy commented Jul 21, 2021

安装

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"

注意:

  1. 这里的 docker.io 不应该被替换为其他关键词;
  2. 阿里云加速器地址在这里获取: https://cr.console.aliyun.com/cn-shanghai/instances/mirrors

重启 k3s:

sudo systemctl restart k3s
@Bpazy
Copy link
Owner Author

Bpazy commented Jul 25, 2021

安装后出现的问题

1. The connection to the server localhost:8080 was refused - did you specify the right host or port?

这是 kubectl 的配置文件没有正确指向 k3s 集群导致的,检查以下几点:

  1. kubectl 配置文件不对,执行: mkdir ~/.kube/ && cp /etc/rancher/k3s/k3s.yaml ~/.kube/config
  2. 不要使用 sudo,否则会读取 root 用户的 .kube 目录下配置文件

@Bpazy
Copy link
Owner Author

Bpazy commented Jul 26, 2021

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"
}

@Bpazy Bpazy changed the title k3s 安装历程 一摸 k3s Feb 29, 2024
@Bpazy Bpazy changed the title 一摸 k3s 一摸 k3s: 安装历程 Feb 29, 2024
@Bpazy Bpazy added the K3S label Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant