在Kubernetes上部署nginx

在创建Kubernetes环境之前,请参考下方↓。

尝试在Kubernetes上搭建服务器

我要创建 nginx.deployment.yaml 文件。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.5
        ports:
        - containerPort: 80

进行部署

$ kubectl create -f nginx.deployment.yaml
deployment.extensions "nginx" created

完成了 (完成了)

在这里确认一下pod。

$ kubectl get pod
NAME                     READY     STATUS              RESTARTS   AGE
nginx-64c6b46884-5fh5q   0/1       ContainerCreating   0          54s

由于负载均衡器很昂贵,因此我将公开此节点的端口,以便访问nginx。

nginx服务配置文件.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  externalIPs:
     - <ノードの内部IP>
     - <ノードの内部IP>

在中国本地化后,以下是可能的将句子翻译成中文的一个选项:
节点的内部IP地址可以通过这个来获取。

$ gcloud --format json compute instances list | jq -r '.[].networkInterfaces[].networkIP'
-bash: jq: command not found

哎呀,无法执行… 是吗?
简单地安装bash就行了
顺便一提,jq是能够解析JSON的库

brew install jq

尽管在某些权限上遇到了一些困难,但最终成功安装完成。
重新执行时,可以访问IP地址。

$ gcloud --format json compute instances list | jq -r '.[].networkInterfaces[].networkIP'
10.138.0.15

进行。

$ kubectl create -f nginx.service.yaml
service "nginx" created

我会公开

gcloud compute firewall-rules create http-firewall --allow tcp:80

只需要访问节点的外部IP,就完成了。这里可以获取到外部IP。

gcloud --format json compute instances list | jq -r '.[].networkInterfaces[].accessConfigs[].natIP'
スクリーンショット 2019-05-08 18.22.52.png

最后,我创建了两个YAML文件。

$ ls
nginx.deployment.yaml   nginx.service.yaml

– 部署是什么?
– 部署负责生成和管理副本集(ReplicaSet),而副本集则负责生成和管理Pod。

    • Serviceとは?

コンテナのアクセス方法を把握しなくても、Serviceが受付口だけ知っていれば通信できる

填补

顺便提一下,如果要删除的话,首先要确认 pod 的名称。

kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
nginx-64c6b46884-5fh5q   1/1       Running   0          45m

使用以下命令删除先前获取的pod名称中的不需要的pod。

$ kubectl delete pod <pod名>
pod "nginx-64c6b46884-5fh5q" deleted
广告
将在 10 秒后关闭
bannerAds