一看必會系列:kubernetes使用yaml部署nginx集群

來源:本站原創 容器技術 超過1,965 views圍觀 0條評論

注意空行和格式

 

1 創建nginx-rc.yaml

vim "nginx-rc.yaml" 18L, 350C       

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-controller
spec:
  replicas: 2
  selector:
    name: nginx-selector
  template:
    metadata:
      labels:
        name: nginx-selector
    spec:
      containers:
        – name: nginx
          image: 192.168.142.131:5000/nginx:v1
          ports:
            – containerPort: 80

2 創建nginx-service-nodeport.yaml

vim nginx-service-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service-nodeport
spec:
  ports:
    – port: 8800
      targetPort: 80
      protocol: TCP
  type: NodePort
  selector:
    name: nginx-selector

~                                                                                      
3 創建pod

kubectl create -f nginx-rc.yaml

4 創建service
kubectl create -f nginx-service-nodeport.yaml

 

看RC狀態                                             

[[email protected] ~]# kubectl get rc
NAME               DESIRED   CURRENT   READY     AGE
nginx-controller   2         2         2         26m    2個是正常

 

看service 狀態
[[email protected] ~]# kubectl get service
NAME                       CLUSTER-IP        EXTERNAL-IP   PORT(S)          AGE
my-nginx-974504764-z1zwq   192.168.142.107   <pending>     80:32594/TCP     17h
my-nginx-974504764-z3mtw   192.168.142.252   <pending>     80:30025/TCP     17h
nginx-service-nodeport     192.168.142.53    <nodes>       8800:30572/TCP   26m  service 狀態 8800映射到30672

 

看nodeport可對外提供服務的端口

[[email protected] ~]# kubectl describe service nginx-service-nodeport
Name:            nginx-service-nodeport
Namespace:        default
Labels:            <none>
Selector:        name=nginx-selector
Type:            NodePort
IP:            192.168.142.53
Port:            <unset>    8800/TCP             提供service端口
NodePort:        <unset>   30572/TCP    外部能訪問的端口
Endpoints:        172.17.100.2:80,172.17.73.2:80  容器內部端口
Session Affinity:    None
No events.
[[email protected] ~]#

更詳細的

[[email protected] ~]# kubectl get pods –all-namespaces -o wide
NAMESPACE     NAME                                    READY     STATUS    RESTARTS   AGE       IP             NODE
default       nginx-controller-mcm0s                  1/1       Running   0          28m       172.17.100.2   192.168.142.132
default       nginx-controller-vq0dl                  1/1       Running   0          28m       172.17.73.2    192.168.142.130
kube-system   kubernetes-dashboard-3820983789-jnsdz   1/1       Running   0          18h       172.17.88.3    192.168.142.131
[[email protected] ~]#

 

訪問方式

http://192.168.142.131:30572/

http://192.168.142.130:30572/

因為service使用的是NodePort方式,所以在任何一個節點訪問31152這個端口都可以訪問nginx

image

https://www.cnblogs.com/puroc/p/5764330.html

文章出自:CCIE那點事 http://www.qdxgqk.live/ 版權所有。本站文章除注明出處外,皆為作者原創文章,可自由引用,但請注明來源。 禁止全文轉載。
本文鏈接:http://www.qdxgqk.live/?p=3836轉載請注明轉自CCIE那點事
如果喜歡:點此訂閱本站
?
?
萌宠夺宝游戏