部署环境


IP地址 主机名 功能
10.1.32.230 k8s-deploy-test 部署节点,不承担实际作用
10.1.32.231 k8s-master-test01 master节点
10.1.32.232 k8s-master-test02 master节点
10.1.32.233 k8s-master-test03 master节点
10.1.32.240 k8s-nginx-test 负载均衡节点,实际生产中应为HA架构
10.1.32.234 k8s-node01-test01 node节点
10.1.32.235 k8s-node02-test02 node节点
10.1.32.236 k8s-node03-test03 node节点

验证集群状态


本部署将使用Deloyment的方式部署一个3个副本的Nginx服务,并使用NodePort的方式对外提供服务。


确认组件、节点均正常工作(k8s-master):

验证各组件状态:

kubectl get cs

验证各节点状态:

kubectl get node


部署Deployment资源(k8s-master):

创建Deployment资源文件:

cd /opt/k8s/work
source /opt/k8s/bin/environment.sh

cat > nginx-prod.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
  name: daemon-deployment
  namespace: default
spec:
  replicas: 6
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx-prod
      labels:
        app: nginx
        env: prod
    spec:
      containers:
      - name: daemon-pod-nginx
        image: nginx:1.14.2
        ports:
        - name: http-80
          containerPort: 80
  strategy:
    type: RollingUpdate
    rollingUpdate: ![](/uploads/mindocmindoc-1bk3n9okcf30b/images/m_9b20ed8d2c20e9076ebcba6c23d7f2eb_r.png)
      maxSurge: 1

--- 

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-prod
spec:
  type: NodePort
  selector:
    app: nginx
  clusterIP: 10.254.8.8
  ports:
  - port: 8080
    targetPort: 80
    nodePort: 32666
EOF

查看创建的Deployment、service及pod资源:

kubectl apply -f nginx-prod.yaml
kubectl get all

访问任意Node节点的地址,检验应用是否能访问:

for node_ip in ${NODE_NODE_IPS[@]}
    do
        echo ">>> ${node_ip}"
        curl -I http://${node_ip}:32666/
    done
文档更新时间: 2021-09-02 17:22   作者:闻骏