k8s 之 ClusterIP Service + NodePort Service 部署

ClusterIP Service

# cat << EOF > nginx-ClusterIP-Service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.9.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: ClusterIP
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
EOF


# kubectl apply -f nginx-ClusterIP-Service.yaml

# kubectl get pod

# kubectl get svc

# curl 10.108.84.171

# iptables -t nat -nL | grep 80

# kubectl delete -f nginx-ClusterIP-Service.yaml

Deployment特点:升级、回滚、伸缩

升级

升级 
# kubectl set image deployment nginx-deployment nginx=nginx:latest --record

暂停部署
# kubectl rollout pause deployment.v1.apps/nginx-deployment

查看Deployment状态
# kubectl rollout status deployment nginx-deployment

# kubectl get pod

# kubectl exec -it nginx-deployment-75b69bd684-wcz9w -- nginx -v

回滚至指定deployment的revision

回滚Deployment记录查看
# kubectl rollout history deployment nginx-deployment

先定义,后执行
# kubectl rollout history deployment nginx-deployment --revision=1

执行回滚
# kubectl rollout undo deployment nginx-deployment --to-revision=1

# kubectl get pod

# kubectl exec -it nginx-deployment-d84f7f449-vktmf -- nginx -v

自动伸缩

# kubectl scale deployment --replicas=3 nginx-deployment
伸验证
# kubectl get pod# kubectl scale deployment --replicas=1 nginx-deployment
缩验证
# kubectl get pod

NodePort Service

# cat << EOF > nginx-NodePort-Service.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.9.0
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-service
spec:
  type: NodePort     #此处有变化
  selector:
    app: nginx
  ports:
  - protocol: TCP
    nodePort: 32222  #此处为新添加
    port: 80
    targetPort: 80
EOF


# kubectl apply -f nginx-NodePort-Service.yaml

# kubectl get pod

# kubectl get svc

# kubectl get endpoints

集群中任意主机均可访问

# curl 192.168.1.205:32222
# curl 192.168.1.206:32222
# curl 192.168.1.207:32222

# iptables -t nat -nL | grep 3222

# kubectl delete -f nginx-NodePort-Service.yaml

你可能感兴趣的:(k8s,kubernetes,nginx,docker,centos,负载均衡)