简体   繁体   English

如何更改/设置 k8s 主节点内部 IP 或公共 IP?

[英]How to change/set k8s master node internal-ip or public-ip?

I have installed k3s on a Cloud VM.我已经在云虚拟机上安装了 k3s。 (k3s is very similar to k8s. ) (k3s 与 k8s 非常相似。)

k3s server start as a master node. k3s 服务器作为主节点启动。

And the master node's label shows internal-ip is 192.168.xxx.xxx.而master节点的label显示internal-ip是192.168.xxx.xxx。 And the master node's annotations shows public-ip is also 192.168.xxx.xxx.并且主节点的注解显示public-ip也是192.168.xxx.xxx。

But the real public-ip of CloudVM is 49.xx.xx.xx.但是CloudVM的真实公网ip是49.xx.xx.xx。 So agent from annother machine cannot connecting this master node.所以来自另一台机器的代理无法连接这个主节点。 Because agent always tries to connect proxy "wss://192.168.xxx.xxx:6443/...".因为代理总是尝试连接代理“wss://192.168.xxx.xxx:6443/...”。

If I run ifconfig on the Cloud VM, public-ip(49.xx.xx.xx) does not show.如果我在 Cloud VM 上运行 ifconfig,则不会显示 public-ip(49.xx.xx.xx)。 So k3s not find the right internal-ip or public-ip.所以 k3s 找不到正确的 internal-ip 或 public-ip。

I try to start k3s with --bind-address=49.xx.xx.xx, but start fail.我尝试使用--bind-address=49.xx.xx.xx 启动k3s,但启动失败。 I guess no NIC bind with this ip-address.我猜没有 NIC 与这个 IP 地址绑定。

How to resolve this problem, If I try to create a virtual netcard with address 49.xx.xx.xx?如果我尝试创建地址为 49.xx.xx.xx 的虚拟网卡,如何解决此问题?

The best option to connect Kubernetes master and nodes is using private network.连接 Kubernetes 主节点和节点的最佳选择是使用专用网络。

How to setup K3S master and single node cluster:如何设置 K3S 主节点和单节点集群:

Prerequisites:先决条件:

  • All the machines need to be inside the same private network.所有机器都需要在同一个专用网络内。 For example 192.168.0.0/24例如 192.168.0.0/24
  • All the machines need to communicate with each other.所有机器都需要相互通信。 You can ping them with: $ ping IP_ADDRESS您可以使用以下命令 ping 它们: $ ping IP_ADDRESS

In this example there are 2 virtual machines:在此示例中,有 2 个虚拟机:

  • Master node (k3s) with private ip of 10.156.0.13具有私有 ip 的 10.156.0.13 的主节点 (k3s)
  • Worker node (k3s-2) with private ip of 10.156.0.8工作节点 (k3s-2),私有 ip 为 10.156.0.8

在此处输入图像描述

Establish connection between VM's建立虚拟机之间的连接

The most important thing is to check if the machines can connect with each other.最重要的是检查机器是否可以相互连接。 As I said, the best way would be just to ping them.正如我所说,最好的方法就是 ping 它们。

Provision master node供应主节点

To install K3S on master node you need to invoke command from root user:要在主节点上安装 K3S,您需要从 root 用户调用命令:

$ curl -sfL https://get.k3s.io | sh -

The output of this command should be like this:这个命令的output应该是这样的:

[INFO]  Finding latest release
[INFO]  Using v0.10.2 as release
[INFO]  Downloading hash https://github.com/rancher/k3s/releases/download/v0.10.2/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/rancher/k3s/releases/download/v0.10.2/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s.service
[INFO]  systemd: Enabling k3s unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s.service → /etc/systemd/system/k3s.service.
[INFO]  systemd: Starting k3s

Check if master node is working:检查主节点是否正常工作:

$ kubectl get nodes

Output of above command should be like this:上述命令的 Output 应该是这样的:

NAME   STATUS   ROLES    AGE     VERSION
k3s    Ready    master   2m14s   v1.16.2-k3s.1

Retrieve the IMPORTANT_TOKEN from master node with command:使用以下命令从主节点检索IMPORTANT_TOKEN

$ cat /var/lib/rancher/k3s/server/node-token

This token will be used to connect agent node to master node.此令牌将用于将代理节点连接到主节点。 Copy it复制它

Connect agent node to master node将代理节点连接到主节点

Ensure that node can communicate with master.确保节点可以与主节点通信。 After that you can invoke command from root user:之后,您可以从 root 用户调用命令:

$ curl -sfL https://get.k3s.io | K3S_URL=https://MASTER_NODE_IP:6443 K3S_TOKEN=IMPORTANT_TOKEN sh -

Paste your IMPORTANT_TOKEN into this command.将您的 IMPORTANT_TOKEN 粘贴到此命令中。

In this case the MASTER_NODE_IP is the 10.156.0.13.在这种情况下,MASTER_NODE_IP 是 10.156.0.13。

Output of this command should look like this:此命令的 Output 应如下所示:

