使用kind,在虚拟服务器上创建一个适用于自己的Kubernetes集群

你有没有想要使用一个方便轻松的Kubernetes环境去自由地尝试其功能或者进行认证学习的想法呢?

使用Podman Desktop等本地构建会导致机器变得很重,而使用Kubernetes的托管服务又不能自由地修改控制平面…

因此,我决定在IBM Cloud的虚拟服务器上使用kind来创建一个专属于自己的Kubernetes环境!

“kind”是什么意思?

logo.png

“Kind是官方提供的一款能够轻松创建Kubernetes集群的工具。它是Kubernetes in Docker的简称。由于Docker可以将一个容器作为一个节点运行,所以可以在一台服务器上实现单节点配置或多节点配置。”

让我们在VPC中尝试引入VSI的kind。

构建的步骤

    1. 订购虚拟服务器

 

    1. 安装Docker

 

    1. 安装kind

 

    安装所需的kubectl和其他CLI工具

1. 下订单虚拟服务器

我将订购符合以下要求的服务器。

    • OS: Ubuntu

 

    • スペック:私は4×16で作成しました

 

    • Network: Floating IP

 

    Security: SSHでアクセス可能なSecurity Groupが結びついている

如果不清楚服务器订购方法或SSH密钥注册方法,我推荐阅读这篇文章。

 

2. 安装Docker

我将通过SSH访问已订购的服务器并安装Docker。

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

# Add Docker's official GPG key:
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Use the following command to set up the repository:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" |
  sudo tee /etc/apt/sources.list.d/docker.list >/dev/null

# Update the apt package index
sudo apt-get update

# Install Docker Engine
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

# Test
docker version

3. 安装kind

接下来,我们需要安装 kind。
只需下载 CLI 二进制文件并将其放置即可。

curl -Lo ./kind https://github.com/kubernetes-sigs/kind/releases/latest/download/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/local/bin/kind

# Test
kind version

安装所需的CLI,如kubectl等。

最后,我们将安装kubectl。

curl -LO "https://dl.k8s.io/release/$(curl -LS https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

# Test
kubectl version --client=true

以上!这是一个非常简单的kind引入。只需要安装Docker、kind和kubectl。

使用Kind创建Kubernetes集群。

好的,让我们立即开始使用kind构建Kubernetes集群。

搭建Cluster非常简单!只需要执行一行命令即可。

kind create cluster --name kind

执行结果

root@satokota-vsi:~# kind create cluster --name kind
Creating cluster "kind" ...
 ✓ Ensuring node image (kindest/node:v1.27.3) ? 
 ✓ Preparing nodes ?  
 ✓ Writing configuration ? 
 ✓ Starting control-plane ?️ 
 ✓ Installing CNI ? 
 ✓ Installing StorageClass ? 
Set kubectl context to "kind-kind"
You can now use your cluster with:

kubectl cluster-info --context kind-kind

Thanks for using kind! ?

我们成功创建了一个Kubernetes集群!现在只需要尽情玩乐了!

root@satokota-vsi:~# kubectl get node
NAME                 STATUS   ROLES           AGE    VERSION
kind-control-plane   Ready    control-plane   105s   v1.27.3

root@satokota-vsi:~# kubectl run nginx --image=nginx
pod/nginx created

root@satokota-vsi:~# kubectl get pod
NAME    READY   STATUS              RESTARTS   AGE
nginx   0/1     ContainerCreating   0          2s

root@satokota-vsi:~# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          8s

请参考后面的选项部分,其中详细说明了应用程序发布的方法。

顺便提一下,删除集群只需一条命令即可。

kind delete cluster -n kind

我认为有些人可能希望使用多个节点构建,并且使用特定的Kubernetes版本。通过kind,您可以使用yaml文件来更改要创建的集群配置。由于我已经尝试了几种不同的集群配置,如果您有兴趣,请尝试创建和使用不同的集群。

创建由多个节点组成的集群

一套指挥飞机和两个节点的三台构成

cat >kind-config.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
EOF

kind create cluster --name kind --config kind-config.yaml

确认

root@satokota-vsi:~# kubectl get node
NAME                 STATUS   ROLES           AGE   VERSION
kind-control-plane   Ready    control-plane   42s   v1.27.3
kind-worker          Ready    <none>          18s   v1.27.3
kind-worker2         Ready    <none>          19s   v1.27.3

