单机部署k8s集群,并可以被外网访问

938 阅读1分钟

准备

  • CentOS 7.8
$ cat /etc/redhat-release    
CentOS Linux release 7.8.2003 (Core)
$ kind version                                             
kind v0.9.0-alpha go1.14.6 linux/amd64

关于如何安装kind请查看官网

  • docker
$ docker version
Client: Docker Engine - Community
 Version:           19.03.12
 API version:       1.40
 Go version:        go1.13.10
 Git commit:        48a66213fe
 Built:             Mon Jun 22 15:46:54 2020
 OS/Arch:           linux/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.12
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.13.10
  Git commit:       48a66213fe
  Built:            Mon Jun 22 15:45:28 2020
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.2.13
  GitCommit:        7ad184331fa3e55e52b890ea95e65ba581ae3429
 runc:
  Version:          1.0.0-rc10
  GitCommit:        dc9208a3303feef5b3839f4323d9beb36df0a9dd
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683
  • kubectl
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.6", GitCommit:"dff82dc0de47299ab66c83c626e08b245ab19037", GitTreeState:"clean", BuildDate:"2020-07-15T16:58:53Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.6", GitCommit:"dff82dc0de47299ab66c83c626e08b245ab19037", GitTreeState:"clean", BuildDate:"2020-07-19T22:21:08Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}

创建集群

# hevi为集群名称,随便起
$ kind create cluster --config kind-config.yaml --name hevi

其中的kind-config.yaml:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  kubeProxyMode: "ipvs"
nodes:
- role: control-plane
  extraMounts: 
  - hostPath: /home
    containerPath: /home
  extraPortMappings:
  - containerPort: 31080 # 这个端口在后面创建k8s的Service时要用到
    hostPort: 8080

这里只使用了一个节点,你也可以配置多个节点,具体看官网

创建成功时显示:

Creating cluster "hevi" ...
 ✓ Ensuring node image (kindest/node:v1.18.6) 🖼 
 ✓ Preparing nodes 📦  
 ✓ Writing configuration 📜 
 ✓ Starting control-plane 🕹️ 
 ✓ Installing CNI 🔌 
 ✓ Installing StorageClass 💾 
Set kubectl context to "kind-hevi"
You can now use your cluster with:

kubectl cluster-info --context kind-hevi

Thanks for using kind! 😊

执行:

$ kubectl cluster-info --context kind-hevi

Kubernetes master is running at https://127.0.0.1:35984
KubeDNS is running at https://127.0.0.1:35984/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

查看节点

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                                NAMES
4b0188704325        kindest/node:v1.18.6   "/usr/local/bin/entr…"   3 minutes ago       Up 2 minutes        127.0.0.1:35984->6443/tcp, 0.0.0.0:8080->31080/tcp   hevi-control-plane

这个就是刚刚创建的一个节点(是master节点,又是node节点,后面的pod都会在这个节点上创建),它是的一个docker容器

$ kubectl get nodes
NAME                 STATUS   ROLES    AGE   VERSION
hevi-control-plane   Ready    master   30m   v1.18.6

创建Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - image: wangyanglinux/myapp:v1
          name: myapp
          imagePullPolicy: IfNotPresent
  selector:
    matchLabels:
      app: myapp
  replicas: 2
$ kubectl apply -f myapp-deploy.yaml
deployment.apps/myapp created
$ kubectl get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE                 NOMINATED NODE   READINESS GATES
myapp-5786644968-526bh   1/1     Running   0          2m17s   10.244.0.5   hevi-control-plane   <none>           <none>
myapp-5786644968-9mc9j   1/1     Running   0          2m17s   10.244.0.6   hevi-control-plane   <none>           <none>

创建Service

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
  labels:
    app: myapp
spec:
  type: NodePort
  ports:
    - port: 80
      nodePort: 31080
      targetPort: 80
      protocol: TCP
  selector:
    app: myapp
$ kubectl apply -f myapp-svc.yaml   
service/myapp-service created
$ kubectl get svc                
NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP        5m19s
myapp-service   NodePort    10.107.93.115   <none>        80:31080/TCP   7s

访问

$ curl 127.0.0.1:8080 
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
$ curl 外网IP:8080