[INFO]  Finding latest release
[INFO]  Using v0.10.2 as release
[INFO]  Downloading hash https://github.com/rancher/k3s/releases/download/v0.10.2/sha256sum-amd64.txt
[INFO]  Downloading binary https://github.com/rancher/k3s/releases/download/v0.10.2/k3s
[INFO]  Verifying binary download
[INFO]  Installing k3s to /usr/local/bin/k3s
[INFO]  Creating /usr/local/bin/kubectl symlink to k3s
[INFO]  Creating /usr/local/bin/crictl symlink to k3s
[INFO]  Creating /usr/local/bin/ctr symlink to k3s
[INFO]  Creating killall script /usr/local/bin/k3s-killall.sh
[INFO]  Creating uninstall script /usr/local/bin/k3s-agent-uninstall.sh
[INFO]  env: Creating environment file /etc/systemd/system/k3s-agent.service.env
[INFO]  systemd: Creating service file /etc/systemd/system/k3s-agent.service
[INFO]  systemd: Enabling k3s-agent unit
Created symlink /etc/systemd/system/multi-user.target.wants/k3s-agent.service → /etc/systemd/system/k3s-agent.service.
[INFO]  systemd: Starting k3s-agent

Test测试

Invoke command on master node to check if agent connected successfully:在主节点上调用命令以检查代理是否连接成功:

$ kubectl get nodes

Node which you added earlier should be visible here:您之前添加的节点应该在此处可见:

NAME    STATUS   ROLES    AGE     VERSION
k3s     Ready    master   15m     v1.16.2-k3s.1
k3s-2   Ready    <none>   3m19s   v1.16.2-k3s.1

Above output concludes that the provisioning has happened correctly.以上 output 得出的结论是配置已正确进行。

EDIT1: From this point you can deploy pods and expose them into public IP space. EDIT1:从这一点开始,您可以部署 pod 并将它们公开到公共 IP 空间中。

EDIT2:编辑2:

You can connect the K3S master and worker nodes on public IP network but there are some prerequisites.您可以在公共 IP 网络上连接 K3S 主节点和工作节点,但有一些先决条件。

Prerequsities:先决条件:

  • Master node need to have port 6443/TCP open主节点需要打开 6443/TCP 端口
  • Ensure that master node has reserved static IP address确保主节点已预留static IP地址
  • Ensure that firewall rules are configured to allow access only by IP address of worker nodes (static ip addresses for nodes can help with that)确保将防火墙规则配置为仅允许通过工作节点的 IP 地址进行访问(节点的静态 ip 地址可以帮助实现这一点)

Provisioning of master node主节点供应

The deployment of master node is the same as above.主节点部署同上。 The only difference is that you need to get his public ip address.唯一不同的是,你需要得到他的公共 ip 地址。

Your master node does not need to show your public IP in commands like:您的主节点不需要在以下命令中显示您的公共 IP:

  • $ ip a
  • $ ifconfig

Provisioning worker nodes供应工作节点

The deployment of worker nodes is different only in manner of changing IP address of master node from private one to public one.工作节点的部署不同之处仅在于将主节点的IP地址从私有地址更改为公有地址。 Invoke this command from root account:从 root 帐户调用此命令:
curl -sfL https://get.k3s.io | K3S_URL=https://PUBLIC_IP_OF_MASTER_NODE:6443 K3S_TOKEN=IMPORTANT_TOKEN sh -

Testing the cluster测试集群

To ensure that nodes are connected properly you need to invoke command:要确保节点正确连接,您需要调用命令:

$ kubectl get nodes

The output should be something like this: output 应该是这样的:

NAME    STATUS   ROLES    AGE   VERSION
k3s-4   Ready    <none>   68m   v1.16.2-k3s.1
k3s-1   Ready    master   69m   v1.16.2-k3s.1
k3s-3   Ready    <none>   69m   v1.16.2-k3s.1
k3s-2   Ready    <none>   68m   v1.16.2-k3s.1

All of the nodes should be visible here.所有节点都应该在这里可见。

I also had the same problem, and finally found a solution.我也遇到了同样的问题,终于找到了解决办法。 You can start your server with --node-external-ip , like this sudo k3s server --node-external-ip 49.xx.xx.xx , and agent need config env or start with sudo k3s agent --server https://49.xx.xx.xx:6443 --token ${K3S_TOKEN} , then your local device (edge node) from private IP can connect public cloud.您可以使用--node-external-ip启动服务器,例如sudo k3s server --node-external-ip 49.xx.xx.xx ,并且代理需要配置环境或从sudo k3s agent --server https://49.xx.xx.xx:6443 --token ${K3S_TOKEN} ,然后你的本地设备(边缘节点)来自私有 IP 可以连接公共云。

The flag usage is (listener) IP address that apiserver uses to advertise to members of the cluster (default: node-external-ip/node-ip)标志用法是(listener) IP address that apiserver uses to advertise to members of the cluster (default: node-external-ip/node-ip)

This picture shows my computer connect remote server, I test it, raspberry 4B also success.这张图是我的电脑连接远程服务器,我测试了一下,raspberry 4B也成功了。

在此处输入图像描述

The load balancer not switch public IP to private IP, and when I use git blame get the flag feature add time is 2019.10.26负载均衡器没有将公共 IP 切换到私有 IP,当我使用 git 责备获得标志功能时,添加时间是 2019.10.26

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM