Oddbean new post about | logout
 


## 1. 概述

- 在此示例中,我们使用了[kubernetes](https://github.com/Kubernetes)的[基本部署](https://github.com/Kubernetes/samples/blob/master/deployments/basic/)和[基本服务](https://github.com/Kubernetes/samples/blob/master/services/basic/)示例来演示如何使用kubectl命令行工具部署一个简单的web应用程序。
- 在此示例中,我们使用[nginx](https://github.com/nginx/nginx)作为服务器运行的web应用程序。
- 通过执行kubectl命令来部署和管理应用程序。
- 通过使用kubectl exec命令运行容器中的命令行工具,可以查看和修改应用程序。

## 2. 环境搭建

- 安装kubernetes:参考[下文](https://github.com/Kubernetes/samples/blob/master/README.md#installing-the-tools)。
- 从源代码安装nginx:
```css
sudo apt-get update
sudo apt-get install build-essential libssl-dev zlib1g-dev
cd /usr/src/nginx && ./configure --with-http_ssl_module --add=https://github.com/arut/nginx-ssl-conf.git && make
sudo rm -rf /etc/nginx/ && sudo cp -i /usr/src/nginx/conf/default /etc/nginx/default
sudo systemctl enable nginx && sudo systemctl start nginx
```
- 从源代码安装kubernetes:
```csharp
git clone https://github.com/kubernetes-samples/node-deployments.git
cd node-deployments
kubectl create cluster mycluster --config minion --minion-ip 192.168.100.50
kubectl config use-cluster mycluster
kubectl apply -f deployments/replicaSet.yaml
```
## 3. 部署
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
```
- 创建一个文件夹,保存下面的代码:
```makefile
apiVersion: v1
kind: Ingress
metadata:
  name: nginx-ingress
spec:
  rules:
    - host: ingress.example.com
      http:
        paths:
          - path: /
            backend:
              service:
                name: nginx
                port:
                  name: http
```