Centos7.x 安装Kubernetes(K8s) 1.14.3 nginx 部署 案例

以 nginx 部署为例

Kubernetes 创建 ReplicationController

fox.风
nginx-rc.yml

apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    name: nginx
  template:
    metadata:
      labels:
        name: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          ports:
            - containerPort: 80

Kubernetes 创建 Service

nginx-service.yml

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

执行生成 pod 和 service

kubectl create -f nginx-rc.yaml
kubectl create -f nginx-service.yml

查看 pod

kubectl get pods

输出

NAME          READY   STATUS              RESTARTS   AGE
nginx-b6vst   0/1     ContainerCreating   0          9s
nginx-g56xt   0/1     ContainerCreating   0          9s

ContainerCreating 表示 正在拉取镜像创建中
如果出现 Running 表示 已经拉取完成,镜像 pod 创建完成,运行了

NAME          READY   STATUS    RESTARTS   AGE
nginx-b6vst   1/1     Running   0          2m30s
nginx-g56xt   1/1     Running   0          2m30s

查看 svc

kubectl get svc

输出

NAME            TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)         AGE
kubernetes      ClusterIP   10.1.0.1       <none>        443/TCP         11h
nginx-service   NodePort    10.1.103.154   <none>        80:80/TCP       4s
redis           NodePort    10.1.226.26    <none>        6379:6379/TCP   5h36m

表示创建完成,可以用浏览器直接访问了
浏览器 直接 输入 宿主机IP (我的宿主机IP是 192.168.0.254),那么 浏览器中直接打开http://192.168.0.254
如图
在这里插入图片描述
可以用命令行输出查看

curl 127.0.0.1
或
curl 192.168.0.254

输出

<!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 and
working. 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>

FAQ

删除刚生成的 pod 和 service

kubectl delete -f nginx-rc.yaml
kubectl delete -f nginx-service.yml

参考
https://blog.csdn.net/random_w/article/details/80602207

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页