我创建了一个由3个节点组成的集群。由于是以一个容器和一个节点的方式进行组成,所以当我用Docker ps来确认时,看到有3个容器在运行。

root@satokota-vsi:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED              STATUS          PORTS                       NAMES
635c7ddd9031   kindest/node:v1.27.3   "/usr/local/bin/entr…"   About a minute ago   Up 54 seconds   127.0.0.1:39619->6443/tcp   kind-control-plane
37579a4bf964   kindest/node:v1.27.3   "/usr/local/bin/entr…"   About a minute ago   Up 54 seconds                               kind-worker
8e3cb91f41e9   kindest/node:v1.27.3   "/usr/local/bin/entr…"   About a minute ago   Up 54 seconds                               kind-worker2

打扫卫生

kind delete cluster -n kind

使用 NodePort 来公开应用程序。

通过使用Docker容器的端口映射,将8080端口和NodePort的30000端口进行映射,以便可以从集群外部进行访问。

cat >./kind-config.yaml <<EOF
apiVersion: kind.x-k8s.io/v1alpha4
kind: Cluster
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 30000 #30000 - 32767
    hostPort: 8080
EOF

kind create cluster --name kind --config kind-config.yaml

确认

root@satokota-vsi:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                                NAMES
5d3d608d89da   kindest/node:v1.27.3   "/usr/local/bin/entr…"   41 seconds ago   Up 32 seconds   127.0.0.1:34243->6443/tcp, 0.0.0.0:8080->30000/tcp   kind-control-plane

当端口为0.0.0.0:8080->30000/tcp,访问localhost:8080即可访问已经通过NodePort 30000公开的服务。

我将尝试部署Nginx。

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    resources: {}
  dnsPolicy: ClusterFirst
  restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    run: nginx
  name: nginx
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30000
  selector:
    run: nginx
status:
  loadBalancer: {}
EOF

在确认创建了Pod和Service之后,尝试通过NodePort访问应用程序。

root@satokota-vsi:~# kubectl get pod
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          4s

root@satokota-vsi:~# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        9m48s
nginx        NodePort    10.96.161.198   <none>        80:30000/TCP   7s

root@satokota-vsi:~# docker ps
CONTAINER ID   IMAGE                  COMMAND                  CREATED          STATUS          PORTS                                                NAMES
5d3d608d89da   kindest/node:v1.27.3   "/usr/local/bin/entr…"   10 minutes ago   Up 10 minutes   127.0.0.1:34243->6443/tcp, 0.0.0.0:8080->30000/tcp   kind-control-plane

root@satokota-vsi:~# curl localhost:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
image.png

打扫卫生

kind delete cluster -n kind

[Option] 在Ingress上发布应用程序。

cat >./kind-config.yaml <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  kubeadmConfigPatches:
  - |
    kind: InitConfiguration
    nodeRegistration:
      kubeletExtraArgs:
        node-labels: "ingress-ready=true"
  extraPortMappings:
  - containerPort: 80
    hostPort: 80
    protocol: TCP
  - containerPort: 443
    hostPort: 443
    protocol: TCP
- role: worker
- role: worker
EOF

kind create cluster --name kind --config ./kind-config.yaml

# Install Ingress - nginx
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/kind/deploy.yaml

# Check
kubectl --namespace ingress-nginx get all

我将部署并使用Ingress公开nginx。

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy: {}
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - image: nginx:latest
          name: nginx
          ports:
            - containerPort: 80
          resources:
            requests:
              memory: "32Mi"
              cpu: "100m"
            limits:
              memory: "64Mi"
              cpu: "200m"

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx
  name: nginx-service
spec:
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  selector:
    app: nginx
status:
  loadBalancer: {}

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - http:
        paths:
          - pathType: Prefix
            path: /
            backend:
              service:
                name: nginx-service
                port:
                  number: 80
EOF

在确认创建了Pod或Service的情况下,尝试通过Ingress访问应用程序。

root@satokota-vsi:~# kubectl get deploy,svc,ingress
NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           12s

NAME                    TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/kubernetes      ClusterIP   10.96.0.1      <none>        443/TCP   108s
service/nginx-service   ClusterIP   10.96.64.209   <none>        80/TCP    12s

NAME                                      CLASS    HOSTS   ADDRESS   PORTS   AGE
ingress.networking.k8s.io/nginx-ingress   <none>   *                 80      12s

root@satokota-vsi:~# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
image.png

清洁

kind delete cluster -n kind
广告
将在 10 秒后关闭
bannerAds