以一个nginx的service和deployment来验证集群功能
创建启动文件
mkdir /opt/k8s/ymlcd /opt/k8s/ymlcat > nginx.yml << EOFapiVersion: v1kind: Servicemetadata: name: nginx labels: app: nginxspec: type: NodePort selector: app: nginx ports: - name: http port: 80 targetPort: 80 nodePort: 8080---apiVersion: apps/v1kind: Deploymentmetadata: name: nginx-deploymentspec: selector: matchLabels: app: nginx replicas: 1 template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.9.1 ports: - containerPort: 80EOF
启动服务
kubectl create -f nginx.yml
第一次启动时需要下载k8s.gcr.io/pause:3.1镜像,国内无法直接下载,造成服务无法启动,通过下面操作来解决
docker pull kubeimage/pause:3.1docker tag kubeimage/pause:3.1 k8s.gcr.io/pause:3.1
观察服务启动情况
root@master:/opt/k8s/yml# kubectl get service -o wideNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTORkubernetes ClusterIP 10.254.0.1 <none> 443/TCP 41h <none>nginx NodePort 10.254.8.25 <none> 80:8080/TCP 30m app=nginxroot@master:/opt/k8s/yml# kubectl get pod -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESnginx-deployment-56f8998dbc-955gf 1/1 Running 0 30m 172.30.78.2 slave <none> <none>root@master:/opt/k8s/yml# curl http://192.168.0.114:8080<!DOCTYPE html><html><head><title>Welcome to nginx!</title><style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; }</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page, the nginx web server is successfully installed andworking. Further configuration is required.</p><p>For online documentation and support please refer to<a href="http://nginx.org/">nginx.org</a>.<br/>Commercial support is available at<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p></body></html>