K8S基础环境四联:
(一)ETCD集群部署
(二)安装配置 Flannel Docker
(三)手工部署kubernetes-1.17.0
(四)K8S之HelloWorld


K8S集群搭建完成后,HelloWorld自然也不能缺席。
本文使用K8S按如下结构图关系部署 HelloWorld 容器。
在这里插入图片描述

步骤

1、创建deployment文件(RS=3个副本)
helloworld-nodejs-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: helloworld-nodejs
  name: helloworld-nodejs
spec:
  replicas: 3
  selector:
    matchLabels:
      app: helloworld-nodejs
  template:
    metadata:
      labels:
        app: helloworld-nodejs
    spec:
      containers:
      - image: docker.io/xzxiaoshan/helloworld-nodejs:latest
        name: helloworld-nodejs
        ports:
        - containerPort: 8080

2、创建service文件(NodePort 模式)
helloworld-nodejs-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: helloworld-nodejs
spec:
  selector:
      app: helloworld-nodejs
  ports:
    - name: http
      port: 80
      protocol: TCP
      targetPort: 8080
      nodePort: 30001
  type: NodePort

3、创建deployment和service

kubectl apply -f helloworld-nodejs-deployment.yaml
kubectl apply -f helloworld-nodejs-service.yaml

4、查看结果

[root@server1 k8s]# kubectl get deployment -o wide
NAME                READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS          IMAGES                                          SELECTOR
helloworld-nodejs   3/3     3            3           147m   helloworld-nodejs   docker.io/xzxiaoshan/helloworld-nodejs:latest   app=helloworld-nodejs
[root@server1 k8s]# kubectl get service -o wide
NAME                TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE     SELECTOR
helloworld-nodejs   NodePort    10.96.0.10   <none>        80:30001/TCP   145m    app=helloworld-nodejs
kubernetes          ClusterIP   10.96.0.1    <none>        443/TCP        4h57m   <none>
[root@server1 k8s]# kubectl get rs -o wide
NAME                           DESIRED   CURRENT   READY   AGE    CONTAINERS          IMAGES                                          SELECTOR
helloworld-nodejs-5dfb48565d   3         3         3       147m   helloworld-nodejs   docker.io/xzxiaoshan/helloworld-nodejs:latest   app=helloworld-nodejs,pod-template-hash=5dfb48565d
[root@server1 k8s]# kubectl get pod -o wide
NAME                                 READY   STATUS    RESTARTS   AGE    IP            NODE            NOMINATED NODE   READINESS GATES
helloworld-nodejs-5dfb48565d-g8k6k   1/1     Running   0          147m   10.244.46.4   192.168.1.65   <none>           <none>
helloworld-nodejs-5dfb48565d-pvxbd   1/1     Running   0          147m   10.244.46.3   192.168.1.65   <none>           <none>
helloworld-nodejs-5dfb48565d-vjfgf   1/1     Running   0          147m   10.244.46.2   192.168.1.65   <none>           <none>

5、访问验证

# 1、在所有Node节点集群中 curl 验证
[root@host02 etcd]# curl -L -I http://10.96.0.10:80
HTTP/1.1 200 OK
Date: Sat, 14 Mar 2020 10:36:17 GMT

# 2、使用PC浏览器访问 NodePort 端口验证
http://192.168.1.65:30001

至此结束!
附其他常用命令(就不逐个解释了)

kubectl run hello-world-nodejs --replicas=5 --labels="app=hello-world-nodejs" --image=docker.io/xzxiaoshan/helloworld-nodejs:latest --port=8080
kubectl get deploy helloworld-nodejs -o yaml --export > helloworld-nodejs-deploy.yaml
kubectl get deploy -o wide
kubectl get svc -o wide
kubectl get endpoints helloworld-nodejs -o wide
kubectl get pod -o wide -A
kubectl -n default describe pod xxxx
kubectl -n default delete pod xxxx
kubectl -n default delete pod --all
kubectl scale deployment helloworld-nodejs --replicas=7 
kubectl expose deployment helloworld-nodejs --type=NodePort --port=80 --target-port=8080 --nodePort=30001
ipvsadm -Ln |grep 10.97.168.3.98
kubelet edit svc hello-world-deployment
kubectl get ClusterRole -n kube-system
kubectl get ClusterRoleBinding -n kube-system
kubectl get Deployments -n kube-system
kubectl get Pods -n kube-system
kubectl get ServiceAccounts -n kube-system
kubectl get CustomResourceDefinition -n kube-system
kubectl get replicasets -n kube-system
kubectl get replicationcontrollers -n kube-system
kubectl get services -n kube-system
kubectl get jobs -n kube-system
kubectl describe node <NodeName>
kubectl get node --show-labels
kubectl exec <pod-name> -- date
kubectl exec -it <pod-name> -c <container-name> -- bash
kubectl logs <pod-name> 

(END)

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