部署K8S的一个简单Demo

部署K8S的一个简单Demo

1、准备镜像

  • 创建目录 mkdir ./demo
  • 创建简单的http server
    $ vi server.js 
    
    var http = require('http');
    var handleRequest = function(request, response) {
      console.log('Received request for URL: ' + request.url);
      response.writeHead(200);
      response.end('Hello World!');
    };
    var www = http.createServer(handleRequest);
    www.listen(8088);
    
    
  • 创建Dockerfile
    $ vi Dockerfile 
    	
    FROM node:6.14.2
    EXPOSE 8088
    COPY server.js .
    CMD node server.js
    
  • 通过Dockerfile创建镜像
    $ docker build -t node:demo .
    

2、创建Deployment

  • 创建Deployment 方式一:

    $ kubectl create deployment kube-demo --image=node:demo
    
  • 创建Deployment 方式二:

    $ kubectl apply -f ./deployment.yaml
    
    #deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: kube-demo
      labels:
        app: demo
    spec:
      replicas: 2
      selector:
        matchLabels:
          app: demo
      template:
        metadata:
          labels:
            app: demo
        spec:
          containers:
          - name: demo
            image: kube:demo
            resources:
              limits:
                cpu: 500
                memory: 128Mi
              requests:
                cpu: 500m
                memory: 128Mi
            ports:
            - containerPort: 8088
    
    
  • 查看kubectl日志

    journalctl -xefu kubelet
    
  • 查看Deployment \ pods

    $ kubectl get deployments
    NAME        READY   UP-TO-DATE   AVAILABLE   AGE
    kube-demo   2/2     2            2           10h
    
    $ kubectl get pods
    NAME                         READY   STATUS    RESTARTS   AGE
    kube-demo-79b9647795-lx7f4   1/1     Running   0          10h
    kube-demo-79b9647795-x4swx   1/1     Running   0          10h
    

3、创建Service

  • 创建方式一:命令行

    $ kubectl expose deployment kube-demo --type=LoadBalancer --port=8090
    
  • 创建方式二:yaml文件

    kubectl create -f service.yaml
    
    #service.yaml 
    apiVersion: v1
    kind: Service
    metadata:
      name: kube-demo
      labels:
        name: kube-demo
    spec:
      type: NodePort    #或使用 LoadBalancer
      ports:
      - port: 8090  #这里的端口和clusterIP 对应,供内部访问。
        targetPort: 8088 #端口一定要和container暴露出来的端口对应
        protocol: TCP
        nodePort: 32143   # 所有的节点都会开放此端口,此端口供外部调用。
      selector:
        app: demo # 名称与deployment中定义的app名称保持一致
    
  • 查看服务 NodePort

    $ kubectl get services
    NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
    kube-demo    NodePort    10.107.253.208   <none>        8090:32143/TCP   11s
    kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP          3d23h
    
    
  • 查看pod及服务

    $ kubectl get pod,svc -n kube-system
    NAME                                         READY   STATUS    RESTARTS   AGE
    pod/coredns-5c98db65d4-6wtmm                 1/1     Running   2          3d23h
    pod/coredns-5c98db65d4-fhns5                 1/1     Running   2          3d23h
    pod/etcd-docker-desktop                      1/1     Running   0          3d23h
    pod/kube-apiserver-docker-desktop            1/1     Running   0          3d23h
    pod/kube-controller-manager-docker-desktop   1/1     Running   0          3d23h
    pod/kube-proxy-q5r4c                         1/1     Running   0          3d23h
    pod/kube-scheduler-docker-desktop            1/1     Running   0          3d23h
    
    NAME               TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
    service/kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   3d23h
    

4、验证访问

  • 通过curl
    $ curl 127.0.0.1:32143
    Hello World!
    

5、清理

  • 清理
    kubectl delete service hello-node
    kubectl delete deployment hello-node
    

参考资料: kubernetes.io
代码地址:https://github.com/WangShuai822/kube-demo

你可能感兴趣的:(DevOps,K8S)