1. 创建nginx service

  • 创建nginx-svc.yaml文件
apiVersion: v1
kind: Service
metadata:
  namespace: default
  name: nginx-svc
  labels: 
    name: nginx-svc
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30001
  selector:
    name: nginx-pod
  • 创建nginx的service服务
kubectl create -f nginx-svc.yaml
  • 查看nginx的service实例
root@master:~# kubectl get svc -n default -owide
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE    SELECTOR
nginx-svc    NodePort    10.99.63.108   <none>        80:30001/TCP   34m    name=nginx-pod
root@master:~#

2. 创建nginx 副本控制器

  • 创建nginx-rc.yaml文件
apiVersion: v1
kind: ReplicationController
metadata:
  namespace: default
  name: nginx-rc
  labels:
    name: nginx-rc
spec:
  replicas: 1
  selector:
    name: nginx-pod
  template:
    metadata:
      labels: 
        name: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
  • 创建nginx的ReplicationController
kubectl create -f nginx-rc.yaml
  • 查看nginx的pod
root@master:~# kubectl get pod -n default -owide
NAME             READY   STATUS    RESTARTS   AGE   IP                NODE    NOMINATED NODE   READINESS GATES
nginx-rc-4qfjd   1/1     Running   0          32m   192.168.166.130   node1   <none>           <none>
root@master:~# 

3. 访问nginx

  • 集群内部访问nginx
curl http://10.99.63.108:80

#10.99.63.108 为nginx service的CLUSTER-IP
#80 为nginx在容器端口
  • 集群外部访问
curl http://192.168.1.121:30001

# 192.168.1.121为master或者node1的IP地址
# 30001 为nginx的svc中配置的nodePort

– end –

Logo

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

更多推荐