In any device (for example battery, electrolytic cell or diode) the anode is the electrode towards which, inside the device negative charge carriers flow, or away from which positive charge carriers flow. In other words, inside the device conventional current is from anode to cathode (so outside the device, in accordance with Kirchhoff's first law, it is from cathode to anode).
In the electron gun shown, if the accelerating voltage is connected the right way round, the heated cylinder is the cathode, and the perforated electrode through which the electron beam emerges is the anode. If you connect the voltage the wrong way round, the gun simply won't work (but we still go on calling the heated electrode the cathode and the perforated one the anode).
Similarly, with a pn diode the p-type material is the anode and the n-type material is the cathode. [We are not considering the case of breakdown for a large reverse voltage.]
In a water electrolysis cell with two platinum electrodes, which is anode and which is cathode depends only on which way round we connect the battery. The electrode to which we connect the positive terminal of the battery is the anode, because inside the electrolysis cell, negative ions will move towards this electrode.
In a battery on discharge the anode is the terminal to which negative ions travel, so this electrode acquires a negative charge. Again, conventional current inside the cell is from anode to cathode, and outside the cell from cathode to anode. If we are charging a battery, we use an external power supply to drive a current through the battery in the reverse direction, so the electrode that was the cathode on discharge now becomes the anode and vice versa.
[As pointed out in the comments, chemists define the anode of a galvanic or electrolytic cell as the electrode at which oxidation occurs, that is electrons are released. This results in a conventional current outside the device from cathode to anode and $through$ the device from anode to cathode! The advantages of defining anode and cathode in terms of conventional current are that (a) no knowledge of the chemistry of the device is needed (b) it would be odd to discuss the operation of a thermionic diode or of an electron gun in terms of oxidation and reduction.]